Skip to content

Commit f318157

Browse files
committed
fixes #117 the previous fix didn't replace variables correctly.
1 parent 5ddaca9 commit f318157

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

src/main/java/org/apache/ibatis/migration/MigrationReader.java

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2017 the original author or authors.
2+
* Copyright 2010-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -96,8 +96,7 @@ public int read(char[] cbuf, int off, int len) throws IOException {
9696
int result = in.read(cbuf, off, len);
9797
if (result == -1) {
9898
if (lineBuffer.length() > 0 && (!undo || inUndo)) {
99-
buffer.append(lineBuffer).append(lineSeparator);
100-
lineBuffer.setLength(0);
99+
addToBuffer(lineBuffer);
101100
}
102101
if (buffer.length() > 0) {
103102
break;
@@ -115,10 +114,8 @@ public int read(char[] cbuf, int off, int len) throws IOException {
115114
switch (part) {
116115
case AFTER_UNDO_TAG:
117116
if (undo) {
118-
replaceVariables();
119-
buffer.append(lineBuffer.delete(afterCommentPrefixIndex, afterDoubleSlashIndex)
120-
.insert(afterCommentPrefixIndex, ' ')).append(lineSeparator);
121-
lineBuffer.setLength(0);
117+
addToBuffer(lineBuffer.delete(afterCommentPrefixIndex, afterDoubleSlashIndex)
118+
.insert(afterCommentPrefixIndex, ' '));
122119
inUndo = true;
123120
} else {
124121
// Won't read from the file anymore.
@@ -132,11 +129,11 @@ public int read(char[] cbuf, int off, int len) throws IOException {
132129
}
133130
break;
134131
case NOT_UNDO_LINE:
135-
if (!undo || (undo && inUndo)) {
136-
replaceVariables();
137-
buffer.append(lineBuffer).append(lineSeparator);
132+
if (!undo || inUndo) {
133+
addToBuffer(lineBuffer);
134+
} else {
135+
lineBuffer.setLength(0);
138136
}
139-
lineBuffer.setLength(0);
140137
break;
141138
default:
142139
break;
@@ -154,13 +151,19 @@ public int read(char[] cbuf, int off, int len) throws IOException {
154151
return readFromBuffer(cbuf, off, len);
155152
}
156153

157-
private void replaceVariables() {
154+
private void addToBuffer(StringBuilder line) {
155+
replaceVariables(line);
156+
buffer.append(line).append(lineSeparator);
157+
lineBuffer.setLength(0);
158+
}
159+
160+
private void replaceVariables(StringBuilder line) {
158161
if (variableStatus == VariableStatus.FOUND_POSSIBLE_VARIABLE) {
159-
String lineBufferStr = lineBuffer.toString();
162+
String lineBufferStr = line.toString();
160163
String processed = PropertyParser.parse(lineBufferStr, variables);
161164
if (!lineBufferStr.equals(processed)) {
162-
lineBuffer.setLength(0);
163-
lineBuffer.append(processed);
165+
line.setLength(0);
166+
line.append(processed);
164167
}
165168
}
166169
variableStatus = VariableStatus.NOTHING;

src/test/java/org/apache/ibatis/migration/MigrationReaderTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2017 the original author or authors.
2+
* Copyright 2010-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -233,7 +233,7 @@ public void shouldReplaceVariables_Undo() throws Exception {
233233
+ "--//@UNDO ${c}\n"
234234
+ "undo ${a} part${b} \n"
235235
+ "-- ${a}\n"
236-
+ "${c} \\${b}\n";
236+
+ "${c} \\${b}";
237237
Properties vars = new Properties();
238238
vars.put("a", "AAA");
239239
vars.put("b", "BBB");

0 commit comments

Comments
 (0)