You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Ja...@rzf.fin-nrw.de on 2004/07/08 14:43:47 UTC

Class.forName() in test environment

I´ve added the capability to specify a class which the <modified> selector
should load as cache|algorithm|comparator. Test passed.

BUT

I have to add "-lib build\testcases" for running the tests, otherwise
the selector wont find a mock class which resides in the test package.

A first view into the build.xml seems to add the test classes (and sources)
to the classpath, but it doesnt ...

Has someone an idea?


Jan



src/main/.../selectors/modfiedselector
    ModifiedSelector.java:   Object rv =
Class.forName(classname).newInstance();

src/testcases/.../selectors/
    ModifiedSelectorTest.java: 
        String algo =
getAlgoName("org.apache.tools.ant.types.selectors.MockAlgorithm");
        ...
        private String getAlgoName(String classname) {
            ModifiedSelector sel = new ModifiedSelector();
            sel.setAlgorithmClass(classname);
            // let the selector do its checks
            sel.validate();
            String s1 = sel.toString();
            int posStart = s1.indexOf("algorithm=") + 10;
            int posEnd   = s1.indexOf(" comparator=");
            String algo  = s1.substring(posStart, posEnd);
            ...
            return algo;
        }

    MockAlgorithm.java
    MockCache.java
    MockComparator.java

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: Class.forName() in test environment

Posted by Stefan Bodewig <bo...@apache.org>.
On Thu, 08 Jul 2004, Steve Loughran <st...@apache.org> wrote:

> I'd suspect the classloader used for loading the algorithm is not
> the one you want. There may be a context class loader (We dont use
> that in ant, yet, do we?),

We do - in some places.  And I think the junit task sets it.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: Class.forName() in test environment

Posted by Steve Loughran <st...@apache.org>.
Jan.Materne@rzf.fin-nrw.de wrote:
> I´ve added the capability to specify a class which the <modified> selector
> should load as cache|algorithm|comparator. Test passed.
> 
> BUT
> 
> I have to add "-lib build\testcases" for running the tests, otherwise
> the selector wont find a mock class which resides in the test package.
> 
> A first view into the build.xml seems to add the test classes (and sources)
> to the classpath, but it doesnt ...
> 

I'd suspect the classloader used for loading the algorithm is not the 
one you want. There may be a context class loader (We dont use that in 
ant, yet, do we?), or you find the taskdef that declares the algorithm, 
the classpath of it and you are away. Yes, this probably means a new 
kind of ant type...

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Re: Class.forName() in test environment

Posted by Stefan Bodewig <bo...@apache.org>.
On Thu, 8 Jul 2004, Jan Materne <Ja...@rzf.fin-nrw.de> wrote:

> A first view into the build.xml seems to add the test classes (and
> sources) to the classpath, but it doesnt ...

Delegating class loaders.

The core Ant classes are loaded by a classloader closer to the system
classloader than the one the nested <classpath> defines, so your
Class.forName never has a chance to reach up for it.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org