TestSrcContentpublic class TestSrcContent extends TestCase This TestCase verifies that content of the source files adheres
to certain coding practices by matching regular expressions
(string patterns):
- Verify that Log4J logger is not being used directly
("org.apache.log4j" is not in source files).
- Verify that System.out.println is not used except
in wsdl to/from java tooling.
- Verify that log.info(), log.warn(), log.error(), and log.fatal()
use Messages.getMessage() (i18n).
- Verify that exceptions are created with Messages.getMessage() (i18n).
To add new patterns, search for and append to the
private attribute 'avoidPatterns'.
Based on code in TestMessages.java. |
Fields Summary |
---|
private static final String | LS | private String | errors | private static final FileNameContentPattern[] | avoidPatternsPatterns to be checked. Each pattern has three parameters:
(i) a pattern that matches filenames that are to be checked,
(ii) a pattern to be searched for in the chosen files
(iii) whether the pattern is to be allowed (typically false indicating
not allowed)
See the Axis Developer's Guide for more information. |
Constructors Summary |
---|
public TestSrcContent(String name)
super(name);
|
Methods Summary |
---|
private void | checkFile(java.io.File file)
try {
FileInputStream fis = new FileInputStream(file);
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
String content = new String(bytes);
for (int i = 0; i < avoidPatterns.length; i++) {
if (avoidPatterns[i].noMatch(file.getPath(), content)) {
// if (content.indexOf(avoidStrings[i]) >= 0) {
errors = errors
+ "File: " + file.getPath() + ": "
+ (avoidPatterns[i].getExpectContent()
? "Expected: "
: "Unexpected: ")
+ avoidPatterns[i].getContentPattern()
+ LS;
}
}
}
catch (Throwable t) {
errors = errors
+ "File: " + file.getPath()
+ ": " + t.getMessage()
+ LS;
}
| public static junit.framework.Test | suite()
return new TestSuite(TestSrcContent.class);
| public void | testSourceFiles()If this test is run from xml-axis/java, then walk through the source
tree (xml-axis/java/src), calling checkFile for each file.
String baseDir = System.getProperty("user.dir");
File srcDir = new File(baseDir, "src");
if (srcDir.exists()) {
walkTree(srcDir);
}
if (!errors.equals("")) {
throw new AssertionFailedError(errors);
}
| private void | walkTree(java.io.File srcDir)Walk the source tree
File[] files = srcDir.listFiles();
for (int i = 0; i < files.length; ++i) {
if (files[i].isDirectory()) {
walkTree(files[i]);
}
else {
checkFile(files[i]);
}
}
|
|