mirror of
https://github.com/nicolabs/ciform.git
synced 2026-04-16 15:14:25 +02:00
# classes renamed and code better placed
This commit is contained in:
parent
97faad77d8
commit
34e8d484b2
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* <p>This JUnit {@link TestCase} allows the direct execution of Javascript libraries (<tt>.js</tt> files) containing JsUnit tests.</p>
|
||||
*
|
||||
* <p>It removes the need to write HTML test pages for the JsUnit tests to be executed.<br/>
|
||||
* For that, it simply generates a JsUnit test suite page giving a list of files to include and executes it against
|
||||
* JUnit.<br/>Parameters are given as system {@link Properties}.</p>
|
||||
*
|
||||
* @see System#setProperties()
|
||||
* @see TestPage
|
||||
* @author http://nicobo.net/contact?subject=jsunit+ant
|
||||
*/
|
||||
public class TestLibRunner extends StandaloneTest
|
||||
{
|
||||
private TestLibRunnerConfigurationSource configurationSource;
|
||||
|
||||
|
||||
|
||||
//
|
||||
// INITIALISATION
|
||||
//
|
||||
|
||||
public TestLibRunner( TestLibRunnerConfigurationSource source )
|
||||
{
|
||||
super( source );
|
||||
setConfigurationSource( source );
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected TestLibRunnerConfigurationSource getConfigurationSource()
|
||||
{
|
||||
return configurationSource;
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected void setConfigurationSource(
|
||||
TestLibRunnerConfigurationSource configurationSource )
|
||||
{
|
||||
this.configurationSource = configurationSource;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//
|
||||
// JUNIT SPECIFICATIONS
|
||||
//
|
||||
|
||||
/**
|
||||
* Entry point for JUnit < v4 (and used first if existing)
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
TestSuite suite = new TestSuite();
|
||||
|
||||
// Executes the test against each browser given in the initial configuration
|
||||
ConfigurationSource originalSource = Configuration.resolveSource();
|
||||
Configuration configuration = new Configuration( originalSource );
|
||||
for ( Iterator itb = configuration.getBrowsers().iterator(); itb.hasNext(); )
|
||||
{
|
||||
final Browser browser = (Browser) itb.next();
|
||||
|
||||
// the configuration source is adjusted to only return the current browser
|
||||
// TODO write the test page only once (then delete it after all tests have been executed)
|
||||
suite.addTest( new TestLibRunner( new TestLibRunnerConfigurationSource( originalSource ) {
|
||||
public String browserFileNames()
|
||||
{
|
||||
return browser.getFileName();
|
||||
}
|
||||
} ) );
|
||||
}
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void tearDown() throws Exception
|
||||
{
|
||||
getConfigurationSource().getTestPage().deleteOnExit();
|
||||
super.tearDown();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,187 @@
|
|||
package net.jsunit;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
|
||||
import net.jsunit.configuration.Configuration;
|
||||
import net.jsunit.configuration.ConfigurationProperty;
|
||||
import net.jsunit.configuration.ConfigurationSource;
|
||||
import net.jsunit.configuration.DelegatingConfigurationSource;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 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 TestLibRunnerConfigurationSource extends
|
||||
DelegatingConfigurationSource
|
||||
{
|
||||
/** 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 <tt>testRunner.html</tt> file */
|
||||
public static final String PROP_TESTRUNNER = "jsunit.testRunner";
|
||||
/** Property name defining the Javascript files to include in the test page (path-like). */
|
||||
public static final String PROP_JAVASCRIPTS = "jsunit.in.javascripts";
|
||||
/** Property name defining the name of the project to use as the title of the generated page */
|
||||
public static final String PROP_PROJECT = "jsunit.in.project";
|
||||
|
||||
protected static final String PARAM_TESTPAGE = "testPage";
|
||||
|
||||
private File testPage = null;
|
||||
|
||||
|
||||
|
||||
public TestLibRunnerConfigurationSource( ConfigurationSource source )
|
||||
{
|
||||
super( source );
|
||||
}
|
||||
|
||||
|
||||
|
||||
public TestLibRunnerConfigurationSource()
|
||||
{
|
||||
this( Configuration.resolveSource() );
|
||||
}
|
||||
|
||||
|
||||
|
||||
//
|
||||
// UTILITY METHODS
|
||||
//
|
||||
|
||||
/**
|
||||
* If the test page is not set yet or does not exist, creates it.
|
||||
*/
|
||||
public File getTestPage() throws URISyntaxException, IOException
|
||||
{
|
||||
if ( testPage == null || !testPage.exists() )
|
||||
{
|
||||
testPage = buildTestPage().writeToFile(); // throw URI, IO
|
||||
}
|
||||
return testPage;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setTestPage( File testPage )
|
||||
{
|
||||
this.testPage = testPage;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getTestRunner() throws URISyntaxException, IOException
|
||||
{
|
||||
return getRequiredURISystemProperty( PROP_TESTRUNNER );
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** The best effort to get a well formed URI */
|
||||
protected 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.
|
||||
*/
|
||||
protected 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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* <p>Builds a new test page from the current System properties.</p>
|
||||
*
|
||||
* <p>See <tt>PROP_*</tt> constants for the list of recognised properties.</p>
|
||||
*/
|
||||
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
|
||||
//
|
||||
|
||||
/**
|
||||
* <p>Builds the URL based so that it points to the generated test page.</p>
|
||||
*
|
||||
* <p>Before calling this method, make sure {@link #setTestSuitePage(File)} has been correctly set.</p>
|
||||
*
|
||||
* <p>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).</p>
|
||||
*
|
||||
* @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 );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
232
ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/TestPage.java
Normal file
232
ant-jsunit-hieatt/trunk/src/main/java/net/jsunit/TestPage.java
Normal file
|
|
@ -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.<br/>
|
||||
* The corresponding value for this key in the {@link Map} must be a {@link Collection}<String> containing
|
||||
* all the <tt>src</tt> attributes of the corresponding <script/> tags.
|
||||
* @see #setIncludes(Map)
|
||||
*/
|
||||
public static final String INCLUDE_JAVASCRIPT = "text/javascript";
|
||||
|
||||
//
|
||||
// PRIVATE PROPERTIES AND INITIALISATION
|
||||
//
|
||||
|
||||
private static final String TEMPLATE_FILENAME = "TestPageTemplate.html";
|
||||
private static final String TEMPLATE_TAG_PROJECT = "@project@";
|
||||
private static final String TEMPLATE_TAG_JSUNITCORE = "@jsUnitCore.js@";
|
||||
private static final String TEMPLATE_TAG_INCLUDES = "@includes@";
|
||||
|
||||
private String project;
|
||||
private String jsUnitCore;
|
||||
private Map includes;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param project The name of the project (used as a title in the page)
|
||||
* @param JsUnitCore The <tt>src</tt> value of a <script/> tag to include JsUnit's core Javascript library
|
||||
* @param includes A map of all the external resources to include in the page, indexed on their type.
|
||||
* Use the <tt>INCLUDE_*</tt> constants as keys (types).
|
||||
* @throws IOException
|
||||
* @throws URISyntaxException
|
||||
*/
|
||||
public TestPage( String project, String JsUnitCore, Map includes )
|
||||
throws IOException, URISyntaxException
|
||||
{
|
||||
setProject( project );
|
||||
setJsUnitCore( JsUnitCore );
|
||||
setIncludes( includes );
|
||||
}
|
||||
|
||||
|
||||
|
||||
//
|
||||
// ACCESSORS
|
||||
//
|
||||
|
||||
public String getProject()
|
||||
{
|
||||
return project;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setProject( String project )
|
||||
{
|
||||
this.project = project;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getJsUnitCore()
|
||||
{
|
||||
return jsUnitCore;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setJsUnitCore( String jsUnitCore )
|
||||
{
|
||||
this.jsUnitCore = jsUnitCore;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Map getIncludes()
|
||||
{
|
||||
return includes;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setIncludes( Map includes )
|
||||
{
|
||||
this.includes = includes;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//
|
||||
// UTILITY METHODS
|
||||
//
|
||||
|
||||
/**
|
||||
* <p>Builds complete JsUnit test suite page from the current environment.</p>
|
||||
*
|
||||
* <p>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)}).</p>
|
||||
*
|
||||
* @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( "<script type=\"text/javascript\" src=\"" );
|
||||
includesBuffer.append( javascript/*new File( javascripts[i] ).toURI()*/);
|
||||
includesBuffer.append( "\"></script>\n" );
|
||||
}
|
||||
out = out.replace( TEMPLATE_TAG_INCLUDES, includesBuffer.toString() );
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* <p>Writes this page to a file.</p>
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* <p>Writes this page to a file.</p>
|
||||
*
|
||||
* @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();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * <p>Builds the URL to pass to {@link Configuration#setTestURL(URL)}</p>
|
||||
// *
|
||||
// * @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();
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
@ -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 <tt>testRunner.html</tt> 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( "<script type=\"text/javascript\" src=\"" );
|
||||
includesBuffer.append( javascript/*new File( javascripts[i] ).toURI()*/);
|
||||
includesBuffer.append( "\"></script>\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();
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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 );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,12 @@
|
|||
<html>
|
||||
<head>
|
||||
<!--
|
||||
This page was automatically generated to run a JsUnit test.
|
||||
You can delete it safely one the test is finished.
|
||||
If you have comments or feedbacks on this page,
|
||||
please visit http://nicobo.net/contact?subject=jsunit+ant
|
||||
-->
|
||||
|
||||
<title>Test Page for @project@</title>
|
||||
|
||||
<!-- JsUnit requirement -->
|
||||
|
|
@ -10,10 +17,12 @@
|
|||
|
||||
<!-- "Automagically" detects test functions -->
|
||||
<script type="text/javascript">
|
||||
// This function is specified in JsUnit's documentation.
|
||||
function exposeTestFunctionNames()
|
||||
{
|
||||
var tests = new Array();
|
||||
for ( var o in window ) {
|
||||
// matches all functions which name starts with 'test'
|
||||
if( o.match(/^test/) && typeof(window[o]) == "function" ) {
|
||||
tests.push(o);
|
||||
}
|
||||
|
|
@ -24,7 +33,7 @@
|
|||
</head>
|
||||
<body>
|
||||
<!--
|
||||
A minimal body to inform readers of the way to use this file.
|
||||
A minimal body to inform potential readers of the way to use this file.
|
||||
-->
|
||||
<h1>Test Page for @project@</h1>
|
||||
<p>
|
||||
|
|
@ -1,232 +0,0 @@
|
|||
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 );
|
||||
// // }
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
package net.jsunit;
|
||||
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestResult;
|
||||
import junit.textui.TestRunner;
|
||||
|
||||
|
||||
|
||||
public class GeneratedStandaloneTestTest extends TestCase
|
||||
{
|
||||
|
||||
public void testTestStandaloneRun()
|
||||
{
|
||||
GeneratedStandaloneTest test = new GeneratedStandaloneTest( "A test for a test !" );
|
||||
TestResult result = TestRunner.run( test );
|
||||
assert (result.failureCount() == 0 && result.errorCount() == 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
package net.jsunit;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestResult;
|
||||
import junit.textui.TestRunner;
|
||||
|
||||
|
||||
|
||||
public class TestLibRunnerTest extends TestCase
|
||||
{
|
||||
private String urlfail = getClass().getResource( "fail.js" ).toString();
|
||||
private String urlSuccess1 = getClass().getResource( "success1.js" ).toString();
|
||||
private String urlSuccess2 = getClass().getResource( "success2.js" ).toString();
|
||||
|
||||
|
||||
|
||||
protected void setUp() throws Exception
|
||||
{
|
||||
System.setProperty( TestLibRunnerConfigurationSource.PROP_PROJECT, getClass().getName() );
|
||||
// TODO ...
|
||||
urlfail = getClass().getResource( "fail.js" ).toString();
|
||||
urlSuccess1 = getClass().getResource( "success1.js" ).toString();
|
||||
urlSuccess2 = getClass().getResource( "success2.js" ).toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void testTestStandaloneRun()
|
||||
{
|
||||
// fails if the test fails
|
||||
System.setProperty( TestLibRunnerConfigurationSource.PARAM_TESTPAGE, urlfail );
|
||||
TestLibRunner test = new TestLibRunner( new TestLibRunnerConfigurationSource() );
|
||||
TestResult result = TestRunner.run( test );
|
||||
assert (result.failureCount() > 0 && result.errorCount() == 0);
|
||||
|
||||
// succeeds if the test succeeds
|
||||
System.setProperty( TestLibRunnerConfigurationSource.PARAM_TESTPAGE, urlSuccess1 );
|
||||
test = new TestLibRunner( new TestLibRunnerConfigurationSource() );
|
||||
result = TestRunner.run( test );
|
||||
assert (result.failureCount() == 0 && result.errorCount() == 0);
|
||||
|
||||
// fails if at least one test fails
|
||||
System.setProperty( TestLibRunnerConfigurationSource.PARAM_TESTPAGE, urlfail
|
||||
+ File.pathSeparator + urlSuccess1 );
|
||||
test = new TestLibRunner( new TestLibRunnerConfigurationSource() );
|
||||
result = TestRunner.run( test );
|
||||
assert (result.failureCount() > 0 && result.errorCount() == 0);
|
||||
|
||||
// succeeds if no test fails
|
||||
System.setProperty( TestLibRunnerConfigurationSource.PARAM_TESTPAGE, urlSuccess1
|
||||
+ File.pathSeparator + urlSuccess2 );
|
||||
test = new TestLibRunner( new TestLibRunnerConfigurationSource() );
|
||||
result = TestRunner.run( test );
|
||||
assert (result.failureCount() == 0 && result.errorCount() == 0);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue