protected final void setupData(InputStream... dataSetStream) { try { IDataSet[] dataSets = new IDataSet[dataSetStream.length]; for (int i = 0; i < dataSetStream.length; i++) { ReplacementDataSet dataSet = new ReplacementDataSet(new FlatXmlDataSet(dataSetStream[i])); dataSet.addReplacementObject("[null]", null); dataSets[i] = dataSet; } CompositeDataSet compositeDataSet = new CompositeDataSet(dataSets); databaseTester.setDataSet(compositeDataSet); connection = databaseTester.getConnection(); connection .getConnection() .prepareStatement("set referential_integrity FALSE") .execute(); // HSQL DB DatabaseConfig config = connection.getConfig(); config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory()); DatabaseOperation.CLEAN_INSERT.execute(connection, databaseTester.getDataSet()); connection .getConnection() .prepareStatement("set referential_integrity TRUE") .execute(); // HSQL DB } catch (Exception e) { throw translateException("Could not setup DBUnit data", e); } }
protected void dbUnitSetup() { List<EntityManager> entityManagerList = BeanProvider.getContextualReferences(EntityManager.class, true, false); for (EntityManager entityManager : entityManagerList) { if (entityManager.getTransaction().isActive()) { em = entityManager; break; } } if (em == null) { throw new IllegalStateException("Entity Manager == null"); } Connection conn = DbConnectionProvider.unwrap(em); if (conn == null) { throw new IllegalStateException("Connection == null"); } this.connection = conn; try { databaseTester = new JdbcConnectionDatabaseTester(connection); databaseTester.setOperationListener(new NoOpOperationListener()); dbUnitConnection = databaseTester.getConnection(); dbUnitRunner = new DbUnitRunner(dbUnitConnection); } catch (Exception e) { throw new RuntimeException(e); } }
@Before public void setUp() throws Exception { IDataSet dataSet = new FlatXmlDataSetBuilder().build(new File("src/test/resources/CustomerTest.xml")); databaseTester.setDataSet(dataSet); databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT); databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL); databaseTester.onSetup(); }
@BeforeClass public static void setUpBeforeClass() throws Exception { // NOTE: since you also have a physical database you may not want to load data in the database, // but, you may as well final Properties environment = new Properties(); databaseTester = new JndiDatabaseTester( environment, GeneralJpaSpringConfig.JNDI_DATASOURCE_LOOKUP_NAME, SCHEMA_NULL); final IDataSet dataSet = new FlatXmlDataSetBuilder().build(PaymentProcessorIT.class.getResource("/orders_data.xml")); databaseTester.setDataSet(dataSet); databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT); databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL); }
public void onSetUp() throws Exception { databaseTester.onSetup(); pipelineTimeline.clearWhichIsEvilAndShouldNotBeUsedInRealWorld(); if (sqlMapClient != null) { sqlMapClient.flushDataCache(); } }
@Test public void testCrud() throws Exception { databaseTester.onTearDown(); // crear db before tests // CREATE Assert.assertEquals(0, customerService.count()); Customer customer = new Customer(); customer.setName("Name"); customer.setEmail("*****@*****.**"); customer.setAddress("address"); customer.setPhone("111333444"); customerService.add(customer); Assert.assertEquals(1, customerService.count()); // READ Customer readcustomer = customerService.list().get(0); Assert.assertEquals(readcustomer.getName(), "Name"); Assert.assertEquals(readcustomer.getEmail(), "*****@*****.**"); Assert.assertEquals(readcustomer.getAddress(), "address"); Assert.assertEquals(readcustomer.getPhone(), "111333444"); // UPDATE readcustomer.setName("updatedName"); customerService.update(readcustomer); Customer updatedCustomer = customerService.list().get(0); Assert.assertEquals(updatedCustomer.getName(), "updatedName"); // DELETE customerService.delete(updatedCustomer.getId()); Assert.assertEquals(0, customerService.count()); }
@AroundInvoke public Object execute(InvocationContext invocationContext) throws Exception { // Execute following logic only if @Test method Test testAnnotation = invocationContext.getMethod().getAnnotation(Test.class); if (testAnnotation == null) { return invocationContext.proceed(); } dbUnitSetup(); DbUnitTestContext testContext = new DbUnitTestContext(); Object returnValue = null; Class<?> testClass = ProxyUtils.getUnproxiedClass(invocationContext.getTarget().getClass()); testContext.setTestClass(testClass); testContext.setTestInstance(invocationContext.getTarget()); testContext.setTestMethod(invocationContext.getMethod()); DbUnitConfiguration dbUnitConfiguration = testClass.getAnnotation(DbUnitConfiguration.class); try { dbUnitRunner.prepareTesterSetup(testContext, dbUnitConfiguration, databaseTester); // try { databaseTester.onSetup(); // } catch (Throwable e) { // do not propagate db setup exception or transaction // will not rollback // e.printStackTrace(); // } returnValue = invocationContext.proceed(); dbUnitRunner.verifyExpected(testContext); } finally { dbUnitRunner.prepareTesterTearDown(testContext, dbUnitConfiguration, databaseTester); databaseTester.onTearDown(); try { if (connection != null) { connection.close(); } } catch (Exception e) { connection = null; } } return returnValue; }
public void beforeTestMethod(TestContext testCtx) throws Exception { // Check for existence of DataSets annotation for the method under testing DataSets dataSetAnnotation = testCtx.getTestMethod().getAnnotation(DataSets.class); if (dataSetAnnotation == null) { return; } String dataSetName = dataSetAnnotation.setUpDataSet(); // Populate data set if data set name exists if (!dataSetName.equals("")) { databaseTester = (IDatabaseTester) testCtx.getApplicationContext().getBean("databaseTester"); XlsDataFileLoader xlsDataFileLoader = (XlsDataFileLoader) testCtx.getApplicationContext().getBean("xlsDataFileLoader"); IDataSet dataSet = xlsDataFileLoader.load(dataSetName); databaseTester.setDataSet(dataSet); databaseTester.onSetup(); } }
private void initialize(DataSource dataSource) throws AmbiguousTableNameException { databaseTester = new DataSourceDatabaseTester(dataSource); databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT); databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL); DefaultDataSet dataSet = new DefaultDataSet(); dataSet.addTable(new DefaultTable("agents")); dataSet.addTable(new DefaultTable("pipelines")); dataSet.addTable(new DefaultTable("materials")); dataSet.addTable(new DefaultTable("modifications")); dataSet.addTable(new DefaultTable("pipelineMaterialRevisions")); dataSet.addTable(new DefaultTable("modifiedFiles")); dataSet.addTable(new DefaultTable("notificationfilters")); dataSet.addTable(new DefaultTable("users")); dataSet.addTable(new DefaultTable("artifactPropertiesGenerator")); dataSet.addTable(new DefaultTable("stages")); dataSet.addTable(new DefaultTable("buildCauseBuffer")); dataSet.addTable(new DefaultTable("pipelineLabelCounts")); dataSet.addTable(new DefaultTable("environmentVariables")); dataSet.addTable(new DefaultTable("properties")); dataSet.addTable(new DefaultTable("artifactPlans")); dataSet.addTable(new DefaultTable("buildStateTransitions")); dataSet.addTable(new DefaultTable("resources")); dataSet.addTable(new DefaultTable("builds")); dataSet.addTable(new DefaultTable("oauthclients")); dataSet.addTable(new DefaultTable("oauthauthorizations")); dataSet.addTable(new DefaultTable("oauthtokens")); dataSet.addTable(new DefaultTable("gadgetOauthClients")); dataSet.addTable(new DefaultTable("gadgetOauthAuthorizationCodes")); dataSet.addTable(new DefaultTable("gadgetOauthAccessTokens")); dataSet.addTable(new DefaultTable("stageArtifactCleanupProhibited")); dataSet.addTable(new DefaultTable("serverBackups")); databaseTester.setDataSet(dataSet); }
@Before public final void startDatabase() throws Exception { if (dbConnector == null) { dbConnector = new MemoryDatabaseConnector(); dbConnector.start(); session = new JpaDatabaseSession(dbConnector); session.start(); } databaseTester = new JdbcDatabaseTester( MemoryDatabaseConnector.DRIVER, MemoryDatabaseConnector.URL, MemoryDatabaseConnector.USER, MemoryDatabaseConnector.PASSWORD); databaseTester.onTearDown(); }
public void onTearDown() throws Exception { databaseTester.onTearDown(); goCache.clear(); }
public void afterTestMethod(TestContext arg0) throws Exception { // Clear up testing data if exists if (databaseTester != null) { databaseTester.onTearDown(); } }
/** Get a connection to the test database; subclasses may override. */ protected IDatabaseConnection getConnection(final IDatabaseTester databaseTester) throws Exception { return databaseTester.getConnection(); }
@After public void tearDownAfterTest() throws Exception { databaseTester.onTearDown(); }
@Before public void setUpBeforeTest() throws Exception { databaseTester.onSetup(); }
@After public void cleanup() throws Exception { databaseTester.onTearDown(); }