You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by smallufo <sm...@gmail.com> on 2010/06/07 04:30:29 UTC
maven test cannot load cross-module resources/properties ?
I have a maven mantained project with some modules . One module contains one
XML file and one parsing class :Parser.java.
Second module depends on the first module. There is a class that extends the
parsing class in the first module , but maven seems cannot test the class in
the second module. Maven test reports :
java.lang.NullPointerException
at java.util.Properties.loadFromXML(Properties.java:851)
at foo.firstModule.Parser.<init>(Parser.java:92)
at foo.secondModule.Program.<init>(Program.java:84)
In "Parser.java" (in the first module) , it uses Properties and InputStream
to read/parse an XML file :
InputStream xmlStream = getClass().getResourceAsStream("Data.xml");
Properties properties = new Properties();
properties.loadFromXML(xmlStream);
The "data.xml" is located in first module's resources/foo/firstModule
directory , and it tests OK in the first module.
Here is my full code :
This is Parser in the first module :
package foo.firstModule;
import java.io.IOException;
import java.io.InputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
public class Parser
{
private Properties properties;
public Parser()
{
InputStream xmlStream = getClass().getResourceAsStream("Data.xml");
properties = new Properties();
try
{
properties.loadFromXML(xmlStream);
}
catch (InvalidPropertiesFormatException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public Properties getProperties()
{
return properties;
}
}
This is Parser's test case , passed .
package foo.firstModule;
import junit.framework.TestCase;
public class ParserTest extends TestCase
{
public void testParser()
{
Parser p = new Parser();
assertEquals(64 , p.getProperties().size());
}
}
This is ParserExtend in the secondModule , which extends Parser in the
firstModule :
package foo.secondModule;
import java.util.Properties;
import foo.firstModule.Parser;
public class ParserExtend extends Parser
{
private Properties properties;
public ParserExtend()
{
this.properties = getProperties();
}
public int getSize()
{
return properties.size();
}
}
This is ParserExtend's test case :
package foo.secondModule;
import junit.framework.TestCase;
public class ParserExtendTest extends TestCase
{
public void testParserExtend()
{
ParserExtend pe = new ParserExtend();
assertEquals(64 , pe.getSize());
}
}
The above test case failed because
*Properties.loadFromXML*(Properties.java:851)
throws NPE
However , if I don't extend Parser , just new the Parser :
package foo.secondModule;
import java.util.Properties;
import foo.firstModule.Parser;
public class ParserInit
{
private Properties properties;
public ParserInit()
{
Parser p = new Parser();
this.properties = p.getProperties();
}
public int getSize()
{
return properties.size();
}
}
and this is the test case :
package foo.secondModule;
import junit.framework.TestCase;
public class ParserInitTest extends TestCase
{
public void testParserInit()
{
ParserInit pi = new ParserInit();
assertEquals(64 , pi.getSize());
}
}
The test case passes !
This is my whole test scenario. It seems it 's maven and junit's classloader
problem.
Anybody able to find how to pass the ParserExtend's test case ? Thanks in
advanced.
Re: maven test cannot load cross-module resources/properties ?
Posted by Anders Hammar <an...@hammar.net>.
I'm still thinking that the xml file should be in foo/secondModule, because
that's the package of the second module class (that extends the one in
firstModule).
/Anders
On Mon, Jun 7, 2010 at 10:52, smallufo <sm...@gmail.com> wrote:
> 2010/6/7 Anders Hammar <an...@hammar.net>
> >
> > Could it be that it is looking for Data.xml at path
> > foo/secondModule/Data.xml. I'm guessing that this is a plain Java thing
> and
> > not related to Maven.
> >
> > /Anders
>
>
> Hi
> To solve this problem , I tried to use maven-dependency-plugin:unpack to
> solve my problem :
> In my secondModule's POM , I added these snippets :
>
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-dependency-plugin</artifactId>
> <version>2.1</version>
> <executions>
> <execution>
> <id>data-copying</id>
> <phase>test-compile</phase>
> <goals>
> <goal>unpack</goal>
> </goals>
> <configuration>
> <artifactItems>
> <artifactItem>
> <groupId>foo</groupId>
> <artifactId>firstModule</artifactId>
> <type>jar</type>
> <includes>foo/firstModule/Data.xml</includes>
>
> <outputDirectory>${project.build.directory}/classes</outputDirectory>
> </artifactItem>
> </artifactItems>
> </configuration>
> </execution>
> </executions>
> </plugin>
>
> In this POM file , I try to unpack the first module , and copy the Data.xml
> to classes/foo/firstModule/ directory , and then run tests.
> And indeed , it is copied to the right directory , I can find the
> "Data.xml"
> file in "target/classes/foo/firstModule" directory.
> But maven test still complains it cannot read the File
> (Properties.loadFromXML() throws NPE).
>
> I tried other output directory , such as
> ${project.build.directory}/resources , and
> ${project.build.directory}/test-classes , but all in vain...
>
Re: maven test cannot load cross-module resources/properties ?
Posted by smallufo <sm...@gmail.com>.
2010/6/7 Anders Hammar <an...@hammar.net>
>
> Could it be that it is looking for Data.xml at path
> foo/secondModule/Data.xml. I'm guessing that this is a plain Java thing
and
> not related to Maven.
>
> /Anders
Hi
To solve this problem , I tried to use maven-dependency-plugin:unpack to
solve my problem :
In my secondModule's POM , I added these snippets :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>data-copying</id>
<phase>test-compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>foo</groupId>
<artifactId>firstModule</artifactId>
<type>jar</type>
<includes>foo/firstModule/Data.xml</includes>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
In this POM file , I try to unpack the first module , and copy the Data.xml
to classes/foo/firstModule/ directory , and then run tests.
And indeed , it is copied to the right directory , I can find the "Data.xml"
file in "target/classes/foo/firstModule" directory.
But maven test still complains it cannot read the File
(Properties.loadFromXML() throws NPE).
I tried other output directory , such as
${project.build.directory}/resources , and
${project.build.directory}/test-classes , but all in vain...
Re: maven test cannot load cross-module resources/properties ?
Posted by Anders Hammar <an...@hammar.net>.
Could it be that it is looking for Data.xml at path
foo/secondModule/Data.xml. I'm guessing that this is a plain Java thing and
not related to Maven.
/Anders
On Mon, Jun 7, 2010 at 04:30, smallufo <sm...@gmail.com> wrote:
> I have a maven mantained project with some modules . One module contains
> one
> XML file and one parsing class :Parser.java.
>
> Second module depends on the first module. There is a class that extends
> the
> parsing class in the first module , but maven seems cannot test the class
> in
> the second module. Maven test reports :
>
> java.lang.NullPointerException
>
> at java.util.Properties.loadFromXML(Properties.java:851)
>
> at foo.firstModule.Parser.<init>(Parser.java:92)
>
> at foo.secondModule.Program.<init>(Program.java:84)
>
> In "Parser.java" (in the first module) , it uses Properties and InputStream
> to read/parse an XML file :
>
> InputStream xmlStream = getClass().getResourceAsStream("Data.xml");
> Properties properties = new Properties();
>
> properties.loadFromXML(xmlStream);
>
> The "data.xml" is located in first module's resources/foo/firstModule
> directory , and it tests OK in the first module.
>
> Here is my full code :
>
> This is Parser in the first module :
>
> package foo.firstModule;
>
> import java.io.IOException;
> import java.io.InputStream;
> import java.util.InvalidPropertiesFormatException;
> import java.util.Properties;
>
> public class Parser
> {
>
> private Properties properties;
>
> public Parser()
>
> {
>
> InputStream xmlStream = getClass().getResourceAsStream("Data.xml");
>
> properties = new Properties();
>
> try
>
> {
>
> properties.loadFromXML(xmlStream);
>
> }
>
> catch (InvalidPropertiesFormatException e)
>
> {
>
> e.printStackTrace();
>
> }
>
> catch (IOException e)
>
> {
>
> e.printStackTrace();
>
> }
>
> }
>
> public Properties getProperties()
>
> {
>
> return properties;
>
> }
> }
>
> This is Parser's test case , passed .
>
> package foo.firstModule;
>
> import junit.framework.TestCase;
>
> public class ParserTest extends TestCase
> {
>
> public void testParser()
>
> {
>
> Parser p = new Parser();
>
> assertEquals(64 , p.getProperties().size());
>
> }
> }
>
> This is ParserExtend in the secondModule , which extends Parser in the
> firstModule :
>
> package foo.secondModule;
>
> import java.util.Properties;
>
> import foo.firstModule.Parser;
>
> public class ParserExtend extends Parser
> {
>
> private Properties properties;
>
> public ParserExtend()
>
> {
>
> this.properties = getProperties();
>
> }
>
> public int getSize()
>
> {
>
> return properties.size();
>
> }
> }
>
> This is ParserExtend's test case :
>
> package foo.secondModule;
>
> import junit.framework.TestCase;
>
> public class ParserExtendTest extends TestCase
> {
>
> public void testParserExtend()
>
> {
>
> ParserExtend pe = new ParserExtend();
>
> assertEquals(64 , pe.getSize());
>
> }
> }
>
> The above test case failed because
> *Properties.loadFromXML*(Properties.java:851)
> throws NPE
>
> However , if I don't extend Parser , just new the Parser :
>
> package foo.secondModule;
>
> import java.util.Properties;
>
> import foo.firstModule.Parser;
>
> public class ParserInit
> {
>
> private Properties properties;
>
> public ParserInit()
>
> {
>
> Parser p = new Parser();
>
> this.properties = p.getProperties();
>
> }
>
> public int getSize()
>
> {
>
> return properties.size();
>
> }
> }
>
> and this is the test case :
>
> package foo.secondModule;
>
> import junit.framework.TestCase;
>
> public class ParserInitTest extends TestCase
> {
>
> public void testParserInit()
>
> {
>
> ParserInit pi = new ParserInit();
>
> assertEquals(64 , pi.getSize());
>
> }
> }
>
> The test case passes !
>
> This is my whole test scenario. It seems it 's maven and junit's
> classloader
> problem.
>
> Anybody able to find how to pass the ParserExtend's test case ? Thanks in
> advanced.
>