Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch07] EmbeddedDbSqlRegistry Test.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 authored and Dongho Sim committed Oct 28, 2017
1 parent 8972740 commit 869443f
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ch07.springbook.sql.registry;

import java.util.Map;

import javax.sql.DataSource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

public class EmbeddedDbSqlRegistry implements UpdatableSqlRegistry {

private JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}

@Override
public void registerSql(String key, String sql) {
jdbcTemplate.update("INSERT INTO SQLMAP(KEY_, SQL_) values(?, ?)", key, sql);
}

@Override
public String findSql(String key) throws SqlNotFoundException {
try {
return jdbcTemplate.queryForObject("SELECT SQL_ FROM SQLMAP WHERE KEY_ = ?",
String.class, key);
} catch (EmptyResultDataAccessException e) {
throw new SqlNotFoundException("Can not find appropriate sql statement, key: " + key);
}
}

@Override
public void updateSql(String key, String sql) throws SqlUpdateFailureException {
int count = jdbcTemplate.update("UPDATE SQLMAP SET SQL_ = ? WHERE KEY_= ?", sql, key);

if (count == 0) {
throw new SqlUpdateFailureException("Sql not found, key: " + key);
}
}

@Override
public void updateSql(Map<String, String> sqlMap) throws SqlUpdateFailureException {
for (Map.Entry<String, String> entry : sqlMap.entrySet()) {
updateSql(entry.getKey(), entry.getValue());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ch07.springbook;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import ch07.springbook.sql.registry.ConcurrentHashMapSqlRegistry;
import ch07.springbook.sql.registry.SqlNotFoundException;
import ch07.springbook.sql.registry.SqlUpdateFailureException;
import ch07.springbook.sql.registry.UpdatableSqlRegistry;

public abstract class AbstractUpdatableSqlRegistryTest {

private UpdatableSqlRegistry sqlRegistry;

@Before
public void setUp() {
sqlRegistry = createUpdatableSqlRegistry();
}

abstract protected UpdatableSqlRegistry createUpdatableSqlRegistry();

@Before
public void init() {
sqlRegistry = new ConcurrentHashMapSqlRegistry();
sqlRegistry.registerSql("KEY1", "SQL1");
sqlRegistry.registerSql("KEY2", "SQL2");
sqlRegistry.registerSql("KEY3", "SQL3");
}

@Test
public void find() {
checkFindResult("SQL1", "SQL2", "SQL3");
}

@Test(expected = SqlNotFoundException.class)
public void unknownKey(){
sqlRegistry.findSql("SQL9999!@#");
}

@Test
public void updateSingle() {
sqlRegistry.updateSql("KEY2", "Modified2");
checkFindResult("SQL1", "Modified2", "SQL3");
}

@Test
public void updateMulti() {
Map<String, String> sqlMap = new HashMap<>();
sqlMap.put("KEY1", "Modified1");
sqlMap.put("KEY3", "Modified3");

sqlRegistry.updateSql(sqlMap);
checkFindResult("Modified1", "SQL2", "Modified3");
}

@Test(expected = SqlUpdateFailureException.class)
public void updateWithNotExistingKey() {
sqlRegistry.updateSql("SQL9999!@#", "Modified");
}

protected void checkFindResult(String expected1, String expected2, String expected3) {
assertThat(sqlRegistry.findSql("KEY1"), is(expected1));
assertThat(sqlRegistry.findSql("KEY2"), is(expected2));
assertThat(sqlRegistry.findSql("KEY3"), is(expected3));
}
}
Original file line number Diff line number Diff line change
@@ -1,65 +1,13 @@
package ch07.springbook;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import ch07.springbook.sql.registry.ConcurrentHashMapSqlRegistry;
import ch07.springbook.sql.registry.SqlNotFoundException;
import ch07.springbook.sql.registry.SqlUpdateFailureException;
import ch07.springbook.sql.registry.UpdatableSqlRegistry;

public class ConcurrentHashMapSqlRegistryTest {
private UpdatableSqlRegistry sqlRegistry;

@Before
public void init() {
sqlRegistry = new ConcurrentHashMapSqlRegistry();
sqlRegistry.registerSql("KEY1", "SQL1");
sqlRegistry.registerSql("KEY2", "SQL2");
sqlRegistry.registerSql("KEY3", "SQL3");
}

@Test
public void find() {
checkFindResult("SQL1", "SQL2", "SQL3");
}

@Test(expected = SqlNotFoundException.class)
public void unknownKey(){
sqlRegistry.findSql("SQL9999!@#");
}

@Test
public void updateSingle() {
sqlRegistry.updateSql("KEY2", "Modified2");
checkFindResult("SQL1", "Modified2", "SQL3");
}

@Test
public void updateMulti() {
Map<String, String> sqlMap = new HashMap<>();
sqlMap.put("KEY1", "Modified1");
sqlMap.put("KEY3", "Modified3");

sqlRegistry.updateSql(sqlMap);
checkFindResult("Modified1", "SQL2", "Modified3");
}

@Test(expected = SqlUpdateFailureException.class)
public void updateWithNotExistingKey() {
sqlRegistry.updateSql("SQL9999!@#", "Modified");
}
public class ConcurrentHashMapSqlRegistryTest extends AbstractUpdatableSqlRegistryTest {

private void checkFindResult(String expected1, String expected2, String expected3) {
assertThat(sqlRegistry.findSql("KEY1"), is(expected1));
assertThat(sqlRegistry.findSql("KEY2"), is(expected2));
assertThat(sqlRegistry.findSql("KEY3"), is(expected3));
@Override
protected UpdatableSqlRegistry createUpdatableSqlRegistry() {
return new ConcurrentHashMapSqlRegistry();
}
}

31 changes: 31 additions & 0 deletions src/test/java/ch07/springbook/EmbeddedDbSqlRegistryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ch07.springbook;

import org.junit.After;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import ch07.springbook.sql.registry.EmbeddedDbSqlRegistry;
import ch07.springbook.sql.registry.UpdatableSqlRegistry;

public class EmbeddedDbSqlRegistryTest extends AbstractUpdatableSqlRegistryTest {

private EmbeddedDatabase db;

@Override
protected UpdatableSqlRegistry createUpdatableSqlRegistry() {
db = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:sql/embedded/schema.sql")
.build();

EmbeddedDbSqlRegistry embeddedDbSqlRegistry = new EmbeddedDbSqlRegistry();
embeddedDbSqlRegistry.setDataSource(db);

return embeddedDbSqlRegistry;
}

@After
public void tearDown() {
db.shutdown();
}
}

0 comments on commit 869443f

Please sign in to comment.