1
2
3
4
5
6
7
8
9
10
11
12
13
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
35
36
37
38
39 public final class TestCaseTest {
40
41 private Set<String> allClassNames_;
42
43
44
45
46
47
48
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
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
88
89
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
105
106
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 }