Author: tchemit Date: 2010-04-09 18:27:54 +0200 (Fri, 09 Apr 2010) New Revision: 686 Log: add checkPattern method Modified: trunk/src/test/java/org/nuiton/plugin/AbstractMojoTest.java Modified: trunk/src/test/java/org/nuiton/plugin/AbstractMojoTest.java =================================================================== --- trunk/src/test/java/org/nuiton/plugin/AbstractMojoTest.java 2010-04-08 20:03:59 UTC (rev 685) +++ trunk/src/test/java/org/nuiton/plugin/AbstractMojoTest.java 2010-04-09 16:27:54 UTC (rev 686) @@ -35,7 +35,11 @@ import java.beans.Introspector; import java.io.File; +import java.io.IOException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + /** * Base test class for a mojo. * <p/> @@ -238,6 +242,52 @@ } /** + * Checks on the given {@code file} that : + * <ul> + * <li>file exists</li> + * <li>the given {@code pattern} exists {@code required = true} + * (or not {@code required = false}) in the content of the file.</li> + * </ul> + * + * @param file the file to test + * @param pattern the pattern to search + * @param encoding encoding of the file to read + * @param required flag to says if pattern should (or not) be found in + * file's content + * @throws IOException if could not read file + */ + public void checkExistsPattern(File file, + String pattern, + String encoding, + boolean required) throws IOException { + + // checks file exists + assertTrue("File '" + file + "' does not exist, but should...", + file.exists() + ); + + //obtain file content as a string + String content = PluginHelper.readAsString(file, encoding); + + checkPattern(file, content, pattern, required); + } + + public void checkPattern(File file, + String content, + String pattern, + boolean required) throws IOException { + + String errorMessage = required ? "could not find the pattern : " : + "should not have found pattern :"; + + // checks pattern found (or not) in file's content + assertEquals(errorMessage + pattern + " in '" + file + "'", + required, + content.contains(pattern) + ); + } + + /** * To offer inside each test method (annotated by a {@link Test}) the * following properties : * <p/>