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.
[ch07] Implemented ConcurrentHashMapSqlRegistry.
- Loading branch information
Showing
7 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
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
42 changes: 42 additions & 0 deletions
42
src/main/java/ch07/springbook/sql/registry/ConcurrentHashMapSqlRegistry.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,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()); | ||
} | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/main/java/ch07/springbook/sql/registry/SqlUpdateFailureException.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,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); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/ch07/springbook/sql/registry/UpdatableSqlRegistry.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,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; | ||
} |
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
65 changes: 65 additions & 0 deletions
65
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 |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
|