Author: tchemit Date: 2008-10-18 11:25:38 +0000 (Sat, 18 Oct 2008) New Revision: 970 Modified: lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/CompilerTest.java Log: add test cases for validator + improve test of errors Modified: lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/CompilerTest.java =================================================================== --- lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/CompilerTest.java 2008-10-18 11:25:15 UTC (rev 969) +++ lutinjaxx/trunk/maven-jaxx-plugin/src/test/java/org/codelutin/jaxx/CompilerTest.java 2008-10-18 11:25:38 UTC (rev 970) @@ -1,16 +1,18 @@ package org.codelutin.jaxx; +import jaxx.compiler.JAXXCompiler; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.SystemStreamLog; import java.io.File; +import java.lang.reflect.Field; +import java.util.Map; public class CompilerTest extends JaxxBaseTest { - protected String prefix = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "testcases" + File.separator; + protected String prefix = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "testcases" + File.separator; - public String getPrefix() { + public String getPrefix() { return prefix; } @@ -65,34 +67,51 @@ // init mojo to get alls files to treate mojo.init(); String[] files = mojo.getFiles(); + assertEquals(34, mojo.getFiles().length); + mojo.setLog(new SystemStreamLog() { + @Override + public boolean isErrorEnabled() { + return false; + } + + @Override + public void error(Throwable error) { + //do nothing + } + + @Override + public void error(CharSequence content) { + //do nothing + } + + @Override + public void error(CharSequence content, Throwable error) { + //do nothing + } + }); + Field fieldCompilers = JAXXCompiler.class.getDeclaredField("compilers"); + Field fieldErrorCount = JAXXCompiler.class.getDeclaredField("errorCount"); + + fieldCompilers.setAccessible(true); + fieldErrorCount.setAccessible(true); + // execute mjo on each jaxx file to produce the error for (String file : files) { - getLog().info("test bad file "+file); + getLog().info("test bad file " + file); mojo.setFiles(new String[]{file}); try { - mojo.setLog(new SystemStreamLog() { - public boolean isErrorEnabled() { - return false; - } - - public void error(Throwable error) { - //do nothing - } - - public void error(CharSequence content) { - //do nothing - } - - public void error(CharSequence content, Throwable error) { - //do nothing - } - }); mojo.doAction(); // should never pass fail(); } catch (MojoExecutionException e) { // ok jaxx compiler failed assertTrue(true); + + Map<String, JAXXCompiler> compilers = (Map<String, JAXXCompiler>) fieldCompilers.get(null); + assertEquals(1, compilers.size()); + JAXXCompiler compiler = compilers.values().iterator().next(); + Integer nberrors = (Integer) fieldErrorCount.get(compiler); + assertTrue(nberrors > 0); } } } @@ -155,4 +174,64 @@ assertEquals(1, files.length); assertTrue(mojo.getUpdater().getMirrorFile(srcFile).lastModified() > oldTime); } + + public void testValidatorOk() throws Exception { + mojo.execute(); + assertEquals(1, mojo.getFiles().length); + + } + + public void testValidatorErrors() throws Exception { + // init mojo to get alls files to treate + mojo.init(); + String[] files = mojo.getFiles(); + assertEquals(16, mojo.getFiles().length); + mojo.setLog(new SystemStreamLog() { + @Override + public boolean isErrorEnabled() { + return false; + } + + @Override + public void error(Throwable error) { + //do nothing + } + + @Override + public void error(CharSequence content) { + //do nothing + } + + @Override + public void error(CharSequence content, Throwable error) { + //do nothing + } + }); + Field fieldCompilers = JAXXCompiler.class.getDeclaredField("compilers"); + Field fieldErrorCount = JAXXCompiler.class.getDeclaredField("errorCount"); + + fieldCompilers.setAccessible(true); + fieldErrorCount.setAccessible(true); + + // execute mjo on each jaxx file to produce the error + for (String file : files) { + getLog().info("test bad file " + file); + mojo.setFiles(new String[]{file}); + try { + mojo.doAction(); + // should never pass + fail(); + } catch (MojoExecutionException e) { + // ok jaxx compiler failed + assertTrue(true); + + Map<String, JAXXCompiler> compilers = (Map<String, JAXXCompiler>) fieldCompilers.get(null); + assertEquals(1, compilers.size()); + JAXXCompiler compiler = compilers.values().iterator().next(); + Integer nberrors = (Integer) fieldErrorCount.get(compiler); + assertEquals(1, nberrors.intValue()); + } + } + } + } \ No newline at end of file