/** * Specify that all failures which occur while executing SQL scripts should * be logged but should not cause a failure. * <p>Defaults to {@code false}. * @param flag {@code true} if script execution should continue on error * @return {@code this}, to facilitate method chaining * @since 4.0.3 */ public EmbeddedDatabaseBuilder continueOnError(boolean flag) { this.databasePopulator.setContinueOnError(flag); return this; }
/** * Specify the character encoding used in all SQL scripts, if different from * the platform encoding. * @param scriptEncoding the encoding used in scripts * @return {@code this}, to facilitate method chaining * @since 4.0.3 */ public EmbeddedDatabaseBuilder setScriptEncoding(String scriptEncoding) { this.databasePopulator.setSqlScriptEncoding(scriptEncoding); return this; }
/** * Construct a new {@code ScriptStatementFailedException}. * @param stmt the actual SQL statement that failed * @param stmtNumber the statement number in the SQL script (i.e., * the n<sup>th</sup> statement present in the resource) * @param encodedResource the resource from which the SQL statement was read * @param cause the underlying cause of the failure */ public ScriptStatementFailedException(String stmt, int stmtNumber, EncodedResource encodedResource, Throwable cause) { super(buildErrorMessage(stmt, stmtNumber, encodedResource), cause); }
@Test public void scriptWithStandardEscapedLiteral() throws Exception { databasePopulator.addScript(defaultSchema()); databasePopulator.addScript(resource("db-test-data-escaped-literal.sql")); DatabasePopulatorUtils.execute(databasePopulator, db); assertTestDatabaseCreated("'Keith'"); }
@Test public void scriptWithoutStatementSeparator() throws Exception { databasePopulator.setSeparator(ScriptUtils.EOF_STATEMENT_SEPARATOR); databasePopulator.addScript(resource("drop-users-schema.sql")); databasePopulator.addScript(resource("users-schema-without-separator.sql")); databasePopulator.addScript(resource("users-data-without-separator.sql")); DatabasePopulatorUtils.execute(databasePopulator, db); assertUsersDatabaseCreated("Brannen"); }
@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"); }
@Test public void scriptWithEolBetweenTokens() throws Exception { databasePopulator.addScript(usersSchema()); databasePopulator.addScript(resource("users-data.sql")); DatabasePopulatorUtils.execute(databasePopulator, db); assertUsersDatabaseCreated("Brannen"); }
@Test public void scriptWithSingleLineCommentsAndFailedDrop() throws Exception { databasePopulator.addScript(resource("db-schema-failed-drop-comments.sql")); databasePopulator.addScript(resource("db-test-data.sql")); databasePopulator.setIgnoreFailedDrops(true); DatabasePopulatorUtils.execute(databasePopulator, db); assertTestDatabaseCreated(); }
/** * Execute this {@code ResourceDatabasePopulator} against the given * {@link DataSource}. * <p>Delegates to {@link DatabasePopulatorUtils#execute}. * @param dataSource the {@code DataSource} to execute against (never {@code null}) * @throws ScriptException if an error occurs * @since 4.1 * @see #populate(Connection) */ public void execute(DataSource dataSource) throws ScriptException { DatabasePopulatorUtils.execute(this, dataSource); }
/** * Construct a new {@code ResourceDatabasePopulator} with default settings * for the supplied scripts. * @param scripts the scripts to execute to initialize or clean up the database * (never {@code null}) * @since 4.0.3 */ public ResourceDatabasePopulator(Resource... scripts) { this(); setScripts(scripts); }
/** * Read a script from the given resource, using "{@code --}" as the comment prefix * and "{@code ;}" as the statement separator, and build a String containing the lines. * @param resource the {@code EncodedResource} to be read * @return {@code String} containing the script lines * @throws IOException in case of I/O errors */ static String readScript(EncodedResource resource) throws IOException { return readScript(resource, DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR); }
@Override public void populate(Connection connection) throws SQLException, ScriptException { for (DatabasePopulator populator : this.populators) { populator.populate(connection); } }
/** * Specify the start delimiter for block comments in all SQL scripts. * <p>Defaults to {@code "/*"}. * @param blockCommentStartDelimiter the start delimiter for block comments * @return {@code this}, to facilitate method chaining * @since 4.0.3 * @see #setBlockCommentEndDelimiter */ public EmbeddedDatabaseBuilder setBlockCommentStartDelimiter(String blockCommentStartDelimiter) { this.databasePopulator.setBlockCommentStartDelimiter(blockCommentStartDelimiter); return this; }
/** * Specify the end delimiter for block comments in all SQL scripts. * <p>Defaults to <code>"*/"</code>. * @param blockCommentEndDelimiter the end delimiter for block comments * @return {@code this}, to facilitate method chaining * @since 4.0.3 * @see #setBlockCommentStartDelimiter */ public EmbeddedDatabaseBuilder setBlockCommentEndDelimiter(String blockCommentEndDelimiter) { this.databasePopulator.setBlockCommentEndDelimiter(blockCommentEndDelimiter); return this; }
/** * Specify the single-line comment prefix used in all SQL scripts. * <p>Defaults to {@code "--"}. * @param commentPrefix the prefix for single-line comments * @return {@code this}, to facilitate method chaining * @since 4.0.3 */ public EmbeddedDatabaseBuilder setCommentPrefix(String commentPrefix) { this.databasePopulator.setCommentPrefix(commentPrefix); return this; }
/** * Use the {@linkplain #setDatabasePopulator database populator} to set up * the database. */ @Override public void afterPropertiesSet() { execute(this.databasePopulator); }
@Test public void scriptWithMySqlEscapedLiteral() throws Exception { databasePopulator.addScript(defaultSchema()); databasePopulator.addScript(resource("db-test-data-mysql-escaped-literal.sql")); DatabasePopulatorUtils.execute(databasePopulator, db); assertTestDatabaseCreated("\\$Keith\\$"); }
@Test public void scriptWithCommentsWithinStatements() throws Exception { databasePopulator.addScript(usersSchema()); databasePopulator.addScript(resource("users-data-with-comments.sql")); DatabasePopulatorUtils.execute(databasePopulator, db); assertUsersDatabaseCreated("Brannen", "Hoeller"); }
/** * Use the {@linkplain #setDatabaseCleaner database cleaner} to clean up the * database. */ @Override public void destroy() { execute(this.databaseCleaner); }
/** * See SPR-9781 */ @Test(timeout = 1000) public void executesHugeScriptInReasonableTime() throws SQLException { databasePopulator.addScript(defaultSchema()); databasePopulator.addScript(resource("db-test-data-huge.sql")); DatabasePopulatorUtils.execute(databasePopulator, db); }