package net.afnf.blog;

import java.sql.Connection;

import javax.sql.DataSource;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {
        "file:src/main/webapp/WEB-INF/applicationContext.xml",
        "file:src/main/webapp/WEB-INF/applicationContext-db.xml",
        "file:src/main/webapp/WEB-INF/applicationContext-security.xml",
        "classpath:/applicationContext-testdb.xml" })
public abstract class SpringTestBase {

    @Autowired
    private ApplicationContext ctx;

    @Autowired
    protected DataSource dataSource;

    protected void executeSql(String path) {
        Resource resource = new ClassPathResource(path, getClass());
        ResourceDatabasePopulator rdp = new ResourceDatabasePopulator();
        rdp.addScript(resource);
        rdp.setSqlScriptEncoding("UTF-8");
        rdp.setIgnoreFailedDrops(true);
        rdp.setContinueOnError(false);

        JdbcTemplate template = new JdbcTemplate(dataSource);
        Connection conn = DataSourceUtils.getConnection(template.getDataSource());
        rdp.populate(conn);
    }
}

@@ -1656,24 +1656,26 @@
 urce);%0D%0A
+%0D%0A
         
 Connecti
@@ -1666,16 +1666,21 @@
         
+try (
 Connecti
@@ -1741,26 +1741,33 @@
 taSource());
-%0D%0A
+) %7B%0D%0A    
         rdp.
@@ -1779,22 +1779,159 @@
 ate(conn);%0D%0A
+        %7D%0D%0A        catch (Exception e) %7B%0D%0A            throw new IllegalStateException(%22executeSql failed, path=%22 + path, e);%0D%0A        %7D%0D%0A
     %7D%0D%0A%7D%0D%0A

fix connection leak on junit