package com.proofpoint.dbpool;

import com.google.common.io.ByteStreams;
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.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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 testInitFromResource()
            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
    public void testInitFromFile()
        throws Exception
    {
        File initScript = File.createTempFile("initscript",".ddl");
        try {

            InputStream in = getClass().getClassLoader().getResourceAsStream("com/proofpoint/dbpool/h2.ddl");
            OutputStream out = new FileOutputStream(initScript);

            ByteStreams.copy(in, out);

            in.close();
            out.close();

            H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig()
                    .setFilename(file.getAbsolutePath())
                    .setInitScript(initScript.getAbsolutePath())
                    .setCipher(Cipher.AES)
                    .setFilePassword("filePassword");

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

    @Test(expectedExceptions = NullPointerException.class)
    public void testNullFilenameThrows()
            throws Exception
    {
        H2EmbeddedDataSourceConfig config = new H2EmbeddedDataSourceConfig()
                // Filename left as null
                .setInitScript("com/proofpoint/dbpool/h2.ddl")
                .setCipher(Cipher.AES)
                .setFilePassword("filePassword");

        new H2EmbeddedDataSource(config);
    }

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

        new H2EmbeddedDataSource(config);
    }

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

        new H2EmbeddedDataSource(config);
    }
}

@@ -54,22 +54,173 @@
 mon.
-io.ByteStreams
+base.Charsets;%0Aimport com.google.common.io.ByteStreams;%0Aimport com.google.common.io.Files;%0Aimport com.google.common.io.Resources;%0Aimport org.h2.jdbc.JdbcSQLException
 ;%0Aim
@@ -510,32 +510,53 @@
 o.OutputStream;%0A
+import java.net.URL;%0A
 import java.sql.
@@ -1617,17 +1617,16 @@
   try %7B%0A
-%0A
         
@@ -1633,52 +1633,27 @@
     
-InputStream in = getClass().getClassLoader()
+URL url = Resources
 .get
@@ -1664,16 +1664,8 @@
 urce
-AsStream
 (%22co
@@ -1710,114 +1710,799 @@
     
-OutputStream out = new FileOutputStream(initScript);%0A%0A            ByteStreams.copy(in, out);%0A
+Files.copy(Resources.newReaderSupplier(url, Charsets.UTF_8), initScript, Charsets.UTF_8);%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 = JdbcSQLException.class)%0A    public void testInitFromInvalidDdlThrows()%0A            throws Exception
 %0A    
+%7B%0A
         
 in.c
@@ -2501,42 +2501,200 @@
     
-in.close();%0A            out.close(
+File initScript = File.createTempFile(%22initscript%22,%22.ddl%22);%0A        try %7B%0A            String invalidDdl = %22This isn't valid SQL%22;%0A            Files.write(invalidDdl, initScript, Charsets.UTF_8
 );%0A%0A
@@ -3010,169 +3010,39 @@
     
-H2EmbeddedDataSource dataSource = new H2EmbeddedDataSource(config);%0A            dataSource.getConnection().createStatement().executeQuery(%22select * from message%22
+new H2EmbeddedDataSource(config
 );%0A 

Fix resource leak in unit tests; add test for bad DDL