You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Omair-Inam Abdul-Matin <oi...@cs.uwaterloo.ca> on 2004/07/10 05:38:58 UTC

Reactor: tests failing (working dir set to master proj dir not child proj dir)

I'm using JUnit to run some regression tests that need to read in 
configuration and data files.  For this it is necessary that the working 
directory be the same as the project directory.  Recently the project 
was subdivided into a master project and child subprojects.  Now when I 
try to run unit tests by invoking the master build target, the tests 
fail because apparently the working directory is the directory of the 
master project not the current child project.  I believe this topic has 
been brought up in a previous post but I was not able to find the 
solution to the problem.

Can someone please nudge me in the correction direction?

Omair


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


Re: Re: Reactor: tests failing (working dir set to master proj dir not child proj dir)

Posted by Brett Porter <br...@gmail.com>.
this isn't a generic method for getting maven properties, but basedir is special

On Sat, 10 Jul 2004 18:43:25 -0400, Omair-Inam Abdul-Matin
<oi...@cs.uwaterloo.ca> wrote:
> Jason van Zyl wrote:
> > On Sat, 2004-07-10 at 13:08, Omair-Inam Abdul-Matin wrote:
> >
> >>I've tried the following and my unit tests still fail because the
> >>working directory seems to be the directory of the master project.
> >
> >
> > There is no way to reliably set the working directory in Java. To make
> > your tests reactor-able you must reference resources using the "basedir"
> > system property that is made available in tests by the junit plugin.
> >
> > So the general pattern is:
> >
> > public class MyTest extends TestCase
> > {
> >   private String basedir;
> >
> >   protected void setUp()
> >   {
> >       basedir = System.getProperty( "basedir" );
> >   }
> >
> >   public void testThatUsesAResourceFromTheFileSystem()
> >   {
> >       File resource = new File( basedir, "src/test/resources/MyTestResource.txt" );
> >
> >       // do you stuff with the file system resource.
> >   }
> > }
> >
> >
> > 
> Thanks... that's exactly what I was looking for..  I didn't know how to
> fetch maven-defined properties in a java file.
> 
> 
> 
> 
> ---------------------------------------------------------------------
> 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: Reactor: tests failing (working dir set to master proj dir not child proj dir)

Posted by Omair-Inam Abdul-Matin <oi...@cs.uwaterloo.ca>.
Jason van Zyl wrote:
> On Sat, 2004-07-10 at 13:08, Omair-Inam Abdul-Matin wrote:
> 
>>I've tried the following and my unit tests still fail because the 
>>working directory seems to be the directory of the master project.
> 
> 
> There is no way to reliably set the working directory in Java. To make
> your tests reactor-able you must reference resources using the "basedir"
> system property that is made available in tests by the junit plugin.
> 
> So the general pattern is:
> 
> public class MyTest extends TestCase
> {
>   private String basedir;
> 
>   protected void setUp()
>   {
>       basedir = System.getProperty( "basedir" );
>   }
> 
>   public void testThatUsesAResourceFromTheFileSystem()
>   {
>       File resource = new File( basedir, "src/test/resources/MyTestResource.txt" );
> 
>       // do you stuff with the file system resource.
>   }
> }
> 
> 
> 
Thanks... that's exactly what I was looking for..  I didn't know how to 
fetch maven-defined properties in a java file.


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


Re: Reactor: tests failing (working dir set to master proj dir not child proj dir)

Posted by Jason van Zyl <jv...@maven.org>.
On Sat, 2004-07-10 at 13:08, Omair-Inam Abdul-Matin wrote:
> I've tried the following and my unit tests still fail because the 
> working directory seems to be the directory of the master project.

There is no way to reliably set the working directory in Java. To make
your tests reactor-able you must reference resources using the "basedir"
system property that is made available in tests by the junit plugin.

So the general pattern is:

public class MyTest extends TestCase
{
  private String basedir;

  protected void setUp()
  {
      basedir = System.getProperty( "basedir" );
  }

  public void testThatUsesAResourceFromTheFileSystem()
  {
      File resource = new File( basedir, "src/test/resources/MyTestResource.txt" );

      // do you stuff with the file system resource.
  }
}



-- 
jvz.

Jason van Zyl
jason@maven.org
http://maven.apache.org

happiness is like a butterfly: the more you chase it, the more it will
elude you, but if you turn your attention to other things, it will come
and sit softly on your shoulder ...

 -- Thoreau 


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


Re: Reactor: tests failing (working dir set to master proj dir not child proj dir)

Posted by Omair-Inam Abdul-Matin <oi...@cs.uwaterloo.ca>.
I've tried the following and my unit tests still fail because the 
working directory seems to be the directory of the master project.

Does anyone have any ideas how I might go about solving this problem?  I 
want to run each subproject but have the working directory be the 
directory of the subproject *not* the directory of the master project.

Omair

     <goal name="printenv-test-all">
     	<echo>----- MASTER PROJECT ENV -----</echo>
     	<attainGoal name="printenv"/>
     	<!-- only collect the project names -->
     	<m:reactor basedir="${basedir}"
     		includes="*/project.xml"
     		goals="printenv"
     		banner="Building"
     		collectOnly="true"
     		postProcessing="true"
     		ignoreFailures="false"
     	/>

     	
     	<j:forEach var="reactorProject" items="${reactorProjects}">
		<j:set var="subProjectDir"
		       value="${basedir}/${reactorProject.artifactId}"/>
		<echo>${subProjectDir}</echo>		
       		<m:maven
       			basedir="${subProjectDir}"
       			descriptor="${subProjectDir}/project.xml"
       			goals="clean,jar:install"
       			ignoreFailures="false"
       		/>      			
     	</j:forEach>
     	
     </goal>



Omair-Inam Abdul-Matin wrote:
> Dion Gillard wrote:
> 
>> Set the user.home system property in the child subproject?
>>
>> On Fri, 09 Jul 2004 23:38:58 -0400, Omair-Inam Abdul-Matin
>> <oi...@cs.uwaterloo.ca> wrote:
>>
>>> I'm using JUnit to run some regression tests that need to read in
>>> configuration and data files.  For this it is necessary that the working
>>> directory be the same as the project directory.  Recently the project
>>> was subdivided into a master project and child subprojects.  Now when I
>>> try to run unit tests by invoking the master build target, the tests
>>> fail because apparently the working directory is the directory of the
>>> master project not the current child project.  I believe this topic has
>>> been brought up in a previous post but I was not able to find the
>>> solution to the problem.
>>>
>>> Can someone please nudge me in the correction direction?
>>>
>>> Omair
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>>> For additional commands, e-mail: users-help@maven.apache.org
>>>
>>>
>>
>>
>>
> I don't think that'll work. I believe the solution is something along 
> the lines of the following:
> 
> <goal name="printenv-test-all">
>         <echo>----- MASTER PROJECT ENV -----</echo>
>         <attainGoal name="printenv"/>
>         <!-- only collect the project names -->
>         <m:reactor basedir="${basedir}"
>             includes="*/project.xml"
>             goals="printenv"
>             banner="Building"
>             collectOnly="false"
>             postProcessing="true"
>             ignoreFailures="false"
>         />
> 
>        
>         <j:forEach var="reactorProject" items="${reactorProjects}">
>     <!-- set the basedir value here for each project and then
>              attain the desired goal -->
>          
>     </j:forEach>
>        
>     </goal>
> 
> I was surprised why the reactor doc doesn't mention the fact that any 
> instance var of the Project class can be accessed (e.g. 
> ${reactorProject.groupId})  I had to actually go thru the source and 
> then find the instance vars.
> 
> 
> Omair


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


Re: Reactor: tests failing (working dir set to master proj dir not child proj dir)

