org.geotools.resources
Class TestData

java.lang.Object
  extended byorg.geotools.resources.TestData
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
TestData

public class TestData
extends java.lang.Object
implements java.lang.Runnable

Provides access to directories associated with JUnit tests.

We have chosen "" to follow the javadoc "" 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 near the JUnit test cases that uses it. If you need an access to shared test data, import the TestData class from the instead of this one.

Since:
2.0
Version:
$Id: TestData.java 17672 2006-01-19 00:25:55Z desruisseaux $
Author:
James McGill, Simone Giannecchiin (simboss), Martin Desruisseaux

Constructor Summary
protected TestData()
          Do not allow instantiation of this class, except for extending it.
 
Method Summary
protected static void deleteOnExit(java.io.File file)
          Requests that the file or directory denoted by the specified pathname be deleted when the virtual machine terminates.
static java.io.File file(java.lang.Object caller, java.lang.String path)
          Access to getResource(caller, path) as a non-null File.
static java.io.BufferedReader getReader(java.lang.Object caller, java.lang.String name)
          Deprecated. Use openReader(java.lang.Object, java.lang.String) instead. The method throws an exception if the resource is not found, instead of returning null. This make debugging easier, since it replaces infamous NullPointerException by a more explicit error message during tests. Furthermore, the name make it more obvious that the stream is not closed automatically and is also consistent with other method names in this class.
static java.net.URL getResource(java.lang.Object caller, java.lang.String name)
          Locates named test-data resource for caller.
static java.nio.channels.ReadableByteChannel openChannel(java.lang.Object caller, java.lang.String name)
          Provides a channel for named test data.
static java.io.LineNumberReader openReader(java.lang.Object caller, java.lang.String name)
          Provides a BufferedReader for named test data.
static java.io.InputStream openStream(java.lang.Object caller, java.lang.String name)
          Provides a non-null InputStream for named test data.
 void run()
          Deletes all temporary files.
static java.io.File temp(java.lang.Object caller, java.lang.String name)
          Creates a temporary file with the given name.
static void unzipFile(java.lang.Object caller, java.lang.String name)
          Unzip a file in the directory.
static java.net.URL url(java.lang.Object caller, java.lang.String path)
          Access to getResource(caller, path) as a non-null URL.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TestData

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

Method Detail

getResource

public static java.net.URL getResource(java.lang.Object caller,
                                       java.lang.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 .
name - resource name in directory.
Returns:
URL or if the named test-data could not be found.
See Also:
url(java.lang.Object, java.lang.String)

url

public static java.net.URL url(java.lang.Object caller,
                               java.lang.String path)
                        throws java.io.FileNotFoundException
Access to getResource(caller, path) as a non-null URL. At the difference of , 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 .
path - Path to file in .
Returns:
The URL to the resource.
Throws:
java.io.FileNotFoundException - if the resource is not found.
Since:
2.2

file

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

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

temp

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

Parameters:
caller - Calling class or object used to locate .
Returns:
The temporary file in the directory.
Throws:
java.io.IOException - if the file can't be created.

openStream

public static java.io.InputStream openStream(java.lang.Object caller,
                                             java.lang.String name)
                                      throws java.io.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 .
name - of test data to load.
Returns:
The input stream.
Throws:
java.io.FileNotFoundException - if the resource is not found.
java.io.IOException - if an error occurs during an input operation.
Since:
2.2

openReader

public static java.io.LineNumberReader openReader(java.lang.Object caller,
                                                  java.lang.String name)
                                           throws java.io.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:
java.io.FileNotFoundException - if the resource is not found.
java.io.IOException - if an error occurs during an input operation.
Since:
2.2

getReader

public static java.io.BufferedReader getReader(java.lang.Object caller,
                                               java.lang.String name)
                                        throws java.io.IOException
Deprecated. Use openReader(java.lang.Object, java.lang.String) instead. The method throws an exception if the resource is not found, instead of returning null. This make debugging easier, since it replaces infamous NullPointerException by a more explicit error message during tests. Furthermore, the name make it more obvious that the stream is not closed automatically and is also consistent with other method names in this class.

Provides a BufferedReader for named test data. 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 reader, or if the named test data are not found.
Throws:
java.io.IOException - if an error occurs during an input operation.

openChannel

public static java.nio.channels.ReadableByteChannel openChannel(java.lang.Object caller,
                                                                java.lang.String name)
                                                         throws java.io.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:
java.io.FileNotFoundException - if the resource is not found.
java.io.IOException - if an error occurs during an input operation.
Since:
2.2

unzipFile

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

All inflated files will be automatically deleted on exit. Callers don't need to worry about cleanup.

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

deleteOnExit

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


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 java.lang.Runnable


Copyright © GeoTools. All Rights Reserved.