| 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 |
import java.util.regex.Pattern; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 23 |
|
| 24 |
/** |
| 25 |
* Checks for commonly copy-pasted but not-renamed local variables in unit tests. |
| 26 |
* <ul> |
| 27 |
* <li>{@code TestSubscriber} named as {@code to*}</li> |
| 28 |
* <li>{@code TestObserver} named as {@code ts*}</li> |
| 29 |
* <li>{@code PublishProcessor} named as {@code ps*}</li> |
| 30 |
* <li>{@code PublishSubject} named as {@code pp*}</li> |
| 31 |
* <li>{@code Subscription} with single letter name such as "s" or "d"</li> |
| 32 |
* <li>{@code Disposable} with single letter name such as "s" or "d"</li> |
| 33 |
* <li>{@code Flowable} named as {@code o|observable} + number</li> |
| 34 |
* <li>{@code Observable} named as {@code f|flowable} + number</li> |
| 35 |
* <li>{@code Subscriber} named as "o" or "observer"</li> |
| 36 |
* <li>{@code Observer} named as "s" or "subscriber"</li> |
| 37 |
* </ul> |
| 38 |
*/ |
| 39 |
public class CheckLocalVariablesInTests { |
| 40 |
|
| 41 |
static void findPattern(String pattern) throws Exception { |
| 42 |
findPattern(pattern, false); |
| 43 |
} |
| 44 |
|
| 45 |
static void findPattern(String pattern, boolean checkMain) throws Exception { |
| 46 |
File f = TestHelper.findSource("Flowable"); |
| 47 |
if (f == null) { |
| 48 |
System.out.println("Unable to find sources of RxJava"); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
Queue<File> dirs = new ArrayDeque<>(); |
| 53 |
|
| 54 |
StringBuilder fail = new StringBuilder(); |
| 55 |
fail.append("The following code pattern was found: ").append(pattern).append("\n"); |
| 56 |
|
| 57 |
File parent = f.getParentFile().getParentFile(); |
| 58 |
|
| 59 |
if (checkMain) { |
| 60 |
dirs.offer(new File(parent.getAbsolutePath().replace('\\', '/'))); |
| 61 |
} |
| 62 |
dirs.offer(new File(parent.getAbsolutePath().replace('\\', '/').replace("src/main/java", "src/test/java"))); |
| 63 |
|
| 64 |
Pattern p = Pattern.compile(pattern); |
| 65 |
|
| 66 |
int total = 0; |
| 67 |
|
| 68 |
while (!dirs.isEmpty()) { |
| 69 |
f = dirs.poll(); |
| 70 |
|
| 71 |
File[] list = f.listFiles(); |
| 72 |
if (list != null && list.length != 0) { |
| 73 |
|
| 74 |
for (File u : list) { |
| 75 |
if (u.isDirectory()) { |
| 76 |
dirs.offer(u); |
| 77 |
} else { |
| 78 |
String fname = u.getName(); |
| 79 |
if (fname.endsWith(".java")) { |
| 80 |
|
| 81 |
int lineNum = 0; |
| 82 |
BufferedReader in = new BufferedReader(new FileReader(u)); |
| 83 |
try { |
| 84 |
for (;;) { |
| 85 |
String line = in.readLine(); |
| 86 |
if (line != null) { |
| 87 |
lineNum++; |
| 88 |
|
| 89 |
line = line.trim(); |
| 90 |
|
| 91 |
if (!line.startsWith("//") && !line.startsWith("*")) { |
| 92 |
if (p.matcher(line).find()) { |
| 93 |
fail |
| 94 |
.append(fname) |
| 95 |
.append("#L").append(lineNum) |
| 96 |
.append(" ").append(line) |
| 97 |
.append("\n") |
| 98 |
.append(" at ") |
| 99 |
.append(fname.replace(".java", "")) |
| 100 |
.append(".method(") |
| 101 |
.append(fname) |
| 102 |
.append(":") |
| 103 |
.append(lineNum) |
| 104 |
.append(")\n"); |
| 105 |
|
| 106 |
total++; |
| 107 |
} |
| 108 |
} |
| 109 |
} else { |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
} finally { |
| 114 |
in.close(); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
if (total != 0) { |
| 122 |
fail.insert(0, "Found " + total + " instances"); |
| 123 |
System.out.println(fail); |
| 124 |
throw new AssertionError(fail.toString()); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
@Test |
| 129 |
public void subscriberAsTo() throws Exception { |
| 130 |
findPattern("TestSubscriber(Ex)?<.*>\\s+to"); |
| 131 |
} |
| 132 |
|
| 133 |
@Test |
| 134 |
public void observerAsTs() throws Exception { |
| 135 |
findPattern("TestObserver(Ex)?<.*>\\s+ts"); |
| 136 |
} |
| 137 |
|
| 138 |
@Test |
| 139 |
public void subscriberNoArgAsTo() throws Exception { |
| 140 |
findPattern("TestSubscriber(Ex)?\\s+to"); |
| 141 |
} |
| 142 |
|
| 143 |
@Test |
| 144 |
public void observerNoArgAsTs() throws Exception { |
| 145 |
findPattern("TestObserver(Ex)?\\s+ts"); |
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void publishSubjectAsPp() throws Exception { |
| 150 |
findPattern("PublishSubject<.*>\\s+pp"); |
| 151 |
} |
| 152 |
|
| 153 |
@Test |
| 154 |
public void publishProcessorAsPs() throws Exception { |
| 155 |
findPattern("PublishProcessor<.*>\\s+ps"); |
| 156 |
} |
| 157 |
|
| 158 |
@Test |
| 159 |
public void unicastSubjectAsUp() throws Exception { |
| 160 |
findPattern("UnicastSubject<.*>\\s+up"); |
| 161 |
} |
| 162 |
|
| 163 |
@Test |
| 164 |
public void unicastProcessorAsUs() throws Exception { |
| 165 |
findPattern("UnicastProcessor<.*>\\s+us"); |
| 166 |
} |
| 167 |
|
| 168 |
@Test |
| 169 |
public void behaviorProcessorAsBs() throws Exception { |
| 170 |
findPattern("BehaviorProcessor<.*>\\s+bs"); |
| 171 |
} |
| 172 |
|
| 173 |
@Test |
| 174 |
public void behaviorSubjectAsBp() throws Exception { |
| 175 |
findPattern("BehaviorSubject<.*>\\s+bp"); |
| 176 |
} |
| 177 |
|
| 178 |
@Test |
| 179 |
public void connectableFlowableAsCo() throws Exception { |
| 180 |
findPattern("ConnectableFlowable<.*>\\s+co(0-9|\\b)"); |
| 181 |
} |
| 182 |
|
| 183 |
@Test |
| 184 |
public void connectableObservableAsCf() throws Exception { |
| 185 |
findPattern("ConnectableObservable<.*>\\s+cf(0-9|\\b)"); |
| 186 |
} |
| 187 |
|
| 188 |
@Test |
| 189 |
public void queueDisposableInsteadOfQueueFuseable() throws Exception { |
| 190 |
findPattern("QueueDisposable\\.(NONE|SYNC|ASYNC|ANY|BOUNDARY)"); |
| 191 |
} |
| 192 |
|
| 193 |
@Test |
| 194 |
public void queueSubscriptionInsteadOfQueueFuseable() throws Exception { |
| 195 |
findPattern("QueueSubscription\\.(NONE|SYNC|ASYNC|ANY|BOUNDARY)"); |
| 196 |
} |
| 197 |
|
| 198 |
@Test |
| 199 |
public void singleSourceAsMs() throws Exception { |
| 200 |
findPattern("SingleSource<.*>\\s+ms"); |
| 201 |
} |
| 202 |
|
| 203 |
@Test |
| 204 |
public void singleSourceAsCs() throws Exception { |
| 205 |
findPattern("SingleSource<.*>\\s+cs"); |
| 206 |
} |
| 207 |
|
| 208 |
@Test |
| 209 |
public void maybeSourceAsSs() throws Exception { |
| 210 |
findPattern("MaybeSource<.*>\\s+ss"); |
| 211 |
} |
| 212 |
|
| 213 |
@Test |
| 214 |
public void maybeSourceAsCs() throws Exception { |
| 215 |
findPattern("MaybeSource<.*>\\s+cs"); |
| 216 |
} |
| 217 |
|
| 218 |
@Test |
| 219 |
public void completableSourceAsSs() throws Exception { |
| 220 |
findPattern("CompletableSource<.*>\\s+ss"); |
| 221 |
} |
| 222 |
|
| 223 |
@Test |
| 224 |
public void completableSourceAsMs() throws Exception { |
| 225 |
findPattern("CompletableSource<.*>\\s+ms"); |
| 226 |
} |
| 227 |
|
| 228 |
@Test |
| 229 |
public void observableAsC() throws Exception { |
| 230 |
findPattern("Observable<.*>\\s+c\\b"); |
| 231 |
} |
| 232 |
|
| 233 |
@Test |
| 234 |
public void subscriberAsObserver() throws Exception { |
| 235 |
findPattern("Subscriber<.*>\\s+observer[0-9]?\\b"); |
| 236 |
} |
| 237 |
|
| 238 |
@Test |
| 239 |
public void subscriberAsO() throws Exception { |
| 240 |
findPattern("Subscriber<.*>\\s+o[0-9]?\\b"); |
| 241 |
} |
| 242 |
|
| 243 |
@Test |
| 244 |
public void singleAsObservable() throws Exception { |
| 245 |
findPattern("Single<.*>\\s+observable\\b"); |
| 246 |
} |
| 247 |
|
| 248 |
@Test |
| 249 |
public void singleAsFlowable() throws Exception { |
| 250 |
findPattern("Single<.*>\\s+flowable\\b"); |
| 251 |
} |
| 252 |
|
| 253 |
@Test |
| 254 |
public void observerAsSubscriber() throws Exception { |
| 255 |
findPattern("Observer<.*>\\s+subscriber[0-9]?\\b"); |
| 256 |
} |
| 257 |
|
| 258 |
@Test |
| 259 |
public void observerAsS() throws Exception { |
| 260 |
findPattern("Observer<.*>\\s+s[0-9]?\\b"); |
| 261 |
} |
| 262 |
|
| 263 |
@Test |
| 264 |
public void observerNoArgAsSubscriber() throws Exception { |
| 265 |
findPattern("Observer\\s+subscriber[0-9]?\\b"); |
| 266 |
} |
| 267 |
|
| 268 |
@Test |
| 269 |
public void observerNoArgAsS() throws Exception { |
| 270 |
findPattern("Observer\\s+s[0-9]?\\b"); |
| 271 |
} |
| 272 |
|
| 273 |
@Test |
| 274 |
public void flowableAsObservable() throws Exception { |
| 275 |
findPattern("Flowable<.*>\\s+observable[0-9]?\\b"); |
| 276 |
} |
| 277 |
|
| 278 |
@Test |
| 279 |
public void flowableAsO() throws Exception { |
| 280 |
findPattern("Flowable<.*>\\s+o[0-9]?\\b"); |
| 281 |
} |
| 282 |
|
| 283 |
@Test |
| 284 |
public void flowableNoArgAsO() throws Exception { |
| 285 |
findPattern("Flowable\\s+o[0-9]?\\b"); |
| 286 |
} |
| 287 |
|
| 288 |
@Test |
| 289 |
public void flowableNoArgAsObservable() throws Exception { |
| 290 |
findPattern("Flowable\\s+observable[0-9]?\\b"); |
| 291 |
} |
| 292 |
|
| 293 |
@Test |
| 294 |
public void processorAsSubject() throws Exception { |
| 295 |
findPattern("Processor<.*>\\s+subject(0-9)?\\b"); |
| 296 |
} |
| 297 |
|
| 298 |
@Test |
| 299 |
public void maybeAsObservable() throws Exception { |
| 300 |
findPattern("Maybe<.*>\\s+observable\\b"); |
| 301 |
} |
| 302 |
|
| 303 |
@Test |
| 304 |
public void maybeAsFlowable() throws Exception { |
| 305 |
findPattern("Maybe<.*>\\s+flowable\\b"); |
| 306 |
} |
| 307 |
|
| 308 |
@Test |
| 309 |
public void completableAsObservable() throws Exception { |
| 310 |
findPattern("Completable\\s+observable\\b"); |
| 311 |
} |
| 312 |
|
| 313 |
@Test |
| 314 |
public void completableAsFlowable() throws Exception { |
| 315 |
findPattern("Completable\\s+flowable\\b"); |
| 316 |
} |
| 317 |
|
| 318 |
@Test |
| 319 |
public void subscriptionAsFieldS() throws Exception { |
| 320 |
findPattern("Subscription\\s+s[0-9]?;", true); |
| 321 |
} |
| 322 |
|
| 323 |
@Test |
| 324 |
public void subscriptionAsD() throws Exception { |
| 325 |
findPattern("Subscription\\s+d[0-9]?", true); |
| 326 |
} |
| 327 |
|
| 328 |
@Test |
| 329 |
public void subscriptionAsSubscription() throws Exception { |
| 330 |
findPattern("Subscription\\s+subscription[0-9]?;", true); |
| 331 |
} |
| 332 |
|
| 333 |
@Test |
| 334 |
public void subscriptionAsDParenthesis() throws Exception { |
| 335 |
findPattern("Subscription\\s+d[0-9]?\\)", true); |
| 336 |
} |
| 337 |
|
| 338 |
@Test |
| 339 |
public void queueSubscriptionAsD() throws Exception { |
| 340 |
findPattern("Subscription<.*>\\s+q?d[0-9]?\\b", true); |
| 341 |
} |
| 342 |
|
| 343 |
@Test |
| 344 |
public void booleanSubscriptionAsbd() throws Exception { |
| 345 |
findPattern("BooleanSubscription\\s+bd[0-9]?;", true); |
| 346 |
} |
| 347 |
|
| 348 |
@Test |
| 349 |
public void atomicSubscriptionAsS() throws Exception { |
| 350 |
findPattern("AtomicReference<Subscription>\\s+s[0-9]?;", true); |
| 351 |
} |
| 352 |
|
| 353 |
@Test |
| 354 |
public void atomicSubscriptionAsSInit() throws Exception { |
| 355 |
findPattern("AtomicReference<Subscription>\\s+s[0-9]?\\s", true); |
| 356 |
} |
| 357 |
|
| 358 |
@Test |
| 359 |
public void atomicSubscriptionAsSubscription() throws Exception { |
| 360 |
findPattern("AtomicReference<Subscription>\\s+subscription[0-9]?", true); |
| 361 |
} |
| 362 |
|
| 363 |
@Test |
| 364 |
public void atomicSubscriptionAsD() throws Exception { |
| 365 |
findPattern("AtomicReference<Subscription>\\s+d[0-9]?", true); |
| 366 |
} |
| 367 |
|
| 368 |
@Test |
| 369 |
public void disposableAsS() throws Exception { |
| 370 |
// the space before makes sure it doesn't match onSubscribe(Subscription) unnecessarily |
| 371 |
findPattern("Disposable\\s+s[0-9]?\\b", true); |
| 372 |
} |
| 373 |
|
| 374 |
@Test |
| 375 |
public void disposableAsFieldD() throws Exception { |
| 376 |
findPattern("Disposable\\s+d[0-9]?;", true); |
| 377 |
} |
| 378 |
|
| 379 |
@Test |
| 380 |
public void atomicDisposableAsS() throws Exception { |
| 381 |
findPattern("AtomicReference<Disposable>\\s+s[0-9]?", true); |
| 382 |
} |
| 383 |
|
| 384 |
@Test |
| 385 |
public void atomicDisposableAsD() throws Exception { |
| 386 |
findPattern("AtomicReference<Disposable>\\s+d[0-9]?;", true); |
| 387 |
} |
| 388 |
|
| 389 |
@Test |
| 390 |
public void subscriberAsFieldActual() throws Exception { |
| 391 |
findPattern("Subscriber<.*>\\s+actual[;\\)]", true); |
| 392 |
} |
| 393 |
|
| 394 |
@Test |
| 395 |
public void subscriberNoArgAsFieldActual() throws Exception { |
| 396 |
findPattern("Subscriber\\s+actual[;\\)]", true); |
| 397 |
} |
| 398 |
|
| 399 |
@Test |
| 400 |
public void subscriberAsFieldS() throws Exception { |
| 401 |
findPattern("Subscriber<.*>\\s+s[0-9]?;", true); |
| 402 |
} |
| 403 |
|
| 404 |
@Test |
| 405 |
public void observerAsFieldActual() throws Exception { |
| 406 |
findPattern("Observer<.*>\\s+actual[;\\)]", true); |
| 407 |
} |
| 408 |
|
| 409 |
@Test |
| 410 |
public void observerAsFieldSO() throws Exception { |
| 411 |
findPattern("Observer<.*>\\s+[so][0-9]?;", true); |
| 412 |
} |
| 413 |
|
| 414 |
@Test |
| 415 |
public void observerNoArgAsFieldActual() throws Exception { |
| 416 |
findPattern("Observer\\s+actual[;\\)]", true); |
| 417 |
} |
| 418 |
|
| 419 |
@Test |
| 420 |
public void observerNoArgAsFieldCs() throws Exception { |
| 421 |
findPattern("Observer\\s+cs[;\\)]", true); |
| 422 |
} |
| 423 |
|
| 424 |
@Test |
| 425 |
public void observerNoArgAsFieldSO() throws Exception { |
| 426 |
findPattern("Observer\\s+[so][0-9]?;", true); |
| 427 |
} |
| 428 |
|
| 429 |
@Test |
| 430 |
public void queueDisposableAsD() throws Exception { |
| 431 |
findPattern("Disposable<.*>\\s+q?s[0-9]?\\b", true); |
| 432 |
} |
| 433 |
|
| 434 |
@Test |
| 435 |
public void disposableAsDParenthesis() throws Exception { |
| 436 |
findPattern("Disposable\\s+s[0-9]?\\)", true); |
| 437 |
} |
| 438 |
|
| 439 |
@Test |
| 440 |
public void compositeDisposableAsCs() throws Exception { |
| 441 |
findPattern("CompositeDisposable\\s+cs[0-9]?", true); |
| 442 |
} |
| 443 |
|
| 444 |
} |