| 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 |
/** |
| 19 |
* This TestCase verifies that content of the source files adheres |
| 20 |
* to certain coding practices by matching regular expressions |
| 21 |
* (string patterns): |
| 22 |
* |
| 23 |
* - Verify that Log4J logger is not being used directly |
| 24 |
* ("org.apache.log4j" is not in source files). |
| 25 |
* |
| 26 |
* - Verify that System.out.println is not used except |
| 27 |
* in wsdl to/from java tooling. |
| 28 |
* |
| 29 |
* - Verify that log.info(), log.warn(), log.error(), and log.fatal() |
| 30 |
* use Messages.getMessage() (i18n). |
| 31 |
* |
| 32 |
* - Verify that exceptions are created with Messages.getMessage() (i18n). |
| 33 |
* |
| 34 |
* To add new patterns, search for and append to the |
| 35 |
* private attribute 'avoidPatterns'. |
| 36 |
* |
| 37 |
* Based on code in TestMessages.java. |
| 38 |
*/ |
| 39 |
public class TestSrcContent extends TestCase { |
| 40 |
public TestSrcContent(String name) { |
| 41 |
super(name); |
| 42 |
} // ctor |
| 43 |
|
| 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 |
/** |
| 53 |
* If this test is run from xml-axis/java, then walk through the source |
| 54 |
* tree (xml-axis/java/src), calling checkFile for each file. |
| 55 |
*/ |
| 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 |
} // testSourceFiles |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Walk the source tree |
| 72 |
*/ |
| 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 |
} // walkTree |
| 84 |
|
| 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 |
/** |
| 110 |
* This is not a match IFF |
| 111 |
* - the name matches, AND |
| 112 |
* - the content is not as expected |
| 113 |
*/ |
| 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 |
/** |
| 127 |
* Patterns to be checked. Each pattern has three parameters: |
| 128 |
* (i) a pattern that matches filenames that are to be checked, |
| 129 |
* (ii) a pattern to be searched for in the chosen files |
| 130 |
* (iii) whether the pattern is to be allowed (typically false indicating |
| 131 |
* not allowed) |
| 132 |
* See the Axis Developer's Guide for more information. |
| 133 |
*/ |
| 134 |
private static final FileNameContentPattern avoidPatterns[] = |
| 135 |
{ |
| 136 |
//** |
| 137 |
//** For escape ('\'), remember that Java gets first dibs.. |
| 138 |
//** so double-escape for pattern-matcher to see it. |
| 139 |
//** |
| 140 |
|
| 141 |
// Verify that java files do not use Log4j |
| 142 |
// |
| 143 |
//new FileNameContentPattern(".+\\.java", |
| 144 |
// "org\\.apache\\.log4j", false), |
| 145 |
new FileNameContentPattern(".+([\\\\/])" |
| 146 |
+ "java\\1" |
| 147 |
// + "(?!src\\1org\\1apache\\1axis\\1client\\1HappyClient\\.java)" |
| 148 |
+ "([a-zA-Z0-9_]+\\1)*" |
| 149 |
+ "[^\\\\/]+\\.java", |
| 150 |
"org\\.apache\\.log4j", false), |
| 151 |
|
| 152 |
// Verify that axis java files do not use System.out.println |
| 153 |
// or System.err.println, except: |
| 154 |
// - utils/tcpmon.java |
| 155 |
// - providers/BSFProvider.java |
| 156 |
// - utils/CLArgsParser.java |
| 157 |
// - Version.java |
| 158 |
// - tooling in 'org/apache/axis/wsdl' |
| 159 |
// - client/AdminClient.java |
| 160 |
// - utils/Admin.java |
| 161 |
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 |
// Verify that internationalization is being used properly |
| 177 |
// with logger. Exceptions: |
| 178 |
// - all log.debug calls |
| 179 |
// - client/AdminClient.java |
| 180 |
// - utils/tcpmon.java |
| 181 |
// - utils/Admin.java |
| 182 |
// - handlers/LogMessage.java |
| 183 |
// - tooling in 'org/apache/axis/wsdl' |
| 184 |
// |
| 185 |
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 |
// Verify that exceptions are built with messages. |
| 203 |
|
| 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 |
// Verify that we don't explicitly create NPEs. |
| 213 |
|
| 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 |
// if (content.indexOf(avoidStrings[i]) >= 0) { |
| 234 |
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 |
} // checkFile |
| 251 |
} |