it.geosolutions.resources
Class TestData

Object
  extended by TestData
All Implemented Interfaces:
Runnable

public class TestData
extends Object
implements Runnable

----------------------------- NOTE ----------------------------- This class contains a modified version of the geotools TestData class. ---------------------------------------------------------------- Provides access to test-data directories associated with JUnit tests.

We have chosen "test-data" to follow the javadoc "doc-files" convention of ensuring that data directories don't look anything like normal java packages.

Example:

 class MyClass {
     public void example() {
         Image testImage = new ImageIcon(TestData.url(this, "test.png")).getImage();
         Reader reader = TestData.openReader(this, "script.xml");
         // ... do some process
         reader.close();
     }
 }
 
Where the directory structure goes as bellow:

By convention you should try and locate test-data near the JUnit test cases that uses it. If you need an access to shared test data, import the org.geotools.TestData class from the sample-module instead of this one.

Since:
2.0
Author:
James McGill, Simone Giannecchini, GeoSolutions, Martin Desruisseaux

Field Summary
static String EXTENSIVE_TEST_KEY
          The system property key for more extensive test suite.
static String INTERACTIVE_TEST_KEY
          The system property key for interactive tests.
 
Constructor Summary
protected TestData()
          Do not allow instantiation of this class, except for extending it.
 
Method Summary
protected static void deleteOnExit(File file)
          Requests that the file or directory denoted by the specified pathname be deleted when the virtual machine terminates.
protected static void deleteOnExit(File file, boolean force)
          Requests that the file or directory denoted by the specified pathname be deleted when the virtual machine terminates.
static File file(Object caller, String path)
          Access to getResource(caller, path) as a non-null File.
static URL getResource(Object caller, String name)
          Locates named test-data resource for caller.
static boolean isExtensiveTest()
          Returns true if "test.extensive" system property is set to true.
static boolean isInteractiveTest()
          Returns true if "test.interactive" system property is set to true.
static ReadableByteChannel openChannel(Object caller, String name)
          Provides a channel for named test data.
static LineNumberReader openReader(Object caller, String name)
          Provides a BufferedReader for named test data.
static InputStream openStream(Object caller, String name)
          Provides a non-null InputStream for named test data.
 void run()
          Deletes all temporary files.
static File temp(Object caller, String name)
          Creates a temporary file with the given name.
static File temp(Object caller, String name, boolean delOnExit)
          Creates a temporary file with the given name.
static void unzipFile(Object caller, String name)
          Unzip a file in the test-data directory.
static URL url(Object caller, String path)
          Access to getResource(caller, path) as a non-null URL.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EXTENSIVE_TEST_KEY

public static final String EXTENSIVE_TEST_KEY
The system property key for more extensive test suite. The value for this key is returned by the isExtensiveTest() method. Some test suites will perform more extensive test coverage if this property is set to true. The value for this property is typically defined on the command line as a -D"test.extensive"=true option at Java or Maven starting time.

See Also:
Constant Field Values

INTERACTIVE_TEST_KEY

public static final String INTERACTIVE_TEST_KEY
The system property key for interactive tests. The value for this key is returned by the isInteractiveTest() method. Some test suites will show windows with maps and other artifacts related to testing if this property is set to true. The value for this property is typically defined on the command line as a -D"test.interactive"=true option at Java or Maven starting time.

See Also:
Constant Field Values
Constructor Detail

TestData

protected TestData()
Do not allow instantiation of this class, except for extending it.

Method Detail

isExtensiveTest

public static boolean isExtensiveTest()
Returns true if "test.extensive" system property is set to true. Test suites should check this value before to perform lengthly tests.


isInteractiveTest

public static boolean isInteractiveTest()
Returns true if "test.interactive" system property is set to true. Test suites should check this value before showing any kind of graphical window to the user.


getResource

public static URL getResource(Object caller,
                              String name)
Locates named test-data resource for caller. Note: Consider using the url(caller, name) method instead if the resource should always exists.

Parameters:
caller - Calling class or object used to locate test-data.
name - resource name in test-data directory.
Returns:
URL or null if the named test-data could not be found.
See Also:
url(java.lang.Object, java.lang.String)

url

public static URL url(Object caller,
                      String path)
               throws FileNotFoundException
Access to getResource(caller, path) as a non-null URL. At the difference of getResource, this method throws an exception if the resource is not found. This provides a more explicit explanation about the failure reason than the infamous NullPointerException.

