tags:

views:

118

answers:

0

When I am testing a method using dbunit, it persisted into the table. I am using MySQL and my code is -

@DBUnitConfiguration(locations = "/TestResources/testCampaignRequestServiceImpl-data.xml")
public class TestCampaignRequestServiceImpl extends AbstractDataAccessTest {    

@Test
public void testConvertToCampaign(){
    try {
        CampaignRequest campaignRequesttest = new CampaignRequest(); 
        campaignRequesttest.setCampaignId(TestCaseInputValues.CAMPAIGN_ID);
        CampaignRequest campaignRequestDB=campaignRequestService.findById(campaignRequesttest);
        campaignRequestDB.setCampaignName("1284Campaign");
        campaignRequestDB.setShowcaseId("1284Campaign");
        List<ExtraFeature> featureRequired = new ArrayList<ExtraFeature>();
        featureRequired.add(ExtraFeature.GOLDSTAR_PMI);
        campaignRequestDB.setSelectedExtraFeaturesList(featureRequired);
        campaignRequestService.convertToCampaign(campaignRequestDB);
    } catch (BusinessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }catch (Exception e) {
        e.printStackTrace();
    }
}   
}

And second class -

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DBUnitTestExecutionListener.class})
@ContextConfiguration(locations = "/testconfig/applicationContext.xml")
@Transactional
public abstract class AbstractDataAccessTest {}

and listener is -

public class DBUnitTestExecutionListener extends TransactionalTestExecutionListener {

private static final Log logger = LogFactory.getLog(DBUnitTestExecutionListener.class);

private static final String DEFAULT_DATASOURCE_NAME = "basicDatasource";
private static final String TABLE_TYPES[] = {"TABLE", "ALIAS"};

@Override
public void beforeTestMethod(TestContext testContext) throws Exception {

    super.beforeTestMethod(testContext);

    DataSource dataSource = getDataSource(testContext);
    Connection conn = DataSourceUtils.getConnection(dataSource);
    conn.setAutoCommit(false);
    IDatabaseConnection dbUnitConn = getDBUnitConnection(conn);
 //  setDataSet(dbUnitConn,testContext); can export data by code too....
    try {
        IDataSet dataSets[] = getDataSets(testContext);
        for (IDataSet dataSet : dataSets) {
            DatabaseOperation.CLEAN_INSERT.execute(dbUnitConn, dataSet);
            logger.info("Performed CLEAN_INSERT of IDataSet.");
        }
    } finally {
        DataSourceUtils.releaseConnection(conn, dataSource);
    }
}



private void setDataSet(IDatabaseConnection dbUnitConn, TestContext testContext) {
    try{
        String[] depTableNames = TablesDependencyHelper.getAllDependentTables( dbUnitConn, "client" );
        IDataSet depDataSet = dbUnitConn.createDataSet( depTableNames );
        String location[]=getDataLocations(testContext);
        URL urlFile = DBUnitTestExecutionListener.class.getResource(location[0]);
        System.out.println("urlFile in new method =  "+ urlFile);
        File outFile = new File(urlFile.getPath());
        FileOutputStream fos = new FileOutputStream(outFile);
        FlatXmlDataSet.write(depDataSet, fos );

    } catch (SearchException e) {
        e.printStackTrace();
    }catch (SQLException e) {
        e.printStackTrace();
    } catch (DataSetException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }
}


private DataSource getDataSource(TestContext context) throws Exception {
    DataSource dataSource;
    Map beans = context.getApplicationContext().getBeansOfType(DataSource.class);
    if (beans.size() > 1) {
        dataSource = (DataSource) beans.get(DEFAULT_DATASOURCE_NAME);
        if (dataSource == null) {
            throw new NoSuchBeanDefinitionException("Unable to locate default data source.");
        }
    } else {
        dataSource = (DataSource) beans.values().iterator().next();
    }
    return dataSource;
}

private IDatabaseConnection getDBUnitConnection(Connection c) {
    IDatabaseConnection conn = null;
    try {
        conn = new DatabaseConnection(c);
        DatabaseConfig config = conn.getConfig();
        config.setFeature("http://www.dbunit.org/features/qualifiedTableNames", true);
        config.setProperty("http://www.dbunit.org/properties/tableType", TABLE_TYPES);
    } catch (DatabaseUnitException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    return conn;
}

private IDataSet[] getDataSets(TestContext context) throws Exception {
    String dataFiles[] = getDataLocations(context);
    IDataSet dataSets[] = new IDataSet[dataFiles.length];
    for (int i = 0; i < dataFiles.length; i++) {
        Resource resource = new ClassPathResource(dataFiles[i]);
        Class clazz = getDataSetType(context);
        Constructor con = clazz.getConstructor(InputStream.class);
        dataSets[i] = (IDataSet) con.newInstance(resource.getInputStream());
    }
    return dataSets;
}


protected Class getDataSetType(TestContext context) {
    Class<?> testClass = context.getTestClass();
    DBUnitConfiguration config = testClass.getAnnotation(DBUnitConfiguration.class);
    return config.type();
}

private String[] getDataLocations(TestContext context) {
    Class<?> testClass = context.getTestClass();
    DBUnitConfiguration config = testClass.getAnnotation(DBUnitConfiguration.class);
    if (config == null) {
        throw new IllegalStateException("Test class '" + testClass + " has is missing @DBUnitConfiguration annotation.");
    }
    if (config.locations().length == 0) {
        throw new IllegalStateException("@DBUnitConfiguration annotation doesn't specify any DBUnit configuration locations.");
    }
    return config.locations();
}
}

Please give a flag where I am missing...!!!