| 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.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 23 |
|
| 24 |
/** |
| 25 |
* Check verifying there are no methods with the prefix "test" in the name. |
| 26 |
*/ |
| 27 |
public class TestPrefixInMethodName { |
| 28 |
|
| 29 |
private static final String pattern = "void\\s+test[a-zA-Z0-9]"; |
| 30 |
private static final String replacement = "void "; |
| 31 |
|
| 32 |
@Test |
| 33 |
public void checkAndUpdateTestMethodNames() 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 |
fail.append("The following code pattern was found: ").append(pattern).append("\n"); |
| 44 |
fail.append("Refresh and re-run tests!\n\n"); |
| 45 |
|
| 46 |
File parent = f.getParentFile().getParentFile(); |
| 47 |
|
| 48 |
dirs.offer(new File(parent.getAbsolutePath().replace('\\', '/'))); |
| 49 |
dirs.offer(new File(parent.getAbsolutePath().replace('\\', '/').replace("src/main/java", "src/test/java"))); |
| 50 |
|
| 51 |
Pattern p = Pattern.compile(pattern); |
| 52 |
|
| 53 |
int total = 0; |
| 54 |
|
| 55 |
while (!dirs.isEmpty()) { |
| 56 |
f = dirs.poll(); |
| 57 |
|
| 58 |
File[] list = f.listFiles(); |
| 59 |
if (list != null && list.length != 0) { |
| 60 |
|
| 61 |
for (File u : list) { |
| 62 |
if (u.isDirectory()) { |
| 63 |
dirs.offer(u); |
| 64 |
} else { |
| 65 |
String fname = u.getName(); |
| 66 |
if (fname.endsWith(".java")) { |
| 67 |
|
| 68 |
int lineNum = 0; |
| 69 |
List<String> lines = new ArrayList<>(); |
| 70 |
BufferedReader in = new BufferedReader(new FileReader(u)); |
| 71 |
//boolean found = false; |
| 72 |
try { |
| 73 |
for (; ; ) { |
| 74 |
String line = in.readLine(); |
| 75 |
if (line == null) { |
| 76 |
break; |
| 77 |
} |
| 78 |
lineNum++; |
| 79 |
|
| 80 |
Matcher matcher = p.matcher(line); |
| 81 |
if (!line.startsWith("//") && !line.startsWith("*") && matcher.find()) { |
| 82 |
// found = true; |
| 83 |
fail |
| 84 |
.append(fname) |
| 85 |
.append("#L").append(lineNum) |
| 86 |
.append(" ").append(line) |
| 87 |
.append("\n"); |
| 88 |
total++; |
| 89 |
|
| 90 |
int methodNameStartIndex = matcher.end() - 1; |
| 91 |
char firstChar = Character.toLowerCase(line.charAt(methodNameStartIndex)); |
| 92 |
|
| 93 |
String newLine = matcher.replaceAll(replacement + firstChar); |
| 94 |
|
| 95 |
lines.add(newLine); |
| 96 |
} else { |
| 97 |
lines.add(line); |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
} finally { |
| 102 |
in.close(); |
| 103 |
} |
| 104 |
|
| 105 |
/*if (found && System.getenv("CI") == null) { |
| 106 |
PrintWriter w = new PrintWriter(new FileWriter(u)); |
| 107 |
|
| 108 |
try { |
| 109 |
for (String s : lines) { |
| 110 |
w.println(s); |
| 111 |
} |
| 112 |
} finally { |
| 113 |
w.close(); |
| 114 |
} |
| 115 |
}*/ |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
if (total != 0) { |
| 122 |
fail.append("Found ") |
| 123 |
.append(total) |
| 124 |
.append(" instances"); |
| 125 |
System.out.println(fail); |
| 126 |
throw new AssertionError(fail.toString()); |
| 127 |
} |
| 128 |
} |
| 129 |
} |