diff --git a/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/GeneratedStandaloneTest.java b/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/GeneratedStandaloneTest.java deleted file mode 100644 index ee42f6c..0000000 --- a/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/GeneratedStandaloneTest.java +++ /dev/null @@ -1,72 +0,0 @@ -package net.jsunit; - - -import java.util.Iterator; - -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 ( Iterator itb = configuration.getBrowsers().iterator(); itb.hasNext(); ) - { - final Browser browser = (Browser) itb.next(); - suite.addTest( new GeneratedStandaloneTest( new DelegatingConfigurationSource( originalSource ) { - public String browserFileNames() - { - return browser.getFileName(); - } - } ) ); - } - return suite; - - } - catch ( Exception e ) - { - e.printStackTrace( System.err ); - return null; - } - } -} diff --git a/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/TestLibRunner.java b/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/TestLibRunner.java new file mode 100644 index 0000000..f368582 --- /dev/null +++ b/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/TestLibRunner.java @@ -0,0 +1,98 @@ +package net.jsunit; + + +import java.util.Iterator; +import java.util.Properties; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import net.jsunit.configuration.Configuration; +import net.jsunit.configuration.ConfigurationSource; +import net.jsunit.model.Browser; + + + +/** + *
This JUnit {@link TestCase} allows the direct execution of Javascript libraries (.js files) containing JsUnit tests.
+ * + *It removes the need to write HTML test pages for the JsUnit tests to be executed.
+ * For that, it simply generates a JsUnit test suite page giving a list of files to include and executes it against
+ * JUnit.
Parameters are given as system {@link Properties}.
Builds a new test page from the current System properties.
+ * + *See PROP_* constants for the list of recognised properties.
+ */ + protected static TestPage buildTestPage() throws URISyntaxException, + IOException + { + // a. Gathers parameters from the System + String project = System.getProperty( PROP_PROJECT, "Unknown project" ); + String jsUnitCore = getRequiredURISystemProperty( PROP_COREJS ); + Collection javascripts = Arrays.asList( System.getProperty( PROP_JAVASCRIPTS, "" ).split( File.pathSeparator ) ); + Map includes = new Hashtable(); + includes.put( TestPage.INCLUDE_JAVASCRIPT, javascripts ); + + // b. Builds the test page from the parameters + return new TestPage( project, jsUnitCore, includes ); + } + + + + // + // ConfigurationSource IMPLEMENTATION + // + + /** + *Builds the URL based so that it points to the generated test page.
+ * + *Before calling this method, make sure {@link #setTestSuitePage(File)} has been correctly set.
+ * + *NOTE : This is a bit weird because the test page's file is created in this method, + * but must be deleted by the unit test once done. This is because I had to hack into this class + * to reuse the maximum of existing code (in order to limit the risks of broken code with the future versions).
+ * + * @return The full URL to use with JsUnit (the existing property : {@value ConfigurationProperty#URL} is ignored) + * @throws IllegalArgumentException if a property is missing or is incorrect + * FIXME don't create the test page here + */ + public String url() + { + try + { + return getTestRunner() + "?" + PARAM_TESTPAGE + "=" + + getTestPage().getCanonicalPath(); + } + catch ( URISyntaxException urise ) + { + urise.printStackTrace( System.err ); + throw new IllegalArgumentException( urise ); + } + catch ( IOException ioe ) + { + ioe.printStackTrace( System.err ); + throw new IllegalArgumentException( ioe ); + } + } + +} diff --git a/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/TestPage.java b/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/TestPage.java new file mode 100644 index 0000000..91e57df --- /dev/null +++ b/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/TestPage.java @@ -0,0 +1,232 @@ +package net.jsunit; + + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.MissingResourceException; + + + +/** + * This utility class helps building JsUnit (HTML) test pages, giving a list of files to include. + * + * @author http://nicobo.net/contact?subject=jsunit+ant + */ +public class TestPage +{ + /** + * Key to use to include Javascript tags in the page.Builds complete JsUnit test suite page from the current environment.
+ * + *Make sure all required properties are set and have a correct value before calling this method (see the arguments in {@link #TestPage(String, String, Map)}).
+ * + * @return The content of the generated test page + */ + public String asString() + { + // Reads the template of the test suite page to generate into a local buffer + InputStream is = getClass().getResourceAsStream( TEMPLATE_FILENAME ); + StringBuffer buffer = new StringBuffer(); + try + { + for ( int c = is.read(); c > -1; c = is.read() ) + { + buffer.append( (char) c ); + } + is.close(); + } + catch ( IOException ioe ) + { + throw new MissingResourceException( "Loading the template file", getClass().getName(), TEMPLATE_FILENAME ); + } + + // Replaces the variable parts of the template + String out = buffer.toString(); + + // Project name + out = out.replaceAll( TEMPLATE_TAG_PROJECT, project ); + + // JsUnit's core library + out = out.replace( TEMPLATE_TAG_JSUNITCORE, getJsUnitCore() ); + + // Other includes : currently only Javascript is supported + StringBuffer includesBuffer = new StringBuffer(); + Collection javascripts = (Collection) includes.get( INCLUDE_JAVASCRIPT ); + for ( Iterator itj = javascripts.iterator(); itj.hasNext(); ) + { + URI javascript = (URI) itj.next(); + includesBuffer.append( "\n" ); + } + out = out.replace( TEMPLATE_TAG_INCLUDES, includesBuffer.toString() ); + + return out; + } + + + + /** + *Writes this page to a file.
+ * + * @param file The file to write this page to. + * @return the file (so one can chain operations on it) + * @throws IOException If an open/write/close operation failed on the given file + */ + public File writeTo( File file ) throws IOException + { + FileWriter fw = new FileWriter( file ); + fw.write( asString() ); + fw.close(); + return file; + } + + + + /** + *Writes this page to a file.
+ * + * @param filename If null, writes to a temporary file + * @return the File to which the data was written + * @throws IOException In case the temporary file failed to be created + * @see #writeTo(File) + */ + public File writeToFile( String filename ) throws IOException + { + File file = filename != null ? new File( filename ) : File.createTempFile( "jsunit-", ".tmp" ); + return writeTo( file ); + } + + + + public File writeToFile() throws IOException + { + return writeToFile( null ); + } + + // /** The best effort to get a well formed URI */ + // private static URI asURI( String text ) throws URISyntaxException + // { + // try + // { + // return new URL( text ).toURI(); + // } + // catch ( MalformedURLException murle ) + // { + // return new File( text ).toURI(); + // } + // } + // + // + // + // /** + // *Builds the URL to pass to {@link Configuration#setTestURL(URL)}
+ // * + // * @throws IllegalStateException if a property is missing or is incorrect + // */ + // private String asURL( String testRunner, File testPage ) + // throws URISyntaxException, IOException + // { + // URI uri = asURI( testRunner ); + // return new URI( uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), "testPage=" + // + testPage.getPath(), uri.getFragment() ).toString(); + // } + +} diff --git a/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/ant/StandaloneTestTask.java b/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/ant/JsUnitBatchTestTask.java similarity index 52% rename from ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/ant/StandaloneTestTask.java rename to ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/ant/JsUnitBatchTestTask.java index cd47976..2071817 100644 --- a/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/ant/StandaloneTestTask.java +++ b/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/ant/JsUnitBatchTestTask.java @@ -2,13 +2,6 @@ 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; @@ -16,9 +9,9 @@ import java.util.Map; import java.util.Vector; import net.jsunit.StandaloneTest; -import net.jsunit.configuration.ConfigurationProperty; +import net.jsunit.TestLibRunner; +import net.jsunit.TestLibRunnerConfigurationSource; -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; @@ -26,22 +19,28 @@ import org.apache.tools.ant.types.FileSet; /** - * This task permits convenient access to the {@link StandaloneTest} - * unit test. + * This task permits convenient access to the {@link TestLibRunner} unit test. * * @author http://nicobo.net/contact?subject=jsunit+ant */ -public class StandaloneTestTask extends JUnitTask +public class JsUnitBatchTestTask extends JUnitTask { + // PUBLIC CONSTANTS + /** 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 */ + /** @see TestLibRunnerConfigurationSource#PROP_COREJS */ + public static final String PROP_COREJS = TestLibRunnerConfigurationSource.PROP_COREJS; + /** @see TestLibRunner#PROP_TESTRUNNER */ public static final String PROP_TESTRUNNER = "jsunit.testRunner"; + // PRIVATE FIELDS + /** Inner {@link JsUnitTestTask} elements */ private Collection scriptsList = new Vector(); + + // PARAMETERS FROM THE BUILD SCRIPT / ENVIRONMENT + private boolean runTests = true; private boolean keepTestPage = false; private String jsUnitRoot; @@ -54,7 +53,7 @@ public class StandaloneTestTask extends JUnitTask // INITIALISATION // - public StandaloneTestTask() throws Exception + public JsUnitBatchTestTask() throws Exception { super(); } @@ -219,23 +218,25 @@ public class StandaloneTestTask extends JUnitTask } // Builds the test suite page - File testPage = null; - try - { - testPage = buildTestSuitePage( project, coreJs, scriptsFiles ); - String url = buildURL( testRunner, testPage ); - System.setProperty( ConfigurationProperty.URL.getName(), url ); - // TODO : allow fork and find a way to pass parameters without system properties - setFork( false ); - } - catch ( URISyntaxException urise ) - { - throw new BuildException( urise ); - } - catch ( IOException ioe ) - { - throw new BuildException( ioe ); - } + // File testPageFile = null; + //TestPage testPage = null; + // try + // { + // testPage = new TestPage( project, coreJs, scriptsFiles ); + // // testPageFile = testPage.writeToFile(); + // // String url = testPageFile.toURL()( testRunner, testPage ); + // //System.setProperty( ConfigurationProperty.URL.getName(), url ); + // // TODO : allow fork and find a way to pass parameters without system properties + setFork( false ); + // } + // catch ( URISyntaxException urise ) + // { + // throw new BuildException( urise ); + // } + // catch ( IOException ioe ) + // { + // throw new BuildException( ioe ); + // } // Lets the superclass do the job (will start the included test) if ( isRunTests() ) @@ -250,106 +251,15 @@ public class StandaloneTestTask extends JUnitTask addTest( test ); } - // Really starts the test + // Really executes the tests super.execute(); } - if ( !isKeepTestPage() ) - { - // After execution, removes temporary files if asked - testPage.deleteOnExit(); - } - } - - - - // - // UTILITY METHODS - // - - /** The best effort to get a well formed URI */ - private static URI buildURI( String text ) throws URISyntaxException - { - try - { - return new URL( text ).toURI(); - } - catch ( MalformedURLException murle ) - { - return new File( text ).toURI(); - } - } - - - - /** - * Writes a complete JsUnit test suite page from the current environment - * into a temporary file. - * - * @return The generated test page - * @throws IllegalArgumentException - * if a required property is missing - */ - private static File buildTestSuitePage( String project, String JsUnitCore, - Map includes, String filename ) throws IOException, - URISyntaxException - { - // Reads the template of the test suite page to generate into a local - // buffer - InputStream is = StandaloneTest.class.getResourceAsStream( "TestSuite.html" ); - StringBuffer buffer = new StringBuffer(); - for ( int c = is.read(); c > -1; c = is.read() ) - { - buffer.append( (char) c ); - } - is.close(); - - // Replaces the variable parts of the template - String out = buffer.toString(); - out = out.replaceAll( "@project@", project ); - out = out.replace( "@jsUnitCore.js@", JsUnitCore ); - StringBuffer includesBuffer = new StringBuffer(); - // Currently only Javascript is supported - Collection javascripts = (Collection) includes.get( JsUnitTestTask.TYPE_JAVASCRIPT ); - //for ( int i = 0; i < javascripts.length; i++ ) - for ( Iterator itj = javascripts.iterator(); itj.hasNext(); ) - { - URI javascript = (URI) itj.next(); - includesBuffer.append( "\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(); + // if ( !isKeepTestPage() ) + // { + // // After execution, removes temporary files if asked + // testPageFile.deleteOnExit(); + // } } } diff --git a/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/ant/JsUnitTestTask.java b/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/ant/JsUnitTestTask.java index 59b123e..d2f77b4 100644 --- a/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/ant/JsUnitTestTask.java +++ b/ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/ant/JsUnitTestTask.java @@ -4,6 +4,8 @@ package net.jsunit.ant; import java.util.List; import java.util.Vector; +import net.jsunit.TestPage; + import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest; import org.apache.tools.ant.types.FileSet; @@ -46,7 +48,7 @@ public class JsUnitTestTask extends JUnitTest - public JsUnitTestTask( String name ) + public JsUnitTestTask( String name, TestPage testPage ) { super( name ); } diff --git a/ant-jsunit-hieatt/trunk/src/main/resources/net/jsunit/ant/TestSuite.html b/ant-jsunit-hieatt/trunk/src/main/resources/net/jsunit/TestPageTemplate.html similarity index 64% rename from ant-jsunit-hieatt/trunk/src/main/resources/net/jsunit/ant/TestSuite.html rename to ant-jsunit-hieatt/trunk/src/main/resources/net/jsunit/TestPageTemplate.html index 660a885..e758c70 100644 --- a/ant-jsunit-hieatt/trunk/src/main/resources/net/jsunit/ant/TestSuite.html +++ b/ant-jsunit-hieatt/trunk/src/main/resources/net/jsunit/TestPageTemplate.html @@ -1,5 +1,12 @@ + +