You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by James Depaul <jj...@us.ibm.com> on 2007/09/26 21:00:20 UTC

Resource relative paths in test cases...


Hello -

I'm working on maven-izing a project that's composed of three modules.  I
have the POMs configured with proper dependencies.  Compile all works now
and now I'm tying to execute the Test Cases and have some problems - it has
to do with how Maven resolves the resource locations...

The test cases are using DBUNIT framework to insert data into the db before
running.  Inside the getDataSet() method in test case I need to create an
InputStream to the XML file that contains the test data for the run.  The
code implementation that I have that works perfectly in Eclipse is this:

    protected IDataSet getDataSet() throws Exception {
        String fileName = "cods_user_auth-seed.xml"; // data file
        URL rootPath = UserAdminUserTest.class.getResource("/data/" +
fileName);   // root path location + specific location of the file
        System.out.println("rootPath=" + rootPath);
        return new FlatXmlDataSet(
                new FileInputStream(rootPath.getFile())); // get the file
as InputStream
    }


When I run in Eclipse, the rootPath that gets resolved is this:  >>>>>
rootPath=file:/C:/workspaces/maersk/cods/CodsWebTesting/bin/data/cods_user_auth-seed.xml

When I run in maven, the rootPath resolves to this (which is not found):
>>>>>
rootPath=file:/C:/workspaces/maersk/cods/target/test-classes/com/ibm/cods/web/data/service/adminuseradmin/
data/cods_user_auth-seed.xml

I know this is probably a Java resource question, but could someone
recommend a uniform way I can use to retrieve the resource/file that will
resolve equally well in Eclipse and in Maven2?  I'm sooo close...

Thanks,
James

Re: Resource relative paths in test cases...

Posted by Patrick Schneider <ps...@gmail.com>.
- Absolute pathnames, beginning with "/" are fully qualified file names
within the jar file.
- Relative pathnames, not beginning with "/" are relative to the class upon
which getResourceAsStream is called...."

Maybe try the getClass().getClassLoader().getResourceAsStream() method with
a relative path?


Patrick

On 9/26/07, James Depaul <jj...@us.ibm.com> wrote:
>
> Sorry, I must be dense, but I do not understand how that thread helps
> me... I've tried this code:
>
> InputStream xml = getClass().getClassLoader().getResourceAsStream("/data/"
> + fileName);
> return new FlatXmlDataSet(xml);
>
> But now Eclipse cannot find the files... and neither can Maven.
>
> My original data xml files are under CodsWebTesting/test/data directory. I
> use maven to copy them into 'target\test-classes\data' directory - but maven
> is not finding it there when I run mvn test... There's not much I can do
> abut the structure of the CodsWebTesting project at this point...
>
> How could I convince maven to look for these XML files in
> /target/test-classes/data dir or some other dir under /target ?
>
> Thanks,
> James
>
>
>
>
> [image: Inactive hide details for "Wayne Fay" <wa...@gmail.com>]"Wayne
> Fay" <wa...@gmail.com>
>
>
>
>     *"Wayne Fay" <wa...@gmail.com>*
>
>             09/26/2007 02:47 PM Please respond to
>             "Maven Users List" <us...@maven.apache.org>
>
>
> To
>
> "Maven Users List" <us...@maven.apache.org>
> cc
>
>
> Subject
>
> Re: Resource relative paths in test cases...
>
> This comes up pretty frequently. In the future, try searching the
> archives first...
>
> Here's a search query with a few hits from the last 3 mos:
>
> http://www.nabble.com/forum/Search.jtp?query=classloader+getclass&local=y&forum=178&daterange=4&startdate=&enddate=
>
> Wayne
>
> On 9/26/07, Insitu <ab...@oqube.com> wrote:
> > James Depaul <jj...@us.ibm.com> writes:
> >
> > > Hello -
> > >
> > > I'm working on maven-izing a project that's composed of three modules.
>  I
> > > have the POMs configured with proper dependencies.  Compile all works
> now
> > > and now I'm tying to execute the Test Cases and have some problems -
> it has
> > > to do with how Maven resolves the resource locations...
> > >
> > > The test cases are using DBUNIT framework to insert data into the db
> before
> > > running.  Inside the getDataSet() method in test case I need to create
> an
> > > InputStream to the XML file that contains the test data for the run.
>  The
> > > code implementation that I have that works perfectly in Eclipse is
> this:
> > >
> > >     protected IDataSet getDataSet() throws Exception {
> > >         String fileName = "cods_user_auth-seed.xml"; // data file
> > >         URL rootPath = UserAdminUserTest.class.getResource("/data/" +
> > > fileName);   // root path location + specific location of the file
> > >         System.out.println("rootPath=" + rootPath);
> > >         return new FlatXmlDataSet(
> > >                 new FileInputStream(rootPath.getFile())); // get the
> file
> > > as InputStream
> > >     }
> > >
> >
> > This assumes  "cods_user_auth-seed.xml" is located at
> > "src/test/resources":
> >
> >     InputStream xml =
> >     getClass().getClassLoader().getResourceAsStream("/data/" +
> >       fileName);
> >     return new FlatXmlDataSet(xml);
> >
> > HTH
> > --
> > OQube < software engineering \ génie logiciel >
> > Arnaud Bailly, Dr.
> > \web> http://www.oqube.com
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> > For additional commands, e-mail: users-help@maven.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>
>

