I have a question that relates to testing via taskdefs & classloaders 
under ant.

I have not been able to conduct tests after defining a classpath within 
ant's build.xml file.  If I define the classpath externally (i.e. within 
the shell that executes ant), I am able to conduct my tests.  If I 
instead define a classpath in either taskdefs, or in the targets 
themselves, I get ClassNotFound exceptions.

Back when cactus was j2eeunit, Vincent explained that use of explicit 
classpath entries in either taskdefs or targets causes ant to run each of 
the tasks [having custom classpaths] under different classloaders, and so 
it is not possible for one task to call another (as done by the cactus 
server task) under these circumstances.  This explains why things work 
when the classpath is configured externally by the shell - all of the 
tasks run under the same classloader. 

I would now like to dispense with the use of a wrapper script that sets 
up the classpath for me, and instead define the classpath within my 
build.xml file.  I want to use a combination of path and refid 
statements, as shown below. 

Suggestions appreciated.

Here's an example of what I'd like to do:

  <!-- Build classpath -->
  <path id="classpath">
    <fileset dir="../lib">
      <include name="**/*.jar"/>
    </fileset>
    <fileset dir="../../../../common/lib">
      <include name="**/servlet*.jar"/>
    </fileset>
  </path>

...

<taskdef name="runservertests" 
classname="org.apache.commons.cactus.ant.RunServerTestsTask">
      <classpath refid="classpath"/>
<taskdef/>
<taskdef name="junit" 
classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
      <classpath refid="classpath"/>
<taskdef/>
<taskdef name="batchtest" 
classname="org.apache.tools.ant.taskdefs.optional.junit.BatchTest">
      <classpath refid="classpath"/>
<taskdef/>

...

<target name="run-tests">
        <junit printsummary="yes" haltonfailure="yes" haltonerror="yes" 
fork="yes">
          <formatter type="plain" usefile="false"/>
          <batchtest>
            <fileset dir="${src.dir}">
              <include name="**/*ServletTest.java"/>
            </fileset>
          </batchtest>
        </junit>
</target>

<target name="tests" depends="init-tasks">
        <runservertests testURL="http://localhost:8080";
            startTarget="start_tomcat_40"
            stopTarget="stop_tomcat_40"
            testTarget="run-tests"/>
</target>

Reply via email to