View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.source;
16  
17  import java.lang.reflect.Field;
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.htmlunit.html.DefaultElementFactory;
22  import org.htmlunit.html.HtmlPage;
23  import org.htmlunit.javascript.configuration.JavaScriptConfigurationTest;
24  
25  /**
26   * Use to generate test cases similar to the ones in the 'general' package.
27   *
28   * @author Ahmed Ashour
29   */
30  public final class ElementTestSource {
31  
32      private ElementTestSource() {
33      }
34  
35      /**
36       * Generate test case for each one HTML elements.
37       * @param htmlGeneratorMethod the method name which is called to generate the HTML, it expects a tag name parameter
38       * @param defaultAlerts default string inside the parenthesis of <tt>@Alerts</tt>, can be null
39       */
40      public static void generateTestForHtmlElements(final String htmlGeneratorMethod,
41              final String defaultAlerts) {
42          final Map<String, String> namesMap = new HashMap<>();
43          for (final String className : JavaScriptConfigurationTest.getClassesForPackage(HtmlPage.class)) {
44              try {
45                  final Class<?> c = Class.forName(className);
46                  final Field field = c.getDeclaredField("TAG_NAME");
47                  namesMap.put(field.get(null).toString(), c.getName());
48                  try {
49                      final Field field2 = c.getDeclaredField("TAG_NAME2");
50                      namesMap.put(field2.get(null).toString(), c.getName());
51                  }
52                  catch (final Exception ignored) {
53                      // ignore
54                  }
55              }
56              catch (final Exception ignored) {
57                  // ignore
58              }
59          }
60          for (final String tag : DefaultElementFactory.SUPPORTED_TAGS_) {
61              System.out.println();
62              System.out.println("    /**");
63              if (namesMap.containsKey(tag)) {
64                  System.out.println("     * Test {@link " + namesMap.get(tag) + "}.");
65                  System.out.println("     *");
66              }
67              System.out.println("     * @throws Exception if the test fails");
68              System.out.println("     */");
69              System.out.println("    @Test");
70              System.out.print("    @Alerts(");
71              if (defaultAlerts != null) {
72                  System.out.print(defaultAlerts);
73              }
74              System.out.println(")");
75              System.out.println("    public void " + tag + "() throws Exception {");
76              System.out.println("        loadPageWithAlerts2(" + htmlGeneratorMethod + "(\"" + tag + "\"));");
77              System.out.println("    }");
78          }
79      }
80  }