mirror of
https://github.com/nicolabs/ciform.git
synced 2025-09-07 16:10:07 +02:00
# fixed : unit test is working
This commit is contained in:
parent
3b99410519
commit
599be62970
|
@ -40,3 +40,11 @@ target.dir=${basedir}/target
|
|||
|
||||
# Where to copy the api documentation
|
||||
doc.dir=${target.dir}/api
|
||||
|
||||
# JsUnit files
|
||||
jsunit.dir=${lib.dir}/jsunit
|
||||
jsunit.testRunner=${jsunit.dir}/testRunner.html
|
||||
jsunit.coreJs=${jsunit.dir}/app/jsUnitCore.js
|
||||
# define the following in a 'local.properties' file in the same directory than this file
|
||||
#jsunit.browsers=/usr/bin/konqueror:/usr/bin/firefox
|
||||
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
<!--
|
||||
Build script for the project "ant-jsunit-hieatt".
|
||||
Build script for the project "ant-jsunit-hieatt".
|
||||
|
||||
This script is an Ant script using Ivy extension to resolve dependencies.
|
||||
You will need both Ant (http://ant.apache.org/) and Ivy (http://ant.apache.org/ivy) to use it.
|
||||
This script is an Ant script using Ivy extension to resolve dependencies.
|
||||
You will need both Ant (http://ant.apache.org/) and Ivy (http://ant.apache.org/ivy) to use it.
|
||||
|
||||
TODO ? organise build files in /tests/, /bin/, ... so we can package easily ?
|
||||
-->
|
||||
<project name="ant-jsunit-hieatt" default="verify" xmlns:ivy="antlib:org.apache.ivy.ant">
|
||||
<project name="ant-jsunit-hieatt" default="verify"
|
||||
xmlns:ivy="antlib:org.apache.ivy.ant"
|
||||
xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors">
|
||||
|
||||
|
||||
<!-- ==================== -->
|
||||
|
@ -16,6 +20,37 @@
|
|||
<!-- default properties -->
|
||||
<property file="build.properties"/>
|
||||
|
||||
<!-- Source path : sources and resources -->
|
||||
<path id="srcpath">
|
||||
<dirset dir="${src.dir}">
|
||||
<include name="java"/>
|
||||
<include name="resources"/>
|
||||
</dirset>
|
||||
<dirset dir="${tests.dir}">
|
||||
<include name="java"/>
|
||||
<include name="javascript"/>
|
||||
</dirset>
|
||||
</path>
|
||||
|
||||
|
||||
<!-- Resources only -->
|
||||
<path id="rc.files">
|
||||
<fileset dir="${src.dir}/java" excludes="**/*.java"/>
|
||||
<fileset dir="${src.dir}/resources" excludes="**/*.java"/>
|
||||
<fileset dir="${tests.dir}/java" excludes="**/*.java"/>
|
||||
<fileset dir="${tests.dir}/javascript" excludes="**/*.java"/>
|
||||
</path>
|
||||
|
||||
|
||||
<!-- Classpath for Java operations -->
|
||||
<path id="lib.files">
|
||||
<fileset dir="${lib.dir}">
|
||||
<include name="**/*.jar"/>
|
||||
<exclude name="jsunit/java/lib/ant.jar"/>
|
||||
<exclude name="jsunit/java/lib/junit.jar"/>
|
||||
</fileset>
|
||||
</path>
|
||||
|
||||
|
||||
<!-- ==================== -->
|
||||
<!-- Public targets : they are greatly inspired from the Maven build lifecycle -->
|
||||
|
@ -41,20 +76,14 @@
|
|||
|
||||
<target name="compile" depends="validate" description="Compiles the source code of the project.">
|
||||
|
||||
<javac srcdir="${src.dir}" destdir="${classes.dir}" source="1.4">
|
||||
<src path="${src.dir}/java" />
|
||||
<src path="${src.dir}/resources" />
|
||||
<src path="${tests.dir}/java" />
|
||||
<src path="${tests.dir}/javascript" />
|
||||
<classpath>
|
||||
<fileset dir="${lib.dir}">
|
||||
<include name="*.jar"/>
|
||||
</fileset>
|
||||
</classpath>
|
||||
<javac destdir="${classes.dir}" classpathref="lib.files" source="1.4" debug="true">
|
||||
<src refid="srcpath" />
|
||||
</javac>
|
||||
|
||||
<dirset dir="/home/cbonar/src/jsunit/java/bin"/>
|
||||
<dirset dir="${jsunit.dir}/java/config"/>
|
||||
<!-- Copies all remaining files (= all files under the source tree, except Java sources) to the build dir. -->
|
||||
<copy todir="${classes.dir}">
|
||||
<path refid="rc.files"/>
|
||||
</copy>
|
||||
|
||||
</target>
|
||||
|
||||
|
@ -62,10 +91,25 @@
|
|||
<!-- NOTE : These tests should not require the code be packaged or deployed. -->
|
||||
<target name="test" depends="compile" description="Tests the compiled source code using a suitable unit testing framework.">
|
||||
|
||||
<junit fork="true">
|
||||
<test name="java.net.jsunit.GeneratedStandaloneTestTest" todir="${tests.logs}">
|
||||
<junit fork="false" haltonerror="true" haltonfailure="true">
|
||||
|
||||
<sysproperty key="jsunit.testRunner" value="${jsunit.testRunner}" />
|
||||
<sysproperty key="jsunit.coreJs" value="${jsunit.coreJs}" />
|
||||
<sysproperty key="logsDirectory" value="${tests.logs}" />
|
||||
<sysproperty key="browserFileNames" value="${jsunit.browsers}" />
|
||||
|
||||
<classpath>
|
||||
<path refid="lib.files" />
|
||||
<pathelement path="${classes.dir}"/>
|
||||
<!-- TODO : make the following a parameter -->
|
||||
<dirset dir="${jsunit.dir}/java/config"/>
|
||||
</classpath>
|
||||
|
||||
<test name="net.jsunit.TestLibRunnerTest" todir="${tests.logs}">
|
||||
<formatter type="brief" usefile="false" />
|
||||
<formatter type="xml" />
|
||||
</test>
|
||||
|
||||
</junit>
|
||||
|
||||
</target>
|
||||
|
|
|
@ -77,7 +77,7 @@ public class TestLibRunner extends StandaloneTest
|
|||
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)
|
||||
// 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()
|
||||
{
|
||||
|
|
|
@ -3,12 +3,8 @@ 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;
|
||||
|
||||
|
@ -16,6 +12,7 @@ import net.jsunit.configuration.Configuration;
|
|||
import net.jsunit.configuration.ConfigurationProperty;
|
||||
import net.jsunit.configuration.ConfigurationSource;
|
||||
import net.jsunit.configuration.DelegatingConfigurationSource;
|
||||
import net.jsunit.utility.SourcePathUtil;
|
||||
|
||||
|
||||
|
||||
|
@ -59,7 +56,7 @@ public class TestLibRunnerConfigurationSource extends
|
|||
{
|
||||
if ( testPage == null || !testPage.exists() )
|
||||
{
|
||||
testPage = buildTestPage().writeToFile(); // throw URI, IO
|
||||
testPage = buildTestPage().writeToFile(); // throw URIx, IOx
|
||||
}
|
||||
return testPage;
|
||||
}
|
||||
|
@ -73,36 +70,24 @@ public class TestLibRunnerConfigurationSource extends
|
|||
|
||||
|
||||
|
||||
public String getTestRunner() throws URISyntaxException, IOException
|
||||
public URI 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Simply throw an exception id the property is not found (helps
|
||||
* keeping the code clear).
|
||||
*
|
||||
* @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 )
|
||||
protected static URI getRequiredURISystemProperty( String key )
|
||||
throws URISyntaxException
|
||||
{
|
||||
String val = System.getProperty( key );
|
||||
|
@ -113,7 +98,7 @@ public class TestLibRunnerConfigurationSource extends
|
|||
throw new IllegalArgumentException( "Missing property : " + key );
|
||||
}
|
||||
|
||||
return getURI( val ).toString();
|
||||
return SourcePathUtil.normalizePath( val );
|
||||
}
|
||||
|
||||
|
||||
|
@ -128,10 +113,10 @@ public class TestLibRunnerConfigurationSource extends
|
|||
{
|
||||
// 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 ) );
|
||||
String jsUnitCore = getRequiredURISystemProperty( PROP_COREJS ).toASCIIString();
|
||||
String javascripts = System.getProperty( PROP_JAVASCRIPTS, "" );
|
||||
Map includes = new Hashtable();
|
||||
includes.put( TestPage.INCLUDE_JAVASCRIPT, javascripts );
|
||||
includes.put( TestPage.INCLUDE_JAVASCRIPT, SourcePathUtil.sourcePathToURI( javascripts ) );
|
||||
|
||||
// b. Builds the test page from the parameters
|
||||
return new TestPage( project, jsUnitCore, includes );
|
||||
|
@ -148,20 +133,21 @@ public class TestLibRunnerConfigurationSource extends
|
|||
*
|
||||
* <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>
|
||||
* <p>NOTE : This is a bit weird because the test page's file is created in this method (through a call to {@link #getTestPage()}),
|
||||
* but must be deleted from "outside" by the calling unit test once done. This is because I had to hack into this class
|
||||
* to reuse a 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
|
||||
* FIXME ? don't create the test page here ?
|
||||
*/
|
||||
public String url()
|
||||
{
|
||||
try
|
||||
{
|
||||
return getTestRunner() + "?" + PARAM_TESTPAGE + "="
|
||||
+ getTestPage().getCanonicalPath();
|
||||
URI tr = getTestRunner();
|
||||
return new URI( tr.getScheme(), tr.getUserInfo(), tr.getHost(), tr.getPort(), tr.getPath(), PARAM_TESTPAGE
|
||||
+ "=" + getTestPage().getCanonicalPath(), tr.getFragment() ).toASCIIString();
|
||||
}
|
||||
catch ( URISyntaxException urise )
|
||||
{
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package net.jsunit;
|
||||
|
||||
|
||||
import net.jsunit.utility.SourcePathUtil;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Describes the system properties recognised by {@link TestLibRunner}.
|
||||
*
|
||||
|
@ -12,7 +16,8 @@ public interface TestLibRunnerParameters
|
|||
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). */
|
||||
/** Property name defining the Javascript files to include in the test page (path-like).
|
||||
* @see SourcePathUtil */
|
||||
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";
|
||||
|
|
|
@ -5,24 +5,44 @@ import java.io.File;
|
|||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.MissingResourceException;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.transform.OutputKeys;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerConfigurationException;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import net.jsunit.utility.SourcePathUtil;
|
||||
|
||||
import org.w3c.dom.DOMImplementation;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This utility class helps building JsUnit (HTML) test pages, provided a list of files to include.
|
||||
* <p>This utility class helps building JsUnit (HTML) test pages, provided a list of files to include.</p>
|
||||
*
|
||||
* <p>TODO : allow the inclusion of distant files, not only local ones</p>
|
||||
* @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
|
||||
* The corresponding value for this key in the {@link Map} must be a {@link Collection}<{@link URI}> containing
|
||||
* all the <tt>src</tt> attributes of the corresponding <script/> tags.
|
||||
* @see #setIncludes(Map)
|
||||
*/
|
||||
|
@ -111,6 +131,49 @@ public class TestPage
|
|||
// UTILITY METHODS
|
||||
//
|
||||
|
||||
/**
|
||||
* This method is to make sure we only include well-formed elements in the HTML page (since
|
||||
* the resources to include are coming from outside, we cannot say if they're correctly formated or not).
|
||||
* @see http://www.javazoom.net/services/newsletter/xmlgeneration.html
|
||||
*/
|
||||
private String buildJavascriptTag( String src ) throws URISyntaxException,
|
||||
UnsupportedOperationException
|
||||
{
|
||||
try
|
||||
{
|
||||
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
|
||||
DOMImplementation dom = domBuilder.getDOMImplementation();
|
||||
Document xmlDoc = dom.createDocument( null, null, null );
|
||||
Element tag = xmlDoc.createElement( "script" );
|
||||
tag.setAttribute( "type", "text/javascript" );
|
||||
tag.setAttribute( "src", SourcePathUtil.normalizePath( src ).toASCIIString() );
|
||||
|
||||
TransformerFactory tf = TransformerFactory.newInstance();
|
||||
Transformer serializer = tf.newTransformer();
|
||||
serializer.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "yes" );
|
||||
serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
|
||||
StringWriter sw = new StringWriter();
|
||||
serializer.transform( new DOMSource( tag ), new StreamResult( sw ) );
|
||||
|
||||
return sw.toString();
|
||||
}
|
||||
catch ( ParserConfigurationException pce )
|
||||
{
|
||||
throw new UnsupportedOperationException( pce );
|
||||
}
|
||||
catch ( TransformerConfigurationException tce )
|
||||
{
|
||||
throw new UnsupportedOperationException( tce );
|
||||
}
|
||||
catch ( TransformerException te )
|
||||
{
|
||||
throw new UnsupportedOperationException( te );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* <p>Builds complete JsUnit test suite page from the current environment.</p>
|
||||
*
|
||||
|
@ -118,7 +181,7 @@ public class TestPage
|
|||
*
|
||||
* @return The content of the generated test page
|
||||
*/
|
||||
public String asString()
|
||||
public String asString() throws URISyntaxException
|
||||
{
|
||||
// Reads the template of the test suite page to generate into a local buffer
|
||||
InputStream is = getClass().getResourceAsStream( TEMPLATE_FILENAME );
|
||||
|
@ -147,14 +210,14 @@ public class TestPage
|
|||
|
||||
// Other includes : currently only Javascript is supported
|
||||
StringBuffer includesBuffer = new StringBuffer();
|
||||
|
||||
Collection javascripts = (Collection) getIncludes().get( INCLUDE_JAVASCRIPT );
|
||||
for ( Iterator itj = javascripts.iterator(); itj.hasNext(); )
|
||||
{
|
||||
String javascript = (String) itj.next();
|
||||
includesBuffer.append( "<script type=\"text/javascript\" src=\"" );
|
||||
includesBuffer.append( javascript/*new File( javascripts[i] ).toURI()*/);
|
||||
includesBuffer.append( "\"></script>\n" );
|
||||
URI javascript = (URI) itj.next();
|
||||
includesBuffer.append( buildJavascriptTag( javascript.toASCIIString() ) );
|
||||
}
|
||||
|
||||
out = out.replace( TEMPLATE_TAG_INCLUDES, includesBuffer.toString() );
|
||||
|
||||
return out;
|
||||
|
@ -169,7 +232,7 @@ public class TestPage
|
|||
* @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
|
||||
public File writeTo( File file ) throws IOException, URISyntaxException
|
||||
{
|
||||
FileWriter fw = new FileWriter( file );
|
||||
fw.write( asString() );
|
||||
|
@ -187,45 +250,18 @@ public class TestPage
|
|||
* @throws IOException In case the temporary file failed to be created
|
||||
* @see #writeTo(File)
|
||||
*/
|
||||
public File writeToFile( String filename ) throws IOException
|
||||
public File writeToFile( String filename ) throws IOException,
|
||||
URISyntaxException
|
||||
{
|
||||
File file = filename != null ? new File( filename ) : File.createTempFile( "jsunit-", ".tmp" );
|
||||
File file = filename != null ? new File( filename ) : File.createTempFile( "tmp-jsunit-", ".html" );
|
||||
return writeTo( file );
|
||||
}
|
||||
|
||||
|
||||
|
||||
public File writeToFile() throws IOException
|
||||
public File writeToFile() throws IOException, URISyntaxException
|
||||
{
|
||||
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();
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,156 @@
|
|||
package net.jsunit.utility;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Utility class to deal with source paths, as in Java's Classpath.
|
||||
*
|
||||
* @see http://java.sun.com/j2se/1.3/docs/tooldocs/win32/classpath.html
|
||||
* @see http://ant.apache.org/manual/index.html
|
||||
* @author http://nicobo.net/contact?subject=jsunit+ant
|
||||
*/
|
||||
public class SourcePathUtil
|
||||
{
|
||||
|
||||
/**
|
||||
* <p>The best effort to get a well formed URI to a file.</p>
|
||||
*
|
||||
* <p>If the given path does not have a scheme, makes sure the returned {@link URI} will have the "file:" scheme,
|
||||
* so it can be transformed into a {@link URL}.<br>
|
||||
* Useful to easily manipulate easily filenames given in a source path.</p>
|
||||
*
|
||||
* @return a URI representing the given path
|
||||
*/
|
||||
public static URI normalizePath( String path ) throws URISyntaxException
|
||||
{
|
||||
try
|
||||
{
|
||||
URI uri = new URI( path );
|
||||
if ( uri.getScheme() == null )
|
||||
{
|
||||
// if the URI is correct but misses the scheme part
|
||||
uri = new File( path ).toURI();
|
||||
}
|
||||
return uri.normalize();
|
||||
}
|
||||
catch ( URISyntaxException urise )
|
||||
{
|
||||
// path can be a valid file but still don't fit to URI syntax
|
||||
return new File( path ).toURI().normalize();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Creates a source path from a list of strings (which are supposedly file names).
|
||||
* Takes care of the special cases where the separator would be inside file names too.
|
||||
*/
|
||||
public static String filenamesToSourcePath( List pathElements,
|
||||
String separator )
|
||||
{
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
for ( Iterator itp = pathElements.iterator(); itp.hasNext(); )
|
||||
{
|
||||
String pathElement = ((Object) itp.next()).toString();
|
||||
buffer.append( pathElement.replaceAll( separator, "\\\\"
|
||||
+ separator ) );
|
||||
if ( itp.hasNext() )
|
||||
{
|
||||
buffer.append( separator );
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Uses {@link File#pathSeparator} as the separator
|
||||
* @see #filenamesToSourcePath(List, String)
|
||||
*/
|
||||
public static String filenamesToSourcePath( List pathElements )
|
||||
{
|
||||
return filenamesToSourcePath( pathElements, File.pathSeparator );
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Splits a source path that was encoded with {@link #filenamesToSourcePath(List, String)}
|
||||
* @return The list of filenames / components as {@link String}, unescaped if necessary
|
||||
*/
|
||||
public static List sourcePathToString( String sourcePath, String separator )
|
||||
{
|
||||
List list = new Vector();
|
||||
|
||||
// First correctly extracts the path elements
|
||||
String[] pathElements = sourcePath.split( "(?<!\\\\)" + separator );
|
||||
// Then unescapes the potentials in-separators
|
||||
for ( int p = 0; p < pathElements.length; p++ )
|
||||
{
|
||||
list.add( pathElements[p].replaceAll( "\\\\" + separator, separator ) );
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Uses {@link File#pathSeparator} as the separator
|
||||
* @see #sourcePathToString(String, String)
|
||||
*/
|
||||
public static List sourcePathToString( String sourcePath )
|
||||
{
|
||||
return sourcePathToString( sourcePath, File.pathSeparator );
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return a {@link List}<{@link URI}>
|
||||
* @throws URISyntaxException If any of the source path component is not a correct URI
|
||||
* @see {@link #sourcePathToString(String, String)}
|
||||
* @see {@link #normalizePath(String)}
|
||||
*/
|
||||
public static List sourcePathToURI( String sourcePath, String separator )
|
||||
throws URISyntaxException, UnsupportedEncodingException
|
||||
{
|
||||
List list = new Vector();
|
||||
|
||||
for ( Iterator its = sourcePathToString( sourcePath, separator ).iterator(); its.hasNext(); )
|
||||
{
|
||||
String pathElement = (String) its.next();
|
||||
if ( pathElement.trim().length() > 0 )
|
||||
{
|
||||
list.add( normalizePath( pathElement ) );
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Uses {@link File#pathSeparator} as the separator
|
||||
* @see #sourcePathToURI(String, String)
|
||||
*/
|
||||
public static List sourcePathToURI( String sourcePath )
|
||||
throws URISyntaxException, UnsupportedEncodingException
|
||||
{
|
||||
return sourcePathToURI( sourcePath, File.pathSeparator );
|
||||
}
|
||||
|
||||
}
|
|
@ -2,60 +2,100 @@ package net.jsunit;
|
|||
|
||||
|
||||
import java.io.File;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestResult;
|
||||
import junit.textui.TestRunner;
|
||||
import net.jsunit.configuration.ConfigurationProperty;
|
||||
import net.jsunit.utility.SourcePathUtil;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This test is a bit unusual since :
|
||||
* <ol>
|
||||
* <li>It is a test for a JUnit {@link TestCase}
|
||||
* <li>It requires parameters to run correctly, that must be defined as system {@link Properties}.
|
||||
* </ol>
|
||||
*
|
||||
* @see #setUp()
|
||||
* @author http://nicobo.net/contact?subject=jsunit+ant
|
||||
*/
|
||||
public class TestLibRunnerTest extends TestCase implements
|
||||
TestLibRunnerParameters
|
||||
{
|
||||
private String urlfail;
|
||||
private String urlSuccess1;
|
||||
private String urlSuccess2;
|
||||
private File jsFail;
|
||||
private File jsSuccess1;
|
||||
private File jsSuccess2;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* <p>The following system properties must be set before calling this test :<ul>
|
||||
* <li>{@link TestLibRunnerParameters#PROP_TESTRUNNER}
|
||||
* <li>{@link TestLibRunnerParameters#PROP_COREJS}
|
||||
* <li>{@link ConfigurationProperty#BROWSER_FILE_NAMES}
|
||||
* </ul></p>
|
||||
*
|
||||
* <p>This test also requires the following entries to be in the classpath :<ul>
|
||||
* <li><tt><jsunit>/java/lib/*.jar</tt>
|
||||
* <li>jsunit.jar (can be found in <tt><jsunit>/java/bin</tt>)
|
||||
* <li><tt><jsunit>/java/config</tt> (containing <tt>.xml</tt> configs)
|
||||
* </ul>
|
||||
* ... where <jsunit> is the directory where JsUnit is installed (see {@link ConfigurationProperty#RESOURCE_BASE}).</p>
|
||||
*
|
||||
* <p>In addition, any property defined in {@link ConfigurationProperty} can be set to refine the execution of the test.</p>
|
||||
*/
|
||||
protected void setUp() throws Exception
|
||||
{
|
||||
// Sets some more properties
|
||||
System.setProperty( PROP_PROJECT, getClass().getName() );
|
||||
System.setProperty( PROP_TESTRUNNER, new File( "/../lib/jsunit/testRunner.html" ).toURI().toString() );
|
||||
System.setProperty( PROP_COREJS, new File( "/../lib/jsunit/app/jsUnitCore.js" ).toURI().toString() );
|
||||
urlfail = getClass().getResource( "fail.js" ).toString();
|
||||
urlSuccess1 = getClass().getResource( "success1.js" ).toString();
|
||||
urlSuccess2 = getClass().getResource( "success2.js" ).toString();
|
||||
//System.setProperty( "resourceBase", "/home/cbonar/src/ant-jsunit-hieatt/lib/jsunit" );
|
||||
// if not set, sets a port less likely to conflict with existing services than the default one
|
||||
if ( !System.getProperties().containsKey( ConfigurationProperty.PORT.getName() ) )
|
||||
{
|
||||
System.setProperty( ConfigurationProperty.PORT.getName(), "45678" );
|
||||
}
|
||||
|
||||
// Loads the resource scripts that contains the test functions
|
||||
jsFail = new File( getClass().getResource( "fail.js" ).toURI() );
|
||||
jsSuccess1 = new File( getClass().getResource( "success1.js" ).toURI() );
|
||||
jsSuccess2 = new File( getClass().getResource( "success2.js" ).toURI() );
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void testTestStandaloneRun()
|
||||
public void testTestStandaloneRun() throws MalformedURLException
|
||||
{
|
||||
// fails if the test fails
|
||||
System.setProperty( TestLibRunnerConfigurationSource.PARAM_TESTPAGE, urlfail );
|
||||
TestLibRunner test = new TestLibRunner( new TestLibRunnerConfigurationSource() );
|
||||
TestResult result = TestRunner.run( test );
|
||||
assertTrue( result.failureCount() > 0 && result.errorCount() == 0 );
|
||||
String urlFail = jsFail.toURI().toURL().toString();
|
||||
String urlSuccess1 = jsSuccess1.toURI().toURL().toString();
|
||||
String urlSuccess2 = jsSuccess2.toURI().toURL().toString();
|
||||
|
||||
// succeeds if the test succeeds
|
||||
System.setProperty( TestLibRunnerConfigurationSource.PARAM_TESTPAGE, urlSuccess1 );
|
||||
test = new TestLibRunner( new TestLibRunnerConfigurationSource() );
|
||||
result = TestRunner.run( test );
|
||||
assertTrue( result.failureCount() == 0 && result.errorCount() == 0 );
|
||||
// must fail if the test fails
|
||||
List filesFail1 = Arrays.asList( new String[] { urlFail } );
|
||||
System.setProperty( PROP_JAVASCRIPTS, SourcePathUtil.filenamesToSourcePath( filesFail1 ) );
|
||||
TestLibRunner testFail1 = new TestLibRunner( new TestLibRunnerConfigurationSource() );
|
||||
assertFalse( TestRunner.run( testFail1 ).wasSuccessful() );
|
||||
|
||||
// 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 );
|
||||
assertTrue( result.failureCount() > 0 && result.errorCount() == 0 );
|
||||
// must succeed if the test succeeds
|
||||
List filesSuccess1 = Arrays.asList( new String[] { urlSuccess1 } );
|
||||
System.setProperty( PROP_JAVASCRIPTS, SourcePathUtil.filenamesToSourcePath( filesSuccess1 ) );
|
||||
TestLibRunner testSuccess1 = new TestLibRunner( new TestLibRunnerConfigurationSource() );
|
||||
assertTrue( TestRunner.run( testSuccess1 ).wasSuccessful() );
|
||||
|
||||
// succeeds if no test fails
|
||||
System.setProperty( TestLibRunnerConfigurationSource.PARAM_TESTPAGE, urlSuccess1
|
||||
+ File.pathSeparator + urlSuccess2 );
|
||||
test = new TestLibRunner( new TestLibRunnerConfigurationSource() );
|
||||
result = TestRunner.run( test );
|
||||
assertTrue( result.failureCount() == 0 && result.errorCount() == 0 );
|
||||
// must fail if at least one test fails
|
||||
List filesFail2 = Arrays.asList( new String[] { urlFail, urlSuccess1 } );
|
||||
System.setProperty( PROP_JAVASCRIPTS, SourcePathUtil.filenamesToSourcePath( filesFail2 ) );
|
||||
TestLibRunner testFail2 = new TestLibRunner( new TestLibRunnerConfigurationSource() );
|
||||
assertFalse( TestRunner.run( testFail2 ).wasSuccessful() );
|
||||
|
||||
// must succeed if all tests succeed
|
||||
List filesSuccess2 = Arrays.asList( new String[] { urlSuccess1, urlSuccess2 } );
|
||||
System.setProperty( PROP_JAVASCRIPTS, SourcePathUtil.filenamesToSourcePath( filesSuccess2 ) );
|
||||
TestLibRunner testSuccess2 = new TestLibRunner( new TestLibRunnerConfigurationSource() );
|
||||
assertTrue( TestRunner.run( testSuccess2 ).wasSuccessful() );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
package net.jsunit.utility;
|
||||
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
|
||||
|
||||
public class SourcePathUtilTest extends TestCase
|
||||
{
|
||||
|
||||
public void testFilenamesToSourcePathToList()
|
||||
{
|
||||
List filenames = Arrays.asList( new String[] { "/tmp/toto", "d:\\\\temp\\tutu", "Win32;sep", "Linux:sep" } );
|
||||
|
||||
// a test with Linux's path separator
|
||||
String spLinux = SourcePathUtil.filenamesToSourcePath( filenames, ":" );
|
||||
assertEquals( filenames, SourcePathUtil.sourcePathToString( spLinux, ":" ) );
|
||||
|
||||
// a test with Windows' path separator
|
||||
String spWindows = SourcePathUtil.filenamesToSourcePath( filenames, ";" );
|
||||
assertEquals( filenames, SourcePathUtil.sourcePathToString( spWindows, ";" ) );
|
||||
|
||||
// a test with the current OS's path separator
|
||||
String spLocal = SourcePathUtil.filenamesToSourcePath( filenames );
|
||||
assertEquals( filenames, SourcePathUtil.sourcePathToString( spLocal ) );
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void testNormalizePath() throws URISyntaxException,
|
||||
UnsupportedEncodingException
|
||||
{
|
||||
URI tmp_toto = new URI( "file", null, null, -1, "/tmp/toto", null, null );
|
||||
URI quote = new URI( "file", null, null, -1, "/tmp/to\"to", null, null );
|
||||
URI space = new URI( "file", null, null, -1, "/tmp/to to", null, null );
|
||||
|
||||
try
|
||||
{
|
||||
assertEquals( tmp_toto, SourcePathUtil.normalizePath( "/tmp/toto" ) );
|
||||
assertEquals( tmp_toto, SourcePathUtil.normalizePath( "file:/tmp/toto" ) );
|
||||
assertEquals( quote, SourcePathUtil.normalizePath( "/tmp/to\"to" ) );
|
||||
assertEquals( quote, SourcePathUtil.normalizePath( "file:/tmp/to%22to" ) );
|
||||
assertEquals( space, SourcePathUtil.normalizePath( "/tmp/to to" ) );
|
||||
assertEquals( space, SourcePathUtil.normalizePath( "file:/tmp/to%20to" ) );
|
||||
}
|
||||
catch ( URISyntaxException urise )
|
||||
{
|
||||
urise.printStackTrace( System.err );
|
||||
fail( urise.getMessage() );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue