package com.proofpoint.dbpool;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

import com.proofpoint.dbpool.H2EmbeddedDataSourceConfig.Cipher;

public class H2EmbeddedDataSourceTest
{
    private File file;

    @BeforeMethod
    public void setup()
            throws IOException
    {
        file = File.createTempFile("h2db-", ".db");
    }

    @AfterMethod
    public void teardown()
    {
        file.delete();
    }

    @Test
    public void test()
            throws Exception
    {
        H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig()
                .setFilename(file.getAbsolutePath())
                .setInitScript("com/proofpoint/dbpool/h2.ddl")
                .setCipher(Cipher.AES)
                .setFilePassword("filePassword");

        H2EmbeddedDataSource dataSource = new H2EmbeddedDataSource(config);
        dataSource.getConnection().createStatement().executeQuery("select * from message");
    }

    @Test(expectedExceptions = FileNotFoundException.class)
    public void testCantFindInitScript()
            throws Exception
    {
        H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig()
                .setFilename(file.getAbsolutePath())
                .setInitScript("foo")
                .setCipher(Cipher.AES)
                .setFilePassword("filePassword");

        H2EmbeddedDataSource dataSource = new H2EmbeddedDataSource(config);
        dataSource.getConnection().createStatement().executeQuery("select * from message");

    }
}

@@ -25,16 +25,57 @@
 bpool;%0A%0A
+import com.google.common.io.ByteStreams;%0A
 import o
@@ -264,27 +264,117 @@
 java.io.
-IOException
+FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream
 ;%0Aimport
@@ -775,16 +775,32 @@
 oid test
+InitFromResource
 ()%0A     
@@ -1279,32 +1279,1991 @@
 ssage%22);%0A    %7D%0A%0A
+    @Test%0A    public void testInitFromFile()%0A        throws Exception%0A    %7B%0A        File initScript = File.createTempFile(%22initscript%22,%22.ddl%22);%0A        try %7B%0A%0A            InputStream in = getClass().getClassLoader().getResourceAsStream(%22com/proofpoint/dbpool/h2.ddl%22);%0A            OutputStream out = new FileOutputStream(initScript);%0A%0A            ByteStreams.copy(in, out);%0A%0A            in.close();%0A            out.close();%0A%0A            H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig()%0A                    .setFilename(file.getAbsolutePath())%0A                    .setInitScript(initScript.getAbsolutePath())%0A                    .setCipher(Cipher.AES)%0A                    .setFilePassword(%22filePassword%22);%0A%0A            H2EmbeddedDataSource dataSource = new H2EmbeddedDataSource(config);%0A            dataSource.getConnection().createStatement().executeQuery(%22select * from message%22);%0A        %7D%0A        finally %7B%0A            initScript.delete();%0A        %7D%0A    %7D%0A%0A    @Test(expectedExceptions = NullPointerException.class)%0A    public void testNullFilenameThrows()%0A            throws Exception%0A    %7B%0A        H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig()%0A                // Filename left as null%0A                .setInitScript(%22com/proofpoint/dbpool/h2.ddl%22)%0A                .setCipher(Cipher.AES)%0A                .setFilePassword(%22filePassword%22);%0A%0A        H2EmbeddedDataSource notConstructed = new H2EmbeddedDataSource(config);%0A    %7D%0A%0A    @Test(expectedExceptions = IllegalArgumentException.class)%0A    public void testEmptyFilenameThrows()%0A        throws Exception%0A    %7B%0A        H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig()%0A                .setFilename(%22%22)%0A                .setInitScript(%22com/proofpoint/dbpool/h2.ddl%22)%0A                .setCipher(Cipher.AES)%0A                .setFilePassword(%22filePassword%22);%0A%0A        H2EmbeddedDataSource notConstructed = new H2EmbeddedDataSource(config);%0A    %7D%0A%0A
     @Test(expect
@@ -3665,34 +3665,38 @@
 dDataSource 
-dataSource
+notConstructed
  = new H2Emb
@@ -3724,101 +3724,8 @@
 g);%0A
-        dataSource.getConnection().createStatement().executeQuery(%22select * from message%22);%0A%0A
     

improve unit test coverage for H2EmbeddedDataSource