-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract methods for constructing H2 JDBC urls to factory class
- Loading branch information
Showing
3 changed files
with
153 additions
and
42 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
61 changes: 61 additions & 0 deletions
61
src/main/java/org/zapodot/junit/db/internal/H2JdbcUrlFactory.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,61 @@ | ||
package org.zapodot.junit.db.internal; | ||
|
||
import org.zapodot.junit.db.EmbeddedDatabaseRule; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author zapodot | ||
*/ | ||
public class H2JdbcUrlFactory { | ||
|
||
private H2JdbcUrlFactory() { | ||
} | ||
|
||
static final String H2_IN_MEMORY_JDBC_URL_PREFIX = "jdbc:h2:mem:"; | ||
|
||
public static Map<String, String> filterInitProperties(final Map<String, String> jdbcUrlProperties) { | ||
if (jdbcUrlProperties == null) { | ||
return null; | ||
} else { | ||
final Map<String, String> propertiesCopy = new LinkedHashMap<>(); | ||
for (final Map.Entry<String, String> property : jdbcUrlProperties.entrySet()) { | ||
if (!EmbeddedDatabaseRule.PROP_INIT_SQL.equalsIgnoreCase(property.getKey())) { | ||
propertiesCopy.put(property.getKey(), property.getValue()); | ||
} | ||
} | ||
return propertiesCopy; | ||
} | ||
} | ||
|
||
private static String createJdbcUrlParameterString(final Map<String, String> properties) { | ||
if (properties == null) { | ||
return ""; | ||
} | ||
final StringBuilder paramStringBuilder = new StringBuilder(""); | ||
for (final Map.Entry<String, String> property : properties.entrySet()) { | ||
if (property.getValue() != null) { | ||
paramStringBuilder.append(';') | ||
.append(property.getKey()) | ||
.append('=') | ||
.append(property.getValue()); | ||
} | ||
} | ||
return paramStringBuilder.toString(); | ||
} | ||
|
||
public static String buildWithNameAndProperties(final String name, final Map<String, String> properties) { | ||
if (name == null) { | ||
throw new NullPointerException("The value of the \"name\" parameter can not be null"); | ||
} | ||
return new StringBuilder(H2_IN_MEMORY_JDBC_URL_PREFIX) | ||
.append(name) | ||
.append(createJdbcUrlParameterString(properties)) | ||
.toString(); | ||
} | ||
|
||
public static String buildFilteringInitProperties(final String name, final Map<String, String> properties) { | ||
return buildWithNameAndProperties(name, filterInitProperties(properties)); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/test/java/org/zapodot/junit/db/internal/H2JdbcUrlFactoryTest.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,89 @@ | ||
package org.zapodot.junit.db.internal; | ||
|
||
import org.junit.Test; | ||
import org.zapodot.junit.db.EmbeddedDatabaseRule; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* @author zapodot | ||
*/ | ||
public class H2JdbcUrlFactoryTest { | ||
|
||
@Test | ||
public void testFilterInitPropertiesNoInit() throws Exception { | ||
Map<String, String> properties = new HashMap<>(); | ||
properties.put("someKey", "somevalue"); | ||
assertEquals(properties, H2JdbcUrlFactory.filterInitProperties(properties)); | ||
} | ||
|
||
@Test | ||
public void testFilterInitPropertiesInit() throws Exception { | ||
final Map<String, String> properties = createPropertyWithInitValue("somevalue"); | ||
assertEquals(Collections.emptyMap(), H2JdbcUrlFactory.filterInitProperties(properties)); | ||
} | ||
|
||
private Map<String, String> createPropertyWithInitValue(final String initPropValue) { | ||
final Map<String, String> properties = new HashMap<>(); | ||
properties.put(EmbeddedDatabaseRule.PROP_INIT_SQL, initPropValue); | ||
return properties; | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void testBuildWithNameNull() throws Exception { | ||
H2JdbcUrlFactory.buildWithNameAndProperties(null, null); | ||
} | ||
|
||
@Test | ||
public void testBuildWitNameAndNoFilter() throws Exception { | ||
final String name = "name"; | ||
final String jdbcUrl = H2JdbcUrlFactory.buildWithNameAndProperties(name, createPropertyWithInitValue("something")); | ||
assertTrue(jdbcUrl.contains(EmbeddedDatabaseRule.PROP_INIT_SQL)); | ||
} | ||
|
||
@Test | ||
public void testBuildWitNameAndFilter() throws Exception { | ||
final String jdbcUrl = H2JdbcUrlFactory.buildFilteringInitProperties("name", createPropertyWithInitValue("something")); | ||
assertFalse(jdbcUrl.contains(EmbeddedDatabaseRule.PROP_INIT_SQL)); | ||
} | ||
|
||
@Test | ||
public void testBuildWithoutFilterAndNullProperties() throws Exception { | ||
final String dbName = "name"; | ||
final String jdbcUrl = H2JdbcUrlFactory.buildWithNameAndProperties(dbName, null); | ||
assertEquals(H2JdbcUrlFactory.H2_IN_MEMORY_JDBC_URL_PREFIX + dbName, jdbcUrl); | ||
|
||
} | ||
|
||
@Test | ||
public void testBuildWithFilterAndNullProperties() throws Exception { | ||
final String dbName = "name"; | ||
final String jdbcUrl = H2JdbcUrlFactory.buildFilteringInitProperties(dbName, null); | ||
assertEquals(H2JdbcUrlFactory.H2_IN_MEMORY_JDBC_URL_PREFIX + dbName, jdbcUrl); | ||
|
||
} | ||
|
||
@Test(expected = IllegalAccessException.class) | ||
public void testInstantiation() throws Exception { | ||
final Constructor<H2JdbcUrlFactory> declaredConstructor = H2JdbcUrlFactory.class.getDeclaredConstructor(); | ||
declaredConstructor.newInstance(); | ||
|
||
} | ||
|
||
/** | ||
* This test is added only to reach 100% test coverage | ||
* @throws Exception | ||
*/ | ||
@Test | ||
public void testFakeInstantiation() throws Exception { | ||
final Constructor<H2JdbcUrlFactory> declaredConstructor = H2JdbcUrlFactory.class.getDeclaredConstructor(); | ||
declaredConstructor.setAccessible(true); | ||
declaredConstructor.newInstance(); | ||
|
||
} | ||
} |