/*
 *    Copyright 2010 The myBatis Team
 *
 *    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.mybatis.guice;

import java.io.File;
import java.io.StringReader;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.jdbc.ScriptRunner;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
import org.mybatis.guice.datasource.builtin.PooledDataSourceProvider;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.name.Names;

/**
 * 
 *
 * @version $Id$
 */
public final class GuiceTestRunner extends BlockJUnit4ClassRunner {

    private final Injector injector;

    public GuiceTestRunner(Class<?> klass) throws InitializationError {
        super(klass);

        try {
            // db URL setup
            File tmp = File.createTempFile("mybatis-guice_TEST", ".dat");
            tmp.delete();
            final String connectionURL = "jdbc:derby:"
                + tmp.getAbsolutePath()
                + ";create=true";

            final Properties myBatisProperties = new Properties();
            myBatisProperties.setProperty("mybatis.environment.id", "test");
            myBatisProperties.setProperty("JDBC.driver", "org.apache.derby.jdbc.EmbeddedDriver");
            myBatisProperties.setProperty("JDBC.url", connectionURL);
            myBatisProperties.setProperty("JDBC.username", "");
            myBatisProperties.setProperty("JDBC.password", "");
            myBatisProperties.setProperty("JDBC.autoCommit", "true");

            final Contact contact = new Contact();
            contact.setFirstName("John");
            contact.setLastName("Doe");

            // bindings
            this.injector = Guice.createInjector(new MyBatisModule(PooledDataSourceProvider.class)
                        .addSimpleAliases(Contact.class)
                        .addMapperClasses(ContactMapper.class),
                    new Module() {
                        public void configure(Binder binder) {
                            Names.bindProperties(binder, myBatisProperties);
                            binder.bind(Contact.class).toInstance(contact);
                        }
                    });

            // prepare the test db
            DataSource dataSource = injector.getInstance(DataSource.class);
            ScriptRunner runner = new ScriptRunner(dataSource.getConnection());
            runner.setAutoCommit(true);
            runner.setStopOnError(true);
            runner.runScript(new StringReader(
                    "CREATE TABLE contact (id INT NOT NULL PRIMARY KEY GENERATED "
                    + "ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), first_name "
                    + "VARCHAR(20) NOT NULL, last_name VARCHAR(20) NOT NULL);"));
            runner.closeConnection();
        } catch (Exception e) {
            throw new RuntimeException("An error occurred when initializing", e);
        }
    }

    @Override
    protected Object createTest() throws Exception {
        return this.injector.getInstance(this.getTestClass().getJavaClass());
    }

}

@@ -974,16 +974,132 @@
 rovider;
+%0Aimport org.mybatis.guice.datasource.helper.JdbcHelper;%0Aimport org.mybatis.guice.datasource.helper.JdbcHelperModule;
 %0A%0Aimport
@@ -1642,137 +1642,8 @@
 e();
-%0A            final String connectionURL = %22jdbc:derby:%22%0A                + tmp.getAbsolutePath()%0A                + %22;create=true%22;
 %0A%0A  
@@ -1836,125 +1836,55 @@
 DBC.
-driver%22, %22org.apache.derby.jdbc.EmbeddedDriver%22);%0A            myBatisProperties.setProperty(%22JDBC.url%22, connectionURL
+schema%22, tmp.getAbsolutePath() + %22;create=true%22
 );%0A 
@@ -2239,16 +2239,16 @@
 indings%0A
-
         
@@ -2292,16 +2292,85 @@
 tor(new 
+JdbcHelperModule(JdbcHelper.Derby_Embedded),%0A                    new 
 MyBatisM

test now uses the helper to build and bind jdbc url and bind the driver class name