5. Create a Unit Test to Check the Extractor

Create a unit test so that your extractor can be work fine.

Test the Extractor (Unit Test)

You can write a unit test to test your extractor to check that your extractor works. In the test, you must inject a dummy IExtractorMetadata instance. The IExtractorMetadata is used for initialization by the extractor. The dummy instance provides the extractor with information such as CP name, data source type, product version, connection property details and so on.

If required, download and import the Junit and EasyMock library to your project from the following sites:

http://junit.org/

http://easymock.org/

package com.hp.itba.dwh.extractors.sample.test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.easymock.EasyMock; import org.easymock.IMocksControl; import org.junit.BeforeClass; import org.junit.Ignore;
import org.junit.Test;

import com.hp.itba.dwh.dcs.api.IDataSource;
import com.hp.itba.dwh.dcs.api.IExtractor;
import com.hp.itba.dwh.dcs.api.IExtractorMetadata;
import com.hp.itba.dwh.dcs.exception.ExtractorException;
import com.hp.itba.dwh.extractor.test.SampleExtractor;

public class SampleExtractorTest {
static IExtractor extractor;









public static void setup() {
extractor = new SampleExtractor();
IMocksControl control = EasyMock.createControl();
IExtractorMetadata metadata = control.createMock(IExtractorMetadata.class); 
EasyMock.expect(metadata.getCpName()).andReturn("Sample").anyTimes(); 
EasyMock.expect(metadata.getDataSourceType()).andReturn("generic").anyTimes();

EasyMock.expect(metadata.getProductVersion()).andReturn("1.0").anyTimes();




EasyMock.expect(metadata.getFieldDelimiter()).andReturn("|").anyTimes(); EasyMock.expect(metadata.getRowDelimiter()).andReturn("#").anyTimes(); EasyMock.expect(metadata.getOutputPath()).andReturn(new File
("output").getAbsolutePath()).anyTimes();
IDataSource ds = control.createMock(IDataSource.class); List<String[]> value = new ArrayList<String[]>();
value.add(new String[] {"productName", "Sample", "false"});
//…
EasyMock.expect(ds.getConnProperties()).andReturn(value).anyTimes(); EasyMock.expect(metadata.getDataSource()).andReturn(ds).anyTimes();

EasyMock.expectLastCall().anyTimes();
control.replay();
try {
extractor.init("sample_1", metadata);
// extractor.setDbHelper(dbHelper);
} catch (ExtractorException e) { Assert.fail(e.getMessage());
}

}

public void testCheckConnection() {
try {
extractor.checkConnection();
} catch (ExtractorException e) { Assert.fail(e.getMessage());
}
}

public void testExtract() {
try {
extractor.extract("1", null);
} catch (ExtractorException e) { Assert.fail(e.getMessage());




}
}
}