Class ModuleTestBase
- All Implemented Interfaces:
junit.framework.Test
- Direct Known Subclasses:
ProcessorTestBase
public abstract class ModuleTestBase
extends junit.framework.TestCase
- 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
-
Constructor Details
-
ModuleTestBase
public ModuleTestBase()Magical constructor that attempts to auto-create static key field descriptions for your module class.If
getSourceCodeDir()
andgetResourceDir()
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
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
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
Returns the class of the module to test. Deduces from test class name if possible.- Returns:
- the class of the module to test
-
makeModule
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
Tests that the module can be serialized. The value returned bymakeModule()
is serialized to a byte array, and then deserialized, and then serialized to a second byte array. The results are passed toverifySerialization(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 ExceptionVerifies 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 serializedfirstBytes
- the byte array the first object was serialized tosecond
- the second object that was serializedsecondBytes
- the byte array the second object was serialized to- Throws:
Exception
- if anyt problem occurs
-
runTest
- Overrides:
runTest
in classjunit.framework.TestCase
- Throws:
Throwable
-
getRecorder
- Throws:
IOException
-
makeCrawlURI
protected CrawlURI makeCrawlURI(String uri) throws org.apache.commons.httpclient.URIException, IOException- Throws:
org.apache.commons.httpclient.URIException
IOException
-