1 package test.utils; 2 3 import junit.framework.AssertionFailedError; 4 import junit.framework.Test; 5 import junit.framework.TestCase; 6 import junit.framework.TestSuite; 7 import org.apache.oro.text.regex.MalformedPatternException; 8 import org.apache.oro.text.regex.Pattern; 9 import org.apache.oro.text.regex.PatternCompiler; 10 import org.apache.oro.text.regex.PatternMatcher; 11 import org.apache.oro.text.regex.Perl5Compiler; 12 import org.apache.oro.text.regex.Perl5Matcher; 13 14 15 import java.io.File ; 16 import java.io.FileInputStream ; 17 18 39 public class TestSrcContent extends TestCase { 40 public TestSrcContent(String name) { 41 super(name); 42 } 44 public static Test suite() { 45 return new TestSuite(TestSrcContent.class); 46 } 47 48 private static final String LS = System.getProperty("line.separator"); 49 50 private String errors = ""; 51 52 56 public void testSourceFiles() { 57 String baseDir = System.getProperty("user.dir"); 58 File srcDir = new File (baseDir, "src"); 59 60 if (srcDir.exists()) { 61 walkTree(srcDir); 62 } 63 64 if (!errors.equals("")) { 65 throw new AssertionFailedError(errors); 66 } 67 } 69 70 73 private void walkTree(File srcDir) { 74 File [] files = srcDir.listFiles(); 75 for (int i = 0; i < files.length; ++i) { 76 if (files[i].isDirectory()) { 77 walkTree(files[i]); 78 } 79 else { 80 checkFile(files[i]); 81 } 82 } 83 } 85 86 static private class FileNameContentPattern 87 { 88 private PatternCompiler compiler = new Perl5Compiler(); 89 private PatternMatcher matcher = new Perl5Matcher(); 90 91 private Pattern namePattern = null; 92 private Pattern contentPattern = null; 93 private boolean expectContent = true; 94 95 FileNameContentPattern(String namePattern, 96 String contentPattern, 97 boolean expectContentInFile) 98 { 99 try { 100 this.namePattern = compiler.compile(namePattern); 101 this.contentPattern = compiler.compile(contentPattern); 102 this.expectContent = expectContentInFile; 103 } 104 catch (MalformedPatternException e) { 105 throw new AssertionFailedError(e.getMessage()); 106 } 107 } 108 109 114 boolean noMatch(String name, String content) 115 { 116 return 117 matcher.matches(name, namePattern) && 118 matcher.contains(content, contentPattern) != expectContent; 119 } 120 121 String getContentPattern() { return contentPattern.getPattern(); } 122 123 boolean getExpectContent() { return expectContent; } 124 }; 125 126 134 private static final FileNameContentPattern avoidPatterns[] = 135 { 136 141 new FileNameContentPattern(".+([\\\\/])" 146 + "java\\1" 147 + "([a-zA-Z0-9_]+\\1)*" 149 + "[^\\\\/]+\\.java", 150 "org\\.apache\\.log4j", false), 151 152 new FileNameContentPattern(".+([\\\\/])" 162 + "java\\1src\\1org\\1apache\\1axis\\1" 163 + "(?!utils\\1tcpmon\\.java" 164 + "|client\\1AdminClient\\.java" 165 + "|utils\\1Admin\\.java" 166 + "|utils\\1SOAPMonitor\\.java" 167 + "|providers\\1BSFProvider\\.java" 168 + "|utils\\1CLArgsParser\\.java" 169 + "|transport\\1jms\\1SimpleJMSListener\\.java" 170 + "|Version\\.java" 171 + "|wsdl\\1)" 172 + "([a-zA-Z0-9_]+\\1)*" 173 + "[^\\\\/]+\\.java", 174 "System\\.(out|err)\\.println", false), 175 176 new FileNameContentPattern(".+([\\\\/])" 186 + "java\\1src\\1org\\1apache\\1axis\\1" 187 + "(?!utils\\1tcpmon\\.java" 188 + "|client\\1AdminClient\\.java" 189 + "|utils\\1Admin\\.java" 190 + "|utils\\1SOAPMonitor\\.java" 191 + "|handlers\\1LogMessage\\.java" 192 + "|wsdl\\1)" 193 + "([a-zA-Z0-9_]+\\1)*" 194 + "[^\\\\/]+\\.java", 195 196 "log\\.(info|warn|error|fatal)" 197 + "[ \\t]*\\(" 198 + "(?=[ \\t]*\\\")", 199 200 false), 201 202 204 new FileNameContentPattern(".+([\\\\/])" 205 + "java\\1src\\1org\\1apache\\1axis\\1" 206 + "([a-zA-Z0-9_]+\\1)*" 207 + "[^\\\\/]+\\.java", 208 "new[ \\t]+[a-zA-Z0-9_]*" 209 + "Exception\\(\\)", 210 false), 211 212 214 new FileNameContentPattern(".+([\\\\/])" 215 + "java\\1src\\1org\\1apache\\1axis\\1" 216 + "([a-zA-Z0-9_]+\\1)*" 217 + "[^\\\\/]+\\.java", 218 "new[ \\t]+" 219 + "NullPointerException", 220 false), 221 222 }; 223 224 private void checkFile(File file) { 225 try { 226 FileInputStream fis = new FileInputStream (file); 227 byte[] bytes = new byte[fis.available()]; 228 fis.read(bytes); 229 String content = new String (bytes); 230 231 for (int i = 0; i < avoidPatterns.length; i++) { 232 if (avoidPatterns[i].noMatch(file.getPath(), content)) { 233 errors = errors 235 + "File: " + file.getPath() + ": " 236 + (avoidPatterns[i].getExpectContent() 237 ? "Expected: " 238 : "Unexpected: ") 239 + avoidPatterns[i].getContentPattern() 240 + LS; 241 } 242 } 243 } 244 catch (Throwable t) { 245 errors = errors 246 + "File: " + file.getPath() 247 + ": " + t.getMessage() 248 + LS; 249 } 250 } } 252 | Popular Tags |