|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.geotools.resources.TestData
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.
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 |
protected TestData()
Method Detail |
public static java.net.URL getResource(java.lang.Object caller, java.lang.String name)
url
(caller, name)
method instead if the resource should always
exists.
caller
- Calling class or object used to locate .name
- resource name in directory.
url(java.lang.Object, java.lang.String)
public static java.net.URL url(java.lang.Object caller, java.lang.String path) throws java.io.FileNotFoundException
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
.
caller
- Calling class or object used to locate .path
- Path to file in .
java.io.FileNotFoundException
- if the resource is not found.public static java.io.File file(java.lang.Object caller, java.lang.String path) throws java.io.IOException
getResource(caller, path)
as a non-null
File
. You can access the directory with:
TestData.file(MyClass.class, null);
caller
- Calling class or object used to locate .path
- Path to file in .
java.io.FileNotFoundException
- if the file is not found.
java.io.IOException
- if the resource can't be fetched for an other reason.public static java.io.File temp(java.lang.Object caller, java.lang.String name) throws java.io.IOException
caller
- Calling class or object used to locate .
java.io.IOException
- if the file can't be created.public static java.io.InputStream openStream(java.lang.Object caller, java.lang.String name) throws java.io.IOException
InputStream
for named test data.
It is the caller responsability to close this stream after usage.
caller
- Calling class or object used to locate .name
- of test data to load.
java.io.FileNotFoundException
- if the resource is not found.
java.io.IOException
- if an error occurs during an input operation.public static java.io.LineNumberReader openReader(java.lang.Object caller, java.lang.String name) throws java.io.IOException
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.
caller
- The class of the object associated with named data.name
- of test data to load.
java.io.FileNotFoundException
- if the resource is not found.
java.io.IOException
- if an error occurs during an input operation.public static java.io.BufferedReader getReader(java.lang.Object caller, java.lang.String name) throws java.io.IOException
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.
BufferedReader
for named test data.
It is the caller responsability to close this reader after usage.
caller
- The class of the object associated with named data.name
- of test data to load.
java.io.IOException
- if an error occurs during an input operation.public static java.nio.channels.ReadableByteChannel openChannel(java.lang.Object caller, java.lang.String name) throws java.io.IOException
caller
- The class of the object associated with named data.name
- of test data to load.
java.io.FileNotFoundException
- if the resource is not found.
java.io.IOException
- if an error occurs during an input operation.public static void unzipFile(java.lang.Object caller, java.lang.String name) throws java.io.IOException
All inflated files will be automatically deleted on exit. Callers don't need to worry about cleanup.
caller
- The class of the object associated with named data.name
- The file name to unzip in place.
java.io.FileNotFoundException
- if the specified zip file is not found.
java.io.IOException
- if an error occurs during an input or output operation.protected static void deleteOnExit(java.io.File file)
public void run()
run
in interface java.lang.Runnable
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |