From eca540f632a80aa002d1c4304d73eaf4513ab27e Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Fri, 12 Feb 2010 20:35:34 +0100 Subject: junit test code cleanup and some build preperations for hudson. --- test/junit/com/sun/gluegen/AbstractTest.java | 120 -------------------- test/junit/com/sun/gluegen/BasicTest.java | 9 +- test/junit/com/sun/gluegen/BuildUtil.java | 123 +++++++++++++++++++++ test/junit/com/sun/gluegen/StructAccessorTest.java | 33 +++--- test/junit/com/sun/gluegen/build.xml | 15 ++- 5 files changed, 159 insertions(+), 141 deletions(-) delete mode 100644 test/junit/com/sun/gluegen/AbstractTest.java create mode 100644 test/junit/com/sun/gluegen/BuildUtil.java (limited to 'test') diff --git a/test/junit/com/sun/gluegen/AbstractTest.java b/test/junit/com/sun/gluegen/AbstractTest.java deleted file mode 100644 index 9262347..0000000 --- a/test/junit/com/sun/gluegen/AbstractTest.java +++ /dev/null @@ -1,120 +0,0 @@ -package com.sun.gluegen; - -import java.io.File; -import java.net.URISyntaxException; -import org.apache.tools.ant.DefaultLogger; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.ProjectHelper; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Ignore; - -import static java.lang.System.*; - -/** - * @author Michael Bien - */ -@Ignore -public abstract class AbstractTest { - - static final Project project = new Project(); - - protected static String gluegenRoot; - protected static String path; - protected static String output; - - - @BeforeClass - public static void setUp() throws Exception { - - out.println(" - - - System info - - - "); - out.println("OS: " + System.getProperty("os.name")); - out.println("VM: " + System.getProperty("java.vm.name")); - - // setup paths - try { - File executionRoot = new File(AbstractTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()); - out.println("execution root: " + executionRoot); - gluegenRoot = executionRoot.getParentFile().getParentFile().getParentFile().getParentFile().toString(); - out.println("gluegen project root: " + gluegenRoot); - } catch (URISyntaxException ex) { - throw new RuntimeException("can not determine gluegen root", ex); - } - - path = gluegenRoot + "/test/junit/com/sun/gluegen"; - output = gluegenRoot + "/build/test"; - - out.println("path: "+path); - out.println("output: "+output); - out.println(" - - - - - - - - - - - - "); - - deleteDirectory(new File(output+"/gensrc")); - - //setup ant build file - project.setBaseDir(new File(gluegenRoot)); - - DefaultLogger logger = new DefaultLogger(); - logger.setErrorPrintStream(err); - logger.setOutputPrintStream(out); - logger.setMessageOutputLevel(Project.MSG_INFO); - project.addBuildListener(logger); - - project.init(); - - File buildFile = new File(path, "build.xml"); - ProjectHelper.configureProject(project, buildFile); - } - - @AfterClass - public static void tearDown() { -// deleteDirectory(new File(output)); - } - - /** - * fails if ant script fails (which is a good thing). - * executeTarget throws RuntimeException on failure - */ - public final void compileJava() { - project.executeTarget("compile.java"); - } - - /** - * fails if ant script fails (which is a good thing) - * executeTarget throws RuntimeException on failure - */ - public final void compileNatives() { - project.executeTarget("compile.native"); - } - - static final void generate(String config) { - out.println("generate: "+config); - GlueGen.main( - new String[] { - "-I"+path, - "-O"+output+"/gensrc", -// "-Ecom.sun.gluegen.DebugEmitter", - "-C"+path+"/"+config+".cfg", - path+"/"+config+".h" - } - ); - out.println("done"); - } - - static final void deleteDirectory(File path) { - if(path.exists()) { - - File[] files = path.listFiles(); - for (int i = 0; i < files.length; i++) { - if (files[i].isDirectory()) { - deleteDirectory(files[i]); - } else { - files[i].delete(); - } - } - - path.delete(); - } - } - - -} diff --git a/test/junit/com/sun/gluegen/BasicTest.java b/test/junit/com/sun/gluegen/BasicTest.java index 89a4c21..dc6efb9 100644 --- a/test/junit/com/sun/gluegen/BasicTest.java +++ b/test/junit/com/sun/gluegen/BasicTest.java @@ -6,14 +6,16 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.nio.ByteOrder; +import org.junit.AfterClass; import org.junit.Test; import static java.lang.System.*; +import static com.sun.gluegen.BuildUtil.*; /** * * @author Michael Bien */ -public class BasicTest extends AbstractTest { +public class BasicTest { @Test public void generateBindingTest() { @@ -98,4 +100,9 @@ public class BasicTest extends AbstractTest { } + @AfterClass + public static void tearDown() { +// cleanGeneratedFiles(); + } + } diff --git a/test/junit/com/sun/gluegen/BuildUtil.java b/test/junit/com/sun/gluegen/BuildUtil.java new file mode 100644 index 0000000..de4f8e2 --- /dev/null +++ b/test/junit/com/sun/gluegen/BuildUtil.java @@ -0,0 +1,123 @@ +package com.sun.gluegen; + +import java.io.File; +import java.net.URISyntaxException; +import org.apache.tools.ant.DefaultLogger; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.ProjectHelper; + +import static java.lang.System.*; + +/** + * @author Michael Bien + */ +public final class BuildUtil { + + private static final Project project; + + public static final String gluegenRoot; + public static final String path; + public static final String output; + + static { + + out.println(" - - - System info - - - "); + out.println("OS: " + System.getProperty("os.name")); + out.println("VM: " + System.getProperty("java.vm.name")); + + // setup paths + try { + File executionRoot = new File(BuildUtil.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + out.println("execution root: " + executionRoot); + gluegenRoot = executionRoot.getParentFile().getParentFile().getParentFile().getParentFile().toString(); + out.println("gluegen project root: " + gluegenRoot); + } catch (URISyntaxException ex) { + throw new RuntimeException("can not determine gluegen root", ex); + } + + path = gluegenRoot + "/test/junit/com/sun/gluegen"; + output = gluegenRoot + "/build/test"; + + out.println("path: "+path); + out.println("output: "+output); + out.println(" - - - - - - - - - - - - "); + + cleanGeneratedFiles(); + + //setup ant build file + project = new Project(); + project.setBaseDir(new File(gluegenRoot)); + + DefaultLogger logger = new DefaultLogger(); + logger.setErrorPrintStream(out); + logger.setOutputPrintStream(out); + logger.setMessageOutputLevel(Project.MSG_INFO); + project.addBuildListener(logger); + + project.init(); + + File buildFile = new File(path, "build.xml"); + if(!buildFile.exists()) { + throw new RuntimeException("buildfile "+buildFile+" does not exist"); + } + + ProjectHelper.configureProject(project, buildFile); + } + + public static void cleanGeneratedFiles() { + out.println("cleaning generated files"); + deleteDirectory(new File(output+"/gensrc")); + out.println("done"); + } + + /** + * fails if ant script fails (which is a good thing). + * executeTarget throws RuntimeException on failure + */ + public static void compileJava() { + out.println("compiling java files"); + project.executeTarget("compile.java"); + out.println("done"); + } + + /** + * fails if ant script fails (which is a good thing) + * executeTarget throws RuntimeException on failure + */ + public static void compileNatives() { + out.println("compiling native files"); + project.executeTarget("compile.native"); + out.println("done"); + } + + public static void generate(String bindingName) { + + out.println("generate binding: " + bindingName); + + GlueGen.main( "-I"+path, + "-O"+output+"/gensrc", + // "-Ecom.sun.gluegen.DebugEmitter", + "-C"+path+"/"+bindingName+".cfg", + path+"/"+bindingName+".h" ); + + out.println("done"); + } + + public static void deleteDirectory(File path) { + if(path.exists()) { + + File[] files = path.listFiles(); + for (int i = 0; i < files.length; i++) { + if (files[i].isDirectory()) { + deleteDirectory(files[i]); + } else { + files[i].delete(); + } + } + + path.delete(); + } + } + + +} diff --git a/test/junit/com/sun/gluegen/StructAccessorTest.java b/test/junit/com/sun/gluegen/StructAccessorTest.java index f89eb43..0cd2953 100644 --- a/test/junit/com/sun/gluegen/StructAccessorTest.java +++ b/test/junit/com/sun/gluegen/StructAccessorTest.java @@ -14,14 +14,16 @@ import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.ToolProvider; import junit.framework.Assert; +import org.junit.AfterClass; import org.junit.Test; import static java.lang.System.*; +import static com.sun.gluegen.BuildUtil.*; /** * * @author Michael Bien */ -public class StructAccessorTest extends AbstractTest { +public class StructAccessorTest { @Test public void generateStruct() { @@ -30,13 +32,13 @@ public class StructAccessorTest extends AbstractTest { @Test public void compileStructJava() { - super.compileJava(); + compileJava(); } @Test public void compileStructNatives() { // this will only copy gluegen-rt to the right place - super.compileNatives(); + compileNatives(); } @Test @@ -58,7 +60,7 @@ public class StructAccessorTest extends AbstractTest { // yeah, java 6 has even a compiler api... private void compile(File[] files, String destination) throws IOException { - out.println("compiling files:\n" + Arrays.asList(files)); + out.println("compiling files:\n " + Arrays.asList(files)); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector collector = new DiagnosticCollector(); @@ -66,15 +68,17 @@ public class StructAccessorTest extends AbstractTest { Iterable fileObj = fileManager.getJavaFileObjects(files); - compiler.getTask( new OutputStreamWriter(out), - fileManager, - collector, - Arrays.asList("-d", destination/*, "-verbose"*/), - null, - fileObj ).call(); + boolean success = compiler.getTask( new OutputStreamWriter(out), + fileManager, + collector, + Arrays.asList("-d", destination/*, "-verbose"*/), + null, + fileObj ).call(); + + fileManager.close(); List> list = collector.getDiagnostics(); - if(!list.isEmpty()) { + if(!list.isEmpty() || !success) { for (Diagnostic d : list) { out.println("Error on line "+ d.getLineNumber()); out.println("Compiler Message:\n"+d.getMessage(Locale.ENGLISH)); @@ -82,12 +86,13 @@ public class StructAccessorTest extends AbstractTest { Assert.fail("compilation failed"); } - fileManager.close(); - out.println("done"); } - + @AfterClass + public static void tearDown() { +// cleanGeneratedFiles(); + } } diff --git a/test/junit/com/sun/gluegen/build.xml b/test/junit/com/sun/gluegen/build.xml index 58ab781..08405bf 100644 --- a/test/junit/com/sun/gluegen/build.xml +++ b/test/junit/com/sun/gluegen/build.xml @@ -4,25 +4,28 @@ Tests GlueGen - - - + + + - + + + + - + - + -- cgit v1.2.3