| 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 |
* Test verifying there are no 2..5 empty newlines in the code. |
| 25 |
*/ |
| 26 |
public class TooManyEmptyNewLines { |
| 27 |
|
| 28 |
@Test |
| 29 |
public void tooManyEmptyNewLines2() throws Exception { |
| 30 |
findPattern(2); |
| 31 |
} |
| 32 |
|
| 33 |
@Test |
| 34 |
public void tooManyEmptyNewLines3() throws Exception { |
| 35 |
findPattern(3); |
| 36 |
} |
| 37 |
|
| 38 |
@Test |
| 39 |
public void tooManyEmptyNewLines4() throws Exception { |
| 40 |
findPattern(4); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void tooManyEmptyNewLines5() throws Exception { |
| 45 |
findPattern(5); |
| 46 |
} |
| 47 |
|
| 48 |
static void findPattern(int newLines) throws Exception { |
| 49 |
File f = TestHelper.findSource("Flowable"); |
| 50 |
if (f == null) { |
| 51 |
System.out.println("Unable to find sources of TestHelper.findSourceDir()"); |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
Queue<File> dirs = new ArrayDeque<>(); |
| 56 |
|
| 57 |
StringBuilder fail = new StringBuilder(); |
| 58 |
fail.append("The following code pattern was found: "); |
| 59 |
fail.append("\\R"); |
| 60 |
for (int i = 0; i < newLines; i++) { |
| 61 |
fail.append("\\R"); |
| 62 |
} |
| 63 |
fail.append("\n"); |
| 64 |
|
| 65 |
File parent = f.getParentFile().getParentFile(); |
| 66 |
|
| 67 |
dirs.offer(new File(parent.getAbsolutePath().replace('\\', '/'))); |
| 68 |
dirs.offer(new File(parent.getAbsolutePath().replace('\\', '/').replace("src/main/java", "src/test/java"))); |
| 69 |
|
| 70 |
int total = 0; |
| 71 |
|
| 72 |
while (!dirs.isEmpty()) { |
| 73 |
f = dirs.poll(); |
| 74 |
|
| 75 |
File[] list = f.listFiles(); |
| 76 |
if (list != null && list.length != 0) { |
| 77 |
|
| 78 |
for (File u : list) { |
| 79 |
if (u.isDirectory()) { |
| 80 |
dirs.offer(u); |
| 81 |
} else { |
| 82 |
String fname = u.getName(); |
| 83 |
if (fname.endsWith(".java")) { |
| 84 |
|
| 85 |
List<String> lines = new ArrayList<>(); |
| 86 |
BufferedReader in = new BufferedReader(new FileReader(u)); |
| 87 |
try { |
| 88 |
for (;;) { |
| 89 |
String line = in.readLine(); |
| 90 |
if (line == null) { |
| 91 |
break; |
| 92 |
} |
| 93 |
lines.add(line); |
| 94 |
} |
| 95 |
} finally { |
| 96 |
in.close(); |
| 97 |
} |
| 98 |
|
| 99 |
for (int i = 0; i < lines.size() - newLines; i++) { |
| 100 |
String line1 = lines.get(i); |
| 101 |
if (line1.isEmpty()) { |
| 102 |
int c = 1; |
| 103 |
for (int j = i + 1; j < lines.size(); j++) { |
| 104 |
if (lines.get(j).trim().isEmpty()) { |
| 105 |
c++; |
| 106 |
} else { |
| 107 |
break; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if (c == newLines) { |
| 112 |
fail |
| 113 |
.append(fname) |
| 114 |
.append("#L").append(i + 1) |
| 115 |
.append("\n") |
| 116 |
.append(" at ") |
| 117 |
.append(fname.replace(".java", "")) |
| 118 |
.append(".method(") |
| 119 |
.append(fname) |
| 120 |
.append(":").append(i + 1) |
| 121 |
.append(")\n") |
| 122 |
; |
| 123 |
total++; |
| 124 |
i += c; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
if (total != 0) { |
| 134 |
fail.insert(0, "Found " + total + " instances\n"); |
| 135 |
System.out.println(fail); |
| 136 |
throw new AssertionError(fail.toString()); |
| 137 |
} |
| 138 |
} |
| 139 |
} |