Skip to content

Commit cd6331c

Browse files
committed
fix issue when timestamp is wrongly printed when just a line follow up should be printed
1 parent a951998 commit cd6331c

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

app/src/processing/app/AbstractTextMonitor.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.text.SimpleDateFormat;
1414
import java.util.Date;
1515
import java.util.StringTokenizer;
16+
import java.util.concurrent.atomic.AtomicBoolean;
1617

1718
import javax.swing.Box;
1819
import javax.swing.BoxLayout;
@@ -181,6 +182,7 @@ public void message(final String msg) {
181182
static class UpdateTextAreaAction implements Runnable {
182183

183184
private static final String LINE_SEPARATOR = "\n";
185+
private static AtomicBoolean isStartingLine = new AtomicBoolean(true);
184186

185187
private String msg;
186188
private boolean addTimeStamp;
@@ -209,16 +211,15 @@ public void run() {
209211
private String addTimestamps(String text) {
210212
String now = new SimpleDateFormat("HH:mm:ss.SSS -> ").format(new Date());
211213
final StringBuilder sb = new StringBuilder(text.length() + now.length());
212-
boolean isStartingLine = true;
213214
StringTokenizer tokenizer = new StringTokenizer(text, LINE_SEPARATOR, true);
214215
while (tokenizer.hasMoreTokens()) {
215-
if (isStartingLine) {
216+
if (isStartingLine.get()) {
216217
sb.append(now);
217218
}
218219
String token = tokenizer.nextToken();
219220
sb.append(token);
220221
// tokenizer returns "\n" as a single token
221-
isStartingLine = token.equals(LINE_SEPARATOR);
222+
isStartingLine.set(token.equals(LINE_SEPARATOR));
222223
}
223224
return sb.toString();
224225
}

app/test/processing/app/UpdateTextAreaActionTest.java

+33
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class UpdateTextAreaActionTest {
1616
@Before public void setUp() {
1717
textAreaFIFO = mock(TextAreaFIFO.class);
1818
text = ArgumentCaptor.forClass(String.class);
19+
sendNewLineInOrderToHaveCleanTestStart();
1920
}
2021

2122
@Test
@@ -64,4 +65,36 @@ public void emptyLinesHaveTimestampToo() {
6465
+ TIMESTAMP_REGEX + " -> line_2");
6566
}
6667

68+
@Test
69+
public void newLinesAreRememberedWhenNewBufferIsUsed() {
70+
// given #1
71+
AbstractTextMonitor.UpdateTextAreaAction action;
72+
action = new AbstractTextMonitor.UpdateTextAreaAction(
73+
textAreaFIFO, true, false, "first line without newline");
74+
75+
// when #1
76+
action.run();
77+
78+
//then #1
79+
verify(textAreaFIFO, atLeastOnce()).append(text.capture());
80+
assertThat(text.getValue()).matches(TIMESTAMP_REGEX + " -> first line without newline");
81+
82+
// given #2
83+
action = new AbstractTextMonitor.UpdateTextAreaAction(
84+
textAreaFIFO, true, false, "more text for first line");
85+
86+
// when #2
87+
action.run();
88+
89+
//then #2
90+
verify(textAreaFIFO, atLeastOnce()).append(text.capture());
91+
assertThat(text.getValue()).matches("more text for first line");
92+
}
93+
94+
95+
private void sendNewLineInOrderToHaveCleanTestStart() {
96+
AbstractTextMonitor.UpdateTextAreaAction action = new AbstractTextMonitor.UpdateTextAreaAction(
97+
textAreaFIFO, true, false, "\n");
98+
action.run();
99+
}
67100
}

0 commit comments

Comments
 (0)