1: Test A Pipeline Stage In Isolation

Generally, it should be possible to test a component all by itself. To do this, create a new Job, a Job Object (an AspireObject), and then fill out all of the members of the Job and the Job Object that are used by your stage. Then, send the job to your stage with the process() method and inspect the job after your stage is done with it. Use JobFactory.newInstance() to create a job. See Aspire Job Class for more information.

This is the easiest method for testing a pipeline stage. This is also the method set up by the aspire-stage-archetype Maven Archetype automatically.

Note: The examples in this section are just examples. Replace the data (the configurations, test strings, etc.) with the needs for your own stage.

Example

1. Initialize the Unit Test Helper.

The Unit Test Helper provides methods which help simplify unit testing, including creating directories and files for test output, automatically comparing files to "known good" versions, and scanning strings for regular expressions, plus a number of other useful features.

   UnitTestHelper uth = new UnitTestHelper(this.getClass());

2. Get an instance of your new stage

   ExtractTextStage myStage = new ExtractTextStage();

3. Initialize your stage with initialize():

   myStage.initialize(AXML.stringToDom(
       "<config>" +
         "<tikaConfig>src/main/resources/tika-config.xml</tikaConfig>" +
         "<metadataMap>" +
           "<map from=\"DC.title\" to=\"myDublinCoreTitle\"/>" +
           "<map from=\"DC.description\" to=\"myDublinCoreDescription\"/>" +
         "</metadataMap>" +
       "</config>"));

Note: AXML is an Aspire utility class for easily creating and manipulating XML. It wraps the W3C XML DOM object.

4. Initialize an AspireObject:

    AspireObject doc = new AspireObject(new StringReader(
        "<doc><fetchUrl>http://dublincore.org/</fetchUrl></doc>"));

5. Create a job using the document as the object.

   Job j = JobFactory.newInstance(doc);

6. Process the job with your stage:

    myStage.process(j);

7. Check that the necessary changes to the AspireDocument have been made:

   System.out.println(doc.toString(true));
   assertTrue(uth.scanStringForRegex(doc.toString(true), "The Dublin Core Metadata Initiative"));

2: Test Within an Instance of Aspire

Test your pipeline stage within an entire instance of Aspire, embedded inside your unit test.

This uses the ComponentTestBench, available from the aspire-ctb artifact.

 

  • No labels