Debian Codegraph @

src/test/java/io/reactivex/rxjava3/validators/ParamValidationNaming.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.File;
17 import java.nio.file.Files;
18 import java.util.*;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.core.Observable;
24 import io.reactivex.rxjava3.disposables.*;
25 import io.reactivex.rxjava3.flowables.ConnectableFlowable;
26 import io.reactivex.rxjava3.observables.ConnectableObservable;
27 import io.reactivex.rxjava3.parallel.ParallelFlowable;
28 import io.reactivex.rxjava3.processors.*;
29 import io.reactivex.rxjava3.schedulers.Schedulers;
30 import io.reactivex.rxjava3.subjects.*;
31 import io.reactivex.rxjava3.testsupport.TestHelper;
32
33 /**
34 * Check if the parameter name in Objects.requireNonNull
35 * and ObjectHelper.verifyPositive calls match the parameter
36 * name in the message.
37 */
38 public class ParamValidationNaming {
39
40 @Test
41 public void checkCompletable() throws Exception {
42 processFile(Completable.class);
43 }
44
45 @Test
46 public void checkSingle() throws Exception {
47 processFile(Single.class);
48 }
49
50 @Test
51 public void checkMaybe() throws Exception {
52 processFile(Maybe.class);
53 }
54
55 @Test
56 public void checkObservable() throws Exception {
57 processFile(Observable.class);
58 }
59
60 @Test
61 public void checkFlowable() throws Exception {
62 processFile(Flowable.class);
63 }
64
65 @Test
66 public void checkParallelFlowable() throws Exception {
67 processFile(ParallelFlowable.class);
68 }
69
70 @Test
71 public void checkConnectableObservable() throws Exception {
72 processFile(ConnectableObservable.class);
73 }
74
75 @Test
76 public void checkConnectableFlowable() throws Exception {
77 processFile(ConnectableFlowable.class);
78 }
79
80 @Test
81 public void checkSubject() throws Exception {
82 processFile(Subject.class);
83 }
84
85 @Test
86 public void checkFlowableProcessor() throws Exception {
87 processFile(FlowableProcessor.class);
88 }
89
90 @Test
91 public void checkDisposable() throws Exception {
92 processFile(Disposable.class);
93 }
94
95 @Test
96 public void checkScheduler() throws Exception {
97 processFile(Scheduler.class);
98 }
99
100 @Test
101 public void checkSchedulers() throws Exception {
102 processFile(Schedulers.class);
103 }
104
105 @Test
106 public void checkAsyncSubject() throws Exception {
107 processFile(AsyncSubject.class);
108 }
109
110 @Test
111 public void checkBehaviorSubject() throws Exception {
112 processFile(BehaviorSubject.class);
113 }
114
115 @Test
116 public void checkPublishSubject() throws Exception {
117 processFile(PublishSubject.class);
118 }
119
120 @Test
121 public void checkReplaySubject() throws Exception {
122 processFile(ReplaySubject.class);
123 }
124
125 @Test
126 public void checkUnicastSubject() throws Exception {
127 processFile(UnicastSubject.class);
128 }
129
130 @Test
131 public void checkSingleSubject() throws Exception {
132 processFile(SingleSubject.class);
133 }
134
135 @Test
136 public void checkMaybeSubject() throws Exception {
137 processFile(MaybeSubject.class);
138 }
139
140 @Test
141 public void checkCompletableSubject() throws Exception {
142 processFile(CompletableSubject.class);
143 }
144
145 @Test
146 public void checkAsyncProcessor() throws Exception {
147 processFile(AsyncProcessor.class);
148 }
149
150 @Test
151 public void checkBehaviorProcessor() throws Exception {
152 processFile(BehaviorProcessor.class);
153 }
154
155 @Test
156 public void checkPublishProcessor() throws Exception {
157 processFile(PublishProcessor.class);
158 }
159
160 @Test
161 public void checkReplayProcessor() throws Exception {
162 processFile(ReplayProcessor.class);
163 }
164
165 @Test
166 public void checkUnicastProcessor() throws Exception {
167 processFile(UnicastProcessor.class);
168 }
169
170 @Test
171 public void checkMulticastProcessor() throws Exception {
172 processFile(MulticastProcessor.class);
173 }
174
175 @Test
176 public void checkCompositeDisposable() throws Exception {
177 processFile(CompositeDisposable.class);
178 }
179
180 static void processFile(Class<?> clazz) throws Exception {
181 String baseClassName = clazz.getSimpleName();
182 File f = TestHelper.findSource(baseClassName, clazz.getPackage().getName());
183 if (f == null) {
184 return;
185 }
186 String fullClassName = clazz.getName();
187
188 int errorCount = 0;
189 StringBuilder errors = new StringBuilder();
190
191 List<String> lines = Files.readAllLines(f.toPath());
192
193 for (int j = 0; j < lines.size(); j++) {
194 String line = lines.get(j).trim();
195
196 for (ValidatorStrings validatorStr : VALIDATOR_STRINGS) {
197 int strIdx = line.indexOf(validatorStr.code);
198 if (strIdx >= 0) {
199
200 int comma = line.indexOf(',', strIdx + validatorStr.code.length());
201
202 String paramName = line.substring(strIdx + validatorStr.code.length(), comma);
203
204 int quote = line.indexOf('"', comma);
205
206 String message = line.substring(quote + 1, Math.min(line.length(), quote + 2 + paramName.length()));
207
208 if (line.contains("\"A Disposable")) {
209 continue;
210 }
211
212 if (!line.contains("\"The RxJavaPlugins")
213 && !(message.startsWith(paramName)
214 && (message.endsWith(" ") || message.endsWith("\"")))) {
215 errorCount++;
216 errors.append("L")
217 .append(j)
218 .append(" : Wrong validator message parameter name\r\n ")
219 .append(line)
220 .append("\r\n")
221 .append(" ").append(paramName).append(" != ").append(message)
222 .append("\r\n at ")
223 .append(fullClassName)
224 .append(".method(")
225 .append(f.getName())
226 .append(":")
227 .append(j + 1)
228 .append(")\r\n")
229 ;
230 }
231
232 int midx = j - 1;
233 // find the method declaration
234 for (; midx >= 0; midx--) {
235 String linek = lines.get(midx).trim();
236 if (linek.startsWith("public") || linek.startsWith("private")
237 || linek.startsWith("protected")
238 || linek.startsWith("static")
239 || linek.startsWith(baseClassName)) {
240 break;
241 }
242 }
243
244 if (line.contains("\"The RxJavaPlugins")) {
245 continue;
246 }
247
248 // find JavaDoc of throws
249 boolean found = false;
250 for (int k = midx - 1; k >= 0; k--) {
251 String linek = lines.get(k).trim();
252 if (linek.startsWith("/**")) {
253 break;
254 }
255 if (linek.startsWith("}")) {
256 found = true; // no method JavaDoc present
257 break;
258 }
259 if (linek.startsWith(validatorStr.javadoc)) {
260 // see if a @code paramName is present
261 String paramStr = "{@code " + paramName + "}";
262 for (int m = k; m < lines.size(); m++) {
263 String linem = lines.get(m).trim();
264 if (linem.startsWith("* @see")
265 || linem.startsWith("* @since")
266 || linem.startsWith("*/")) {
267 break;
268 }
269 if (linem.contains(paramStr)) {
270 found = true;
271 break;
272 }
273 }
274 break;
275 }
276 }
277
278 if (!found) {
279 errorCount++;
280 errors.append("L")
281 .append(j)
282 .append(" : missing '")
283 .append(validatorStr.javadoc)
284 .append("' for argument validation: ")
285 .append(paramName)
286 .append("\r\n ")
287 .append(line)
288 .append("\r\n at ")
289 .append(fullClassName)
290 .append(".method(")
291 .append(f.getName())
292 .append(":")
293 .append(j + 1)
294 .append(")\r\n")
295 ;
296 }
297 }
298 }
299
300 for (ValidatorStrings validatorStr : EXCEPTION_STRINGS) {
301 int strIdx = line.indexOf(validatorStr.code);
302 if (strIdx >= 0) {
303
304 int midx = j - 1;
305 // find the method declaration
306 for (; midx >= 0; midx--) {
307 String linek = lines.get(midx).trim();
308 if (linek.startsWith("public") || linek.startsWith("private")
309 || linek.startsWith("protected")
310 || linek.startsWith("static")
311 || linek.startsWith(baseClassName)) {
312 break;
313 }
314 }
315
316 // find JavaDoc of throws
317 boolean found = false;
318 for (int k = midx - 1; k >= 0; k--) {
319 String linek = lines.get(k).trim();
320 if (linek.startsWith("/**")) {
321 break;
322 }
323 if (linek.startsWith("}")) {
324 found = true; // no JavaDoc
325 break;
326 }
327 if (linek.startsWith(validatorStr.javadoc)) {
328 found = true;
329 }
330 }
331
332 if (!found) {
333 errorCount++;
334 errors.append("L")
335 .append(j)
336 .append(" : missing '")
337 .append(validatorStr.javadoc)
338 .append("' for exception\r\n ")
339 .append(line)
340 .append("\r\n at ")
341 .append(fullClassName)
342 .append(".method(")
343 .append(f.getName())
344 .append(":")
345 .append(j + 1)
346 .append(")\r\n")
347 ;
348 }
349 }
350 }
351
352 if (line.startsWith("public") || line.startsWith("protected") || line.startsWith("final") || line.startsWith("private")
353 || line.startsWith("static")) {
354 for (ValidatorStrings validatorStr : TYPICAL_ARGUMENT_STRINGS) {
355 // find the method declaration ending {
356 for (int i = j; i < lines.size(); i++) {
357 String linei = lines.get(i).trim();
358
359 // space + code for capturing type declarations
360 String varPattern = " " + validatorStr.code;
361 if (linei.contains(varPattern + ")")
362 || linei.contains(varPattern + ",")
363 || linei.endsWith(varPattern)) {
364 // ignore nullable-annotated arguments
365 if (!linei.matches(".*\\@Nullable\\s.*" + validatorStr.code + ".*")) {
366 boolean found = false;
367 for (int k = i - 1; k >= 0; k--) {
368 String linek = lines.get(k).trim();
369 if (linek.startsWith("/**")) {
370 break;
371 }
372 if (linek.startsWith("}")) {
373 found = true; // no method JavaDoc present
374 break;
375 }
376 if (linek.startsWith(validatorStr.javadoc)) {
377 // see if a @code paramName is present
378 String paramStr = "{@code " + validatorStr.code + "}";
379 for (int m = k; m < lines.size(); m++) {
380 String linem = lines.get(m).trim();
381 if (linem.startsWith("* @see")
382 || linem.startsWith("* @since")
383 || linem.startsWith("*/")) {
384 break;
385 }
386 if (linem.contains(paramStr)) {
387 found = true;
388 break;
389 }
390 }
391 break;
392 }
393 }
394
395 if (!found) {
396 errorCount++;
397 errors.append("L")
398 .append(j)
399 .append(" : missing '")
400 .append(validatorStr.javadoc)
401 .append("' for typical argument: ")
402 .append(validatorStr.code)
403 .append("\r\n ")
404 .append(line)
405 .append("\r\n at ")
406 .append(fullClassName)
407 .append(".method(")
408 .append(f.getName())
409 .append(":")
410 .append(j + 1)
411 .append(")\r\n")
412 ;
413 }
414 }
415 }
416
417 if (linei.endsWith("{") || linei.endsWith(";")) {
418 break;
419 }
420 }
421 }
422 }
423 }
424
425 if (errorCount != 0) {
426 errors.insert(0, errorCount + " problems\r\n");
427 errors.setLength(errors.length() - 2);
428 throw new AssertionError(errors.toString());
429 }
430 }
431
432 static final class ValidatorStrings {
433 final String code;
434 final String javadoc;
435 ValidatorStrings(String code, String javadoc) {
436 this.code = code;
437 this.javadoc = javadoc;
438 }
439 }
440
441 static final List<ValidatorStrings> VALIDATOR_STRINGS = Arrays.asList(
442 new ValidatorStrings("Objects.requireNonNull(", "* @throws NullPointerException"),
443 new ValidatorStrings("ObjectHelper.verifyPositive(", "* @throws IllegalArgumentException")
444 );
445
446 static final List<ValidatorStrings> EXCEPTION_STRINGS = Arrays.asList(
447 new ValidatorStrings("throw new NullPointerException(", "* @throws NullPointerException"),
448 new ValidatorStrings("throw new IllegalArgumentException(", "* @throws IllegalArgumentException"),
449 new ValidatorStrings("throw new IndexOutOfBoundsException(", "* @throws IndexOutOfBoundsException")
450 );
451
452 static final List<ValidatorStrings> TYPICAL_ARGUMENT_STRINGS = Arrays.asList(
453 new ValidatorStrings("source", "* @throws NullPointerException"),
454 new ValidatorStrings("source1", "* @throws NullPointerException"),
455 new ValidatorStrings("source2", "* @throws NullPointerException"),
456 new ValidatorStrings("source3", "* @throws NullPointerException"),
457 new ValidatorStrings("source4", "* @throws NullPointerException"),
458 new ValidatorStrings("source5", "* @throws NullPointerException"),
459 new ValidatorStrings("source6", "* @throws NullPointerException"),
460 new ValidatorStrings("source7", "* @throws NullPointerException"),
461 new ValidatorStrings("source8", "* @throws NullPointerException"),
462 new ValidatorStrings("source9", "* @throws NullPointerException"),
463 new ValidatorStrings("sources", "* @throws NullPointerException"),
464 new ValidatorStrings("mapper", "* @throws NullPointerException"),
465 new ValidatorStrings("combiner", "* @throws NullPointerException"),
466 new ValidatorStrings("zipper", "* @throws NullPointerException"),
467 new ValidatorStrings("predicate", "* @throws NullPointerException"),
468 new ValidatorStrings("item", "* @throws NullPointerException"),
469 new ValidatorStrings("item1", "* @throws NullPointerException"),
470 new ValidatorStrings("item2", "* @throws NullPointerException"),
471 new ValidatorStrings("item3", "* @throws NullPointerException"),
472 new ValidatorStrings("item4", "* @throws NullPointerException"),
473 new ValidatorStrings("item5", "* @throws NullPointerException"),
474 new ValidatorStrings("item6", "* @throws NullPointerException"),
475 new ValidatorStrings("item7", "* @throws NullPointerException"),
476 new ValidatorStrings("item8", "* @throws NullPointerException"),
477 new ValidatorStrings("item9", "* @throws NullPointerException"),
478 new ValidatorStrings("item10", "* @throws NullPointerException"),
479 new ValidatorStrings("unit", "* @throws NullPointerException"),
480 new ValidatorStrings("scheduler", "* @throws NullPointerException"),
481 new ValidatorStrings("other", "* @throws NullPointerException"),
482 new ValidatorStrings("fallback", "* @throws NullPointerException"),
483 new ValidatorStrings("defaultItem", "* @throws NullPointerException"),
484 new ValidatorStrings("defaultValue", "* @throws NullPointerException"),
485 new ValidatorStrings("stop", "* @throws NullPointerException"),
486 new ValidatorStrings("stopPredicate", "* @throws NullPointerException"),
487 new ValidatorStrings("handler", "* @throws NullPointerException"),
488 new ValidatorStrings("bufferSupplier", "* @throws NullPointerException"),
489 new ValidatorStrings("openingIndicator", "* @throws NullPointerException"),
490 new ValidatorStrings("closingIndicator", "* @throws NullPointerException"),
491 new ValidatorStrings("boundary", "* @throws NullPointerException"),
492 new ValidatorStrings("boundaryIndicator", "* @throws NullPointerException"),
493 new ValidatorStrings("selector", "* @throws NullPointerException"),
494 new ValidatorStrings("resultSelector", "* @throws NullPointerException"),
495 new ValidatorStrings("keySelector", "* @throws NullPointerException"),
496 new ValidatorStrings("valueSelector", "* @throws NullPointerException"),
497 new ValidatorStrings("valueSupplier", "* @throws NullPointerException"),
498 new ValidatorStrings("collectionSupplier", "* @throws NullPointerException"),
499 new ValidatorStrings("onNext", "* @throws NullPointerException"),
500 new ValidatorStrings("onError", "* @throws NullPointerException"),
501 new ValidatorStrings("onComplete", "* @throws NullPointerException"),
502 new ValidatorStrings("onEvent", "* @throws NullPointerException"),
503 new ValidatorStrings("onAfterNext", "* @throws NullPointerException"),
504 new ValidatorStrings("onAfterTerminate", "* @throws NullPointerException"),
505 new ValidatorStrings("onTerminate", "* @throws NullPointerException"),
506 new ValidatorStrings("onSuccess", "* @throws NullPointerException"),
507 new ValidatorStrings("onSubscribe", "* @throws NullPointerException"),
508 new ValidatorStrings("onNotification", "* @throws NullPointerException"),
509 new ValidatorStrings("onCancel", "* @throws NullPointerException"),
510 new ValidatorStrings("onDispose", "* @throws NullPointerException"),
511 new ValidatorStrings("onRequest", "* @throws NullPointerException"),
512 new ValidatorStrings("onNextMapper", "* @throws NullPointerException"),
513 new ValidatorStrings("onErrorMapper", "* @throws NullPointerException"),
514 new ValidatorStrings("onCompleteSupplier", "* @throws NullPointerException"),
515 new ValidatorStrings("clazz", "* @throws NullPointerException"),
516 new ValidatorStrings("next", "* @throws NullPointerException"),
517 new ValidatorStrings("reducer", "* @throws NullPointerException"),
518 new ValidatorStrings("seed", "* @throws NullPointerException"),
519 new ValidatorStrings("seedSupplier", "* @throws NullPointerException"),
520 new ValidatorStrings("mapSupplier", "* @throws NullPointerException"),
521 new ValidatorStrings("collectionFactory", "* @throws NullPointerException"),
522 new ValidatorStrings("factory", "* @throws NullPointerException"),
523 new ValidatorStrings("stage", "* @throws NullPointerException"),
524 new ValidatorStrings("stream", "* @throws NullPointerException"),
525 new ValidatorStrings("collector", "* @throws NullPointerException"),
526 new ValidatorStrings("subscriptionIndicator", "* @throws NullPointerException"),
527 new ValidatorStrings("itemDelayIndicator", "* @throws NullPointerException"),
528 new ValidatorStrings("future", "* @throws NullPointerException"),
529
530 new ValidatorStrings("maxConcurrency", "* @throws IllegalArgumentException"),
531 new ValidatorStrings("parallelism", "* @throws IllegalArgumentException"),
532 new ValidatorStrings("prefetch", "* @throws IllegalArgumentException"),
533 new ValidatorStrings("bufferSize", "* @throws IllegalArgumentException"),
534 new ValidatorStrings("capacityHint", "* @throws IllegalArgumentException"),
535 new ValidatorStrings("capacity", "* @throws IllegalArgumentException"),
536 new ValidatorStrings("count", "* @throws IllegalArgumentException"),
537 new ValidatorStrings("skip", "* @throws IllegalArgumentException"),
538 new ValidatorStrings("times", "* @throws IllegalArgumentException"),
539 new ValidatorStrings("n", "* @throws IllegalArgumentException")
540 );
541
542 }