This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
155 additions
and
56 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/ch07/springbook/sql/registry/EmbeddedDbSqlRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/test/java/ch07/springbook/AbstractUpdatableSqlRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
60 changes: 4 additions & 56 deletions
60
src/test/java/ch07/springbook/ConcurrentHashMapSqlRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
src/test/java/ch07/springbook/EmbeddedDbSqlRegistryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |