Class OfficeFrameTestCase

java.lang.Object
junit.framework.Assert
junit.framework.TestCase
net.officefloor.frame.test.OfficeFrameTestCase
All Implemented Interfaces:
junit.framework.Test
Direct Known Subclasses:
AbstractChangesTestCase, AbstractOfficeConstructTestCase, AbstractPolyglotObjectTest, AbstractPolyglotProcedureTest

public abstract class OfficeFrameTestCase extends junit.framework.TestCase
TestCase providing additional helper functions.
Author:
Daniel Sagenschneider
  • Field Details

  • Constructor Details

    • OfficeFrameTestCase

      public OfficeFrameTestCase()
      Default constructor, so will use setName(String).
    • OfficeFrameTestCase

      public OfficeFrameTestCase(String name)
      Initiate allowing specifying name of test.
      Parameters:
      name - Test name.
  • Method Details

    • runBare

      public void runBare() throws Throwable
      Overrides:
      runBare in class junit.framework.TestCase
      Throws:
      Throwable
    • isSkipStressTests

      public static boolean isSkipStressTests()

      Indicates if not to run stress tests.

      Stress tests should normally be run, but in cases of quick unit testing running for functionality the stress tests can reduce turn around time and subsequently the effectiveness of the tests. This is therefore provided to maintain effectiveness of unit tests.

      Furthermore, builds time out on Travis so avoid running.

      Returns:
      true to ignore doing a stress test.
    • isSkipTestsUsingDocker

      public static boolean isSkipTestsUsingDocker()

      Indicates if not to run tests using docker.

      Some environments do not support docker, so this enables disabling these tests.

      Returns:
      true to ignore doing a docker test.
    • isSkipTestsUsingGCloud

      public static boolean isSkipTestsUsingGCloud()

      Indicates if not to run tests using GCloud (Google Cloud).

      Some environments do not have GCloud available, so this enables disabling these tests.

      Returns:
      true to ignore doing a GCloud test.
    • fail

      public static RuntimeException fail(Throwable ex)

      Triggers failure due to exception.

      This is useful to not have to provide throws clauses on tests.

      Parameters:
      ex - Failure.
      Returns:
      RuntimeException to allow throw fail(ex); for compilation. Note this is never returned as always throws exception.
    • throwException

      public static void throwException(Throwable ex) throws Exception

      Propagates the Throwable as either:

      1. downcast Exception
      2. downcast Error
      3. Throwable wrapped with an Exception

      This is useful for such methods as TestCase.tearDown() that do not allow throwing Throwable.

      Parameters:
      ex - Throwable to propagate as an Exception.
      Throws:
      Exception - The failure.
    • assertFail

      public static <F extends Throwable> F assertFail(OfficeFrameTestCase.FailOperation operation, Class<F> expectedFailureType)
      Asserts the failure.
      Type Parameters:
      F - Failure type.
      Parameters:
      operation - OfficeFrameTestCase.FailOperation that is expected fail.
      expectedFailureType - Expect type of failure.
      Returns:
      Actual failure for further assertions.
    • assertFail

      public static <F extends Throwable> F assertFail(Class<F> expectedFailureType, Object object, String methodName, Object... parameters)
      Provides simplified facade to verify Method will fail.
      Type Parameters:
      F - Failure type.
      Parameters:
      expectedFailureType - Expected failure of method.
      object - Object to invoke Method on.
      methodName - Name of the Method.
      parameters - Parameters for the Method.
      Returns:
      Actual failure for further assertions.
    • assertFail

      public static <F extends Throwable> F assertFail(Class<F> expectedFailureType, Object object, Method method, Object... parameters)
      Provides simplified facade to verify Method will fail.
      Type Parameters:
      F - Failure type.
      Parameters:
      expectedFailureType - Expected failure of method.
      object - Object to invoke Method on.
      method - Method.
      parameters - Parameters for the Method.
      Returns:
      Actual failure for further assertions.
    • assertTextEquals

      public static void assertTextEquals(String message, String expected, String actual)
      Asserts the input texts match taking into account platform differences.
      Parameters:
      message - Message.
      expected - Raw expected text.
      actual - Raw actual text.
    • createPlatformIndependentText

      public static String createPlatformIndependentText(String rawText)
      Creates the platform independent text for comparing.
      Parameters:
      rawText - Raw text.
      Returns:
      Platform independent text.
    • assertXmlEquals

      public static void assertXmlEquals(String message, String expected, String actual)
      Asserts the input XML's match with white spacing removed.
      Parameters:
      message - Message.
      expected - Raw expected text.
      actual - Raw actual text.
    • removeXmlWhiteSpacing

      public static String removeXmlWhiteSpacing(String xml)
      Removes the white spacing from the XML.
      Parameters:
      xml - XML.
      Returns:
      XML with white spacing removed.
    • assertGraph

      public static <O> void assertGraph(O expectedRoot, O actualRoot, String... ignoreMethodNames) throws Exception
      Assets that the input graph is as expected.
      Type Parameters:
      O - Type.
      Parameters:
      expectedRoot - Expected root.
      actualRoot - Actual root.
      ignoreMethodNames - Listing of methods to be ignored in checking.
      Throws:
      Exception - If fails.
    • assertContents

      public static void assertContents(File expected, File actual) throws IOException
      Asserts the contents of the input File instances are the same.
      Parameters:
      expected - Expected file.
      actual - Actual file.
      Throws:
      IOException - If fails to read contents.
    • assertContents

      public static void assertContents(Reader expected, Reader actual)
      Asserts the contents of the input Reader instances are the same.
      Parameters:
      expected - Expected content.
      actual - Actual content.
    • assertList

      @SafeVarargs public static <O> void assertList(ListItemMatcher<O> matcher, List<O> list, O... expectedItems)
      Asserts that the input list is as expected.
      Type Parameters:
      O - Type.
      Parameters:
      matcher - Matches the items of the list.
      list - List to be checked.
      expectedItems - Items expected to be in the list.
    • assertList

      @SafeVarargs public static <O> void assertList(List<O> list, O... expectedItems)
      Asserts that the input list equals the expected.
      Type Parameters:
      O - Type.
      Parameters:
      list - List to be checked.
      expectedItems - Items expected in the list.
    • assertList

      @SafeVarargs public static <O> void assertList(String[] methods, List<O> list, O... expectedItems)
      Asserts that properties on items within list match.
      Type Parameters:
      O - Type.
      Parameters:
      methods - Method names to specify the properties on the items to match.
      list - List to be checked.
      expectedItems - Items expected in the list.
    • assertList

      @SafeVarargs public static <O> void assertList(String[] methods, O[] array, O... expectedItems)
      Asserts that properties on items within the array match.
      Type Parameters:
      O - Type.
      Parameters:
      methods - Method names to specify the properties on the items to match.
      array - Array to be checked.
      expectedItems - Items expected in the array.
    • assertList

      @SafeVarargs public static <O> void assertList(String sortMethod, String[] methods, List<O> list, O... expectedItems)
      Asserts that properties on items within list match after the list is sorted.
      Type Parameters:
      O - Type.
      Parameters:
      sortMethod - Name of method on the items to sort the list by to ensure match in order.
      methods - Method names to specify the properties on the items to match.
      list - List to be checked.
      expectedItems - Items expected in the list.
    • assertProperties

      public static <O> void assertProperties(O expected, O actual, String... methods)
      Asserts that properties on the input objects match for the specified methods.
      Type Parameters:
      O - Type.
      Parameters:
      expected - Expected item.
      actual - Actual item.
      methods - Method names to specify the properties on the item to match.
    • getProperty

      public static Object getProperty(Object object, String methodName)
      Obtains the property on the Object.
      Parameters:
      object - Object.
      methodName - Method name to obtain property value.
      Returns:
      Value of property.
    • getItem

      public static <T> T getItem(Collection<T> items, String methodName, Object value)
      Obtains the item within the items whose property by methodName matches the input value.
      Type Parameters:
      T - Item type.
      Parameters:
      items - Items to search.
      methodName - Property on the item.
      value - Value of property the item should match.
      Returns:
      Item with the matching property.
    • setName

      public void setName(String name)
      Overrides:
      setName in class junit.framework.TestCase
    • isGuiAvailable

      protected boolean isGuiAvailable()
      Indicates if the GUI is available.
      Returns:
      true if the GUI is available.
    • setVerbose

      public void setVerbose(boolean isVerbose)
      Specifies to provide verbose output to aid in debugging.
      Parameters:
      isVerbose - true to turn on verbose output.
    • setDebugVerbose

      public void setDebugVerbose()
      Specifies to provide debug verbose output to aid in debugging.
    • setLogGC

      public void setLogGC()
      Turns on logging of GC as part of test.
    • createNewClassLoader

      public ClassLoader createNewClassLoader()

      Creates a new ClassLoader from current process's java class path.

      Class instances loaded via this ClassLoader will be different to the current ClassLoader. This is to allow testing multiple ClassLoader environments (such as Eclipse plug-ins).

      Returns:
      New ClassLoader.
    • displayGraph

      public void displayGraph(Object root) throws Exception
      Displays the graph of objects starting at root.
      Parameters:
      root - Root of graph to display.
      Throws:
      Exception - If fails.
    • displayGraph

      public void displayGraph(Object root, String... ignoreMethodNames) throws Exception
      Displays the graph of objects starting at root ignoring following verticies by the input method names.
      Parameters:
      root - Root of graph to display.
      ignoreMethodNames - Method names to ignore.
      Throws:
      Exception - If fails.
    • createMock

      public final <M> M createMock(Class<M> classToMock)
      Creates a mock object registering the mock object with registry for management.
      Type Parameters:
      M - Interface type.
      Parameters:
      classToMock - Class to be mocked.
      Returns:
      Mock object.
    • createSynchronizedMock

      public final <M> M createSynchronizedMock(Class<M> interfaceToMock)
      Creates a thread safe mock object.
      Type Parameters:
      M - Interface type.
      Parameters:
      interfaceToMock - Class to mock.
      Returns:
      Mock object.
    • param

      public <T> T param(T value)
      Wraps a parameter value when attempting to capture.
      Type Parameters:
      T - Value type.
      Parameters:
      value - Value.
      Returns:
      Value for parameter.
    • paramType

      public <T> T paramType(Class<T> type)
      Wraps a parameter type expected.
      Type Parameters:
      T - Value type.
      Parameters:
      type - Expected type.
      Returns:
      Value for parameter.
    • recordReturn

      public final <T> void recordReturn(Object mockObject, T ignore, T recordedReturn)
      Convenience method to record a method and its return on a mock object.
      Type Parameters:
      T - Expected result type.
      Parameters:
      mockObject - Mock object.
      ignore - Result of operation on the mock object. This is only provided to obtain correct return type for recording return.
      recordedReturn - Value that is recorded to be returned from the mock object.
    • recordReturn

      public final <T> void recordReturn(Object mockObject, T ignore, T recordedReturn, ArgumentsMatcher matcher)
      Convenience method to record a method, an ArgumentsMatcher and return value.
      Type Parameters:
      T - Expected result type.
      Parameters:
      mockObject - Mock object.
      ignore - Result of operation on the mock object. This is only provided to obtain correct return type for recording return.
      recordedReturn - Value that is recorded to be returned from the mock object.
      matcher - ArgumentsMatcher.
    • recordVoid

      public final void recordVoid(Object mockObject, ArgumentsMatcher matcher)
      Convenience method to record void method.
      Parameters:
      mockObject - Mock object.
      matcher - ArgumentsMatcher.
    • recordThrows

      public final <T> void recordThrows(Object mockObject, T ignore, Throwable exception)
      Convenience method to record an Exception.
      Type Parameters:
      T - Expected result type.
      Parameters:
      mockObject - Mock object.
      ignore - Result of operation on the mock object. This is only provided to obtain correct return type for recording return.
      exception - Throwable.
    • replayMockObjects

      protected final void replayMockObjects()
      Flags all the mock objects to replay.
    • verifyMockObjects

      protected final void verifyMockObjects()
      Verifies all mock objects.
    • doTest

      protected final <R, T extends Throwable> R doTest(OfficeFrameTestCase.TestLogic<R,T> test) throws T
      Undertakes test wrapping with mock object replay and verify.
      Type Parameters:
      R - Return type of test logic.
      T - Possible Throwable.
      Parameters:
      test - Test logic to wrap in replay/verify.
      Returns:
      Result of test logic.
      Throws:
      T - If logic throws Exception.
    • doMultiThreadedTest

      protected final <T extends Throwable> void doMultiThreadedTest(int threadCount, int iterationCount, OfficeFrameTestCase.MultithreadedTestLogic<T> test) throws T
      Undertakes multi-threaded testing of OfficeFrameTestCase.TestLogic.
      Type Parameters:
      T - Throwable type.
      Parameters:
      threadCount - Number of Thread instances to run in parallel.
      iterationCount - Number of iterations of OfficeFrameTestCase.TestLogic per Thread.
      test - OfficeFrameTestCase.TestLogic.
      Throws:
      T - Possible failure from failing OfficeFrameTestCase.TestLogic.
    • doMultiThreadedTest

      protected final <T extends Throwable> void doMultiThreadedTest(int threadCount, int iterationCount, int timeout, OfficeFrameTestCase.MultithreadedTestLogic<T> test) throws T
      Undertakes multi-threaded testing of OfficeFrameTestCase.TestLogic.
      Type Parameters:
      T - Throwable type.
      Parameters:
      threadCount - Number of Thread instances to run in parallel.
      iterationCount - Number of iterations of OfficeFrameTestCase.TestLogic per Thread.
      timeout - Timeout.
      test - OfficeFrameTestCase.TestLogic.
      Throws:
      T - Possible failure from failing OfficeFrameTestCase.TestLogic.
    • captureLoggerOutput

      protected final <T extends Throwable> String captureLoggerOutput(OfficeFrameTestCase.TestCapture<T> test) throws T
      Capture std err of test logic.
      Type Parameters:
      T - Possible Exception type.
      Parameters:
      test - Test logic to capture std err.
      Returns:
      std err output.
      Throws:
      T - Possible Throwable.
    • findFile

      public File findFile(String relativePath) throws FileNotFoundException
      Obtains the file at the relative path.
      Parameters:
      relativePath - Relative path to the file.
      Returns:
      File.
      Throws:
      FileNotFoundException - If file could not be found.
    • findFile

      public File findFile(Class<?> packageClass, String fileName) throws FileNotFoundException
      Obtains the file by the input file name located in the package of the input class.
      Parameters:
      packageClass - Class to obtain the relative path from for its package.
      fileName - Name of file within the package directory.
      Returns:
      File within the package directory.
      Throws:
      FileNotFoundException - Should the file not be found.
    • getFileLocation

      public String getFileLocation(Class<?> packageClass, String fileName)
      Obtains the file location of the input file located in the package of the input class.
      Parameters:
      packageClass - Class to obtain the relative path from for its package.
      fileName - Name of the file within the package directory.
      Returns:
      Path to the file.
    • clearDirectory

      public void clearDirectory(File directory)
      Creates the input directory.
      Parameters:
      directory - Directory to be cleared.
    • deleteDirectory

      public void deleteDirectory(File directory)
      Deletes the input directory.
      Parameters:
      directory - Directory to be deleted.
    • copyDirectory

      public void copyDirectory(File source, File target) throws IOException
      Copies the contents of the source directory to the target directory.
      Parameters:
      source - Source directory.
      target - Target directory.
      Throws:
      IOException - If fails to copy the directory.
    • findInputStream

      public InputStream findInputStream(Class<?> packageClass, String fileName) throws FileNotFoundException

      Obtains the input stream to the file by the input file name located in the package of the input class.

      Note: this also searches the class path for the file.

      Parameters:
      packageClass - Class to obtain the relative path from for its package.
      fileName - Name of file within the package directory.
      Returns:
      File within the package directory.
      Throws:
      FileNotFoundException - Should the file not be found.
    • getPackageRelativePath

      public String getPackageRelativePath(Class<?> packageClass)
      Obtains the relative path of the package of the class.
      Parameters:
      packageClass - Class to obtain the relative path from for its package.
      Returns:
      Relative path of class's package.
    • getFileContents

      public String getFileContents(File file) throws FileNotFoundException, IOException
      Obtains the contents of the output file.
      Parameters:
      file - File to obtain contents from.
      Returns:
      Contents of the output file.
      Throws:
      FileNotFoundException - Should output file not yet be created.
      IOException - Should fail to read from output file.
    • createFile

      public void createFile(File target, InputStream content) throws IOException
      Creates the target file with the content.
      Parameters:
      content - Content for the file.
      target - Target file.
      Throws:
      IOException - If fails to create.
    • sleep

      public void sleep(int time)
      Facade helper function for invoking Thread.sleep(long).
      Parameters:
      time - Sleep time in seconds.
    • timeout

      public void timeout(long startTime)
      Facade method to timeout operations after 3 seconds.
      Parameters:
      startTime - Start time from System.currentTimeMillis().
    • timeout

      public void timeout(long startTime, int secondsToRun)
      Facade method to timeout operations after a second.
      Parameters:
      startTime - Start time from System.currentTimeMillis().
      secondsToRun - Seconds to run before timeout.
    • waitForTrue

      public <T extends Throwable> void waitForTrue(OfficeFrameTestCase.WaitForTruePredicate<T> check) throws T
      Waits for the check to be true.
      Type Parameters:
      T - Possible failure type.
      Parameters:
      check - Check.
      Throws:
      T - Possible failure.
    • waitForTrue

      public <T extends Throwable> void waitForTrue(OfficeFrameTestCase.WaitForTruePredicate<T> check, int secondsToRun) throws T
      Waits for the check to be true.
      Type Parameters:
      T - Possible failure type.
      Parameters:
      check - Check.
      secondsToRun - Seconds to wait before timing out.
      Throws:
      T - Possible failure.
    • isPrintMessages

      protected boolean isPrintMessages()
      Determines if printing messages.
      Returns:
      true to print messages.
    • printHeapMemoryDiagnostics

      public void printHeapMemoryDiagnostics()
      Prints heap memory details.
    • getDisplayRunTime

      public String getDisplayRunTime(long startTime)
      Obtains run time in human readable form.
      Parameters:
      startTime - Start time of running.
      Returns:
      Run time in human readable form.
    • getDisplayRunTime

      public String getDisplayRunTime(long startTime, long endTime)
      Obtains run time in human readable form.
      Parameters:
      startTime - Start time of running.
      endTime - End time of running.
      Returns:
      Run time in human readable form.
    • printMessage

      public void printMessage(String message)
      Prints a message regarding the test.
      Parameters:
      message - Message to be printed.
    • printMessage

      public void printMessage(InputStream message) throws IOException
      Prints a message regarding the test.
      Parameters:
      message - Message to be printed.
      Throws:
      IOException - If fails to print message.
    • printMessage

      public void printMessage(Reader message) throws IOException
      Prints a message regarding the test.
      Parameters:
      message - Message to be printed.
      Throws:
      IOException - If fails to print message.