Example usage for org.springframework.transaction.support TransactionCallbackWithoutResult TransactionCallbackWithoutResult

List of usage examples for org.springframework.transaction.support TransactionCallbackWithoutResult TransactionCallbackWithoutResult

Introduction

In this page you can find the example usage for org.springframework.transaction.support TransactionCallbackWithoutResult TransactionCallbackWithoutResult.

Prototype

TransactionCallbackWithoutResult

Source Link

Usage

From source file:org.springframework.test.context.jdbc.DatabaseInitializerTestExecutionListener.java

/**
 * Execute the SQL scripts configured via the supplied
 * {@link DatabaseInitializer @DatabaseInitializer} for the given
 * {@link ExecutionPhase} and {@link TestContext}.
 *
 * <p>Special care must be taken in order to properly support the
 * {@link DatabaseInitializer#requireNewTransaction requireNewTransaction}
 * flag.//from ww w .  j  a v a 2 s. c  o  m
 *
 * @param databaseInitializer the {@code @DatabaseInitializer} to parse
 * @param executionPhase the current execution phase
 * @param testContext the current {@code TestContext}
 * @param classLevel {@code true} if {@link DatabaseInitializer @DatabaseInitializer}
 * was declared at the class level
 */
@SuppressWarnings("serial")
private void executeDatabaseInitializer(DatabaseInitializer databaseInitializer, ExecutionPhase executionPhase,
        TestContext testContext, boolean classLevel) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Processing %s for execution phase [%s] and test context %s.",
                databaseInitializer, executionPhase, testContext));
    }

    if (executionPhase != databaseInitializer.executionPhase()) {
        return;
    }

    final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.setSqlScriptEncoding(databaseInitializer.encoding());
    populator.setSeparator(databaseInitializer.separator());
    populator.setCommentPrefix(databaseInitializer.commentPrefix());
    populator.setBlockCommentStartDelimiter(databaseInitializer.blockCommentStartDelimiter());
    populator.setBlockCommentEndDelimiter(databaseInitializer.blockCommentEndDelimiter());
    populator.setContinueOnError(databaseInitializer.continueOnError());
    populator.setIgnoreFailedDrops(databaseInitializer.ignoreFailedDrops());

    String[] scripts = getScripts(databaseInitializer, testContext, classLevel);
    scripts = TestContextResourceUtils.convertToClasspathResourcePaths(testContext.getTestClass(), scripts);
    populator.setScripts(
            TestContextResourceUtils.convertToResources(testContext.getApplicationContext(), scripts));
    if (logger.isDebugEnabled()) {
        logger.debug("Executing SQL scripts: " + ObjectUtils.nullSafeToString(scripts));
    }

    final DataSource dataSource = TestContextTransactionUtils.retrieveDataSource(testContext,
            databaseInitializer.dataSource());
    final PlatformTransactionManager transactionManager = TestContextTransactionUtils
            .retrieveTransactionManager(testContext, databaseInitializer.transactionManager());

    int propagation = databaseInitializer.requireNewTransaction()
            ? TransactionDefinition.PROPAGATION_REQUIRES_NEW
            : TransactionDefinition.PROPAGATION_REQUIRED;

    TransactionAttribute transactionAttribute = TestContextTransactionUtils
            .createDelegatingTransactionAttribute(testContext, new DefaultTransactionAttribute(propagation));

    new TransactionTemplate(transactionManager, transactionAttribute)
            .execute(new TransactionCallbackWithoutResult() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    populator.execute(dataSource);
                };
            });
}

From source file:org.springframework.ws.samples.airline.dao.jpa.DatabaseInitializer.java

@PostConstruct
public void initDatabase() {
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {
        protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
            logger.info("Initializing Database");
            createAirports();//w  w w.  ja  v a  2  s . c o m
            createFlights();
            createFrequentFlyers();
        }
    });
}

From source file:org.yes.cart.service.dto.impl.DtoContentServiceImplTezt.java

@Test
public void testGetAll() throws Exception {

    getTx().execute(new TransactionCallbackWithoutResult() {
        public void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                List<CategoryDTO> list = dtoService.getAllByShopId(20L);
                assertFalse(list.isEmpty());
            } catch (Exception e) {
                assertTrue(e.getMessage(), false);

            }//from   w  w w.java  2 s.c o  m

            status.setRollbackOnly();
        }
    });

}

From source file:org.yes.cart.service.dto.impl.DtoContentServiceImplTezt.java

@Test
public void testGetAllWithAvailabilityFilter() throws Exception {

    getTx().execute(new TransactionCallbackWithoutResult() {
        public void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                List<CategoryDTO> list = dtoService.getAllWithAvailabilityFilter(20L, true);
                assertFalse(list.isEmpty());
                assertFalse(isCategoryPresent(list, 10110L)); //content 2008
                assertFalse(isCategoryPresent(list, 10111L)); //content 2108
            } catch (Exception e) {
                assertTrue(e.getMessage(), false);

            }/*from   www.  ja  v a 2s.  co  m*/

            status.setRollbackOnly();
        }
    });

}

From source file:org.yes.cart.service.dto.impl.DtoContentServiceImplTezt.java

@Test
public void testCreate() throws Exception {

    getTx().execute(new TransactionCallbackWithoutResult() {
        public void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                CategoryDTO dto = getDto();
                dto = dtoService.create(dto);
                assertTrue(dto.getCategoryId() > 0);
            } catch (Exception e) {
                assertTrue(e.getMessage(), false);

            }/*from w ww. j  a  va  2 s .c o  m*/

            status.setRollbackOnly();
        }
    });

}

From source file:org.yes.cart.service.dto.impl.DtoContentServiceImplTezt.java

@Test
public void testUpdate() throws Exception {

    getTx().execute(new TransactionCallbackWithoutResult() {
        public void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                CategoryDTO dto = getDto();
                dto = dtoService.create(dto);
                assertTrue(dto.getCategoryId() > 0);
                dto.setDescription("description");
                dto = dtoService.update(dto);
                assertEquals("description", dto.getDescription());
            } catch (Exception e) {
                assertTrue(e.getMessage(), false);

            }/*from w  w w. java2s  . c  om*/

            status.setRollbackOnly();
        }
    });

}

From source file:org.yes.cart.service.dto.impl.DtoContentServiceImplTezt.java

@Test
public void testRemove() throws Exception {

    getTx().execute(new TransactionCallbackWithoutResult() {
        public void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                CategoryDTO dto = getDto();
                dto = dtoService.create(dto);
                assertTrue(dto.getCategoryId() > 0);
                long id = dto.getCategoryId();
                dtoService.remove(id);/*from  ww w. j  a  v  a 2 s  . co m*/
                dto = dtoService.getById(id);
                assertNull(dto);
            } catch (Exception e) {
                fail(e.getMessage());

            }

            status.setRollbackOnly();
        }
    });

}

From source file:org.yes.cart.service.dto.impl.DtoContentServiceImplTezt.java

@Test
public void testGetAllByShopIdNoContent() throws Exception {

    getTx().execute(new TransactionCallbackWithoutResult() {
        public void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                List<CategoryDTO> list = dtoService.getAllByShopId(50L);
                assertNull(list); // No Content Root
            } catch (Exception e) {

                fail(ExceptionUtils.getStackTrace(e));

            }/*  w ww .ja v a2 s .c  o m*/

            status.setRollbackOnly();
        }
    });

}

From source file:org.yes.cart.service.dto.impl.DtoContentServiceImplTezt.java

@Test
public void testGetAllByShopIdWithCreate() throws Exception {
    clearCache();/*from   w  ww . ja  va  2 s  .  co  m*/
    getTx().execute(new TransactionCallbackWithoutResult() {
        public void doInTransactionWithoutResult(TransactionStatus status) {
            try {
                dtoService.createContentRoot(50L);

                List<CategoryDTO> list = dtoService.getAllByShopId(50L);
                assertNotNull(list);
                assertFalse(list.isEmpty());
                assertEquals(1, list.size());
            } catch (Exception e) {
                fail(e.getMessage());

            }

            status.setRollbackOnly();
        }
    });

}

From source file:org.yes.cart.service.dto.impl.DtoContentServiceImplTezt.java

@Test
public void testDeleteAttributeValue() throws Exception {

    final List<? extends AttrValueDTO> list = dtoService.getEntityAttributes(10105L);
    final int totalSize = list.size();

    getTx().execute(new TransactionCallbackWithoutResult() {
        public void doInTransactionWithoutResult(TransactionStatus status) {
            try {

                for (int i = 0; i < totalSize; i++) {
                    AttrValueDTO dto = list.get(i);
                    if (dto.getVal() != null) {
                        dtoService.deleteAttributeValue(dto.getAttrvalueId());
                    }/*from   www .  j a va  2 s.  c  o m*/
                }

            } catch (Exception e) {
                e.printStackTrace();
                fail(e.getMessage());

            }

            //status.setRollbackOnly();
        }
    });
    clearCache();

    final List<? extends AttrValueDTO> list2 = dtoService.getEntityAttributes(10105L);
    //assertEquals(totalSize, list2.size());
    for (AttrValueDTO dto : list2) {
        assertNull(dto.getVal());
    }

}