Class ModuleTestBase

java.lang.Object
junit.framework.Assert
junit.framework.TestCase
org.archive.state.ModuleTestBase
All Implemented Interfaces:
junit.framework.Test
Direct Known Subclasses:
ProcessorTestBase

public abstract class ModuleTestBase
extends junit.framework.TestCase
Base class for unit testing Module implementations.
Author:
pjack
  • Constructor Summary

    Constructors
    Constructor Description
    ModuleTestBase()
    Magical constructor that attempts to auto-create static key field descriptions for your module class.
  • Method Summary

    Modifier and Type Method Description
    protected Class<?> getModuleClass()
    Returns the class of the module to test.
    protected org.archive.util.Recorder getRecorder()  
    protected File getResourceDir()
    Returns the location of the Java resources directory for your project.
    protected File getSourceCodeDir()
    Returns the location of the source code directory for your project.
    protected CrawlURI makeCrawlURI​(String uri)  
    protected Object makeModule()
    Return an example instance of the module.
    protected void runTest()  
    void testSerializationIfAppropriate()
    Tests that the module can be serialized.
    protected void verifySerialization​(Object first, byte[] firstBytes, Object second, byte[] secondBytes)
    Verifies that serialization was successful.

    Methods inherited from class junit.framework.TestCase

    assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, countTestCases, createResult, fail, fail, failNotEquals, failNotSame, failSame, format, getName, run, run, runBare, setName, setUp, tearDown, toString

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ModuleTestBase

      public ModuleTestBase()
      Magical constructor that attempts to auto-create static key field descriptions for your module class.

      If getSourceCodeDir() and getResourceDir() both return non-null values, then the constructor will look in the resources directory for an English resource file for the class. If it finds one, nothing magical happens.

      Otherwise, the source code for the module being tested is loaded, and parsed to extract the JavaDoc descriptions for the static key fields. The results are stored in the appropriate English locale file in the resource directory.

      Note the parsing is naive; at minimum, you should load the resulting locale file and remove any HTML markup.

  • Method Details

    • getSourceCodeDir

      protected File getSourceCodeDir()
      Returns the location of the source code directory for your project. This defaults to "src/main/java", which is the standard for projects built with maven2. If you use a different source code directory, you should override this method.

      If you want to disable automatic key description generation, return null from this method.

      Returns:
      the source code directory for the project
    • getResourceDir

      protected File getResourceDir()
      Returns the location of the Java resources directory for your project. This defaults to "src/resources/java", which is the standard for projects built with maven2. If you use a different source code directory -- for instance, if your resources directory is the same as your source code directory -- you should override this method.

      If you want to disable automatic key description generation, return null from this method.

      Returns:
      the source code directory for the project
    • getModuleClass

      protected Class<?> getModuleClass()
      Returns the class of the module to test. Deduces from test class name if possible.
      Returns:
      the class of the module to test
    • makeModule

      protected Object makeModule() throws Exception
      Return an example instance of the module. This is used by testSerialization to ensure the module can be serialized.
      Returns:
      an example instance of the module
      Throws:
      Exception - if the module cannot be constructed for any reason
    • testSerializationIfAppropriate

      public void testSerializationIfAppropriate() throws Exception
      Tests that the module can be serialized. The value returned by makeModule() is serialized to a byte array, and then deserialized, and then serialized to a second byte array. The results are passed to verifySerialization(java.lang.Object, byte[], java.lang.Object, byte[]), which will simply compare the two byte arrays for equality. (That won't always work; see that method for details).

      If nothing else, this test is useful for catching NotSerializable exceptions for your module or classes it depends on.

      Throws:
      Exception - if the module cannot be serialized
    • verifySerialization

      protected void verifySerialization​(Object first, byte[] firstBytes, Object second, byte[] secondBytes) throws Exception
      Verifies that serialization was successful.

      By default, this method simply compares the first and second byte arrays for equality. That may not work if you use custom serialization -- for instance, if you're serializing a timestamp. If that's the case you should override this method to compare the given objects, or to simply do nothing. (If this method does nothing, then the testSerializationIfAppropriate() test is still useful for catching NotSerializable problems).

      Parameters:
      first - the first object that was serialized
      firstBytes - the byte array the first object was serialized to
      second - the second object that was serialized
      secondBytes - the byte array the second object was serialized to
      Throws:
      Exception - if anyt problem occurs
    • runTest

      protected void runTest() throws Throwable
      Overrides:
      runTest in class junit.framework.TestCase
      Throws:
      Throwable
    • getRecorder

      protected org.archive.util.Recorder getRecorder() throws IOException
      Throws:
      IOException
    • makeCrawlURI

      protected CrawlURI makeCrawlURI​(String uri) throws org.apache.commons.httpclient.URIException, IOException
      Throws:
      org.apache.commons.httpclient.URIException
      IOException