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

Commit

Permalink
[ch07] Implemented ConcurrentHashMapSqlRegistry.
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 d5c2b66 commit 310dd7f
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/main/java/ch07/springbook/sql/OxmSqlService.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public void setSqlMap(Resource sqlMap) {
oxmSqlReader.setSqlMap(sqlMap);
}

public void setSqlRegistry(SqlRegistry sqlRegistry) {
this.sqlRegistry = sqlRegistry;
}

@Override
public String getSql(String key) throws SqlRetrievalFailureException {
return baseSqlService.getSql(key);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ch07.springbook.sql.registry;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapSqlRegistry implements UpdatableSqlRegistry {

private Map<String, String> sqlMap = new ConcurrentHashMap<>();

@Override
public void registerSql(String key, String sql) {
sqlMap.put(key, sql);
}

@Override
public String findSql(String key) throws SqlNotFoundException {
String sql = sqlMap.get(key);

if (sql == null) {
throw new SqlNotFoundException("Can not find appropriate sql statement, key: " + key);
}

return sql;
}

@Override
public void updateSql(String key, String sql) throws SqlUpdateFailureException {

if (sqlMap.get(key) == null) {
throw new SqlUpdateFailureException("Sql not found, key: " + key);
}

sqlMap.put(key, sql);
}

@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
Expand Up @@ -3,8 +3,6 @@
import java.util.HashMap;
import java.util.Map;

import ch07.springbook.sql.SqlRetrievalFailureException;

public class HashMapSqlRegistry implements SqlRegistry {

private Map<String, String> sqlMap = new HashMap<>();
Expand All @@ -19,7 +17,7 @@ public String findSql(String key) throws SqlNotFoundException {
String sql = sqlMap.get(key);

if (sql == null) {
throw new SqlRetrievalFailureException("Can not find appropriate sql statement, key: " + key);
throw new SqlNotFoundException("Can not find appropriate sql statement, key: " + key);
}

return sql;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ch07.springbook.sql.registry;

public class SqlUpdateFailureException extends RuntimeException {

public SqlUpdateFailureException(String message) {
super(message);
}

public SqlUpdateFailureException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ch07.springbook.sql.registry;

import java.util.Map;

public interface UpdatableSqlRegistry extends SqlRegistry {
void updateSql(String key, String sql) throws SqlUpdateFailureException;
void updateSql(Map<String, String> sqlMap) throws SqlUpdateFailureException;
}
5 changes: 5 additions & 0 deletions src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@
<bean id="sqlService" class="ch07.springbook.sql.OxmSqlService" >
<property name="unmarshaller" ref="unmarshaller" />
<property name="sqlMap" value="classpath:sql/sqlmap.xml" />
<property name="sqlRegistry" ref="sqlRegistry" />
</bean>

<bean id="unmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="ch07.springbook.sql.reader.jaxb" />
</bean>

<bean id="sqlRegistry" class="ch07.springbook.sql.registry.ConcurrentHashMapSqlRegistry">
</bean>

<!--
<bean id="sqlService" class="ch07.springbook.sql.SimpleSqlService">
<property name="sqlMap">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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");
}

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));
}
}

0 comments on commit 310dd7f

Please sign in to comment.