Class AbstractGenericTest
- Direct Known Subclasses:
AbstractGuiTest
-
Field Summary
Modifier and TypeFieldDescriptionorg.junit.rules.TestRule
static final String
static final String
org.junit.rules.TestRule
This rule handles theRepeated
annotationorg.junit.rules.RuleChain
static final String
org.junit.rules.TestWatcher
Fields inherited from class generic.test.AbstractGTest
BATCH_MODE, DEFAULT_WAIT_DELAY, DEFAULT_WAIT_TIMEOUT, DEFAULT_WINDOW_TIMEOUT, PARALLEL_MODE, PRIVATE_LONG_WAIT_TIMEOUT, testName
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected ApplicationConfiguration
protected ApplicationLayout
static String
Returns a string which is a printout of a stack trace for each thread running in the current JVMstatic File
createTempDirectory
(String name) Creates a sub-directory with the given name as a child of the Java temp directory.createTempFile
(String name) Creates a file in the Application temp directory using the given name as a prefix and the given suffix.createTempFile
(String name, String suffix) Creates a file in the Application temp directory using the given name as a prefix and the given suffix.Creates a temp file for the current test, using the test name as a prefix for the filename.createTempFileForTest
(String suffix) Creates a temp file for the current test, using the test name as a prefix for the filename.createTempFilePath
(String name) Creates a file path with a filename that is under the system temp directory.createTempFilePath
(String name, String extension) Creates a file path with a filename that is under the system temp directory.static void
deleteMatchingTempFiles
(String namePattern) Delete any files under the this test case's specific temp directory that match the give regexPattern
static void
deleteSimilarTempFiles
(String nameText) Delete any files under the Java temp directory that have the given text in their name.static File
findTestDataFile
(String path) Returns the file within the data directory of the TestResources module that matches the given relative path.static File
Returns the directory into which tests can write debug files, such as files containing print statements or image files.static FontMetrics
getFontMetrics
(Font font) Returns a font metrics for the given font using a generic buffered image graphics context.static Object
getInstanceField
(String fieldName, Object ownerInstance) Gets the instance field by the given name on the given object instance.static <T> T
getInstanceFieldByClassType
(Class<T> classType, Object ownerInstance) Get the first field object contained within object ownerInstance which has the type classType.static File
getTestDataDir
(String relativePath) Returns a file that points to the location on disk of the given relative path name.static File
Returns the data directory containing test programs and datastatic File
getTestDataFile
(String path) Returns the file within the data directory of the TestResources module that matches the given relative pathprotected boolean
Determine if test failure occur (for use within tear down methods)protected void
initializeLayout
(ApplicationLayout layout) A method to update anyApplicationLayout
valuesprotected void
A place to initialize and needed static propertiesstatic Object
invokeConstructor
(Class<?> containingClass, Class<?>[] parameterTypes, Object[] args) Uses reflection to execute the constructor for the given class with the given parameters.static Object
invokeInstanceMethod
(String methodName, Object ownerInstance) This method is just a "pass through" method forinvokeInstanceMethod(String, Object, Class[], Object[])
so that callers do not need to pass null to that method when the underlying instance method does not have any parameters.static Object
invokeInstanceMethod
(String methodName, Object ownerInstance, Class<?>[] parameterTypes, Object[] args) Uses reflection to execute the method denoted by the given method name.loadTextResource
(Class<?> cls, String name) Load a text resource file into an ArrayList.loadTextResource
(String name) void
static void
setErrorsExpected
(boolean expected) Signals that the client expected the System Under Test (SUT) to report errors.static void
setInstanceField
(String fieldName, Object ownerInstance, Object value) Sets the instance field by the given name on the given object instance.protected void
setLogLevel
(Class<?> loggerClazz, org.apache.logging.log4j.Level newLevel) A convenience method forsetLogLevel(String, Level)
.protected void
setLogLevel
(String loggerName, org.apache.logging.log4j.Level newLevel) A convenience method to change the log level of the given logger name.protected void
A callback for subclasses when a test has failed.static String
toString
(Collection<?> collection) Prints the contents of the given collection by way of theObject.toString()
method.static Window
Returns the window parent of c.Methods inherited from class generic.test.AbstractGTest
assertArraysEqualOrdered, assertArraysEqualUnordered, assertArraysEqualUnordered, assertContainsExactly, assertContainsExactly, assertContainsString, assertContainsStringIgnoringCase, assertListEqualOrdered, assertListEqualOrdered, assertListEqualsArrayOrdered, assertListEqualsArrayUnordered, assertListEqualUnordered, bytes, failWithException, getName, getRandomInt, getRandomInt, getRandomString, getRandomString, getTestDirectoryPath, sleep, waitFor, waitFor, waitFor, waitFor, waitFor, waitForCondition, waitForCondition, waitForCondition, waitForConditionWithoutFailing, waitForValue, waitForValueWithoutFailing
-
Field Details
-
TESTDATA_DIRECTORY_NAME
- See Also:
-
DEFAULT_TOOL_NAME
- See Also:
-
DEFAULT_TEST_TOOL_NAME
- See Also:
-
watchman
public org.junit.rules.TestWatcher watchman -
concurrentTestExceptionRule
public org.junit.rules.TestRule concurrentTestExceptionRule -
ruleChain
public org.junit.rules.RuleChain ruleChain -
repeatedRule
public org.junit.rules.TestRule repeatedRuleThis rule handles theRepeated
annotationDuring batch mode, this rule should never be needed. This rule is included here as a convenience, in case a developer wants to use the
Repeated
annotation to diagnose a non-deterministic test failure. Without this rule, the annotation would be silently ignored.
-
-
Constructor Details
-
AbstractGenericTest
public AbstractGenericTest()
-
-
Method Details
-
resetLogging
public void resetLogging() -
initializeSystemProperties
protected void initializeSystemProperties()A place to initialize and needed static properties -
initializeLayout
A method to update anyApplicationLayout
values- Parameters:
layout
- the layout to initialize
-
createApplicationLayout
- Throws:
IOException
-
createApplicationConfiguration
-
hasTestFailed
protected boolean hasTestFailed()Determine if test failure occur (for use within tear down methods)- Returns:
- true if test failure detected
-
testFailed
A callback for subclasses when a test has failed. This will be called aftertearDown()
. This means that any diagnostics will have to take into account items that have already been disposed.- Parameters:
e
- the exception that happened when the test failed
-
setLogLevel
A convenience method forsetLogLevel(String, Level)
.- Parameters:
loggerClazz
- the logger classnewLevel
- the new level
-
setLogLevel
A convenience method to change the log level of the given logger name. The logger name is typically the class name that contains specialized logging. You may also pass a package name to get logging for all classes in that package. SeeConfigurator.setLevel(String, Level)
The console appender's log level will be changed if needed to ensure that messages for the given log level are displayed.
The log system will be reset to the default settings when the test is finished.
- Parameters:
loggerName
- the logger namenewLevel
- the new log level to use
-
windowForComponent
Returns the window parent of c. If c is a window, then c is returned.Warning: this differs from
SwingUtilities.windowForComponent(Component)
in that the latter method will not return the given component if it is a window.- Parameters:
c
- the component- Returns:
- the window
-
loadTextResource
Load a text resource file into an ArrayList. Each line of the file is stored as an item in the list.- Parameters:
cls
- class where resource existsname
- resource filename- Returns:
- list of lines contained in file
- Throws:
IOException
- if an exception occurs reading the given resource
-
loadTextResource
- Throws:
IOException
-
getTestDataFile
Returns the file within the data directory of the TestResources module that matches the given relative pathA
FileNotFoundException
is throw if the file does not exist.- Parameters:
path
- path relative to the data directory of the TestResources module.- Returns:
- the file within the data directory of the TestResources module that matches the given relative path
- Throws:
FileNotFoundException
- if the given file does not exist
-
getTestDataDir
Returns a file that points to the location on disk of the given relative path name. The path is relative to the test resources directory.- Parameters:
relativePath
- the path of the file- Returns:
- a file that points to the location on disk of the relative path.
- Throws:
FileNotFoundException
- If the directory does not existIOException
- if the given path does not represent a directory
-
findTestDataFile
Returns the file within the data directory of the TestResources module that matches the given relative path.Null is returned if the file could not be found.
- Parameters:
path
- path relative to the data directory of the TestResources module.- Returns:
- the file within the data directory of the TestResources module that matches the given relative path
-
getTestDataDirectory
Returns the data directory containing test programs and data- Returns:
- the data directory containing test programs and data
-
getInstanceFieldByClassType
Get the first field object contained within object ownerInstance which has the type classType. This method is only really useful if it is known that only a single field of classType exists within the ownerInstance.- Type Parameters:
T
- the type- Parameters:
classType
- the class type of the desired fieldownerInstance
- the object instance that owns the field- Returns:
- field object of type classType or null
-
setInstanceField
public static void setInstanceField(String fieldName, Object ownerInstance, Object value) throws RuntimeException Sets the instance field by the given name on the given object instance.Note: if the field is static, then the
ownerInstance
field can be the class of the object that contains the variable.- Parameters:
fieldName
- The name of the field to retrieve.ownerInstance
- The object instance from which to get the variable instance.value
- The value to use when setting the given field- Throws:
RuntimeException
- if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.- See Also:
-
getInstanceField
public static Object getInstanceField(String fieldName, Object ownerInstance) throws RuntimeException Gets the instance field by the given name on the given object instance. The value is a primitive wrapper if it is a primitive type.Note: if the field is static, then the
ownerInstance
field can be the class of the object that contains the variable.- Parameters:
fieldName
- The name of the field to retrieve.ownerInstance
- The object instance from which to get the variable instance.- Returns:
- The field instance.
- Throws:
RuntimeException
- if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.- Since:
- Tracker Id 267
- See Also:
-
invokeConstructor
public static Object invokeConstructor(Class<?> containingClass, Class<?>[] parameterTypes, Object[] args) throws RuntimeException Uses reflection to execute the constructor for the given class with the given parameters. The new instance of the given class will be returned.- Parameters:
containingClass
- The class that contains the desired constructor.parameterTypes
- The parameter types that the constructor takes. This value can be null or zero length if there are no parameters to passargs
- The parameter values that should be passed to the constructor. This value can be null or zero length if there are no parameters to pass- Returns:
- The new class instance
- Throws:
RuntimeException
- if there is a problem accessing the constructor using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.
-
invokeInstanceMethod
public static Object invokeInstanceMethod(String methodName, Object ownerInstance, Class<?>[] parameterTypes, Object[] args) throws RuntimeException Uses reflection to execute the method denoted by the given method name. If any value is returned from the method execution, then it will be returned from this method. Otherwise,null
is returned.Note: if the method is static, then the
ownerInstance
field can be the class of the object that contains the method.- Parameters:
methodName
- The name of the method to execute.ownerInstance
- The object instance of which the method will be executed.parameterTypes
- The parameter types that the method takes.args
- The parameter values that should be passed to the method. This value can be null or zero length if there are no parameters to pass- Returns:
- The return value as returned from executing the method.
- Throws:
RuntimeException
- if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.- Since:
- Tracker Id 267
- See Also:
-
invokeInstanceMethod
public static Object invokeInstanceMethod(String methodName, Object ownerInstance) throws RuntimeException This method is just a "pass through" method forinvokeInstanceMethod(String, Object, Class[], Object[])
so that callers do not need to pass null to that method when the underlying instance method does not have any parameters.- Parameters:
methodName
- The name of the method to execute.ownerInstance
- The object instance of which the method will be executed.- Returns:
- The return value as returned from executing the method.
- Throws:
RuntimeException
- if there is a problem accessing the field using reflection. A RuntimeException is used so that calling tests can avoid using a try/catch block, but will still fail when an error is encountered.- See Also:
-
createStackTraceForAllThreads
Returns a string which is a printout of a stack trace for each thread running in the current JVM- Returns:
- the stack trace string
-
toString
Prints the contents of the given collection by way of theObject.toString()
method.- Parameters:
collection
- The contents of which to print- Returns:
- A string representation of the given collection
-
getFontMetrics
Returns a font metrics for the given font using a generic buffered image graphics context.- Parameters:
font
- the font- Returns:
- the font metrics
-
setErrorsExpected
public static void setErrorsExpected(boolean expected) Signals that the client expected the System Under Test (SUT) to report errors. Use this when you wish to verify that errors are reported and you do not want those errors to fail the test. The default value for this setting is false, which means that any errors reported will fail the running test.- Parameters:
expected
- true if errors are expected.
-
getDebugFileDirectory
Returns the directory into which tests can write debug files, such as files containing print statements or image files.This is not a temporary directory that will be deleted between tests, which is useful in that the debug files will persist after a test run.
Examples of this directory:
- server: {share dir}/junits.new/JunitTest_version/reports
- local gradle: {user home}/git/{repo}/ghidra/build/JUnit/reports
- eclipse: {module}/bin/
- Returns:
- the directory
-
createTempDirectory
Creates a sub-directory with the given name as a child of the Java temp directory. The given name will be the prefix of the new directory name, with any additional text as created byFiles.createTempDirectory(Path, String, java.nio.file.attribute.FileAttribute...)
. Any left-over test directories will be cleaned-up before creating the new directory.Note: you should not call this method multiple times, as each call will cleanup the previously created directories.
- Parameters:
name
- the name of the directory to create- Returns:
- the newly created directory
- Throws:
IOException
- of there is a problem creating the new directory
-
createTempFilePath
Creates a file path with a filename that is under the system temp directory. The path returned will not point to an existing file. The suffix of the file will be.tmp
.- Parameters:
name
- the filename- Returns:
- a new file path
- Throws:
IOException
- if there is any problem ensuring that the created path is non-existent- See Also:
-
createTempFilePath
Creates a file path with a filename that is under the system temp directory. The path returned will not point to an existing file. This method is the same ascreateTempFilePath(String)
, except that you must provide the extension.- Parameters:
name
- the filenameextension
- the file extension- Returns:
- a new file path
- Throws:
IOException
- if there is any problem ensuring that the created path is non-existent- See Also:
-
createTempFileForTest
Creates a temp file for the current test, using the test name as a prefix for the filename. This method callscreateTempFile(String)
, which will cleanup any pre-existing temp files whose name pattern matches this test name. This helps to avoid old temp files from accumulating.- Returns:
- the new temp file
- Throws:
IOException
- if there is a problem creating the new file
-
createTempFileForTest
Creates a temp file for the current test, using the test name as a prefix for the filename. This method callscreateTempFile(String)
, which will cleanup any pre-existing temp files whose name pattern matches this test name. This helps to avoid old temp files from accumulating.- Parameters:
suffix
- the suffix to provide for the temp file- Returns:
- the new temp file
- Throws:
IOException
- if there is a problem creating the new file
-
createTempFile
Creates a file in the Application temp directory using the given name as a prefix and the given suffix. The final filename will also include the current test name, as well as any data added byFile.createTempFile(String, String, File)
. The file suffix will be.tmp
The file will be marked to delete on JVM exit. This will not work if the JVM is taken down the hard way, as when pressing the stop button in Eclipse.
- Parameters:
name
- the prefix to put on the file, before the test name- Returns:
- the newly created file
- Throws:
IOException
- if there is a problem creating the new file- See Also:
-
createTempFile
Creates a file in the Application temp directory using the given name as a prefix and the given suffix. The final filename will also include the current test name, as well as any data added byFile.createTempFile(String, String, File)
.The file will be marked to delete on JVM exit. This will not work if the JVM is taken down the hard way, as when pressing the stop button in Eclipse.
Note: This method will create the file on disk! If you need the file to not exist, then you must delete the file yourself. Alternatively, you could instead call
createTempFilePath(String, String)
, which will ensure that the created temp file is deleted.Finally, this method will delete any files that match the given name and suffix values before creating the given temp file. This is important, as it will delete any files already created by the test that match this info.
- Parameters:
name
- the prefix to put on the file, before the test namesuffix
- the file suffix- Returns:
- the newly created file
- Throws:
IOException
- if there is a problem creating the new file- See Also:
-
deleteSimilarTempFiles
Delete any files under the Java temp directory that have the given text in their name.- Parameters:
nameText
- the partial name text to match against the files- See Also:
-
deleteMatchingTempFiles
Delete any files under the this test case's specific temp directory that match the give regexPattern
- Parameters:
namePattern
- the pattern to match against the files- See Also:
-