Debian Codegraph @

src/test/java/io/reactivex/rxjava3/validators/NewLinesBeforeAnnotation.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.validators;
15
16 import java.io.*;
17 import java.util.*;
18
19 import org.junit.Test;
20
21 import io.reactivex.rxjava3.testsupport.TestHelper;
22
23 /**
24 * These tests verify the code style that a typical closing curly brace
25 * and the next annotation @ indicator
26 * are not separated by less than or more than one empty line.
27 * <p>Thus this is detected:
28 * <pre><code>
29 * }
30 * &#64;Override
31 * </code></pre>
32 * <p>
33 * as well as
34 * <pre><code>
35 * }
36 *
37 *
38 * &#64;Override
39 * </code></pre>
40 */
41 public class NewLinesBeforeAnnotation {
42
43 @Test
44 public void missingEmptyNewLine() throws Exception {
45 findPattern(0);
46 }
47
48 @Test
49 public void tooManyEmptyNewLines2() throws Exception {
50 findPattern(2);
51 }
52
53 @Test
54 public void tooManyEmptyNewLines3() throws Exception {
55 findPattern(3);
56 }
57
58 @Test
59 public void tooManyEmptyNewLines4() throws Exception {
60 findPattern(4);
61 }
62
63 @Test
64 public void tooManyEmptyNewLines5() throws Exception {
65 findPattern(5);
66 }
67
68 static void findPattern(int newLines) throws Exception {
69 File f = TestHelper.findSource("Flowable");
70 if (f == null) {
71 System.out.println("Unable to find sources of RxJava");
72 return;
73 }
74
75 Queue<File> dirs = new ArrayDeque<>();
76
77 StringBuilder fail = new StringBuilder();
78 fail.append("The following code pattern was found: ");
79 fail.append("\\}\\R");
80 for (int i = 0; i < newLines; i++) {
81 fail.append("\\R");
82 }
83 fail.append("[ ]+@\n");
84
85 File parent = f.getParentFile().getParentFile();
86
87 dirs.offer(new File(parent.getAbsolutePath().replace('\\', '/')));
88 dirs.offer(new File(parent.getAbsolutePath().replace('\\', '/').replace("src/main/java", "src/test/java")));
89
90 int total = 0;
91
92 while (!dirs.isEmpty()) {
93 f = dirs.poll();
94
95 File[] list = f.listFiles();
96 if (list != null && list.length != 0) {
97
98 for (File u : list) {
99 if (u.isDirectory()) {
100 dirs.offer(u);
101 } else {
102 String fname = u.getName();
103 if (fname.endsWith(".java")) {
104
105 List<String> lines = new ArrayList<>();
106 BufferedReader in = new BufferedReader(new FileReader(u));
107 try {
108 for (;;) {
109 String line = in.readLine();
110 if (line == null) {
111 break;
112 }
113 lines.add(line);
114 }
115 } finally {
116 in.close();
117 }
118
119 for (int i = 0; i < lines.size() - 1; i++) {
120 String line = lines.get(i);
121 if (line.endsWith("}") && !line.trim().startsWith("*") && !line.trim().startsWith("//")) {
122 int emptyLines = 0;
123 boolean found = false;
124 for (int j = i + 1; j < lines.size(); j++) {
125 String line2 = lines.get(j);
126 if (line2.trim().startsWith("@")) {
127 found = true;
128 break;
129 }
130 if (!line2.trim().isEmpty()) {
131 break;
132 }
133 emptyLines++;
134 }
135
136 if (emptyLines == newLines && found) {
137 fail
138 .append(fname)
139 .append("#L").append(i + 1)
140 .append(" ");
141 for (int k = 0; k < emptyLines + 2; k++) {
142 fail
143 .append(lines.get(k + i))
144 .append("\\R");
145 }
146 fail.append("\n");
147 total++;
148 }
149 }
150 }
151 }
152 }
153 }
154 }
155 }
156 if (total != 0) {
157 fail.append("Found ")
158 .append(total)
159 .append(" instances");
160 System.out.println(fail);
161 throw new AssertionError(fail.toString());
162 }
163 }
164 }