Skip to content

Commit

Permalink
Extract methods for constructing H2 JDBC urls to factory class
Browse files Browse the repository at this point in the history
  • Loading branch information
zapodot committed Nov 20, 2015
1 parent 8dc21d8 commit 8ff4f3f
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 42 deletions.
45 changes: 3 additions & 42 deletions src/main/java/org/zapodot/junit/db/EmbeddedDatabaseRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.runners.model.Statement;
import org.zapodot.junit.db.internal.CloseSuppressedConnectionFactory;
import org.zapodot.junit.db.internal.EmbeddedDataSource;
import org.zapodot.junit.db.internal.H2JdbcUrlFactory;

import javax.sql.DataSource;
import java.sql.Connection;
Expand Down Expand Up @@ -53,46 +54,6 @@ public static Builder builder() {
return Builder.instance();
}

private 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 (!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();
}

private static String createH2InMemoryCreateUrl(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("jdbc:h2:mem:")
.append(name)
.append(createJdbcUrlParameterString(properties))
.toString();
}

/**
* Gives access to the current H2 JDBC connection. The connection returned by this method will suppress all "close" calls
*
Expand Down Expand Up @@ -120,7 +81,7 @@ public boolean isAutoCommit() {
* @return a JDBC url string
*/
public String getConnectionJdbcUrl() {
return createH2InMemoryCreateUrl(getInMemoryDatabaseName(), filterInitProperties(_jdbcUrlProperties));
return H2JdbcUrlFactory.buildFilteringInitProperties(getInMemoryDatabaseName(), _jdbcUrlProperties);
}

/**
Expand All @@ -129,7 +90,7 @@ public String getConnectionJdbcUrl() {
* @return a JDBC URL string
*/
private String generateJdbcUrl() {
return createH2InMemoryCreateUrl(getInMemoryDatabaseName(), _jdbcUrlProperties);
return H2JdbcUrlFactory.buildWithNameAndProperties(getInMemoryDatabaseName(), _jdbcUrlProperties);
}

private String getInMemoryDatabaseName() {
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/org/zapodot/junit/db/internal/H2JdbcUrlFactory.java
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));
}
}
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();

}
}

0 comments on commit 8ff4f3f

Please sign in to comment.