public class TestData extends Object implements Runnable
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:
MyClass.java
test-data/test.png
test-data/script.xml
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 TestData class
from the sample-module instead of this one.
| Modifier and Type | Field and Description |
|---|---|
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.
|
| Modifier | Constructor and Description |
|---|---|
protected |
TestData()
Do not allow instantiation of this class, except for extending it.
|
| Modifier and Type | Method and Description |
|---|---|
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 BufferedReader |
getReader(Object caller,
String name)
Deprecated.
Use
openReader(java.lang.Object, java.lang.String) instead. The openReader 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 openReader name make it more obvious that
the stream is not closed automatically and is also consistent with other method names in
this class. |
static URL |
getResource(Object caller,
String name)
Locates named test-data resource for caller.
|
static boolean |
isBaseJavaPlatform()
Returns
true if the running Java virtual machine is 1.5. |
static boolean |
isExtensiveTest()
|
static boolean |
isInteractiveTest()
|
static boolean |
isMediaLibAvailable()
Returns
true if JAI MediaLib acceleration is available. |
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 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. |
public static final String EXTENSIVE_TEST_KEY
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"org.geotools.test.extensive"=true
option at Java or Maven starting time.public static final String INTERACTIVE_TEST_KEY
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"org.geotools.test.interactive"=true option at Java or Maven starting time.protected TestData()
public static boolean isBaseJavaPlatform()
true if the running Java virtual machine is 1.5. This is the lowest Java
version currently supported by Geotools. This version will increase in future Geotools
version.
This method was used for some broken JUnit tests that were know to run on JSE 1.4 but not on JSE 1.6 for example.
true if we are running on the target Java platform.public static boolean isMediaLibAvailable()
true if JAI MediaLib acceleration is available.
This method is used to disable some checks in unit tests that fail when JAI is run in pure java mode.
true if JAI medialib are available.public static boolean isExtensiveTest()
true if "org.geotools.test.extensive" system property is set to true.
Test suites should check this value before to perform lengthly tests.true if extensive tests are enabled.public static boolean isInteractiveTest()
true if "org.geotools.test.interactive" system property is set to true. Test suites should check this value before showing any kind of graphical window to the
user.true if interactive tests are enabled.public static URL getResource(Object caller, String name)
url(caller, name) method instead if the resource should always exists.caller - Calling class or object used to locate test-data.name - resource name in test-data directory.null if the named test-data could not be found.url(java.lang.Object, java.lang.String)public static URL url(Object caller, String path) throws FileNotFoundException
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.caller - Calling class or object used to locate test-data.path - Path to file in test-data.test-data resource.FileNotFoundException - if the resource is not found.public static File file(Object caller, String path) throws FileNotFoundException, IOException
getResource(caller, path) as a non-null
File. You can access the test-data directory with:
TestData.file(MyClass.class, null);
caller - Calling class or object used to locate test-data.path - Path to file in test-data.test-data resource.FileNotFoundException - if the file is not found.IOException - if the resource can't be fetched for an other reason.public static File temp(Object caller, String name) throws IOException
test-data directory and will be deleted on exit.caller - Calling class or object used to locate test-data.name - A base name for the temporary file.test-data directory.IOException - if the file can't be created.public static InputStream openStream(Object caller, String name) throws FileNotFoundException, 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 test-data.name - of test data to load.FileNotFoundException - if the resource is not found.IOException - if an error occurs during an input operation.public static LineNumberReader openReader(Object caller, String name) throws FileNotFoundException, 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.FileNotFoundException - if the resource is not found.IOException - if an error occurs during an input operation.@Deprecated public static BufferedReader getReader(Object caller, String name) throws IOException
openReader(java.lang.Object, java.lang.String) instead. The openReader 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 openReader 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.null if the named test data are not found.IOException - if an error occurs during an input operation.public static ReadableByteChannel openChannel(Object caller, String name) throws FileNotFoundException, IOException
caller - The class of the object associated with named data.name - of test data to load.FileNotFoundException - if the resource is not found.IOException - if an error occurs during an input operation.public static void unzipFile(Object caller, String name) throws FileNotFoundException, IOException
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.
caller - The class of the object associated with named data.name - The file name to unzip in place.FileNotFoundException - if the specified zip file is not found.IOException - if an error occurs during an input or output operation.protected static void deleteOnExit(File file)
file - The file to delete on exit.protected static void deleteOnExit(File file, boolean force)
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.Copyright © 1996–2019 Geotools. All rights reserved.