You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Alain Javier Guarnieri del Gesu <ja...@ajgdg.com> on 2003/11/03 22:59:30 UTC

JUnit Test Temp Directory

What do you feel is the best place to create temporary files and
directories for a unit test?

Should I use a directory under target?

Should I create a temp file using File.createTempFile()? 

The latter does not help if I want a temp directory.

Thoughts, please.

-- 
Alain Javier Guarnieri del Gesu - javi@ajgdg.com

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


Re: JUnit Test Temp Directory

Posted by Alain Javier Guarnieri del Gesu <ja...@ajgdg.com>.
* Peter Donald <pe...@realityforge.org> [2003-11-03 22:14]:
> On Tue, 4 Nov 2003 08:59 am, Alain Javier Guarnieri del Gesu wrote:

> > What do you feel is the best place to create temporary files and
> > directories for a unit test?
> >
> > Should I use a directory under target?
> >
> > Should I create a temp file using File.createTempFile()?
> >
> > The latter does not help if I want a temp directory.
> 
> I generally put it under target/test-data. Heres the code I use in
> quite a few of my unit tests to generate base directory for test
> data.

Thank you for the thoughts and the code. That's what I'll do then.

-- 
Alain Javier Guarnieri del Gesu - javi@ajgdg.com

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


Re: JUnit Test Temp Directory

Posted by Peter Donald <pe...@realityforge.org>.
On Tue, 4 Nov 2003 08:59 am, Alain Javier Guarnieri del Gesu wrote:
> What do you feel is the best place to create temporary files and
> directories for a unit test?
>
> Should I use a directory under target?
>
> Should I create a temp file using File.createTempFile()?
>
> The latter does not help if I want a temp directory.

I generally put it under target/test-data. Heres the code I use in quite a few 
of my unit tests to generate base directory for test data.


    private static final File generateDirectory()
        throws IOException
    {
        final File baseDirectory = getBaseDirectory();
        final File dir =
            File.createTempFile( "mgtest", ".tmp", baseDirectory 
).getCanonicalFile();
        dir.delete();
        dir.mkdirs();
        assertTrue( "dir.exists()", dir.exists() );
        return dir;
    }

    private static final File getBaseDirectory()
    {
        final String tempDir = System.getProperty( "java.io.tmpdir" );
        final String baseDir = System.getProperty( "basedir", tempDir );

        final File base = new File( baseDir ).getAbsoluteFile();
        final String pathname =
            base + File.separator + "target" + File.separator + "test-data";
        final File dir = new File( pathname );
        dir.mkdirs();
        return dir;
    }

-- 
Cheers,

Peter Donald
-----------------------------------------------------------
 Don't take life too seriously -- 
                          you'll never get out of it alive.
-----------------------------------------------------------


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