Re: Resource relative paths in test cases...

Posted by James Depaul <jj...@us.ibm.com>.
Sorry, I must be dense, but I do not understand how that thread helps me...
I've tried this code:

        InputStream xml =
getClass().getClassLoader().getResourceAsStream("/data/" + fileName);
            return new FlatXmlDataSet(xml);

But now Eclipse cannot find the files... and neither can Maven.

My original data xml files are under CodsWebTesting/test/data directory.  I
use maven to copy them into 'target\test-classes\data' directory - but
maven is not finding it there when I run mvn test...   There's not much I
can do abut the structure of the CodsWebTesting project at this point...

How could I convince maven to look for these XML files in
/target/test-classes/data dir or some other dir under /target ?

Thanks,
James






                                                                           
             "Wayne Fay"                                                   
             <waynefay@gmail.c                                             
             om>                                                        To 
                                       "Maven Users List"                  
             09/26/2007 02:47          <us...@maven.apache.org>            
             PM                                                         cc 
                                                                           
                                                                   Subject 
             Please respond to         Re: Resource relative paths in test 
               "Maven Users            cases...                            
                   List"                                                   
             <users@maven.apac                                             
                  he.org>                                                  
                                                                           
                                                                           
                                                                           




This comes up pretty frequently. In the future, try searching the
archives first...

Here's a search query with a few hits from the last 3 mos:
http://www.nabble.com/forum/Search.jtp?query=classloader+getclass&local=y&forum=178&daterange=4&startdate=&enddate=


Wayne

On 9/26/07, Insitu <ab...@oqube.com> wrote:
> James Depaul <jj...@us.ibm.com> writes:
>
> > Hello -
> >
> > I'm working on maven-izing a project that's composed of three modules.
I
> > have the POMs configured with proper dependencies.  Compile all works
now
> > and now I'm tying to execute the Test Cases and have some problems - it
has
> > to do with how Maven resolves the resource locations...
> >
> > The test cases are using DBUNIT framework to insert data into the db
before
> > running.  Inside the getDataSet() method in test case I need to create
an
> > InputStream to the XML file that contains the test data for the run.
The
> > code implementation that I have that works perfectly in Eclipse is
this:
> >
> >     protected IDataSet getDataSet() throws Exception {
> >         String fileName = "cods_user_auth-seed.xml"; // data file
> >         URL rootPath = UserAdminUserTest.class.getResource("/data/" +
> > fileName);   // root path location + specific location of the file
> >         System.out.println("rootPath=" + rootPath);
> >         return new FlatXmlDataSet(
> >                 new FileInputStream(rootPath.getFile())); // get the
file
> > as InputStream
> >     }
> >
>
> This assumes  "cods_user_auth-seed.xml" is located at
> "src/test/resources":
>
>     InputStream xml =
>     getClass().getClassLoader().getResourceAsStream("/data/" +
>       fileName);
>     return new FlatXmlDataSet(xml);
>
> HTH
> --
> OQube < software engineering \ génie logiciel >
> Arnaud Bailly, Dr.
> \web> http://www.oqube.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: Resource relative paths in test cases...

