com.vladmihalcea.sql.SQLStatementCountValidator Java Examples

The following examples show how to use com.vladmihalcea.sql.SQLStatementCountValidator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomerControllerITTest.java    From POC with Apache License 2.0 5 votes vote down vote up
/**
 * Convenience method for testing that gives us the customer id based on test
 * customers name. Need this as IDs will increment as tests are rerun
 * @param firstName the customer firstName.
 * @return customer Id
 */
private Long getCustomerIdByFirstName(String firstName) {
	SQLStatementCountValidator.reset();
	long customerId = this.customerRepository.findOptionalIdByFirstName(firstName).orElse(0L);
	SQLStatementCountValidator.assertInsertCount(0);
	SQLStatementCountValidator.assertUpdateCount(0);
	SQLStatementCountValidator.assertDeleteCount(0);
	SQLStatementCountValidator.assertSelectCount(1);
	return customerId;
}
 
Example #2
Source File: HibernateSQLStatementCountTest.java    From vladmihalcea.wordpress.com with Apache License 2.0 4 votes vote down vote up
@Before
public void beforeTest() {
    CleanDbUtil.cleanStore(transactionTemplate, entityManager);
    SQLStatementCountValidator.reset();
}
 
Example #3
Source File: HibernateSQLStatementCountTest.java    From vladmihalcea.wordpress.com with Apache License 2.0 4 votes vote down vote up
@Test
public void test() {

    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {

            Company company = new Company();
            company.setName("TV Company");
            entityManager.persist(company);

            Product product1 = new Product("tvCode");
            product1.setName("TV");
            product1.setCompany(company);

            WarehouseProductInfo warehouseProductInfo1 = new WarehouseProductInfo();
            warehouseProductInfo1.setQuantity(101);
            product1.addWarehouse(warehouseProductInfo1);

            Product product2 = new Product("cdCode");
            product2.setName("CD");
            product2.setCompany(company);

            WarehouseProductInfo warehouseProductInfo2 = new WarehouseProductInfo();
            warehouseProductInfo2.setQuantity(50);
            product2.addWarehouse(warehouseProductInfo2);

            entityManager.persist(product1);
            entityManager.persist(product2);
            entityManager.flush();

            final JdbcTemplate otherDataSourceJdbcTemplate = new JdbcTemplate(otherDataSource);
            List<Map<String, Object>> versions = otherDataSourceJdbcTemplate.queryForList(" select * from version ");
            assertTrue(versions.isEmpty());
            return null;
        }
    });

    try {
        SQLStatementCountValidator.reset();
        warehouseProductInfoService.findAllWithNPlusOne();
        SQLStatementCountValidator.assertSelectCount(1);
    } catch (SQLSelectCountMismatchException e) {
        assertEquals(3, e.getRecorded());
    }

    SQLStatementCountValidator.reset();
    warehouseProductInfoService.findAllWithFetch();
    SQLStatementCountValidator.assertSelectCount(1);

    SQLStatementCountValidator.reset();
    warehouseProductInfoService.newWarehouseProductInfo();
    SQLStatementCountValidator.assertSelectCount(1);
    SQLStatementCountValidator.assertInsertCount(2);
}
 
Example #4
Source File: LazyLoadNoTransPropertyOffIntegrationTest.java    From tutorials with MIT License 3 votes vote down vote up
@Test
public void whenCallTransactionalMethodWithPropertyOff_thenTestPass() {
    SQLStatementCountValidator.reset();

    long docsCount = serviceLayer.countAllDocsTransactional();

    assertEquals(EXPECTED_DOCS_COLLECTION_SIZE, docsCount);

    SQLStatementCountValidator.assertSelectCount(2);
}
 
Example #5
Source File: LazyLoadNoTransPropertyOnIntegrationTest.java    From tutorials with MIT License 3 votes vote down vote up
@Test
public void whenCallNonTransactionalMethodWithPropertyOn_thenGetNplusOne() {
    SQLStatementCountValidator.reset();

    long docsCount = serviceLayer.countAllDocsNonTransactional();

    assertEquals(EXPECTED_DOCS_COLLECTION_SIZE, docsCount);

    SQLStatementCountValidator.assertSelectCount(EXPECTED_USERS_COUNT + 1);
}
 
Example #6
Source File: LazyLoadNoTransPropertyOnIntegrationTest.java    From tutorials with MIT License 3 votes vote down vote up
@Test
public void whenCallTransactionalMethodWithPropertyOn_thenTestPass() {
    SQLStatementCountValidator.reset();

    long docsCount = serviceLayer.countAllDocsTransactional();

    assertEquals(EXPECTED_DOCS_COLLECTION_SIZE, docsCount);

    SQLStatementCountValidator.assertSelectCount(2);
}