Skip to content

Commit

Permalink
Merge pull request #300 from hazendaz/master
Browse files Browse the repository at this point in the history
Use more try with resources and pom cleanup
  • Loading branch information
hazendaz committed May 7, 2023
2 parents d0ec2cf + 02d1dac commit 770553c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 72 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
<configuration>
<excludes combine.children="append">
<exclude>**/template_*</exclude>
<exclude>**/README</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ public void execute(String... sparams) {
printStream.println();
}
printStream.println("-- " + change.getFilename());
Reader migrationReader = getMigrationLoader().getScriptReader(change, undo);
char[] cbuf = new char[1024];
int l;
while ((l = migrationReader.read(cbuf)) > -1) {
printStream.print(l == cbuf.length ? cbuf : Arrays.copyOf(cbuf, l));
try (Reader migrationReader = getMigrationLoader().getScriptReader(change, undo)) {
char[] cbuf = new char[1024];
int l;
while ((l = migrationReader.read(cbuf)) > -1) {
printStream.print(l == cbuf.length ? cbuf : Arrays.copyOf(cbuf, l));
}
}
count++;
printStream.println();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ public UpOperation operate(ConnectionProvider connectionProvider, MigrationLoade
int stepCount = 0;

Map<String, Object> hookBindings = new HashMap<>();
Reader scriptReader = null;
Reader onAbortScriptReader = null;
ScriptRunner runner = getScriptRunner(con, option, printStream);
try {
for (Change change : migrations) {
Expand All @@ -85,8 +83,9 @@ public UpOperation operate(ConnectionProvider connectionProvider, MigrationLoade
hook.beforeEach(hookBindings);
}
println(printStream, Util.horizontalLine("Applying: " + change.getFilename(), 80));
scriptReader = migrationsLoader.getScriptReader(change, false);
runner.runScript(scriptReader);
try (Reader scriptReader = migrationsLoader.getScriptReader(change, false)) {
runner.runScript(scriptReader);
}
insertChangelog(change, con, option);
println(printStream);
if (hook != null) {
Expand All @@ -107,21 +106,15 @@ public UpOperation operate(ConnectionProvider connectionProvider, MigrationLoade
println(printStream, skippedOrMissing);
return this;
} catch (Exception e) {
onAbortScriptReader = migrationsLoader.getOnAbortReader();
if (onAbortScriptReader != null) {
println(printStream);
println(printStream, Util.horizontalLine("Executing onabort.sql script.", 80));
runner.runScript(onAbortScriptReader);
println(printStream);
try (Reader onAbortScriptReader = migrationsLoader.getOnAbortReader()) {
if (onAbortScriptReader != null) {
println(printStream);
println(printStream, Util.horizontalLine("Executing onabort.sql script.", 80));
runner.runScript(onAbortScriptReader);
println(printStream);
}
}
throw e;
} finally {
if (scriptReader != null) {
scriptReader.close();
}
if (onAbortScriptReader != null) {
onAbortScriptReader.close();
}
}
} catch (Throwable e) {
while (e instanceof MigrationException && e.getCause() != null) {
Expand Down
92 changes: 54 additions & 38 deletions src/test/java/org/apache/ibatis/migration/MigrationReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ void shouldReturnDoPart() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, false, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, false, null)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand All @@ -79,8 +80,9 @@ void shouldReturnUndoPart() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, true, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, true, null)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand All @@ -99,8 +101,9 @@ void shouldReturnUndoPart_NoEndBreak() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, true, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, true, null)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand All @@ -119,8 +122,9 @@ void shouldUndoCommentBeLenient() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, true, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, true, null)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand All @@ -140,8 +144,9 @@ void shouldUndoCommentAllowAnyCharBeforeAtMark() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, true, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, true, null)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand All @@ -160,8 +165,9 @@ void shouldRequireDoubleSlashInUndoComment() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, true, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, true, null)) {
result = readAsString(migrationReader);
}

assertEquals("", result);
Expand All @@ -175,8 +181,9 @@ void shouldReturnAllAsDoIfUndoCommentNotFound() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, false, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, false, null)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand All @@ -190,8 +197,9 @@ void shouldReturnAllAsDoIfUndoCommentNotFound_NoEndBreak() throws Exception {
String script = "-- ";

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, false, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, false, null)) {
result = readAsString(migrationReader);
}

assertEquals("-- \n", result);
Expand All @@ -205,8 +213,9 @@ void shouldReturnEmptyUndoIfUndoCommentNotFound() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, true, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, true, null)) {
result = readAsString(migrationReader);
}

assertEquals("", result);
Expand All @@ -217,8 +226,9 @@ void shouldReturnEmptyUndoIfUndoCommentNotFound_NoEndBreak() throws Exception {
String script = "-- ";

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, true, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, true, null)) {
result = readAsString(migrationReader);
}

assertEquals("", result);
Expand All @@ -236,8 +246,9 @@ void shouldRemoveFirstDoubleSlashInEveryComment_Do() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, false, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, false, null)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand All @@ -258,8 +269,9 @@ void shouldRemoveFirstDoubleSlashInEveryComment_Undo() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, true, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, true, null)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand All @@ -281,8 +293,9 @@ void shouldSecondUndoMarkerHaveNoEffect() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, true, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, true, null)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand All @@ -309,8 +322,9 @@ void shouldReplaceVariables_Do() throws Exception {
vars.put("c", "CCC");

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, false, vars));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, false, vars)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand All @@ -336,8 +350,9 @@ void shouldReplaceVariables_Undo() throws Exception {
vars.put("c", "CCC");

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, true, vars));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, true, vars)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand All @@ -364,8 +379,9 @@ void shouldNormalizeLineSeparator_Do() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, false, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, false, null)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand Down Expand Up @@ -393,8 +409,9 @@ void shouldNormalizeLineSeparator_Undo() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, true, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, true, null)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand All @@ -410,7 +427,6 @@ void shouldNormalizeLineSeparator_Undo() throws Exception {
@Test
void shouldRespectSpecifiedOffsetAndLength() throws Exception {
String script = "abcdefghij";
String result = null;
try (InputStream stream = strToInputStream(script);
MigrationReader reader = new MigrationReader(stream, charset, false, null)) {
char[] cbuf = new char[5];
Expand All @@ -428,7 +444,6 @@ void testReadWithBuffer() throws Exception {
+ "undo part\n";
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script);
MigrationReader reader = new MigrationReader(stream, charset, false, null)) {
StringBuilder buffer = new StringBuilder();
Expand All @@ -449,8 +464,9 @@ void shouldRetainLineBreakAfterDelimiter() throws Exception {
// @formatter:on

String result = null;
try (InputStream stream = strToInputStream(script)) {
result = readAsString(new MigrationReader(stream, charset, false, null));
try (InputStream stream = strToInputStream(script);
MigrationReader migrationReader = new MigrationReader(stream, charset, false, null)) {
result = readAsString(migrationReader);
}

// @formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,26 @@ void testGetScriptReader() throws Exception {
JavaMigrationLoader loader = createMigrationLoader();
Change change = new Change();
change.setFilename("org.apache.ibatis.migration.runtime_migration.scripts_java.V002_CreateFirstTable");
Reader reader = loader.getScriptReader(change, false);
Writer writer = new StringWriter();
int c;
while ((c = reader.read()) != -1) {
writer.write(c);
try (Reader reader = loader.getScriptReader(change, false); Writer writer = new StringWriter()) {
int c;
while ((c = reader.read()) != -1) {
writer.write(c);
}
assertTrue(writer.toString().indexOf("CREATE TABLE first_table (ID INTEGER NOT NULL, NAME VARCHAR(16));") > -1);
}
assertTrue(writer.toString().indexOf("CREATE TABLE first_table (ID INTEGER NOT NULL, NAME VARCHAR(16));") > -1);
}

@Test
void testGetBootstrapReader() throws Exception {
JavaMigrationLoader loader = createMigrationLoader();
Reader reader = loader.getBootstrapReader();
Writer writer = new StringWriter();
int c;
while ((c = reader.read()) != -1) {
writer.write(c);
try (Reader reader = loader.getBootstrapReader(); Writer writer = new StringWriter()) {
int c;
while ((c = reader.read()) != -1) {
writer.write(c);
}
assertTrue(
writer.toString().indexOf("CREATE TABLE bootstrap_table (ID INTEGER NOT NULL, NAME VARCHAR(16));") > -1);
}
assertTrue(writer.toString().indexOf("CREATE TABLE bootstrap_table (ID INTEGER NOT NULL, NAME VARCHAR(16));") > -1);
}

protected JavaMigrationLoader createMigrationLoader() {
Expand Down

0 comments on commit 770553c

Please sign in to comment.