Posted by Wayne Fay <wa...@gmail.com>.
This comes up pretty frequently. In the future, try searching the
archives first...

Here's a search query with a few hits from the last 3 mos:
http://www.nabble.com/forum/Search.jtp?query=classloader+getclass&local=y&forum=178&daterange=4&startdate=&enddate=

Wayne

On 9/26/07, Insitu <ab...@oqube.com> wrote:
> James Depaul <jj...@us.ibm.com> writes:
>
> > Hello -
> >
> > I'm working on maven-izing a project that's composed of three modules.  I
> > have the POMs configured with proper dependencies.  Compile all works now
> > and now I'm tying to execute the Test Cases and have some problems - it has
> > to do with how Maven resolves the resource locations...
> >
> > The test cases are using DBUNIT framework to insert data into the db before
> > running.  Inside the getDataSet() method in test case I need to create an
> > InputStream to the XML file that contains the test data for the run.  The
> > code implementation that I have that works perfectly in Eclipse is this:
> >
> >     protected IDataSet getDataSet() throws Exception {
> >         String fileName = "cods_user_auth-seed.xml"; // data file
> >         URL rootPath = UserAdminUserTest.class.getResource("/data/" +
> > fileName);   // root path location + specific location of the file
> >         System.out.println("rootPath=" + rootPath);
> >         return new FlatXmlDataSet(
> >                 new FileInputStream(rootPath.getFile())); // get the file
> > as InputStream
> >     }
> >
>
> This assumes  "cods_user_auth-seed.xml" is located at
> "src/test/resources":
>
>     InputStream xml =
>     getClass().getClassLoader().getResourceAsStream("/data/" +
>       fileName);
>     return new FlatXmlDataSet(xml);
>
> HTH
> --
> OQube < software engineering \ génie logiciel >
> Arnaud Bailly, Dr.
> \web> http://www.oqube.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: Resource relative paths in test cases...

Posted by Insitu <ab...@oqube.com>.
James Depaul <jj...@us.ibm.com> writes:

> Hello -
>
> I'm working on maven-izing a project that's composed of three modules.  I
> have the POMs configured with proper dependencies.  Compile all works now
> and now I'm tying to execute the Test Cases and have some problems - it has
> to do with how Maven resolves the resource locations...
>
> The test cases are using DBUNIT framework to insert data into the db before
> running.  Inside the getDataSet() method in test case I need to create an
> InputStream to the XML file that contains the test data for the run.  The
> code implementation that I have that works perfectly in Eclipse is this:
>
>     protected IDataSet getDataSet() throws Exception {
>         String fileName = "cods_user_auth-seed.xml"; // data file
>         URL rootPath = UserAdminUserTest.class.getResource("/data/" +
> fileName);   // root path location + specific location of the file
>         System.out.println("rootPath=" + rootPath);
>         return new FlatXmlDataSet(
>                 new FileInputStream(rootPath.getFile())); // get the file
> as InputStream
>     }
>

This assumes  "cods_user_auth-seed.xml" is located at
"src/test/resources":

     InputStream xml =
     getClass().getClassLoader().getResourceAsStream("/data/" +
       fileName);
     return new FlatXmlDataSet(xml);

HTH
-- 
OQube < software engineering \ génie logiciel >
Arnaud Bailly, Dr.
\web> http://www.oqube.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org