Posted by Omair-Inam Abdul-Matin <oi...@cs.uwaterloo.ca>.
Dion Gillard wrote:

> Set the user.home system property in the child subproject?
> 
> On Fri, 09 Jul 2004 23:38:58 -0400, Omair-Inam Abdul-Matin
> <oi...@cs.uwaterloo.ca> wrote:
> 
>>I'm using JUnit to run some regression tests that need to read in
>>configuration and data files.  For this it is necessary that the working
>>directory be the same as the project directory.  Recently the project
>>was subdivided into a master project and child subprojects.  Now when I
>>try to run unit tests by invoking the master build target, the tests
>>fail because apparently the working directory is the directory of the
>>master project not the current child project.  I believe this topic has
>>been brought up in a previous post but I was not able to find the
>>solution to the problem.
>>
>>Can someone please nudge me in the correction direction?
>>
>>Omair
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>>For additional commands, e-mail: users-help@maven.apache.org
>>
>>
> 
> 
> 
I don't think that'll work. I believe the solution is something along 
the lines of the following:

<goal name="printenv-test-all">
     	<echo>----- MASTER PROJECT ENV -----</echo>
     	<attainGoal name="printenv"/>
     	<!-- only collect the project names -->
     	<m:reactor basedir="${basedir}"
     		includes="*/project.xml"
     		goals="printenv"
     		banner="Building"
     		collectOnly="false"
     		postProcessing="true"
     		ignoreFailures="false"
     	/>

     	
     	<j:forEach var="reactorProject" items="${reactorProjects}">
	<!-- set the basedir value here for each project and then
              attain the desired goal -->
       	
	</j:forEach>
     	
     </goal>

I was surprised why the reactor doc doesn't mention the fact that any 
instance var of the Project class can be accessed (e.g. 
${reactorProject.groupId})  I had to actually go thru the source and 
then find the instance vars.


Omair


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


Re: Reactor: tests failing (working dir set to master proj dir not child proj dir)

Posted by Dion Gillard <di...@gmail.com>.
Set the user.home system property in the child subproject?

On Fri, 09 Jul 2004 23:38:58 -0400, Omair-Inam Abdul-Matin
<oi...@cs.uwaterloo.ca> wrote:
> I'm using JUnit to run some regression tests that need to read in
> configuration and data files.  For this it is necessary that the working
> directory be the same as the project directory.  Recently the project
> was subdivided into a master project and child subprojects.  Now when I
> try to run unit tests by invoking the master build target, the tests
> fail because apparently the working directory is the directory of the
> master project not the current child project.  I believe this topic has
> been brought up in a previous post but I was not able to find the
> solution to the problem.
> 
> Can someone please nudge me in the correction direction?
> 
> Omair
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
> 
> 


-- 
http://www.multitask.com.au/people/dion/

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


Re: Reactor: tests failing (working dir set to master proj dir not child proj dir)

Posted by Trygve Laugstøl <tr...@ifi.uio.no>.
On Fri, Jul 09, 2004 at 11:38:58PM -0400, Omair-Inam Abdul-Matin wrote:
> I'm using JUnit to run some regression tests that need to read in 
> configuration and data files.  For this it is necessary that the working 
> directory be the same as the project directory.  Recently the project 
> was subdivided into a master project and child subprojects.  Now when I 
> try to run unit tests by invoking the master build target, the tests 
> fail because apparently the working directory is the directory of the 
> master project not the current child project.  I believe this topic has 
> been brought up in a previous post but I was not able to find the 
> solution to the problem.

Try to read the System.getProperty( "basedir" ) should yield the basedir
for your project when you run your testcase from maven. But this is not
the case when you are running the testcase from eclipse so I'd recomend
that you make a super test case and have a method called getBasedir() like
this:

protected String getBasedir()
{
    return System.getProperty( "basedir", new File( "" ).getAbsolutePath() );
}

--
Trygve

> 
> Can someone please nudge me in the correction direction?
> 
> Omair

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