rsitze 02/02/22 07:39:04 Modified: java build.xml java/test/utils TestSrcContent.java Log: Updated tests to check for inappropriate use of System.{out|err}.println Revision Changes Path 1.113 +9 -0 xml-axis/java/build.xml Index: build.xml =================================================================== RCS file: /home/cvs/xml-axis/java/build.xml,v retrieving revision 1.112 retrieving revision 1.113 diff -u -r1.112 -r1.113 --- build.xml 20 Feb 2002 18:59:20 -0000 1.112 +++ build.xml 22 Feb 2002 15:39:04 -0000 1.113 @@ -82,6 +82,8 @@ <property name="clutil.jar" value="lib/clutil.jar"/> <property name="tt-bytecode.jar" value="lib/tt-bytecode.jar"/> + <property name="regexp.jar" value="test/lib/jakarta-oro-2.0.5.jar"/> + <property name="junit.jar" value="lib/junit.jar"/> <property name="packages" value="org.*,javax.*"/> @@ -107,6 +109,7 @@ <path id="classpath"> <pathelement location="${clutil.jar}"/> <pathelement location="${xerces.jar}"/> + <pathelement location="${regexp.jar}"/> <pathelement location="${junit.jar}"/> <pathelement location="${excalibur.jar}"/> <pathelement location="${java.home}/../lib/tools.jar"/> @@ -124,6 +127,10 @@ classname="javax.servlet.Servlet" classpathref="classpath"/> + <available property="regexp.present" + classname="org.apache.oro.text.regex.Pattern" + classpathref="classpath"/> + <available property="junit.present" classname="junit.framework.TestCase" classpathref="classpath"/> @@ -190,6 +197,7 @@ <echo message=""/> <echo message="--- Optional Libraries ---" /> <echo message="servlet.present=${servlet.present}" /> + <echo message="regexp.present=${regexp.present}" /> <echo message="junit.present=${junit.present}" /> <echo message="activation.present=${activation.present}" /> <echo message="mailapi.present=${mailapi.present}" /> @@ -317,6 +325,7 @@ <path refid="classpath"/> </classpath> <include name="test/**/*.java" /> + <exclude name="test/lib/*.java"/> <exclude name="test/inout/*.java" /> <exclude name="test/wsdl/*/*.java" /> <exclude name="test/wsdl/interop3/groupE/**/*.java" /> 1.2 +114 -32 xml-axis/java/test/utils/TestSrcContent.java Index: TestSrcContent.java =================================================================== RCS file: /home/cvs/xml-axis/java/test/utils/TestSrcContent.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TestSrcContent.java 19 Feb 2002 17:38:23 -0000 1.1 +++ TestSrcContent.java 22 Feb 2002 15:39:04 -0000 1.2 @@ -14,20 +14,28 @@ import junit.framework.TestCase; import junit.framework.TestSuite; +import org.apache.oro.text.regex.Pattern; +import org.apache.oro.text.regex.PatternMatcher; +import org.apache.oro.text.regex.PatternCompiler; +import org.apache.oro.text.regex.Perl5Matcher; +import org.apache.oro.text.regex.Perl5Compiler; +import org.apache.oro.text.regex.MalformedPatternException; + import org.apache.axis.utils.JavaUtils; /** * This TestCase verifies that content of the source files adheres - * to certain coding practices by looking for "illegal" strings: + * 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). * - * - !! Someday: look for System.out.println .... !! - * + * - Verify that System.out.println is not used except + * in wsdl to/from java tooling. * - * To add new strings to "avoid", search for and append to the - * private attribute 'avoidStrings'. + * To add new patterns, search for and append to the + * private attribute 'avoidPatterns'. * * Based on code in TestMessages.java. */ @@ -40,6 +48,10 @@ return new TestSuite(TestSrcContent.class); } + private final String LS = System.getProperty("line.separator"); + + private String errors = ""; + /** * If this test is run from xml-axis/java, then walk through the source * tree (xml-axis/java/src), calling checkFile for each file. @@ -48,62 +60,132 @@ String baseDir = System.getProperty("user.dir"); File srcDir = new File(baseDir, "src"); - if (srcDir.exists() && !walkTree(srcDir)) { - throw new AssertionFailedError("Unexpected source file content"); + if (srcDir.exists()) { + walkTree(srcDir); + } + + if (!errors.equals("")) { + throw new AssertionFailedError(errors); } } // testSourceFiles + /** * Walk the source tree */ - private boolean walkTree(File srcDir) { - boolean cleanWalk = true; - + private void walkTree(File srcDir) { File[] files = srcDir.listFiles(); for (int i = 0; i < files.length; ++i) { - - // beware 'shortcuts' in logic operations... if (files[i].isDirectory()) { - cleanWalk = walkTree(files[i]) && cleanWalk; + walkTree(files[i]); } - else if (files[i].getName().endsWith(".java")) { - cleanWalk = checkFile(files[i]) && cleanWalk; + else { + checkFile(files[i]); } } - - return cleanWalk; } // walkTree + + static private class FileNameContentPattern + { + private PatternCompiler compiler = new Perl5Compiler(); + private PatternMatcher matcher = new Perl5Matcher(); + + private Pattern namePattern = null; + private Pattern contentPattern = null; + private boolean expectContent = true; + + FileNameContentPattern(String namePattern, + String contentPattern, + boolean expectContentInFile) + { + try { + this.namePattern = compiler.compile(namePattern); + this.contentPattern = compiler.compile(contentPattern); + this.expectContent = expectContentInFile; + } + catch (MalformedPatternException e) { + throw new AssertionFailedError(e.getMessage()); + } + } + + /** + * This is not a match IFF + * - the name matches, AND + * - the content is not as expected + */ + boolean noMatch(String name, String content) + { + return + matcher.matches(name, namePattern) && + matcher.contains(content, contentPattern) != expectContent; + } + + String getContent() { return contentPattern.getPattern(); } + + boolean getExpectContent() { return expectContent; } + }; + /** * Check for the following in the input file: - * string "org.apache.log4j.Category" in file. + * "org.apache.log4j" */ - private static final String avoidStrings[] = + private static final FileNameContentPattern avoidPatterns[] = { - "org.apache.log4j" - }; + // For escape ('\'), remember that Java gets first dibs.. + // so double-escape for pattern-matcher to see it. - private boolean checkFile(File file) { - boolean cleanFile = true; + // Verify that java files do not use Log4j + new FileNameContentPattern(".+\\.java", + "org\\.apache\\.log4j", false), + + // Verify that axis java files do not use System.out.println + // or System.err.println, expect: + // - AxisFault.java, + // - Version.java + // - client/AdminClient.java + // - providers/BSFProvider.java + // - utils/tcpmon.java + // - tooling in 'org/apache/axis/wsdl' + // + new FileNameContentPattern(".+([\\\\/])" + + "java\\1src\\1org\\1apache\\1axis\\1" + + "(?!AxisFault\\.java" + + "|client\\1AdminClient\\.java" + + "|utils\\1tcpmon\\.java" + + "|providers\\1BSFProvider\\.java" + + "|Version\\.java" + + "|wsdl\\1)" + + "([a-zA-Z0-9_]+\\1)*" + + "[^\\\\/]+\\.java", + "System\\.(out|err)\\.println", false) + }; + private void checkFile(File file) { try { FileInputStream fis = new FileInputStream(file); byte[] bytes = new byte[fis.available()]; fis.read(bytes); - String string = new String(bytes); + String content = new String(bytes); - for (int i = 0; i < avoidStrings.length; i++) { - if (string.indexOf(avoidStrings[i]) >= 0) { - System.out.println(file.getPath() + ": Unexpected '" + avoidStrings[i]); - cleanFile = false; + 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].getContent() + + LS; } } } catch (Throwable t) { - System.out.println(file.getPath() + ": " + t.getMessage()); - cleanFile = false; + errors = errors + + "File: " + file.getPath() + + ": " + t.getMessage() + + LS; } - - return cleanFile; } // checkFile }