Debian Codegraph @

src/test/java/io/reactivex/rxjava3/validators/JavadocForAnnotations.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 static org.junit.Assert.fail;
17
18 import java.io.*;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.testsupport.TestHelper;
24
25 /**
26 * Checks the source code of the base reactive types and locates missing
27 * mention of {@code Backpressure:} and {@code Scheduler:} of methods.
28 */
29 public class JavadocForAnnotations {
30
31 static void checkSource(String baseClassName, boolean scheduler) throws Exception {
32 File f = TestHelper.findSource(baseClassName);
33 if (f == null) {
34 return;
35 }
36
37 StringBuilder b = readFile(f);
38
39 StringBuilder e = new StringBuilder();
40
41 if (scheduler) {
42 scanFor(b, "@SchedulerSupport", "Scheduler:", e, baseClassName);
43 } else {
44 scanFor(b, "@BackpressureSupport", "Backpressure:", e, baseClassName);
45 }
46
47 if (e.length() != 0) {
48 System.out.println(e);
49
50 fail(e.toString());
51 }
52 }
53
54 public static StringBuilder readFile(File f) throws Exception {
55 StringBuilder b = new StringBuilder();
56
57 BufferedReader in = new BufferedReader(new FileReader(f));
58 try {
59 for (;;) {
60 String line = in.readLine();
61
62 if (line == null) {
63 break;
64 }
65
66 b.append(line).append('\n');
67 }
68 } finally {
69 in.close();
70 }
71
72 return b;
73 }
74
75 static final void scanFor(StringBuilder sourceCode, String annotation, String inDoc,
76 StringBuilder e, String baseClassName) {
77 int index = 0;
78 for (;;) {
79 int idx = sourceCode.indexOf(annotation, index);
80
81 if (idx < 0) {
82 break;
83 }
84
85 int j = sourceCode.lastIndexOf("/**", idx);
86
87 // see if the last /** is not before the index (last time the annotation was found
88 // indicating an uncommented method like subscribe()
89 if (j > index) {
90 int k = sourceCode.indexOf(inDoc, j);
91
92 if (k < 0 || k > idx) {
93 // when printed on the console, IDEs will create a clickable link to help navigate to the offending point
94 e.append("java.lang.RuntimeException: missing ").append(inDoc).append(" section\r\n")
95 ;
96 int lc = lineNumber(sourceCode, idx);
97
98 e.append(" at io.reactivex.rxjava3.core.").append(baseClassName)
99 .append(" (").append(baseClassName).append(".java:")
100 .append(lc).append(")").append("\r\n\r\n");
101 }
102 }
103
104 index = idx + annotation.length();
105 }
106 }
107
108 static final void scanForBadMethod(StringBuilder sourceCode, String annotation, String inDoc,
109 StringBuilder e, String baseClassName) {
110 int index = 0;
111 for (;;) {
112 int idx = sourceCode.indexOf(annotation, index);
113
114 if (idx < 0) {
115 break;
116 }
117
118 int j = sourceCode.lastIndexOf("/**", idx);
119
120 // see if the last /** is not before the index (last time the annotation was found
121 // indicating an uncommented method like subscribe()
122 if (j > index) {
123 int k = sourceCode.indexOf(inDoc, j);
124
125 if (k >= 0 && k <= idx) {
126
127 int ll = sourceCode.indexOf("You specify", k);
128 if (ll < 0) {
129 ll = sourceCode.indexOf("you specify", k);
130 }
131 int lm = sourceCode.indexOf("This operator", k);
132 if (lm < 0) {
133 lm = sourceCode.indexOf("this operator", k);
134 }
135 if ((ll < 0 || ll > idx) && (lm < 0 || lm > idx)) {
136
137 int n = sourceCode.indexOf("{@code ", k);
138 int endDD = sourceCode.indexOf("</dd>", k);
139 // make sure the {@code is within the dt/dd section
140
141 if (n < idx && n < endDD) {
142 int m = sourceCode.indexOf("}", n);
143
144 if (m < idx) {
145 String mname = sourceCode.substring(n + 7, m);
146
147 if (!"Scheduler".equals(mname)) {
148
149 int q = sourceCode.indexOf("@SuppressWarnings({", idx);
150
151 int o = sourceCode.indexOf("{", idx);
152
153 if (q + 18 == o) {
154 o = sourceCode.indexOf("{", q + 20);
155 }
156
157 if (o >= 0) {
158
159 int p = sourceCode.indexOf(" " + mname + "(", idx);
160
161 if (p < 0 || p > o) {
162 // when printed on the console, IDEs will create a clickable link to help navigate to the offending point
163 e.append("java.lang.RuntimeException: wrong method name in description of ").append(inDoc).append(" '").append(mname).append("'\r\n")
164 ;
165 int lc = lineNumber(sourceCode, idx);
166
167 e.append(" at io.reactivex.rxjava3.core.").append(baseClassName)
168 .append(".method(").append(baseClassName).append(".java:")
169 .append(lc).append(")").append("\r\n");
170 }
171 }
172 }
173 }
174
175 }
176
177 }
178 }
179 }
180
181 index = idx + annotation.length();
182 }
183 }
184
185 static void checkSchedulerBadMethod(String baseClassName) throws Exception {
186 File f = TestHelper.findSource(baseClassName);
187 if (f == null) {
188 return;
189 }
190
191 StringBuilder b = readFile(f);
192
193 StringBuilder e = new StringBuilder();
194
195 scanForBadMethod(b, "@SchedulerSupport", "Scheduler:", e, baseClassName);
196
197 if (e.length() != 0) {
198 System.out.println(e);
199
200 fail(e.toString());
201 }
202 }
203
204 public static int lineNumber(StringBuilder s, int index) {
205 int cnt = 1;
206 for (int i = 0; i < index; i++) {
207 if (s.charAt(i) == '\n') {
208 cnt++;
209 }
210 }
211 return cnt;
212 }
213
214 @Test
215 public void checkFlowableBackpressure() throws Exception {
216 checkSource(Flowable.class.getSimpleName(), false);
217 }
218
219 @Test
220 public void checkFlowableScheduler() throws Exception {
221 checkSource(Flowable.class.getSimpleName(), true);
222 }
223
224 @Test
225 public void checkObservableBackpressure() throws Exception {
226 checkSource(Observable.class.getSimpleName(), false);
227 }
228
229 @Test
230 public void checkObservableScheduler() throws Exception {
231 checkSource(Observable.class.getSimpleName(), true);
232 }
233
234 @Test
235 public void checkSingleBackpressure() throws Exception {
236 checkSource(Single.class.getSimpleName(), false);
237 }
238
239 @Test
240 public void checkSingleScheduler() throws Exception {
241 checkSource(Single.class.getSimpleName(), true);
242 }
243
244 @Test
245 public void checkCompletableBackpressure() throws Exception {
246 checkSource(Completable.class.getSimpleName(), false);
247 }
248
249 @Test
250 public void checkCompletableScheduler() throws Exception {
251 checkSource(Completable.class.getSimpleName(), true);
252 }
253
254 @Test
255 public void checkMaybeBackpressure() throws Exception {
256 checkSource(Maybe.class.getSimpleName(), false);
257 }
258
259 @Test
260 public void checkMaybeScheduler() throws Exception {
261 checkSource(Maybe.class.getSimpleName(), true);
262 }
263
264 @Test
265 public void checkFlowableSchedulerDoc() throws Exception {
266 checkSchedulerBadMethod(Flowable.class.getSimpleName());
267 }
268
269 @Test
270 public void checkObservableSchedulerDoc() throws Exception {
271 checkSchedulerBadMethod(Observable.class.getSimpleName());
272 }
273
274 @Test
275 public void checkSingleSchedulerDoc() throws Exception {
276 checkSchedulerBadMethod(Single.class.getSimpleName());
277 }
278
279 @Test
280 public void checkCompletableSchedulerDoc() throws Exception {
281 checkSchedulerBadMethod(Completable.class.getSimpleName());
282 }
283
284 @Test
285 public void checkMaybeSchedulerDoc() throws Exception {
286 checkSchedulerBadMethod(Maybe.class.getSimpleName());
287 }
288 }