/** * Create a new embedded database builder with the given {@link ResourceLoader}. * @param resourceLoader the {@code ResourceLoader} to delegate to */ public EmbeddedDatabaseBuilder(ResourceLoader resourceLoader) { this.databaseFactory = new EmbeddedDatabaseFactory(); this.databasePopulator = new ResourceDatabasePopulator(); this.databaseFactory.setDatabasePopulator(this.databasePopulator); this.resourceLoader = resourceLoader; }
protected DatabasePopulator getDatabasePopulator() { ResourceDatabasePopulator dbp = new ResourceDatabasePopulator(); dbp.setScripts(initScripts.toArray(new Resource[initScripts.size()])); return dbp; }
/** * Execute the given SQL script. * <p>Use with caution outside of a transaction! * <p>The script will normally be loaded by classpath. * <p><b>Do not use this method to execute DDL if you expect rollback.</b> * @param sqlResourcePath the Spring resource path for the SQL script * @param continueOnError whether or not to continue without throwing an * exception in the event of an error * @throws DataAccessException if there is an error executing a statement * @see ResourceDatabasePopulator * @see #setSqlScriptEncoding */ protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException { DataSource ds = this.jdbcTemplate.getDataSource(); Assert.state(ds != null, "No DataSource set"); Assert.state(this.applicationContext != null, "No ApplicationContext available"); Resource resource = this.applicationContext.getResource(sqlResourcePath); new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(ds); }
/** * Execute the given SQL script. * <p>Use with caution outside of a transaction! * <p>The script will normally be loaded by classpath. * <p><b>Do not use this method to execute DDL if you expect rollback.</b> * @param sqlResourcePath the Spring resource path for the SQL script * @param continueOnError whether or not to continue without throwing an * exception in the event of an error * @throws DataAccessException if there is an error executing a statement * @see ResourceDatabasePopulator * @see #setSqlScriptEncoding */ protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException { DataSource ds = this.jdbcTemplate.getDataSource(); Assert.state(ds != null, "No DataSource set"); Assert.state(this.applicationContext != null, "No ApplicationContext set"); Resource resource = this.applicationContext.getResource(sqlResourcePath); new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(ds); }
@Test(expected = IllegalArgumentException.class) public void constructWithNullResourceArray() { new ResourceDatabasePopulator((Resource[]) null); }
@Test(expected = IllegalArgumentException.class) public void constructWithNullResource() { new ResourceDatabasePopulator((Resource) null); }
@Test public void constructWithResource() { ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(script1); assertEquals(1, databasePopulator.scripts.size()); }
@Test public void constructWithMultipleResources() { ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(script1, script2); assertEquals(2, databasePopulator.scripts.size()); }
@Test(expected = IllegalArgumentException.class) public void setScriptsWithNullResourceArray() { ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); databasePopulator.setScripts((Resource[]) null); }
@Test(expected = IllegalArgumentException.class) public void addScriptsWithNullResource() { ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); databasePopulator.addScripts((Resource) null); }
@Test(expected = IllegalArgumentException.class) public void addScriptsWithNullResourceArray() { ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); databasePopulator.addScripts((Resource[]) null); }
@Test(expected = IllegalArgumentException.class) public void setScriptsWithNullResource() { ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); databasePopulator.setScripts((Resource) null); }
@Test public void constructWithMultipleResourcesAndThenAddScript() { ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(script1, script2); assertEquals(2, databasePopulator.scripts.size()); databasePopulator.addScript(script3); assertEquals(3, databasePopulator.scripts.size()); }
private DatabasePopulator databasePopulator() throws IOException { ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator(); resourceDatabasePopulator.setContinueOnError(false); resourceDatabasePopulator.addScripts( new PathMatchingResourcePatternResolver().getResources("classpath:/h2/*.sql") ); return resourceDatabasePopulator; }
@Test @Transactional public void transactionalTest() { TransactionTestUtils.assertInTransaction(true); ClassPathResource resource = new ClassPathResource("/org/springframework/test/context/jdbc/data.sql"); new ResourceDatabasePopulator(resource).execute(jdbcTemplate.getDataSource()); assertNumUsers(1); }
@Test public void setScriptsAndThenAddScript() { ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); assertEquals(0, databasePopulator.scripts.size()); databasePopulator.setScripts(script1, script2); assertEquals(2, databasePopulator.scripts.size()); databasePopulator.addScript(script3); assertEquals(3, databasePopulator.scripts.size()); }
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException { Resource resource = this.applicationContext.getResource(sqlResourcePath); new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(jdbcTemplate.getDataSource()); }
protected void executeSqlScript(String sqlResourcePath) throws DataAccessException { Resource resource = applicationContext.getResource(sqlResourcePath); new ResourceDatabasePopulator(resource).execute(this.jdbcTemplate.getDataSource()); }
@Test public void testFactoryBeanLifecycle() throws Exception { EmbeddedDatabaseFactoryBean bean = new EmbeddedDatabaseFactoryBean(); ResourceDatabasePopulator populator = new ResourceDatabasePopulator(resource("db-schema.sql"), resource("db-test-data.sql")); bean.setDatabasePopulator(populator); bean.afterPropertiesSet(); DataSource ds = bean.getObject(); JdbcTemplate template = new JdbcTemplate(ds); assertEquals("Keith", template.queryForObject("select NAME from T_TEST", String.class)); bean.destroy(); }
@Test public void constructorWithMultipleScriptResources() throws Exception { final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(usersSchema(), resource("users-data-with-comments.sql")); DatabasePopulatorUtils.execute(populator, db); assertUsersDatabaseCreated("Brannen", "Hoeller"); }