Class TestData
- Object
-
- TestData
-
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
TestData
public class TestData extends Object implements Runnable
Provides access totest-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 theTestData
class from thesample-module
instead of this one.- Since:
- 2.4
- Author:
- James McGill, Simone Giannecchiin, Martin Desruisseaux
-
-
Field Summary
Fields Modifier and Type Field 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.
-
Constructor Summary
Constructors Modifier Constructor Description protected
TestData()
Do not allow instantiation of this class, except for extending it.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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 togetResource(caller, path)
as a non-nullFile
.static URL
getResource(Object caller, String name)
Locates named test-data resource for caller.static boolean
isBaseJavaPlatform()
Returnstrue
if the running Java virtual machine is 1.5.static boolean
isExtensiveTest()
static boolean
isInteractiveTest()
static boolean
isMediaLibAvailable()
Returnstrue
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 aBufferedReader
for named test data.static InputStream
openStream(Object caller, String name)
Provides a non-nullInputStream
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
unzip(File file, File target)
Unzips the give source file in the target directorystatic void
unzipFile(Object caller, String name)
Unzip a file in thetest-data
directory.static URL
url(Object caller, String path)
Access togetResource(caller, path)
as a non-nullURL
.
-
-
-
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 theisExtensiveTest()
method. Some test suites will perform more extensive test coverage if this property is set totrue
. 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.- 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 theisInteractiveTest()
method. Some test suites will show windows with maps and other artifacts related to testing if this property is set totrue
. 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.- See Also:
- Constant Field Values
-
-
Method Detail
-
isBaseJavaPlatform
public static boolean isBaseJavaPlatform()
Returnstrue
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.
- Returns:
true
if we are running on the target Java platform.
-
isMediaLibAvailable
public static boolean isMediaLibAvailable()
Returnstrue
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.
- Returns:
true
if JAI medialib are available.
-
isExtensiveTest
public static boolean isExtensiveTest()
Returnstrue
if "org.geotools.test.extensive" system property is set totrue
. Test suites should check this value before to perform lengthly tests.- Returns:
true
if extensive tests are enabled.
-
isInteractiveTest
public static boolean isInteractiveTest()
Returnstrue
if "org.geotools.test.interactive" system property is set totrue
. Test suites should check this value before showing any kind of graphical window to the user.- Returns:
true
if interactive tests are enabled.
-
getResource
public static URL getResource(Object caller, String name)
Locates named test-data resource for caller. Note: Consider using theurl
(caller, name)- Parameters:
caller
- Calling class or object used to locatetest-data
.name
- resource name intest-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 togetResource(caller, path)
as a non-nullURL
. At the difference ofgetResource
, this method throws an exception if the resource is not found. This provides a more explicit explanation about the failure reason than the infamousNullPointerException
.- Parameters:
caller
- Calling class or object used to locatetest-data
.path
- Path to file intest-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 FileNotFoundException, IOException
Access togetResource(caller, path)
as a non-nullFile
. You can access thetest-data
directory with:TestData.file(MyClass.class, null);
- Parameters:
caller
- Calling class or object used to locatetest-data
.path
- Path to file intest-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) throws IOException
Creates a temporary file with the given name. The file will be created in thetest-data
directory and will be deleted on exit.- Parameters:
caller
- Calling class or object used to locatetest-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 FileNotFoundException, IOException
Provides a non-nullInputStream
for named test data. It is the caller responsability to close this stream after usage.- Parameters:
caller
- Calling class or object used to locatetest-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 FileNotFoundException, IOException
Provides aBufferedReader
for named test data. The buffered reader is provided as anLineNumberReader
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 FileNotFoundException, 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 FileNotFoundException, IOException
Unzip a file in thetest-data
directory. The zip file content is inflated in place, i.e. inflated files are written in the sametest-data
directory. If a file to be inflated already exists in thetest-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 thisunzipFile
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 onmvn 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
-
unzip
public static void unzip(File file, File target) throws IOException
Unzips the give source file in the target directory- Throws:
IOException
-
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.- Parameters:
file
- The file to delete on exit.
-
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
- Iftrue
, delete the file in all cases. Iffalse
, delete the file if and only if it has been modified. The default value iftrue
.- Since:
- 2.4
-
-