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.junit;
16  
17  import org.htmlunit.junit.annotation.HtmlUnitNYI;
18  
19  /**
20   * This is meant to automatically correct the test case to put either the real browser expectations,
21   * or the {@link HtmlUnitNYI} annotation for HtmlUnit.
22   *
23   * @author Ahmed Ashour
24   * @author Ronald Brill
25   */
26  final class TestCaseCorrector {
27  
28      private TestCaseCorrector() {
29      }
30  
31      // ToDo
32  
33  //    static void correct(final FrameworkMethod method, final boolean realBrowser, final BrowserVersion browserVersion,
34  //            final boolean notYetImplemented, final Throwable t) throws IOException {
35  //        final String testRoot = "src/test/java/";
36  //        String browserString = browserVersion.getNickname().toUpperCase(Locale.ROOT);
37  //        browserString = browserString.replace('-', '_'); // FF-ESR-> FF_ESR
38  //
39  //        final File file = new File(testRoot + method.getDeclaringClass().getName().replace('.', '/') + ".java");
40  //        final List<String> lines = FileUtils.readLines(file, UTF_8);
41  //        final String methodLine = "    public void " + method.getName() + "()";
42  //        if (realBrowser) {
43  //            String defaultExpectation = null;
44  //            for (int i = 0; i < lines.size(); i++) {
45  //                if ("    @Default".equals(lines.get(i))) {
46  //                    defaultExpectation = getDefaultExpectation(lines, i);
47  //                }
48  //                if (lines.get(i).startsWith(methodLine)) {
49  //                    i = addExpectation(lines, i, browserString, (AssertionFailedError) t);
50  //                    break;
51  //                }
52  //                if (i == lines.size() - 2) {
53  //                    addMethodWithExpectation(lines, i, browserString, method.getName(), (AssertionFailedError) t,
54  //                            defaultExpectation);
55  //                    break;
56  //                }
57  //            }
58  //        }
59  //        else if (!notYetImplemented) {
60  //            String defaultExpectation = null;
61  //            for (int i = 0; i < lines.size(); i++) {
62  //                if ("    @Default".equals(lines.get(i))) {
63  //                    defaultExpectation = getDefaultExpectation(lines, i);
64  //                }
65  //                if (lines.get(i).startsWith(methodLine)) {
66  //                    addNotYetImplemented(lines, i, browserString);
67  //                    break;
68  //                }
69  //                if (i == lines.size() - 2) {
70  //                    addNotYetImplementedMethod(lines, i, browserString, method.getName(), defaultExpectation);
71  //                    break;
72  //                }
73  //            }
74  //        }
75  //        else {
76  //            for (int i = 0; i < lines.size(); i++) {
77  //                if (lines.get(i).startsWith(methodLine)) {
78  //                    removeNotYetImplemented(lines, i, browserString);
79  //                    break;
80  //                }
81  //            }
82  //        }
83  //        FileUtils.writeLines(file, UTF_8.name(), lines);
84  //    }
85  //
86  //    private static String getDefaultExpectation(final List<String> lines, final int defaultIndex) {
87  //        int index = defaultIndex;
88  //        while (index >= 0 && !lines.get(index).contains("Alerts")) {
89  //            index--;
90  //        }
91  //        if (index >= 0) {
92  //            final String line = lines.get(index);
93  //            return line.substring(line.indexOf('"') + 1, line.lastIndexOf('"'));
94  //        }
95  //        return null;
96  //    }
97  //
98  //    private static int addExpectation(final List<String> lines, int i,
99  //            final String browserString, final AssertionFailedError comparisonFailure) {
100 //        while (!lines.get(i).startsWith("    @Alerts")) {
101 //            i--;
102 //        }
103 //        final List<String> alerts = CodeStyleTest.alertsToList(lines, i);
104 //        for (final Iterator<String> it = alerts.iterator(); it.hasNext();) {
105 //            if (it.next().startsWith(browserString + " = ")) {
106 //                it.remove();
107 //            }
108 //        }
109 //        alerts.add(browserString + " = " + getActualString(comparisonFailure));
110 //        lines.remove(i);
111 //        while (lines.get(i).startsWith("        ")) {
112 //            lines.remove(i);
113 //        }
114 //
115 //        Collections.sort(alerts);
116 //        String defaultAlert = null;
117 //        for (final String alert : alerts) {
118 //            if (alert.startsWith("DEFAULT = ")) {
119 //                defaultAlert = alert;
120 //                break;
121 //            }
122 //        }
123 //
124 //        if (defaultAlert != null) {
125 //            alerts.remove(defaultAlert);
126 //            alerts.add(0, defaultAlert);
127 //        }
128 //
129 //        for (int x = 0; x < alerts.size(); x++) {
130 //            String line = alerts.get(x);
131 //            if (x == 0) {
132 //                if (!line.contains(" = ")) {
133 //                    line = "DEFAULT = " + line;
134 //                }
135 //                line = "    @Alerts(" + line;
136 //            }
137 //            else {
138 //                line = "            " + line;
139 //            }
140 //            if (x < alerts.size() - 1) {
141 //                line += ",";
142 //            }
143 //            else {
144 //                line += ")";
145 //            }
146 //            lines.add(i++, line);
147 //        }
148 //        return i;
149 //    }
150 //
151 //    private static String getActualString(final AssertionFailedError failure) {
152 //        final int lineLength = 96;
153 //
154 //        String actual = failure.getActual();
155 //        actual = actual.substring(0, actual.length() - 1);
156 //        actual = StringEscapeUtils.escapeJava(actual);
157 //        if (actual.length() > lineLength) {
158 //            final StringBuilder builder = new StringBuilder();
159 //            while (!actual.isEmpty()) {
160 //                int length = actual.lastIndexOf(',', lineLength) + 1;
161 //                if (length == 0 && !actual.isEmpty()) {
162 //                    length = Math.min(lineLength, actual.length());
163 //                }
164 //                if (builder.length() != 0) {
165 //                    builder.append(System.lineSeparator()).append("                + ");
166 //                }
167 //                builder.append('"').append(actual.substring(0, length)).append('"');
168 //                actual = actual.substring(length);
169 //            }
170 //            return builder.toString();
171 //        }
172 //        return "\"" + actual + "\"";
173 //    }
174 //
175 //    private static void removeNotYetImplemented(final List<String> lines,
176 //            final int i, final String browserString) {
177 //        final String previous = lines.get(i - 1);
178 //        if (previous.contains("@NotYetImplemented")) {
179 //            if (previous.indexOf('(') != -1) {
180 //                final int p0 = previous.indexOf('(') + 1;
181 //                final int p1 = previous.lastIndexOf(')');
182 //                String browsers = previous.substring(p0, p1);
183 //                if (browsers.indexOf('{') != -1) {
184 //                    browsers = browsers.substring(1, browsers.length() - 1).trim();
185 //                }
186 //                final Set<String> browserSet = new HashSet<>();
187 //                for (final String browser : browsers.split(",")) {
188 //                    browserSet.add(browser.trim());
189 //                }
190 //                browserSet.remove(browserString);
191 //                if (browserSet.size() == 1) {
192 //                    lines.set(i - 1, "    @NotYetImplemented(" + browserSet.iterator().next() + ")");
193 //                }
194 //                else if (browserSet.size() > 1) {
195 //                    lines.set(i - 1, "    @NotYetImplemented({" + String.join(", ", browserSet) + "})");
196 //                }
197 //                else {
198 //                    lines.remove(i - 1);
199 //                }
200 //            }
201 //            else {
202 //                final List<String> allBrowsers = new ArrayList<>(Arrays.asList("CHROME", "EDGE", "FF", "FF_ESR"));
203 //                for (final Iterator<String> it = allBrowsers.iterator(); it.hasNext();) {
204 //                    if (it.next().equals(browserString)) {
205 //                        it.remove();
206 //                    }
207 //                }
208 //                lines.set(i - 1, "    @NotYetImplemented({" + String.join(", ", allBrowsers) + "})");
209 //            }
210 //        }
211 //    }
212 //
213 //    private static void addNotYetImplementedMethod(final List<String> lines,
214 //            int i, final String browserString, final String methodName, final String defaultExpectations) {
215 //        String parent = methodName;
216 //        final String child = parent.substring(parent.lastIndexOf('_') + 1);
217 //        parent = parent.substring(1, parent.indexOf('_', 1));
218 //
219 //        if (!lines.get(i).isEmpty()) {
220 //            i++;
221 //        }
222 //        lines.add(i++, "");
223 //        lines.add(i++, "    /**");
224 //        lines.add(i++, "     * @throws Exception if the test fails");
225 //        lines.add(i++, "     */");
226 //        lines.add(i++, "    @Test");
227 //        lines.add(i++, "    @Alerts(\"" + defaultExpectations + "\")");
228 //        lines.add(i++, "    @NotYetImplemented(" + browserString + ")");
229 //        lines.add(i++, "    public void _" + parent + "_" + child + "() throws Exception {");
230 //        lines.add(i++, "        test(\"" + parent + "\", \"" + child + "\");");
231 //        lines.add(i++, "    }");
232 //        lines.add(i++, "}");
233 //        while (lines.size() > i) {
234 //            lines.remove(i);
235 //        }
236 //    }
237 //
238 //    private static void addNotYetImplemented(final List<String> lines, final int i, final String browserString) {
239 //        final String previous = lines.get(i - 1);
240 //        if (previous.contains("@NotYetImplemented")) {
241 //            if (previous.indexOf('(') != -1 && !previous.contains(browserString)) {
242 //                final int p0 = previous.indexOf('(') + 1;
243 //                final int p1 = previous.lastIndexOf(')');
244 //                String browsers = previous.substring(p0, p1);
245 //                if (browsers.indexOf('{') != -1) {
246 //                    browsers = browsers.substring(1, browsers.length() - 1).trim();
247 //                }
248 //                browsers += ", " + browserString;
249 //                lines.set(i - 1, "    @NotYetImplemented({" + browsers + "})");
250 //            }
251 //        }
252 //        else {
253 //            lines.add(i, "    @NotYetImplemented(" + browserString + ")");
254 //        }
255 //    }
256 //
257 //    private static void addMethodWithExpectation(final List<String> lines,
258 //            int i, final String browserString, final String methodName, final AssertionFailedError comparisonFailure,
259 //            final String defaultExpectations) {
260 //        String parent = methodName;
261 //        final String child = parent.substring(parent.lastIndexOf('_') + 1);
262 //        final int index = parent.indexOf('_', 1);
263 //        if (index != -1) {
264 //            parent = parent.substring(1, index);
265 //        }
266 //        else {
267 //            parent = parent.substring(1);
268 //        }
269 //
270 //        if (!lines.get(i).isEmpty()) {
271 //            i++;
272 //        }
273 //        lines.add(i++, "");
274 //        lines.add(i++, "    /**");
275 //        lines.add(i++, "     * @throws Exception if the test fails");
276 //        lines.add(i++, "     */");
277 //        lines.add(i++, "    @Test");
278 //        lines.add(i++, "    @Alerts(DEFAULT = \"" + defaultExpectations + "\",");
279 //        lines.add(i++, "            " + browserString + " = " + getActualString(comparisonFailure) + ")");
280 //        if (index != -1) {
281 //            lines.add(i++, "    public void _" + parent + "_" + child + "() throws Exception {");
282 //            lines.add(i++, "        test(\"" + parent + "\", \"" + child + "\");");
283 //        }
284 //        else {
285 //            String method = parent;
286 //            for (final String prefix : HostExtractor.PREFIXES_) {
287 //                if (method.startsWith(prefix)) {
288 //                    method = prefix.toLowerCase(Locale.ROOT) + method.substring(prefix.length());
289 //                    break;
290 //                }
291 //            }
292 //            if (Character.isUpperCase(method.charAt(0))) {
293 //                method = Character.toLowerCase(method.charAt(0)) + method.substring(1);
294 //            }
295 //            lines.add(i++, "    public void " + method + "() throws Exception {");
296 //            lines.add(i++, "        test(\"" + parent + "\");");
297 //        }
298 //        lines.add(i++, "    }");
299 //        lines.add(i++, "}");
300 //        while (lines.size() > i) {
301 //            lines.remove(i);
302 //        }
303 //    }
304 }