/*
 * Copyright (c) 2015 Evident Solutions Oy
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.dalesbred.junit;

import org.dalesbred.Database;
import org.dalesbred.connection.ConnectionProvider;
import org.dalesbred.connection.DriverManagerConnectionProvider;
import org.dalesbred.dialect.Dialect;
import org.dalesbred.transaction.TransactionCallback;
import org.jetbrains.annotations.NotNull;
import org.junit.internal.AssumptionViolatedException;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * <p>
 *   Helper methods for creating databases using a properties-file in classpath.
 * </p>
 * <p>
 *   The properties should have the following keys:
 * </p>
 * <table
 *  <tr><td>jdbc.url</td><td>required</td><td>JDBC url of the database to connect</td></tr>
 *  <tr><td>jdbc.login</td><td>optional</td><td>login for the database</td></th></tr>
 *  <tr><td>jdbc.password</td><td>optional</td><td>password for the database</td></th></tr>
 * </table>
 */
public final class TestDatabaseProvider {

    private TestDatabaseProvider() { }

    /**
     * Loads database with given properties.
     *
     * @throws AssumptionViolatedException if properties were not found
     */
    @NotNull
    public static Database databaseForProperties(@NotNull String propertiesPath) {
        return new Database(createConnectionProvider(propertiesPath));
    }

    /**
     * Loads database with given properties, using specified dialect.
     *
     * @throws AssumptionViolatedException if properties were not found
     */
    @NotNull
    public static Database databaseForProperties(@NotNull String propertiesPath, @NotNull Dialect dialect) {
        return new Database(createConnectionProvider(propertiesPath), dialect);
    }

    @NotNull
    private static ConnectionProvider createConnectionProvider(@NotNull String propertiesPath) {
        Properties props = loadConnectionProperties(propertiesPath);

        String url = props.getProperty("jdbc.url");
        if (url == null)
            throw new RuntimeException("Could not find 'jdbc.url' in '" + propertiesPath + "'.");

        String login = props.getProperty("jdbc.login");
        String password = props.getProperty("jdbc.password");

        return new DriverManagerConnectionProvider(url, login, password);
    }

    @NotNull
    @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
    private static Properties loadConnectionProperties(@NotNull String propertiesName) {
        InputStream in = TransactionCallback.class.getClassLoader().getResourceAsStream(propertiesName);
        if (in == null)
            throw new AssumptionViolatedException("Could not find database configuration file '" + propertiesName + "'.");
        try {
            try {
                Properties properties = new Properties();
                properties.load(in);
                return properties;
            } finally {
                in.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -1573,16 +1573,60 @@
 rties;%0A%0A
+import static org.junit.Assume.assumeTrue;%0A%0A
 /**%0A * %3C
@@ -3424,68 +3424,8 @@
 ull%0A
-    @SuppressWarnings(%22IOResourceOpenedButNotSafelyClosed%22)%0A
     
@@ -3626,73 +3626,18 @@
     
-if (in == null)%0A            throw new AssumptionViolatedException
+assumeTrue
 (%22Co
@@ -3703,17 +3703,56 @@
 e + %22'.%22
-)
+, in != null);%0A        assert in != null
 ;%0A      

Remove reference to deprecated AssumptionViolatedException