Parameters:
caller - Calling class or object used to locate test-data.
path - Path to file in test-data.
Returns:
The URL to the test-data resource.
Throws:
FileNotFoundException - if the resource is not found.
Since:
2.2

file

public static File file(Object caller,
                        String path)
                 throws IOException
Access to getResource(caller, path) as a non-null File. You can access the test-data directory with:
 TestData.file(MyClass.class, null);
 

Parameters:
caller - Calling class or object used to locate test-data.
path - Path to file in test-data.
Returns:
The file to the test-data resource.
Throws:
FileNotFoundException - if the file is not found.
IOException - if the resource can't be fetched for an other reason.

temp

public static File temp(Object caller,
                        String name,
                        boolean delOnExit)
                 throws IOException
Creates a temporary file with the given name. The file will be created in the test-data directory.

Parameters:
caller - Calling class or object used to locate test-data.
name - A base name for the temporary file.
delOnExit - if true, delete the temporary file on exit.
Returns:
The temporary file in the test-data directory.
Throws:
IOException - if the file can't be created.

temp

public static File temp(Object caller,
                        String name)
                 throws IOException
Creates a temporary file with the given name. The file will be created in the test-data directory and will be deleted on exit.

Parameters:
caller - Calling class or object used to locate test-data.
name - A base name for the temporary file.
Returns:
The temporary file in the test-data directory.
Throws:
IOException - if the file can't be created.

openStream

public static InputStream openStream(Object caller,
                                     String name)
                              throws IOException
Provides a non-null InputStream for named test data. It is the caller responsability to close this stream after usage.

Parameters:
caller - Calling class or object used to locate test-data.
name - of test data to load.
Returns:
The input stream.
Throws:
FileNotFoundException - if the resource is not found.
IOException - if an error occurs during an input operation.
Since:
2.2

openReader

public static LineNumberReader openReader(Object caller,
                                          String name)
                                   throws IOException
Provides a BufferedReader for named test data. The buffered reader is provided as an LineNumberReader instance, which is useful for displaying line numbers where error occur. It is the caller responsability to close this reader after usage.

Parameters:
caller - The class of the object associated with named data.
name - of test data to load.
Returns:
The buffered reader.
Throws:
FileNotFoundException - if the resource is not found.
IOException - if an error occurs during an input operation.
Since:
2.2

openChannel

public static ReadableByteChannel openChannel(Object caller,
                                              String name)
                                       throws IOException
Provides a channel for named test data. It is the caller responsability to close this chanel after usage.

Parameters:
caller - The class of the object associated with named data.
name - of test data to load.
Returns:
The chanel.
Throws:
FileNotFoundException - if the resource is not found.
IOException - if an error occurs during an input operation.
Since:
2.2

unzipFile

public static void unzipFile(Object caller,
                             String name)
                      throws IOException
Unzip a file in the test-data directory. The zip file content is inflated in place, i.e. inflated files are written in the same test-data directory. If a file to be inflated already exists in the test-data directory, then the existing file is left untouched and the corresponding ZIP entry is silently skipped. This approach avoid the overhead of inflating the same files many time if this unzipFile method is invoked before every tests.

Inflated files will be automatically deleted on exit if and only if they have been modified. Callers don't need to worry about cleanup, because the files are inflated in the target/.../test-data directory, which is not versionned by SVN and is cleaned by Maven on mvn clean execution.

Parameters:
caller - The class of the object associated with named data.
name - The file name to unzip in place.
Throws:
FileNotFoundException - if the specified zip file is not found.
IOException - if an error occurs during an input or output operation.
Since:
2.2

deleteOnExit

protected static void deleteOnExit(File file)
Requests that the file or directory denoted by the specified pathname be deleted when the virtual machine terminates.


deleteOnExit

protected static void deleteOnExit(File file,
                                   boolean force)
Requests that the file or directory denoted by the specified pathname be deleted when the virtual machine terminates. This method can optionnaly delete the file only if it has been modified, thus giving a chance for test suites to copy their resources only once.

Parameters:
file - The file to delete.
force - If true, delete the file in all cases. If false, delete the file if and only if it has been modified. The default value if true.
Since:
2.4

run

public void run()
Deletes all temporary files. This method is invoked automatically at shutdown time and should not be invoked directly. It is public only as an implementation side effect.

Specified by:
run in interface Runnable


Copyright © 2006-2010 GeoSolutions. All Rights Reserved.