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;
16  
17  import static java.nio.charset.StandardCharsets.ISO_8859_1;
18  import static org.junit.Assert.fail;
19  
20  import java.io.File;
21  import java.util.ArrayList;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.apache.commons.io.FileUtils;
27  import org.htmlunit.html.DefaultElementFactory;
28  import org.htmlunit.html.HtmlTitle;
29  import org.htmlunit.javascript.configuration.ClassConfiguration;
30  import org.htmlunit.javascript.configuration.JavaScriptConfiguration;
31  import org.junit.Test;
32  
33  /**
34   * Tests for various test cases.
35   *
36   * @author Ahmed Ashour
37   * @author Ronald Brill
38   */
39  public final class TestCaseTest {
40  
41      private Set<String> allClassNames_;
42  
43      /**
44       * Tests that all test cases with the pattern used by
45       * {@link org.htmlunit.source.ElementTestSource#generateTestForHtmlElements}
46       * are up to date.
47       *
48       * @throws Exception if the test fails
49       */
50      @Test
51      public void generateTestForHtmlElements() throws Exception {
52          allClassNames_ = getAllConfiguredJsClassNames();
53          generateTestForHtmlElements(new File("src/test/java"));
54      }
55  
56      private void generateTestForHtmlElements(final File dir) throws Exception {
57          final File[] files = dir.listFiles();
58          if (files != null) {
59              for (final File file : files) {
60                  if (file.isDirectory() && !".git".equals(file.getName())) {
61                      generateTestForHtmlElements(file);
62                  }
63                  else if (file.getName().endsWith(".java")) {
64                      final List<String> lines = FileUtils.readLines(file, ISO_8859_1);
65                      for (final String line : lines) {
66                          if (line.contains("(\"xmp\")")) {
67                              final String relativePath = file.getAbsolutePath().substring(
68                                      new File(".").getAbsolutePath().length() - 1);
69                              final HashSet<String> tags =
70                                      new HashSet<>(DefaultElementFactory.SUPPORTED_TAGS_);
71                              // title tag is special
72                              tags.remove(HtmlTitle.TAG_NAME);
73                              checkLines(relativePath, line, lines, "xmp", tags);
74                          }
75                          else if (line.contains("(\"ClientRect\")")) {
76                              final String relativePath = file.getAbsolutePath().substring(
77                                      new File(".").getAbsolutePath().length() - 1);
78                              checkLines(relativePath, line, lines, "ClientRect", allClassNames_);
79                          }
80                      }
81                  }
82              }
83          }
84      }
85  
86      /**
87       * Returns list of all host classes defined.
88       * @return the list
89       * @throws Exception if an error occurs.
90       */
91      public static Set<String> getAllConfiguredJsClassNames() throws Exception {
92          final Set<String> names = new HashSet<>();
93  
94          for (final BrowserVersion browser : BrowserVersion.ALL_SUPPORTED_BROWSERS) {
95              final JavaScriptConfiguration jsConfig = JavaScriptConfiguration.getInstance(browser);
96              for (final ClassConfiguration config : jsConfig.getAll()) {
97                  names.add(config.getClassName());
98              }
99          }
100         return names;
101     }
102 
103     /**
104      * Returns list of all constructors defined.
105      * @return the list
106      * @throws Exception if an error occurs.
107      */
108     public static Set<String> getAllConfiguredJsConstructorNames() throws Exception {
109         final Set<String> names = new HashSet<>();
110 
111         for (final BrowserVersion browser : BrowserVersion.ALL_SUPPORTED_BROWSERS) {
112             final JavaScriptConfiguration jsConfig = JavaScriptConfiguration.getInstance(browser);
113             for (final ClassConfiguration config : jsConfig.getAll()) {
114                 if (config.getJsConstructor() != null) {
115                     names.add(config.getJsConstructor().getKey());
116                 }
117                 else {
118                     names.add(config.getClassName());
119                 }
120                 if (config.getJsConstructorAlias() != null) {
121                     names.add(config.getJsConstructorAlias());
122                 }
123             }
124         }
125 
126         names.add("Image");
127         names.add("Option");
128 
129         return names;
130     }
131 
132     private static void checkLines(final String relativePath, final String line, final List<String> lines,
133             final String elementName, final Set<String> allElements) {
134         final List<String> allExpectedLines = new ArrayList<>();
135         for (final String element : allElements) {
136             allExpectedLines.add(line.replace(elementName, element));
137         }
138         allExpectedLines.removeAll(lines);
139         if (!allExpectedLines.isEmpty()) {
140             fail("You must specify the following line in " + relativePath + ":\n"
141                     + String.join(System.lineSeparator(), allExpectedLines));
142         }
143     }
144 }