ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sean coogan <meanz...@yahoo.com>
Subject Ant junit classloader not loading properly
Date Mon, 15 Dec 2008 22:29:23 GMT
Hi all,
 
I am running a junit test in ant that uses a java.net.URL third party protocol. The java.net.URL
fails to find third party protocol when the test is run in junit ant task. 
 
New protocol handlers can be easily added by having them placed in the same package ("sun.net.www.protocol")
as the java pre-defined protocols (ftp, http, https, etc - for more information see http://java.sun.com/developer/onlineTraining/protocolhandlers/).
 
For testing purposes, I have created a simple class named SCPTest.java (see below) that creates
a url using the custom scp protocol and has the jar containing the scp protocolhandler included
in the execution classpath. When executed, the url object gets created with the scp protocol.
 
When a simple junit test named TestURLProtocolLoading.java (see below) that does exactly the
same as the SCPTest.java class is executed under junit ant (see build.xml below), the test
ends-up failing with the error message of “unknown protocol: scp”.
 
According to http://ant.apache.org/faq.html#delegating-classloader, “When you specify a
nested <classpath> in Ant, Ant creates a new class loader that uses the path you have
specified. It then tries to load additional classes from this classloader.”
Because of this, it seems like my jar containing the scp protocol handler is not getting loaded
by ant’s classloader.
 
Things I have tried:
 - Adding the jar containing the custom protocol to ANT_HOME/lib.
 - Adding the <jvmarg value="-Djava.protocol.handler.pkgs=sun.net.www.protocol"/> to
the junit task in hopes that the protocol would get picked up, it did not.
 
Thank you in advance for any help.
 
Ant 1.7.0
Junit 3.8.1
JDK 1.5.0_14

NOTE: The following tests assume JAVA_HOME and ANT_HOME environment variables are set.

To run the tests first run the compile.bat script.
 
-- build.xml :
 
<?xml version="1.0" encoding="UTF-8"?>
<project default="test" basedir=".">
 
        <property name="src_unit" value="./src" />
        <property name="classes" value="./bin" />
        <property name="junit" value="lib/junit.jar" />
 
        <property name="test.reports" value="./Junit_Reports" />
 
 
        <property name="scp_jar" value="./lib/scp_protocol.jar" />
 
        <path id="classpath.base">
        </path>
 
        <path id="classpath.test">
                <pathelement location="${scp_jar}" />
                <pathelement location="${junit}" />
                <pathelement location="${classes}" />
        </path>
 
        <target name="test" depends="init">
 
                <junit fork="yes" printsummary="no" haltonfailure="no" haltonerror="no">
 
                        <formatter type="xml" />
 
                        <classpath refid="classpath.test" />

                        <batchtest fork="no" todir="${test.reports}">
                                <fileset dir="${src_unit}">
                                        <include
name="**/*TestURLP*.java" />
                                </fileset>
                        </batchtest>
                </junit>
 
                 <junitreport todir="${test.reports}">
                        <fileset dir="${test.reports}">
                                <include name="TEST-*.xml"
/>
                        </fileset>
                        <report todir="${test.reports}" />
                </junitreport>
        </target>
 
        <target name="init" depends="clean">
                <mkdir dir="${test.reports}"/>
                <echo>Created ${test.reports}</echo>
        </target>
 
        <target name="clean">
                <delete dir="${test.reports}" />
                <echo>Removed ${test.reports}</echo>
        </target>
 
</project>
 
 
 -- TestURLProtocolLoading.java : 
 
package test.util;
 
import java.net.URL;
 
import junit.framework.TestCase;
 
public class TestURLProtocolLoading extends TestCase {
 
        public void testURLProtocolLoading() {
                try {
                        URL url = new URL("scp://user:pass@host");
                        System.err.println(url.toExternalForm());
                        url = new URL("foo://user:pass@host");
                        System.err.println(url.toExternalForm());
                } catch (Throwable e) {
                        System.err.println(e.getMessage());
                        fail(e.getMessage());
                }
        }

    public static void main(String args[]) {
        junit.textui.TestRunner.run(TestURLProtocolLoading.class);
    }
}
 
 
 -- SCPTest.java : 
 
package test.util;

import java.net.URL;

public class SCPTest {

    public static void main(String[] args) {
        try {
            URL url = new URL("scp://user:pass@host");
            System.err.println(url.toExternalForm());
            url = new URL("foo://user:pass@host");
            System.err.println(url.toExternalForm());
        } catch (Throwable e) {
            System.err.println(e.getMessage());
        }
    }
}




      
Mime
View raw message