Package generic.test

Class AbstractGenericTest

java.lang.Object
generic.test.AbstractGTest
generic.test.AbstractGenericTest
Direct Known Subclasses:
AbstractGuiTest

public abstract class AbstractGenericTest extends AbstractGTest
Base class for tests that provide some helper methods that are useful for tests that don't require swing/gui support.
  • Field Details

    • TESTDATA_DIRECTORY_NAME

      public static final String TESTDATA_DIRECTORY_NAME
      See Also:
    • DEFAULT_TOOL_NAME

      public static final String DEFAULT_TOOL_NAME
      See Also:
    • DEFAULT_TEST_TOOL_NAME

      public static final String 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 repeatedRule
      This rule handles the Repeated annotation

      During 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

      protected void initializeLayout(ApplicationLayout layout)
      A method to update any ApplicationLayout values
      Parameters:
      layout - the layout to initialize
    • createApplicationLayout

      protected ApplicationLayout createApplicationLayout() throws IOException
      Throws:
      IOException
    • createApplicationConfiguration

      protected ApplicationConfiguration createApplicationConfiguration()
    • hasTestFailed

      protected boolean hasTestFailed()
      Determine if test failure occur (for use within tear down methods)
      Returns:
      true if test failure detected
    • testFailed

      protected void testFailed(Throwable e)
      A callback for subclasses when a test has failed. This will be called after tearDown(). 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

      protected void setLogLevel(Class<?> loggerClazz, org.apache.logging.log4j.Level newLevel)
      A convenience method for setLogLevel(String, Level).
      Parameters:
      loggerClazz - the logger class
      newLevel - the new level
    • setLogLevel

      protected void setLogLevel(String loggerName, org.apache.logging.log4j.Level newLevel)
      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. See Configurator.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 name
      newLevel - the new log level to use
    • windowForComponent

      public static Window windowForComponent(Component c)
      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

      public static List<String> loadTextResource(Class<?> cls, String name) throws IOException
      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 exists
      name - resource filename
      Returns:
      list of lines contained in file
      Throws:
      IOException - if an exception occurs reading the given resource
    • loadTextResource

      public static List<String> loadTextResource(String name) throws IOException
      Throws:
      IOException
    • getTestDataFile

      public static File getTestDataFile(String path) throws FileNotFoundException
      Returns the file within the data directory of the TestResources module that matches the given relative path

      A 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

      public static File getTestDataDir(String relativePath) throws FileNotFoundException, IOException
      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 exist
      IOException - if the given path does not represent a directory
    • findTestDataFile

      public static File findTestDataFile(String path)
      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

      public static File getTestDataDirectory()
      Returns the data directory containing test programs and data
      Returns:
      the data directory containing test programs and data
    • getInstanceFieldByClassType

      public static <T> T getInstanceFieldByClassType(Class<T> classType, Object ownerInstance)
      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 field
      ownerInstance - 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 pass
      args - 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 for invokeInstanceMethod(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

      public static String 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

      public static String toString(Collection<?> collection)
      Prints the contents of the given collection by way of the Object.toString() method.
      Parameters:
      collection - The contents of which to print
      Returns:
      A string representation of the given collection
    • getFontMetrics

      public static FontMetrics getFontMetrics(Font font)
      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

      public static File 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

      public static File createTempDirectory(String name) throws IOException
      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 by Files.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

      public String createTempFilePath(String name) throws IOException
      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

      public String createTempFilePath(String name, String extension) throws IOException
      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 as createTempFilePath(String), except that you must provide the extension.
      Parameters:
      name - the filename
      extension - 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

      public File createTempFileForTest() throws IOException
      Creates a temp file for the current test, using the test name as a prefix for the filename. This method calls createTempFile(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

      public File createTempFileForTest(String suffix) throws IOException
      Creates a temp file for the current test, using the test name as a prefix for the filename. This method calls createTempFile(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

      public File createTempFile(String name) throws IOException
      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 by File.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

      public File createTempFile(String name, String suffix) throws IOException
      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 by File.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 name
      suffix - the file suffix
      Returns:
      the newly created file
      Throws:
      IOException - if there is a problem creating the new file
      See Also:
    • deleteSimilarTempFiles

      public static void deleteSimilarTempFiles(String nameText)
      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

      public static void deleteMatchingTempFiles(String namePattern)
      Delete any files under the this test case's specific temp directory that match the give regex Pattern
      Parameters:
      namePattern - the pattern to match against the files
      See Also: