-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathCodeStyleTest.java
More file actions
983 lines (919 loc) · 38.7 KB
/
CodeStyleTest.java
File metadata and controls
983 lines (919 loc) · 38.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
/*
* Copyright (c) 2002-2026 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.RetryingTest;
/**
* Test of coding style for issues which cannot be detected by Checkstyle.
*
* @author Ahmed Ashour
* @author Ronald Brill
* @author Sven Strickroth
* @author Kristof Neirynck
*/
public class CodeStyleTest {
private static final Charset SOURCE_ENCODING = StandardCharsets.UTF_8;
private static final Pattern LEADING_WHITESPACE = Pattern.compile("^\\s+");
private static final Pattern LOG_STATIC_STRING =
Pattern.compile("^\\s*LOG\\.[a-z]+\\(\"[^\"]*\"(, [a-zA-Z_]+)?\\);");
private final List<String> failures_ = new ArrayList<>();
private String title_ = "unknown";
/**
* After.
* @throws IOException in case of error
*/
@AfterEach
public void after() throws IOException {
final StringBuilder sb = new StringBuilder();
for (final String error : failures_) {
sb.append('\n').append(error);
}
if (System.getenv("EXPORT_FAILURES") != null) {
Files.write(Paths.get("target", title_ + ".txt"), failures_);
}
final int errorsNumber = failures_.size();
if (errorsNumber == 1) {
Assertions.fail(title_ + " error: " + sb);
}
else if (errorsNumber > 1) {
Assertions.fail(title_ + " " + errorsNumber + " errors: " + sb);
}
}
private void addFailure(final String file, final int line, final String error) {
failures_.add(file + ", line " + (line <= 0 ? 1 : line) + ": " + error);
}
/**
* @throws IOException if the test fails
*/
@Test
public void codeStyle() throws IOException {
title_ = "CodeStyle";
final List<File> files = new ArrayList<>();
addAll(new File("src/main"), files);
addAll(new File("src/test"), files);
final List<String> classNames = getClassNames(files);
process(files, classNames);
// for (final String className : classNames) {
// addFailure("Not used " + className);
// }
licenseYear();
versionYear();
parentInPom();
}
private static List<String> getClassNames(final List<File> files) {
final List<String> list = new ArrayList<>();
for (final File file : files) {
String fileName = file.getName();
if (fileName.endsWith(".java")) {
fileName = fileName.substring(0, fileName.length() - 5);
fileName = fileName.substring(fileName.lastIndexOf('.') + 1);
list.add(fileName);
}
}
return list;
}
private void addAll(final File dir, final List<File> files) throws IOException {
final File[] children = dir.listFiles();
if (children != null) {
for (final File child : children) {
if (child.isDirectory()
&& !".git".equals(child.getName())
&& !"brotli".equals(child.getName())
&& !("test".equals(dir.getName()) && "resources".equals(child.getName()))) {
addAll(child, files);
}
else {
files.add(child);
}
}
}
}
private void process(final List<File> files, final List<String> classNames) throws IOException {
final String excludeQuercus = "org" + File.separatorChar + "htmlunit" + File.separatorChar + "util" + File.separatorChar + "quercus" + File.separatorChar + "servlet";
for (final File file : files) {
final String relativePath = file.getAbsolutePath().substring(new File(".").getAbsolutePath().length() - 1);
if (relativePath.contains(excludeQuercus)) {
continue;
}
if (file.getName().endsWith(".java")) {
final List<String> lines = FileUtils.readLines(file, SOURCE_ENCODING);
openingCurlyBracket(lines, relativePath);
year(lines, relativePath);
javaDocFirstLine(lines, relativePath);
classJavaDoc(lines, relativePath);
methodFirstLine(lines, relativePath);
methodLastLine(lines, relativePath);
lineBetweenMethods(lines, relativePath);
runWith(lines, relativePath);
deprecated(lines, relativePath);
staticJSMethod(lines, relativePath);
singleAlert(lines, relativePath);
staticLoggers(lines, relativePath);
loggingEnabled(lines, relativePath);
alerts(lines, relativePath);
className(lines, relativePath);
classNameUsed(lines, classNames, relativePath);
spaces(lines, relativePath);
indentation(lines, relativePath);
if (!"package-info.java".equals(file.getName())) {
authorTagAtLeastOne(lines, relativePath);
authorTagNoDuplicates(lines, relativePath);
}
}
}
}
/**
* Ensures that no opening curly bracket exists by itself in a single line.
*/
private void openingCurlyBracket(final List<String> lines, final String path) {
int index = 1;
for (final String line : lines) {
if ("{".equals(line.trim())) {
addFailure(path, index, "Opening curly bracket is alone");
}
index++;
}
}
/**
* Checks the year in the source.
*/
private void year(final List<String> lines, final String path) {
final int year = Calendar.getInstance(Locale.ROOT).get(Calendar.YEAR);
if (lines.size() < 2 || !lines.get(1).contains("Copyright (c) 2002-" + year)) {
addFailure(path, lines.size() < 2 ? 0 : 1, "Incorrect year");
}
}
/**
* Checks the JavaDoc first line, it should not be empty, and should not start with lower-case.
*/
private void javaDocFirstLine(final List<String> lines, final String relativePath) {
for (int index = 1; index < lines.size(); index++) {
final String previousLine = lines.get(index - 1);
final String currentLine = lines.get(index);
if ("/**".equals(previousLine.trim())) {
if ("*".equals(currentLine.trim()) || currentLine.contains("*/")) {
addFailure(relativePath, index + 1, "Empty first line in JavaDoc");
}
if (currentLine.trim().startsWith("*")) {
final String text = currentLine.trim().substring(1).trim();
if (!text.isEmpty() && Character.isLowerCase(text.charAt(0))) {
addFailure(relativePath, index + 1, "Lower case start of text in JavaDoc");
}
}
}
}
}
/**
* Checks the JavaDoc for class should be superseded with an empty line.
*/
private void classJavaDoc(final List<String> lines, final String relativePath) {
for (int index = 1; index < lines.size(); index++) {
final String previousLine = lines.get(index - 1);
final String currentLine = lines.get(index);
if (currentLine.startsWith("/**") && !previousLine.isEmpty()) {
addFailure(relativePath, index, "No empty line the beginning of JavaDoc");
}
}
}
/**
* Checks the method first line, it should not be empty.
*/
private void methodFirstLine(final List<String> lines, final String relativePath) {
for (int index = 0; index < lines.size() - 1; index++) {
final String line = lines.get(index);
if (StringUtils.isBlank(lines.get(index + 1))
&& line.length() > 4 && index > 0 && lines.get(index - 1).startsWith(" ")
&& Character.isWhitespace(line.charAt(0)) && line.endsWith("{")
&& !line.contains(" class ") && !line.contains(" interface ") && !line.contains(" @interface ")
&& (!Character.isWhitespace(line.charAt(4))
|| line.trim().startsWith("public") || line.trim().startsWith("protected")
|| line.trim().startsWith("private"))) {
addFailure(relativePath, index + 2, "Empty line");
}
}
}
/**
* Checks the method last line, it should not be empty.
*/
private void methodLastLine(final List<String> lines, final String relativePath) {
for (int index = 0; index < lines.size() - 1; index++) {
final String line = lines.get(index);
final String nextLine = lines.get(index + 1);
if (StringUtils.isBlank(line) && " }".equals(nextLine)) {
addFailure(relativePath, index + 1, "Empty line");
}
}
}
/**
* Checks that empty line must exist between consecutive methods.
*/
private void lineBetweenMethods(final List<String> lines, final String relativePath) {
for (int index = 0; index < lines.size() - 1; index++) {
final String line = lines.get(index);
final String nextLine = lines.get(index + 1);
if (" }".equals(line) && !nextLine.isEmpty() && !"}".equals(nextLine)) {
addFailure(relativePath, index + 1, "Non-empty line");
}
if (nextLine.trim().equals("/**") && line.trim().equals("}")) {
addFailure(relativePath, index + 2, "Non-empty line");
}
}
}
/**
* @throws Exception if an error occurs
*/
@Test
public void xmlStyle() throws Exception {
title_ = "XMLStyle";
processXML(new File("."), false);
processXML(new File("src/main/resources"), true);
processXML(new File("src/assembly"), true);
processXML(new File("src/changes"), true);
}
private void processXML(final File dir, final boolean recursive) throws Exception {
final File[] files = dir.listFiles();
if (files != null) {
for (final File file : files) {
if (file.isDirectory() && !".git".equals(file.getName())) {
if (recursive) {
processXML(file, true);
}
}
else {
if (file.getName().endsWith(".xml")) {
final List<String> lines = FileUtils.readLines(file, SOURCE_ENCODING);
final String relativePath = file.getAbsolutePath().substring(
new File(".").getAbsolutePath().length() - 1);
mixedIndentation(lines, relativePath);
trailingWhitespace(lines, relativePath);
badIndentationLevels(lines, relativePath);
}
}
}
}
}
/**
* Verifies that no XML files have mixed indentation (tabs and spaces, mixed).
*/
private void mixedIndentation(final List<String> lines, final String relativePath) {
for (int i = 0; i < lines.size(); i++) {
final String line = lines.get(i);
if (line.indexOf('\t') != -1) {
addFailure(relativePath, i + 1, "Mixed indentation");
}
}
}
/**
* Verifies that no XML files have trailing whitespace.
*/
private void trailingWhitespace(final List<String> lines, final String relativePath) {
for (int i = 0; i < lines.size(); i++) {
final String line = lines.get(i);
if (!line.isEmpty()) {
final char last = line.charAt(line.length() - 1);
if (Character.isWhitespace(last)) {
addFailure(relativePath, i + 1, "Trailing whitespace");
}
}
}
}
/**
* Verifies that no XML files have bad indentation levels (each indentation level is 4 spaces).
*/
private void badIndentationLevels(final List<String> lines, final String relativePath) {
for (int i = 0; i < lines.size(); i++) {
final int indentation = getIndentation(lines.get(i));
if (indentation % 4 != 0) {
addFailure(relativePath, i + 1, "Bad indentation level (" + indentation + ")");
}
}
}
/**
* Checks the year in {@code LICENSE.txt}.
*/
private void licenseYear() throws IOException {
final List<String> lines = FileUtils.readLines(new File("checkstyle.xml"), SOURCE_ENCODING);
boolean check = false;
final String copyright = "Copyright (c) 2002-" + LocalDate.now().getYear();
for (final String line : lines) {
if (line.contains("<property name=\"header\"")) {
if (!line.contains(copyright)) {
addFailure("checkstyle.xml", 0, "Incorrect year in checkstyle.xml");
}
check = true;
}
}
if (!check) {
addFailure("checkstyle.xml", 0, "No \"header\" found");
}
}
/**
* Checks the year in the {@link Version}.
*/
private void versionYear() throws IOException {
final List<String> lines =
FileUtils.readLines(new File("src/main/java/org/htmlunit/Version.java"),
SOURCE_ENCODING);
for (final String line : lines) {
if (line.contains("return \"Copyright (c) 2002-" + Calendar.getInstance(Locale.ROOT).get(Calendar.YEAR))) {
return;
}
}
addFailure("src/main/java/org/htmlunit/Version.java", 0, "Incorrect year in Version.getCopyright()");
}
/**
* Verifies no <parent> tag in {@code pom.xml}.
*/
private void parentInPom() throws IOException {
final List<String> lines = FileUtils.readLines(new File("pom.xml"), SOURCE_ENCODING);
for (int i = 0; i < lines.size(); i++) {
if (lines.get(i).contains("<parent>")) {
addFailure("pom.xml", i + 1, "'pom.xml' should not have <parent> tag");
break;
}
}
}
/**
* Verifies that no direct instantiation of WebClient from a test that runs with BrowserRunner.
*/
private void runWith(final List<String> lines, final String relativePath) {
if (relativePath.replace('\\', '/').contains("src/test/java")
&& !relativePath.contains("CodeStyleTest")
&& !relativePath.contains("WebClient9Test")
&& !relativePath.contains("FaqTest")) {
boolean runWith = false;
boolean browserNone = true;
int index = 1;
for (final String line : lines) {
if (line.contains("@RunWith(BrowserRunner.class)")) {
runWith = true;
}
if (line.contains("@Test")) {
browserNone = false;
}
if (relativePath.contains("JavaScriptEngineTest") && line.contains("nonStandardBrowserVersion")) {
browserNone = true;
}
if (runWith) {
if (!browserNone && line.contains("new WebClient(") && !line.contains("getBrowserVersion()")) {
addFailure(relativePath, index,
"Never directly instantiate WebClient, please use getWebClient() instead.");
}
if (line.contains("notYetImplemented()")) {
addFailure(relativePath, index, "Use @NotYetImplemented instead of notYetImplemented()");
}
}
index++;
}
}
}
/**
* Verifies that deprecated tag is followed by "As of " or "since ", and '@Deprecated' annotation follows.
*/
private void deprecated(final List<String> lines, final String relativePath) {
int i = 0;
for (String line : lines) {
line = line.trim().toLowerCase(Locale.ROOT);
if (line.startsWith("* @deprecated")) {
if (!line.startsWith("* @deprecated as of ") && !line.startsWith("* @deprecated since ")) {
addFailure(relativePath, i + 1,
"@deprecated must be immediately followed by \"As of \" or \"since \"");
}
if (!getAnnotations(lines, i).contains("@Deprecated")) {
addFailure(relativePath, i + 1, "No \"@Deprecated\" annotation");
}
}
i++;
}
}
/**
* Returns all annotation lines that comes after the given 'javadoc' line.
* @param lines source code lines
* @param index the index to start searching from, must be a 'javadoc' line.
*/
private static List<String> getAnnotations(final List<String> lines, int index) {
final List<String> annotations = new ArrayList<>();
while (!lines.get(index++).trim().endsWith("*/")) {
//empty;
}
while (lines.get(index).trim().startsWith("@")) {
annotations.add(lines.get(index++).trim());
}
return annotations;
}
/**
* Verifies that no static JavaScript method exists.
*/
private void staticJSMethod(final List<String> lines, final String relativePath) {
if (relativePath.endsWith("Console.java")) {
return;
}
int i = 0;
for (final String line : lines) {
if (line.contains(" static ")
&& (line.contains(" jsxFunction_") || line.contains(" jsxGet_") || line.contains(" jsxSet_"))
&& !line.contains(" jsxFunction_write") && !line.contains(" jsxFunction_insertBefore")
&& !line.contains(" jsxFunction_drawImage")) {
addFailure(relativePath, i + 1, "Use of static JavaScript function");
}
i++;
}
}
/**
* Single @Alert does not need curly brackets.
*/
private void singleAlert(final List<String> lines, final String relativePath) {
int i = 0;
for (final String line : lines) {
if (line.trim().startsWith("@Alerts") && line.contains("@Alerts({") && line.contains("})")) {
final String alert = line.substring(line.indexOf('{'), line.indexOf('}'));
if (!alert.contains(",") && alert.contains("\"")
&& alert.indexOf('"', alert.indexOf('"') + 1) != -1) {
addFailure(relativePath, i + 1, "No need for curly brackets");
}
}
i++;
}
}
/**
* Verifies that only static loggers exist.
*/
private void staticLoggers(final List<String> lines, final String relativePath) {
int i = 0;
final String logClassName = Log.class.getSimpleName();
for (String line : lines) {
line = line.trim();
if (line.contains(" " + logClassName + " ")
&& !line.contains(" LOG ")
&& !line.contains(" static ")
&& !line.startsWith("//")
&& !line.contains("webConsoleLogger_") // this one is there by design
&& !line.contains("(final Log logger)") // logger as parameter
&& !line.contains("httpclient.wire")) {
addFailure(relativePath, i + 1, "Non-static logger detected");
}
i++;
}
}
/**
* Verifies that there is code to check log enablement.
* <p> For example,
* <code><pre>
* if (log.isDebugEnabled()) {
* ... do something expensive ...
* log.debug(theResult);
* }
* </pre></code>
* </p>
*/
private void loggingEnabled(final List<String> lines, final String relativePath) {
if (relativePath.contains("CodeStyleTest")) {
return;
}
int i = 0;
for (final String line : lines) {
if (line.contains("LOG.trace(") && !LOG_STATIC_STRING.matcher(line).matches()) {
loggingEnabled(lines, i, "Trace", relativePath);
}
else if (line.contains("LOG.debug(") && !LOG_STATIC_STRING.matcher(line).matches()) {
loggingEnabled(lines, i, "Debug", relativePath);
}
i++;
}
}
private void loggingEnabled(final List<String> lines, final int index, final String method,
final String relativePath) {
final int indentation = getIndentation(lines.get(index));
for (int i = index - 1; i >= 0; i--) {
final String line = lines.get(i);
if (getIndentation(line) < indentation && line.contains("LOG.is" + method + "Enabled()")) {
return;
}
if (getIndentation(line) == 4) { // a method
addFailure(relativePath, index + 1, "Must be inside a \"if (LOG.is" + method + "Enabled())\" check");
return;
}
}
}
private static int getIndentation(final String line) {
final Matcher matcher = LEADING_WHITESPACE.matcher(line);
if (matcher.find()) {
return matcher.end() - matcher.start();
}
return 0;
}
/**
* Verifies that \@Alerts is correctly defined.
*/
private void alerts(final List<String> lines, final String relativePath) {
for (int i = 0; i < lines.size(); i++) {
if (lines.get(i).startsWith(" @Alerts(")) {
final List<String> alerts = alertsToList(lines, i, true);
alertVerify(alerts, relativePath, i);
}
}
}
/**
* Verifies that the class name is used.
*/
private static void classNameUsed(final List<String> lines, final List<String> classNames,
final String relativePath) {
String simpleName = relativePath.substring(0, relativePath.length() - 5);
simpleName = simpleName.substring(simpleName.lastIndexOf(File.separator) + 1);
for (final String line : lines) {
for (final Iterator<String> it = classNames.iterator(); it.hasNext();) {
final String className = it.next();
if (line.contains(className) && !className.equals(simpleName)) {
it.remove();
}
}
}
}
/**
* Verifies that the class name is used.
*/
private void className(final List<String> lines, final String relativePath) {
if (relativePath.contains("main") && relativePath.contains("host")) {
String fileName = relativePath.substring(0, relativePath.length() - 5);
fileName = fileName.substring(fileName.lastIndexOf(File.separator) + 1);
String wrongName = null;
int i = 0;
for (final String line : lines) {
if (line.startsWith(" * ")) {
int p0 = line.indexOf("{@code ");
if (p0 != -1) {
p0 = p0 + "{@code ".length();
final int p1 = line.indexOf('}', p0 + 1);
final String name = line.substring(p0, p1);
if (!name.equals(fileName)) {
wrongName = name;
}
}
}
else if (line.startsWith("@JsxClass")) {
int p0 = line.indexOf("className = \"");
if (p0 != -1) {
p0 = p0 + "className = \"".length();
final int p1 = line.indexOf("\"", p0 + 1);
String name = line.substring(p0, p1);
// JsxClass starts with lower case
if (Character.isLowerCase(name.charAt(0))) {
name = StringUtils.capitalize(name);
if (name.equals(fileName)) {
wrongName = null;
}
}
}
}
else if (line.startsWith("public class")) {
if (wrongName != null) {
addFailure(relativePath, i + 1, "Incorrect host class '" + wrongName + "'");
}
return;
}
++i;
}
}
}
/**
* Returns array of String of the alerts which are in the specified index.
*
* @param lines the list of strings
* @param alertsIndex the index in which the \@Alerts is defined
* @return array of alert strings
*/
public static List<String> alertsToList(final List<String> lines, final int alertsIndex) {
return alertsToList(lines, alertsIndex, false);
}
private static List<String> alertsToList(final List<String> lines, final int alertsIndex,
final boolean preserveCommas) {
if (" @Alerts".equals(lines.get(alertsIndex))) {
lines.set(alertsIndex, " @Alerts()");
}
if (!lines.get(alertsIndex).startsWith(" @Alerts(")) {
throw new IllegalArgumentException("No @Alerts found in " + (alertsIndex + 1));
}
final StringBuilder alerts = new StringBuilder();
for (int i = alertsIndex;; i++) {
final String line = lines.get(i);
if (!alerts.isEmpty()) {
alerts.append('\n');
}
if (line.startsWith(" @Alerts(")) {
alerts.append(line.substring(" @Alerts(".length()));
}
else {
alerts.append(line);
}
if (line.endsWith(")")) {
alerts.deleteCharAt(alerts.length() - 1);
break;
}
}
final List<String> list = alertsToList(alerts.toString());
if (!preserveCommas) {
for (int i = 0; i < list.size(); i++) {
String value = list.get(i);
if (value.startsWith(",")) {
value = value.substring(1).trim();
}
list.set(i, value);
}
}
return list;
}
/**
* Verifies a specific \@Alerts definition.
*/
private void alertVerify(final List<String> alerts, final String relativePath, final int lineIndex) {
if (alerts.size() == 1) {
if (alerts.get(0).contains("DEFAULT")) {
addFailure(relativePath, lineIndex + 1, "No need for \"DEFAULT\"");
}
}
else {
final List<String> names = new ArrayList<>();
for (final String alert : alerts) {
String cleanedAlert = alert;
if (alert.charAt(0) == ',') {
if (alert.charAt(1) != '\n') {
addFailure(relativePath, lineIndex + 1, "Expectation must be in a separate line");
}
cleanedAlert = alert.substring(1).trim();
}
final int quoteIndex = cleanedAlert.indexOf('"');
final int equalsIndex = cleanedAlert.indexOf('=');
if (equalsIndex != -1 && equalsIndex < quoteIndex) {
final String name = cleanedAlert.substring(0, equalsIndex - 1);
alertVerifyOrder(name, names, relativePath, lineIndex);
names.add(name);
}
}
}
}
/**
* Converts the given alerts definition to an array of expressions.
*/
private static List<String> alertsToList(final String string) {
final List<String> list = new ArrayList<>();
if ("\"\"".equals(string)) {
list.add(string);
}
else {
final StringBuilder currentToken = new StringBuilder();
boolean insideString = true;
boolean startsWithBraces = false;
for (final String token : string.split("(?<!\\\\)\"")) {
insideString = !insideString;
if (!currentToken.isEmpty()) {
currentToken.append('"');
}
else {
startsWithBraces = token.contains("{");
}
if (!insideString && token.startsWith(",") && !startsWithBraces) {
list.add(currentToken.toString());
currentToken.setLength(0);
startsWithBraces = token.contains("{");
}
if (!insideString && token.contains("}")) {
final int curlyIndex = token.indexOf('}') + 1;
currentToken.append(token, 0, curlyIndex);
list.add(currentToken.toString());
currentToken.setLength(0);
currentToken.append(token, curlyIndex, token.length());
}
else {
if (!insideString && token.contains(",") && !startsWithBraces) {
final String[] expressions = token.split(",");
currentToken.append(expressions[0]);
if (!currentToken.isEmpty()) {
list.add(currentToken.toString());
}
for (int i = 1; i < expressions.length - 1; i++) {
list.add(',' + expressions[i]);
}
currentToken.setLength(0);
currentToken.append(',' + expressions[expressions.length - 1]);
}
else {
currentToken.append(token);
}
}
}
if (!currentToken.isEmpty()) {
if (!currentToken.toString().contains("\"")) {
currentToken.insert(0, '"');
}
int totalQuotes = 0;
for (int i = 0; i < currentToken.length(); i++) {
if (currentToken.charAt(i) == '"' && (i == 0 || currentToken.charAt(i - 1) != '\\')) {
totalQuotes++;
}
}
if (totalQuotes % 2 != 0) {
currentToken.append('"');
}
list.add(currentToken.toString());
}
}
return list;
}
/**
* Verifies \@Alerts specific order.
*
* @param browserName the browser name
* @param previousList the previously defined browser names
*/
private void alertVerifyOrder(final String browserName, final List<String> previousList,
final String relativePath, final int lineIndex) {
switch (browserName) {
case "DEFAULT":
if (!previousList.isEmpty()) {
addFailure(relativePath, lineIndex + 1, "DEFAULT must come first");
}
break;
default:
}
}
/**
* Checks that the file contains at least one {@code @author} tag.
*/
private void authorTagAtLeastOne(final List<String> lines, final String path) {
final boolean hasAuthor = lines.stream()
.anyMatch(line -> line.trim().startsWith("* @author"));
if (!hasAuthor) {
addFailure(path, 0, "Missing @author tag");
}
}
/**
* Checks that the file contains no duplicate {@code @author} tags
* (same author name listed more than once).
*/
private void authorTagNoDuplicates(final List<String> lines, final String path) {
final List<String> authors = new ArrayList<>();
for (int i = 0; i < lines.size(); i++) {
final String trimmed = lines.get(i).trim();
if (trimmed.startsWith("* @author")) {
final String author = trimmed.substring("* @author".length()).trim();
if (authors.contains(author)) {
addFailure(path, i + 1, "Duplicate @author tag: " + author);
}
else {
authors.add(author);
}
}
}
}
/**
* Verifies that no extra leading spaces (in test code).
*/
private void spaces(final List<String> lines, final String relativePath) {
for (int i = 0; i + 1 < lines.size(); i++) {
String line = lines.get(i).trim();
String next = lines.get(i + 1).trim();
if (line.startsWith("+ \"") && next.startsWith("+ \"")) {
line = line.substring(3);
final String lineTrimmed = line.trim();
next = next.substring(3);
if (lineTrimmed.startsWith("<") && next.trim().startsWith("<") || lineTrimmed.startsWith("try")
|| lineTrimmed.startsWith("for") || lineTrimmed.startsWith("function")
|| lineTrimmed.startsWith("if")) {
final int difference = getInitialSpaces(next) - getInitialSpaces(line);
if (difference > 2) {
addFailure(relativePath, i + 2, "Too many initial spaces");
}
else if (difference == 1) {
addFailure(relativePath, i + 2, "Add one more space");
}
}
}
}
}
/**
* Verifies the indentation of the expectations.
*/
private void indentation(final List<String> lines, final String relativePath) {
for (int i = 0; i + 1 < lines.size(); i++) {
final String line = lines.get(i);
if (line.startsWith(" CHROME = ")
|| line.startsWith(" EDGE = ")
|| line.startsWith(" FF = ")
|| line.startsWith(" FF_ESR = ")) {
addFailure(relativePath, i + 2, "Incorrect indentation");
}
}
}
private static int getInitialSpaces(final String s) {
int spaces = 0;
while (spaces < s.length() && s.charAt(spaces) == ' ') {
spaces++;
}
return spaces;
}
/**
* Tests if all JUnit 4 candidate test methods declare <tt>@Test</tt> annotation.
* @throws Exception if the test fails
*/
@Test
public void tests() throws Exception {
title_ = "Tests";
testTests(new File("src/test/java"));
}
private void testTests(final File dir) throws Exception {
final File[] files = dir.listFiles();
if (files == null) {
return;
}
for (final File file : files) {
if (file.isDirectory()) {
if (!".git".equals(file.getName())) {
testTests(file);
}
}
else {
if (file.getName().endsWith(".java")) {
final int index = new File("src/test/java").getAbsolutePath().length();
String name = file.getAbsolutePath();
name = name.substring(index + 1, name.length() - 5);
name = name.replace(File.separatorChar, '.');
final Class<?> clazz;
try {
clazz = Class.forName(name);
}
catch (final Exception e) {
continue;
}
name = file.getName();
if (name.endsWith("Test.java") || name.endsWith("TestCase.java")) {
for (final Constructor<?> ctor : clazz.getConstructors()) {
if (ctor.getParameterTypes().length == 0) {
for (final Method method : clazz.getDeclaredMethods()) {
if (Modifier.isPublic(method.getModifiers())
&& method.getAnnotation(BeforeEach.class) == null
&& method.getAnnotation(BeforeAll.class) == null
&& method.getAnnotation(AfterEach.class) == null
&& method.getAnnotation(AfterAll.class) == null
&& method.getAnnotation(Test.class) == null
&& method.getAnnotation(RepeatedTest.class) == null
&& method.getAnnotation(RetryingTest.class) == null
&& method.getReturnType() == Void.TYPE
&& method.getParameterTypes().length == 0) {
final List<String> lines = FileUtils.readLines(file, SOURCE_ENCODING);
int line = -1;
for (int i = 0; i < lines.size(); ++i) {
if (lines.get(i).contains("public void " + method.getName() + "()")) {
line = i + 1;
break;
}
}
addFailure(file.toString().replace('\\', '/'), line,
"Method '" + method.getName() + "' does not declare @Test annotation");
}
}
}
}
}
}
}
}
}
}