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