From abd201f01a67d4a6a932529fbe465091598744ac Mon Sep 17 00:00:00 2001 From: cbonar Date: Sun, 7 Sep 2008 23:27:42 +0000 Subject: [PATCH] + ant task for JsUnit --- .../src/net/jsunit/ant/JsUnitTestTask.java | 100 +++++ .../net/jsunit/ant/StandaloneTestTask.java | 356 ++++++++++++++++++ .../src/net/jsunit/ant/TestSuite.html | 35 ++ .../jsunit/GeneratedConfigurationSource.java | 232 ++++++++++++ .../net/jsunit/GeneratedStandaloneTest.java | 69 ++++ 5 files changed, 792 insertions(+) create mode 100644 ciform/trunk/tools/jsunit-ant/src/net/jsunit/ant/JsUnitTestTask.java create mode 100644 ciform/trunk/tools/jsunit-ant/src/net/jsunit/ant/StandaloneTestTask.java create mode 100644 ciform/trunk/tools/jsunit-ant/src/net/jsunit/ant/TestSuite.html create mode 100644 ciform/trunk/tools/jsunit-ant/test/net/jsunit/GeneratedConfigurationSource.java create mode 100644 ciform/trunk/tools/jsunit-ant/test/net/jsunit/GeneratedStandaloneTest.java diff --git a/ciform/trunk/tools/jsunit-ant/src/net/jsunit/ant/JsUnitTestTask.java b/ciform/trunk/tools/jsunit-ant/src/net/jsunit/ant/JsUnitTestTask.java new file mode 100644 index 0000000..59b123e --- /dev/null +++ b/ciform/trunk/tools/jsunit-ant/src/net/jsunit/ant/JsUnitTestTask.java @@ -0,0 +1,100 @@ +package net.jsunit.ant; + + +import java.util.List; +import java.util.Vector; + +import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest; +import org.apache.tools.ant.types.FileSet; + + + +/** + *

+ * A simple class to represent a JsUnit test. + *

+ * + *

It is composed of a set of <script> tags, represented by the included {@link FileSet}s. + *

+ * + * @author http://nicobo.net/contact?subject=jsunit+ant + */ +public class JsUnitTestTask extends JUnitTest +{ + + /** Known type for Javascript tags */ + public static final String TYPE_JAVASCRIPT = "text/javascript"; + + // + // INITIALISATION + // + + /** All resources to include in the test suite page */ + private List filesets = new Vector(); + + /** Defaults to {@link #TYPE_JAVASCRIPT} */ + private String type = TYPE_JAVASCRIPT; + + + + public JsUnitTestTask( String name, boolean haltOnError, + boolean haltOnFailure, boolean filtertrace, String type ) + { + super( name, haltOnError, haltOnFailure, filtertrace ); + setType( type ); + } + + + + public JsUnitTestTask( String name ) + { + super( name ); + } + + + + public JsUnitTestTask() + { + super(); + } + + + + public List getFilesets() + { + return filesets; + } + + + + public void setFilesets( List filesets ) + { + this.filesets = filesets; + } + + + + // + // NEW ANT FIELDS + // + + /** The type attribute of the <script> tag to be generated. */ + public String getType() + { + return type; + } + + + + public void setType( String type ) + { + this.type = type; + } + + + + public void addFileSet( FileSet fs ) + { + getFilesets().add( fs ); + } +} diff --git a/ciform/trunk/tools/jsunit-ant/src/net/jsunit/ant/StandaloneTestTask.java b/ciform/trunk/tools/jsunit-ant/src/net/jsunit/ant/StandaloneTestTask.java new file mode 100644 index 0000000..8250aa9 --- /dev/null +++ b/ciform/trunk/tools/jsunit-ant/src/net/jsunit/ant/StandaloneTestTask.java @@ -0,0 +1,356 @@ +package net.jsunit.ant; + + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Collection; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map; +import java.util.Vector; + +import net.jsunit.GeneratedStandaloneTest; +import net.jsunit.StandaloneTest; +import net.jsunit.configuration.ConfigurationProperty; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.taskdefs.optional.junit.JUnitTask; +import org.apache.tools.ant.types.FileSet; + + + +/** + * This task permits convenient access to the {@link GeneratedStandaloneTest} + * unit test. + * + * @author http://nicobo.net/contact?subject=jsunit+ant + */ +public class StandaloneTestTask extends JUnitTask +{ + /** Name of the property pointing to the root directory of the JsUnit installation */ + public static final String PROP_JSUNITROOT = "jsunit.dir"; + /** Name of the property pointing to the JsUnit core library's file */ + public static final String PROP_COREJS = "jsunit.coreJs"; + /** Name of the property pointing to the JsUnit's testRunner.html file */ + public static final String PROP_TESTRUNNER = "jsunit.testRunner"; + + /** Inner {@link JsUnitTestTask} elements */ + private Collection scriptsList = new Vector(); + private boolean runTests = true; + private boolean keepTestPage = false; + private String jsUnitRoot; + private String coreJs; + private String testRunner; + + + + // + // INITIALISATION + // + + public StandaloneTestTask() throws Exception + { + super(); + } + + + + protected Collection getScriptsList() + { + return scriptsList; + } + + + + protected void setScriptsList( Collection scriptsList ) + { + this.scriptsList = scriptsList; + } + + + + // + // ANT Task IMPLEMENTATION + // + + public void addConfiguredTest( JsUnitTestTask anInner ) + { + // Simply saves the gathered inner elements + getScriptsList().add( anInner ); + } + + + + public String getJsUnitRoot() + { + return jsUnitRoot; + } + + + + public void setJsUnitRoot( String jsUnitRoot ) + { + this.jsUnitRoot = jsUnitRoot; + } + + + + public boolean isKeepTestPage() + { + return keepTestPage; + } + + + + public void setKeepTestPage( boolean keepTestPage ) + { + this.keepTestPage = keepTestPage; + } + + + + public boolean isRunTests() + { + return runTests; + } + + + + public void setRunTests( boolean runTests ) + { + this.runTests = runTests; + } + + + + public String getCoreJs() + { + return coreJs; + } + + + + public void setCoreJs( String coreJs ) + { + this.coreJs = coreJs; + } + + + + public String getTestRunner() + { + return testRunner; + } + + + + public void setTestRunner( String testRunner ) + { + this.testRunner = testRunner; + } + + + + public void execute() + { + // A list of the URI of the files to include mapped to their type as key + Map scriptsFiles = new Hashtable(); + + if ( getScriptsList() == null || getScriptsList().size() == 0 ) + { + throw new IllegalArgumentException( "No \n" ); + } + out = out.replace( "@includes@", includesBuffer.toString() ); + + // writes the generated test suite to a temporary file + File testSuitePage = filename != null ? new File( filename ) : File.createTempFile( "jsunit-", ".tmp" ); + FileWriter fw = new FileWriter( testSuitePage ); + fw.write( out ); + fw.close(); + + return testSuitePage; + } + + + + private static File buildTestSuitePage( String project, String JsUnitCore, + Map includes ) throws IOException, URISyntaxException + { + return buildTestSuitePage( project, JsUnitCore, includes, null ); + } + + + + /** + * Before calling this method, make sure {@link #setTestSuitePage(File)} has been correctly set. + * @throws IllegalStateException if a property is missing or is incorrect + */ + private String buildURL( String testRunner, File testPage ) + throws URISyntaxException, IOException + { + URI uri = buildURI( testRunner ); + return new URI( uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), "testPage=" + + testPage.getPath(), uri.getFragment() ).toString(); + } + +} diff --git a/ciform/trunk/tools/jsunit-ant/src/net/jsunit/ant/TestSuite.html b/ciform/trunk/tools/jsunit-ant/src/net/jsunit/ant/TestSuite.html new file mode 100644 index 0000000..660a885 --- /dev/null +++ b/ciform/trunk/tools/jsunit-ant/src/net/jsunit/ant/TestSuite.html @@ -0,0 +1,35 @@ + + + Test Page for @project@ + + + + + + @includes@ + + + + + + +

Test Page for @project@

+

+ This page is a unit test.
+ It must be open with JsUnit's TestRunner. +

+ + \ No newline at end of file diff --git a/ciform/trunk/tools/jsunit-ant/test/net/jsunit/GeneratedConfigurationSource.java b/ciform/trunk/tools/jsunit-ant/test/net/jsunit/GeneratedConfigurationSource.java new file mode 100644 index 0000000..db56921 --- /dev/null +++ b/ciform/trunk/tools/jsunit-ant/test/net/jsunit/GeneratedConfigurationSource.java @@ -0,0 +1,232 @@ +package net.jsunit; + + + + + +/** + * This configuration redefines {@link #url()} to point to a custom file + * and gathers automatically some more properties. + * + * @author http://nicobo.net/contact?subject=jsunit+ant + */ +public class GeneratedConfigurationSource //extends DelegatingConfigurationSource +{ + // // + // // CONSTANTS + // // + // + // /** + // * Property name defining the Javascript files to include in the test suite + // * page. + // */ + // public static final String PROP_JAVASCRIPTS = "jsunit.in.javascripts"; + // + // /** + // * Associates a type of script to the corresponding property where their + // * name is stored. + // */ + // public static final String[][] TYPE_PROP = { { JsUnitTestTask.TYPE_JAVASCRIPT, PROP_JAVASCRIPTS } }; + // + // // + // // PRIVATE FIELDS + // // + // + // /** The generated test suite page */ + // private File testSuitePage = null; + // private String project; + // private String coreJs; + // private String testRunner; + // private Map includes; + // + // + // + // // + // // INITIALISATION + // // + // + // public GeneratedConfigurationSource( /*ConfigurationSource source,*/ + // String project, String coreJs, String testRunner ) + // throws URISyntaxException + // { + // //super( source ); + // + // // Checks for required parameters + // if ( System.getProperty( PROP_JAVASCRIPTS ) == null ) + // { + // throw new IllegalArgumentException( "Missing required property " + // + PROP_JAVASCRIPTS ); + // } + // + // setProject( project ); + // setCoreJs( coreJs ); + // setTestRunner( testRunner ); + // + // // Gathers all parameters + // // TODO Allow generic URI to be included in the generated file + // Map includes = new Hashtable(); + // for ( int s = 0; s < TYPE_PROP.length; s++ ) + // { + // String[] files = System.getProperty( TYPE_PROP[s][1] ).split( File.pathSeparator ); + // // indexed on the property's type + // includes.put( TYPE_PROP[s][0], files ); + // } + // } + // + // + // + // public GeneratedConfigurationSource( /*ConfigurationSource source */) + // throws URISyntaxException + // { + // this( /*Configuration.resolveSource(),*/"a JsUnit test suite", getRequiredURISystemProperty( PROP_COREJS ), getRequiredURISystemProperty( PROP_TESTRUNNER ) ); + // } + // + // + // + // // public GeneratedConfigurationSource() throws URISyntaxException + // // { + // // this( Configuration.resolveSource() ); + // // } + // + // // + // // ACCESSORS + // // + // + // public File getTestSuitePage() + // { + // return testSuitePage; + // } + // + // + // + // public void setTestSuitePage( File testSuitePage ) + // { + // this.testSuitePage = testSuitePage; + // } + // + // + // + // public String getProject() + // { + // return project; + // } + // + // + // + // public void setProject( String project ) + // { + // this.project = project; + // } + // + // + // + // public String getCoreJs() + // { + // return coreJs; + // } + // + // + // + // public void setCoreJs( String coreJs ) + // { + // this.coreJs = coreJs; + // } + // + // + // + // public String getTestRunner() + // { + // return testRunner; + // } + // + // + // + // public void setTestRunner( String testRunner ) + // { + // this.testRunner = testRunner; + // } + // + // + // + // public Map getIncludes() + // { + // return includes; + // } + // + // + // + // public void setIncludes( Map includes ) + // { + // this.includes = includes; + // } + // + // + // + // // + // // UTILITY METHODS + // // + // + // /** The best effort to get a well formed URI */ + // private static URI getURI( String text ) throws URISyntaxException + // { + // try + // { + // return new URL( text ).toURI(); + // } + // catch ( MalformedURLException murle ) + // { + // return new File( text ).toURI(); + // } + // } + // + // + // + // /** + // * @param key + // * the name of the property to retrieve + // * @return A well formed URI based on the value of the given property + // * @throws IllegalArgumentException + // * if the given property doesn't exist. + // */ + // private static String getRequiredURISystemProperty( String key ) + // throws URISyntaxException + // { + // String val = System.getProperty( key ); + // + // if ( val == null ) + // { + // System.err.println( "Missing property : " + key ); + // throw new IllegalArgumentException( "Missing property : " + key ); + // } + // + // return getURI( val ).toString(); + // } + // + // + // + // // + // // ConfigurationSource IMPLEMENTATION + // // + // + // /** + // * Before calling this method, make sure {@link #setTestSuitePage(File)} has been correctly set. + // * @throws IllegalStateException if a property is missing or is incorrect + // */ + // public String makeUrl() throws URISyntaxException, IOException + // { + // // try + // // { + // // returns the full URL to use with JsUnit + // return getRequiredURISystemProperty( PROP_TESTRUNNER ) + "?testPage=" + // + getTestSuitePage().getCanonicalPath(); + // + // // } + // // // URISyntaxException, IOException + // // catch ( Exception e ) + // // { + // // e.printStackTrace( System.err ); + // // throw new IllegalStateException( e ); + // // } + // } + +} diff --git a/ciform/trunk/tools/jsunit-ant/test/net/jsunit/GeneratedStandaloneTest.java b/ciform/trunk/tools/jsunit-ant/test/net/jsunit/GeneratedStandaloneTest.java new file mode 100644 index 0000000..035f9b5 --- /dev/null +++ b/ciform/trunk/tools/jsunit-ant/test/net/jsunit/GeneratedStandaloneTest.java @@ -0,0 +1,69 @@ +package net.jsunit; + + +import junit.framework.Test; +import junit.framework.TestSuite; +import net.jsunit.configuration.Configuration; +import net.jsunit.configuration.ConfigurationSource; +import net.jsunit.configuration.DelegatingConfigurationSource; +import net.jsunit.model.Browser; + + + +/** + * Generates a JsUnit test suite page giving resources and executes it against + * JUnit. + * + * @author http://nicobo.net/contact?subject=jsunit+ant + */ +public class GeneratedStandaloneTest extends StandaloneTest +{ + + // + // INITIALISATION + // + + public GeneratedStandaloneTest( String name ) + { + super( name ); + } + + + + public GeneratedStandaloneTest( ConfigurationSource source ) + { + super( "" );//new GeneratedConfigurationSource( source ) ); + } + + + + // + // JUNIT SPECIFICATIONS + // + + public static Test suite() + { + try + { + TestSuite suite = new TestSuite(); + ConfigurationSource originalSource = Configuration.resolveSource(); + Configuration configuration = new Configuration( originalSource ); + for ( final Browser browser : configuration.getBrowsers() ) + { + suite.addTest( new GeneratedStandaloneTest( new DelegatingConfigurationSource( originalSource ) { + public String browserFileNames() + { + return browser.getFileName(); + } + } ) ); + } + return suite; + + } + catch ( Exception e ) + { + e.printStackTrace( System.err ); + return null; + } + } +}