All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zapodot.junit.db.internal.H2JdbcUrlFactory Maven / Gradle / Ivy

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 filterInitProperties(final Map jdbcUrlProperties) {
        if (jdbcUrlProperties == null) {
            return null;
        } else {
            final Map propertiesCopy = new LinkedHashMap<>();
            for (final Map.Entry 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 properties) {
        if (properties == null) {
            return "";
        }
        final StringBuilder paramStringBuilder = new StringBuilder("");
        for (final Map.Entry 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 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 properties) {
        return buildWithNameAndProperties(name, filterInitProperties(properties));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy