<commit_before>/*
Copyright 2009-2019 <NAME>

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
*/


package org.javalite.activejdbc;

import static org.javalite.activejdbc.test.JdbcProperties.*;

import org.javalite.activejdbc.connection_config.ConnectionJdbcConfig;
import org.javalite.activejdbc.connection_config.ConnectionJndiConfig;
import org.javalite.activejdbc.connection_config.ConnectionConfig;
import org.junit.Test;

import javax.naming.NamingException;
import java.sql.SQLException;
import java.util.Properties;
import static org.javalite.test.jspec.JSpec.a;


/**
 * @author <NAME>
 */
public class ConnectionConfigSpec {

    @Test
    public void testJdbc() {
        ConnectionJdbcConfig spec = new ConnectionJdbcConfig(driver(), url(), user(), password());
        jdbcWithSpec(spec);
    }
    @Test
    public void testJdbcWithProperties() {

        Properties p = new Properties();
        p.setProperty("user", user());
        p.setProperty("password", password());
        ConnectionJdbcConfig spec = new ConnectionJdbcConfig(driver(), url(), p);
        jdbcWithSpec(spec);
    }

    private void jdbcWithSpec(ConnectionConfig spec){
    
        DB db = new DB("default");
        db.open(spec);

        a(db.connection()).shouldNotBeNull();

        db.close();
        DBException e = null;
        try {
            db.connection();
        }
        catch (DBException ex) {
            e = ex;
        }
        a(e).shouldNotBeNull();
    }

    @Test
    public void testJndi() throws NamingException, SQLException {

        ConnectionJndiConfig spec = new ConnectionJndiConfig("java/jdbc/DefaultDS");
        DB db = new DB("default");
        db.open(spec);

        a(db.connection()).shouldNotBeNull();

        db.close();
        DBException e = null;
        try {
            db.connection();
        }
        catch (DBException ex) {
            e = ex;
        }
        a(e).shouldNotBeNull();
    }

    /**
     * This test should only be executed under mysql Maven profile because
     * the database.properties file contains
     */
    @Test
    public void shouldOpenConnectionFromPropertiesFile(){
        DB db = new DB("default");
        db.open();
        a(db.connection()).shouldNotBeNull();
        db.close();
    }
}<commit_msg>#910 Implement ability to open/close DB connections not tied to web requests - fixing test<commit_after>/*
Copyright 2009-2019 <NAME>

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
*/


package org.javalite.activejdbc;

import static org.javalite.activejdbc.test.JdbcProperties.*;

import org.javalite.activejdbc.connection_config.ConnectionJdbcConfig;
import org.javalite.activejdbc.connection_config.ConnectionJndiConfig;
import org.javalite.activejdbc.connection_config.ConnectionConfig;
import org.javalite.activejdbc.connection_config.DbConfiguration;
import org.junit.Test;

import javax.naming.NamingException;
import java.sql.SQLException;
import java.util.Properties;
import static org.javalite.test.jspec.JSpec.a;


/**
 * @author <NAME>
 */
public class ConnectionConfigSpec {

    @Test
    public void testJdbc() {
        ConnectionJdbcConfig spec = new ConnectionJdbcConfig(driver(), url(), user(), password());
        jdbcWithSpec(spec);
    }
    @Test
    public void testJdbcWithProperties() {

        Properties p = new Properties();
        p.setProperty("user", user());
        p.setProperty("password", password());
        ConnectionJdbcConfig spec = new ConnectionJdbcConfig(driver(), url(), p);
        jdbcWithSpec(spec);
    }

    private void jdbcWithSpec(ConnectionConfig spec){
    
        DB db = new DB("default");
        db.open(spec);

        a(db.connection()).shouldNotBeNull();

        db.close();
        DBException e = null;
        try {
            db.connection();
        }
        catch (DBException ex) {
            e = ex;
        }
        a(e).shouldNotBeNull();
    }

    @Test
    public void testJndi()  {

        ConnectionJndiConfig spec = new ConnectionJndiConfig("java/jdbc/DefaultDS");
        DB db = new DB("default");
        db.open(spec);

        a(db.connection()).shouldNotBeNull();

        db.close();
        DBException e = null;
        try {
            db.connection();
        }
        catch (DBException ex) {
            e = ex;
        }
        a(e).shouldNotBeNull();
    }

    /**
     * This test should only be executed under mysql Maven profile because
     * the database.properties file contains
     */
    @Test
    public void shouldOpenConnectionFromPropertiesFile(){
        DbConfiguration.loadConfiguration("/database.properties");
        DB db = new DB("default");
        db.open();
        a(db.connection()).shouldNotBeNull();
        db.close();
    }
}
{'fraction_non_alphanumeric': 0.10437062937062937, 'fraction_numerical': 0.00472027972027972, 'mean_word_length': 2.7812293456708526, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5219428', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1559, 'n_words': 447}