Hi,
A little update on this one. Haven't managed to get the java plugin test task
to work, but have managed to get 2 different ant inspired flavours to work.
...however none of them are ideal in my eyes. Would have preferred if I could
override/configure the java plugin test task..
1) custom task that invokes junit through ant -> have to run with -i on
commandline to get the output (:
2) custom java exec task which calls ant main invoking a xml build file (have
to maintain a separate file)
Alternative 1:
----------------
task testJunit << {
testReportsDir = new File(buildDir, "reports/tests")
testReportsDir.mkdirs()
ant {
taskdef(name: "runTests",
classname:"org.apache.tools.ant.taskdefs.optional.junit.JUnitTask") {
classpath {
pathelement(path:configurations.antJunitTest.asPath)
}
}
runTests(printsummary:"true", haltonfailure:"no", fork:"yes",
forkmode:"perBatch") {
jvmarg(value:"-Djavax.xml.transform.TransformerFactory=com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl")
jvmarg(value:"-Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl")
jvmarg(value:"-Djava.system.class.loader=com.telenor.cosmos.system.test.CosmosWeavingURLClassLoader")
jvmarg(value:"-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl")
classpath {
pathelement(path:configurations.testCompile.asPath)
pathelement(path:new File(buildDir, "classes/test"))
pathelement(path:new File(buildDir, "classes/test/aspects"))
pathelement(path:new File(buildDir, "classes/main"))
}
formatter(type:"plain")
batchtest(todir:testReportsDir, failureproperty:"junit.failure") {
fileset(dir:sourceSets.test.classesDir, includes:"**/*TestCase.class" )
}
}
}
}
Alternative 2:
task testJunitJava(type: JavaExec) {
classpath = configurations.antJunitTest
main = "org.apache.tools.ant.Main"
args = ["-buildfile", new File(projectDir, "build-tests-ltw.xml")]
systemProperties("test.classpath":configurations.testCompile.asPath,
"target.test.aspects":new File(buildDir, "classes/test-aspects"))
}
and the build-tests-ltw.xml (which obviously could be simplified a lot with
sending in properties from the gradle javaexec call):
<project name="external-unittest" default="test" basedir=".">
<target name="test">
<!-- Defaults. Can be overridden on the command line -->
<property name="maven.test.include" value="**/*TestCase.class"/>
<property name="maven.test.exclude" value=""/>
<property name="weaving.class.loader"
value="com.telenor.cosmos.system.test.CosmosWeavingURLClassLoader"/>
<junit printsummary="true" haltonfailure="no" fork="yes"
forkmode="perBatch">
<jvmarg
value="-Djavax.xml.transform.TransformerFactory=com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"/>
<jvmarg
value="-Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"/>
<jvmarg value="-Djava.system.class.loader=${weaving.class.loader}"/>
<jvmarg
value="-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"/>
<jvmarg value="-Xmx768m"/>
<jvmarg value="-XX:MaxPermSize=192m"/>
<classpath>
<pathelement path="${java.class.path}"/>
<pathelement path="${test.classpath}"/>
<pathelement
path="c:\dev\tools\apache-ant-1.8.1\libs17\ant-junit.jar"/>
<pathelement path="${target.test.aspects}"/>
<pathelement path="target/classes/test"/>
<pathelement path="target/classes/main"/>
</classpath>
<formatter type="xml"/>
<batchtest todir="target/test-results"
failureproperty="junit.failure">
<fileset dir="target/classes/test"
includes="${maven.test.include}" excludes="${maven.test.exclude}"/>
</batchtest>
</junit>
<antcall target="writeJunitIfSet"/>
<antcall target="writeJunitIfNotSet"/>
</target>
<target name="writeJunitIfSet" if="junit.failure">
<echo file="target/junit.tmp"
append="false">junit.failure=${junit.failure}
</echo>
</target>
<target name="writeJunitIfNotSet" unless="junit.failure">
<echo file="target/junit.tmp" append="false">#All tests passed
</echo>
</target>
</project>
cheers
Magnus
From: [email protected]
To: [email protected]
Date: Mon, 2 Aug 2010 07:12:56 +0000
Subject: [gradle-user] running tests with a weavingclassloader
HI,
I'm wondering if its possible(/and how) to get the gradle test task (from java
plugin) to run tests using a an aspecj WeavingClassLoader.
In our current build we run a forked java task in ant that invokes ant junit
(which again is forked per batch).
our current ant test;
<target name="test-junit" depends="check-test-junit-uptodate"
unless="test.junit.uptodate">
<mkdir dir="${target.test.reports}"/>
<property name="test.path" refid="test.path.id"/>
<java classname="org.apache.tools.ant.Main" fork="yes" dir=".">
<jvmarg value="-Xmx512m"/>
<jvmarg value="-XX:MaxPermSize=192m"/>
<arg value="-buildfile"/>
<arg value="${common.dir}/build-tests-ltw.xml"/>
<arg line="-Dbasedir='.'"/>
<arg line="-Dtest.classpath='${test.path}'"/>
<arg line="-Dmaven.junit.printSummary='true'"/>
<arg line="-Dmaven.test.include='**/*TestCase.class'"/>
<arg line="-Dweaving.class.loader='${weaving.class.loader}'"/>
<classpath>
<path location="c:\dev\tools\apache-ant-1.8.1\libs17\ant.jar"/>
<path
location="c:\dev\tools\apache-ant-1.8.1\libs17\ant-junit.jar"/>
<path
location="c:\dev\tools\apache-ant-1.8.1\libs17\ant-launcher.jar"/>
<path location="${lib.dir}/junit.jar"/>
</classpath>
</java>
<tstamp/>
<echo file="${junit.timestamp}">${DSTAMP} - ${TSTAMP}</echo>
</target>
contents of build-tests-ltw.xml
<target name="test">
<!-- Defaults. Can be overridden on the command line -->
<property name="maven.test.include" value="**/*TestCase.class"/>
<property name="maven.test.exclude" value=""/>
<property name="weaving.class.loader"
value="com.telenor.cosmos.system.test.CosmosWeavingURLClassLoader"/>
<junit printsummary="true" haltonfailure="no" fork="yes"
forkmode="perBatch">
<jvmarg
value="-Djavax.xml.transform.TransformerFactory=com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"/>
<jvmarg
value="-Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"/>
<jvmarg value="-Djava.system.class.loader=${weaving.class.loader}"/>
<jvmarg
value="-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"/>
<jvmarg value="-Xmx768m"/>
<jvmarg value="-XX:MaxPermSize=192m"/>
<classpath>
<pathelement path="${java.class.path}"/>
<pathelement path="${test.classpath}"/>
<pathelement path="${target.test.aspects}"/>
</classpath>
<formatter type="xml"/>
<batchtest todir="target/test-reports"
failureproperty="junit.failure">
<fileset dir="target/test-classes"
includes="${maven.test.include}" excludes="${maven.test.exclude}"/>
</batchtest>
</junit>
<antcall target="writeJunitIfSet"/>
<antcall target="writeJunitIfNotSet"/>
</target>
...never mind the duplication, the important bit is getting gradle to use the
CosmosWeavingClassLoader (that extends aspectj CosmosWeavingClassLoader).
I tried to simply set
dependencies {
compile project(':cosmos-test') // + a range of other dependencies...
testCompile // various test dependencies
testRuntime project(':cosmos-test') //this is the project containing the
CosmosWeavingClassLoader
}
test {
systemProperties['java.system.class.loader'] =
'com.telenor.cosmos.system.test.CosmosWeavingURLClassLoaders'
}
However that currently only results in;
"Error occurred during initialization of VM
java.lang.Error: java.lang.ClassNotFoundException:
com.telenor.cosmos.system.test.CosmosWeavingURLClassLoaders" ...
any tips/hints would be appreciated.
cheers
Magnus