Example usage for org.springframework.jdbc.datasource.init ResourceDatabasePopulator addScript

List of usage examples for org.springframework.jdbc.datasource.init ResourceDatabasePopulator addScript

Introduction

In this page you can find the example usage for org.springframework.jdbc.datasource.init ResourceDatabasePopulator addScript.

Prototype

public void addScript(Resource script) 

Source Link

Document

Add a script to execute to initialize or clean up the database.

Usage

From source file:org.camelcookbook.transactions.util.DataSourceInitializer.java

public static DataSource initializeDataSource(DataSource dataSource, Resource script) {
    // here we use the same classes that Spring does under the covers to run the schema into the database
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(script);
    DatabasePopulatorUtils.execute(populator, dataSource);

    return dataSource;
}

From source file:org.wso2.carbon.metrics.jdbc.core.BaseReporterTest.java

@BeforeSuite
protected static void init() throws Exception {
    if (logger.isInfoEnabled()) {
        logger.info("Initializing the data source and populating data");
    }/*from   w  ww  . j  av a 2  s . co m*/
    // Setup datasource
    dataSource = JdbcConnectionPool.create("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "sa", "");
    template = new JdbcTemplate(dataSource);
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(new ClassPathResource("dbscripts/h2.sql"));
    populator.populate(dataSource.getConnection());

    // Create initial context
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
    InitialContext ic = new InitialContext();
    ic.createSubcontext("jdbc");
    ic.bind("jdbc/WSO2MetricsDB", dataSource);

    if (logger.isInfoEnabled()) {
        logger.info("Creating Metrics");
    }
    metrics = new Metrics(TestUtils.getConfigProvider("metrics.yaml"));
    metrics.activate();
    metricService = metrics.getMetricService();
    metricManagementService = metrics.getMetricManagementService();
}

From source file:org.cloudfoundry.identity.uaa.scim.test.TestUtils.java

public static void runScript(DataSource dataSource, String stem) throws Exception {
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    String packageName = ClassUtils.getPackageName(TestUtils.class).replace(".", "/");
    populator.addScript(new ClassPathResource(
            packageName.substring(0, packageName.lastIndexOf("/")) + "/" + stem + "-" + platform + ".sql"));
    Connection connection = dataSource.getConnection();
    try {//  www. jav a 2s  .co m
        populator.populate(connection);
    } catch (ScriptStatementFailedException e) {
        // ignore
    } finally {
        DataSourceUtils.releaseConnection(connection, dataSource);
    }
}

From source file:org.wso2.carbon.metrics.data.service.MetricsDataServiceTest.java

public static Test suite() {
    return new TestSetup(new TestSuite(MetricsDataServiceTest.class)) {

        protected void setUp() throws Exception {
            DataSource dataSource = JdbcConnectionPool.create("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "sa", "");
            template = new JdbcTemplate(dataSource);
            ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
            populator.addScript(new ClassPathResource("dbscripts/h2.sql"));
            populator.populate(dataSource.getConnection());

            // Create initial context
            System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
            System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
            InitialContext ic = new InitialContext();
            ic.createSubcontext("jdbc");
            ic.bind("jdbc/WSO2MetricsDB", dataSource);
        }//from w w  w. ja  va 2s .  co  m

        protected void tearDown() throws Exception {
            InitialContext ic = new InitialContext();
            ic.unbind("jdbc/WSO2MetricsDB");
            ic.unbind("jdbc");
        }
    };
}

From source file:org.wso2.carbon.metrics.impl.JDBCCleanupTest.java

@BeforeClass
public static void setupDatasource() throws ScriptException, SQLException {
    dataSource = JdbcConnectionPool.create("jdbc:h2:mem:test-cleanup;DB_CLOSE_DELAY=-1", "sa", "");
    template = new JdbcTemplate(dataSource);
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(new ClassPathResource("dbscripts/h2.sql"));
    populator.populate(dataSource.getConnection());
}

From source file:org.wso2.carbon.metrics.impl.ReporterTest.java

public static Test suite() {
    return new TestSetup(new TestSuite(ReporterTest.class)) {

        protected void setUp() throws Exception {
            DataSource dataSource = JdbcConnectionPool.create("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "sa", "");
            template = new JdbcTemplate(dataSource);
            ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
            populator.addScript(new ClassPathResource("dbscripts/h2.sql"));
            populator.populate(dataSource.getConnection());

            // Create initial context
            System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
            System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
            InitialContext ic = new InitialContext();
            ic.createSubcontext("jdbc");
            ic.bind("jdbc/WSO2MetricsDB", dataSource);

            // Set setup system property to cover database creator logic
            System.setProperty("setup", "");
        }/*from   w w  w  .java 2s  . c o  m*/

        protected void tearDown() throws Exception {
            InitialContext ic = new InitialContext();
            ic.unbind("jdbc/WSO2MetricsDB");
            ic.unbind("jdbc");
        }
    };
}

From source file:org.wso2.carbon.metrics.reporter.JDBCReporterTest.java

@BeforeClass
public static void setupDatasource() throws Exception {
    dataSource = JdbcConnectionPool.create("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "sa", "");
    template = new JdbcTemplate(dataSource);
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(new ClassPathResource("dbscripts/h2.sql"));
    populator.populate(dataSource.getConnection());
}

From source file:org.wso2.carbon.metrics.jdbc.reporter.JdbcReporterTest.java

@BeforeSuite
private static void init() throws Exception {
    dataSource = JdbcConnectionPool.create("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "sa", "");
    template = new JdbcTemplate(dataSource);
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(new ClassPathResource("dbscripts/h2.sql"));
    populator.populate(dataSource.getConnection());
    DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
    transactionTemplate = new TransactionTemplate(dataSourceTransactionManager);
}

From source file:sf.wicklet.gwt.site.server.db.H2Configurator.java

/** @param dbpath Absolute path relative to web application context root. */
public static EmbeddedDatabase createDatabase(final String dbpath, final String... initscripts)
        throws ClassNotFoundException {
    final EmbeddedDatabaseFactory f = new EmbeddedDatabaseFactory();
    final ResourceDatabasePopulator p = new ResourceDatabasePopulator();
    final ResourceLoader l = new DefaultResourceLoader();
    final IWickletSupport support = Config.get().getSupport();
    final File dbfile = support.getContextFile(dbpath + ".h2.db");
    if (!dbfile.exists()) {
        for (final String s : initscripts) {
            p.addScript(l.getResource("file:" + support.getContextFile(s).getAbsolutePath()));
        }//from w  ww.  ja  v  a2 s  .  c  om
    }
    f.setDatabasePopulator(p);
    f.setDatabaseConfigurer(H2Configurator.getInstance(support.getContextFile(dbpath).getAbsolutePath()));
    // Database name is actually a don't care.
    f.setDatabaseName(TextUtil.fileName(dbpath));
    return f.getDatabase();
}

From source file:ch.algotrader.cache.CacheTest.java

@AfterClass
public static void afterClass() {

    // cleanup the database
    ResourceDatabasePopulator dbPopulator = new ResourceDatabasePopulator();
    dbPopulator.addScript(new ByteArrayResource("DROP ALL OBJECTS".getBytes(Charsets.US_ASCII)));

    DatabasePopulatorUtils.execute(dbPopulator, database);

    context.close();/*from w  ww . j  a  va 2  s. c  o  m*/

    net.sf.ehcache.CacheManager ehCacheManager = net.sf.ehcache.CacheManager.getInstance();
    ehCacheManager.shutdown();
}