| 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.testsupport.TestHelper; |
| 23 |
|
| 24 |
/** |
| 25 |
* Check if a {@code catch(Throwable} is followed by a |
| 26 |
* {@code Exceptions.throwIfFatal}, {@code Exceptions.wrapOrThrow} |
| 27 |
* or {@code fail} call. |
| 28 |
* @since 3.0.0 |
| 29 |
*/ |
| 30 |
public class CatchThrowIfFatalCheck { |
| 31 |
|
| 32 |
@Test |
| 33 |
public void check() throws Exception { |
| 34 |
File f = TestHelper.findSource("Flowable"); |
| 35 |
if (f == null) { |
| 36 |
System.out.println("Unable to find sources of RxJava"); |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
Queue<File> dirs = new ArrayDeque<>(); |
| 41 |
|
| 42 |
StringBuilder fail = new StringBuilder(); |
| 43 |
int errors = 0; |
| 44 |
|
| 45 |
File parent = f.getParentFile().getParentFile(); |
| 46 |
|
| 47 |
dirs.offer(new File(parent.getAbsolutePath().replace('\\', '/'))); |
| 48 |
|
| 49 |
while (!dirs.isEmpty()) { |
| 50 |
f = dirs.poll(); |
| 51 |
|
| 52 |
File[] list = f.listFiles(); |
| 53 |
if (list != null && list.length != 0) { |
| 54 |
|
| 55 |
for (File u : list) { |
| 56 |
if (u.isDirectory()) { |
| 57 |
dirs.offer(u); |
| 58 |
} else { |
| 59 |
List<String> lines = Files.readAllLines(u.toPath()); |
| 60 |
|
| 61 |
for (int i = 0; i < lines.size(); i++) { |
| 62 |
String line = lines.get(i).trim(); |
| 63 |
|
| 64 |
if (line.startsWith("} catch (Throwable ")) { |
| 65 |
String next = lines.get(i + 1).trim(); |
| 66 |
boolean throwIfFatal = next.contains("Exceptions.throwIfFatal"); |
| 67 |
boolean wrapOrThrow = next.contains("ExceptionHelper.wrapOrThrow"); |
| 68 |
boolean failCall = next.startsWith("fail("); |
| 69 |
|
| 70 |
if (!(throwIfFatal || wrapOrThrow || failCall)) { |
| 71 |
errors++; |
| 72 |
fail.append("Missing Exceptions.throwIfFatal\n ") |
| 73 |
.append(next) |
| 74 |
.append("\n at ") |
| 75 |
.append(u.getName().replace(".java", "")) |
| 76 |
.append(".method(") |
| 77 |
.append(u.getName()) |
| 78 |
.append(":") |
| 79 |
.append(i + 1) |
| 80 |
.append(")\n") |
| 81 |
; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if (errors != 0) { |
| 91 |
fail.insert(0, "Found " + errors + " cases\n"); |
| 92 |
System.out.println(fail); |
| 93 |
throw new AssertionError(fail.toString()); |
| 94 |
} |
| 95 |
} |
| 96 |
} |