Skip to content

Commit f2ae3fb

Browse files
committed
SwingConsolePaneBenchmark: do some sanity tests
Let's double check the output that was sent to the console, verifying that everything is there. For convenience, we use JUnit for our assertion statements, even though the routine in question is not a unit test per se.
1 parent eba8d38 commit f2ae3fb

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@
104104
<artifactId>jdatepicker</artifactId>
105105
<version>1.3.2</version>
106106
</dependency>
107+
108+
<!-- Test scope dependencies -->
109+
<dependency>
110+
<groupId>junit</groupId>
111+
<artifactId>junit</artifactId>
112+
</dependency>
107113
</dependencies>
108114

109115
<build>

src/test/java/org/scijava/ui/swing/console/SwingConsolePaneBenchmark.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,20 @@
3030

3131
package org.scijava.ui.swing.console;
3232

33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertTrue;
35+
36+
import java.util.Arrays;
3337
import java.util.concurrent.Future;
3438

39+
import javax.swing.JTextPane;
40+
import javax.swing.text.BadLocationException;
41+
import javax.swing.text.Document;
42+
3543
import org.scijava.Context;
3644
import org.scijava.thread.ThreadService;
3745
import org.scijava.ui.UIService;
46+
import org.scijava.ui.swing.sdi.SwingSDIUI;
3847

3948
/**
4049
* A behavioral test and benchmark of {@link SwingConsolePane}.
@@ -60,6 +69,7 @@ public static void main(final String[] args) throws Exception {
6069
{ ": {ERR} iteration #", ": {OUT} iteration #" };
6170
final String outLabel = streamLabels[1];
6271
final String errLabel = streamLabels[0];
72+
final int numStreams = streamLabels.length;
6373

6474
final int initialDelay = 500;
6575

@@ -95,6 +105,52 @@ public void run() {
95105
final long end = System.currentTimeMillis();
96106
System.out.println();
97107
System.out.println("Benchmark took " + (end - start) + " ms");
108+
109+
// Finally, check for completeness of output.
110+
// NB: We do this **also on the EDT** so that all output has flushed.
111+
final String completenessMessage = "Checking for completeness of output...";
112+
System.out.println();
113+
System.out.println(completenessMessage);
114+
threadService.queue(new Runnable() {
115+
116+
@Override
117+
public void run() {
118+
System.out.println();
119+
final SwingSDIUI ui =
120+
(SwingSDIUI) context.service(UIService.class).getVisibleUIs().get(0);
121+
final JTextPane textPane = ui.getConsolePane().getTextPane();
122+
final Document doc = textPane.getDocument();
123+
try {
124+
final String text = doc.getText(0, doc.getLength());
125+
final String[] lines = text.split("\n");
126+
Arrays.sort(lines);
127+
128+
int lineIndex = 0;
129+
assertEquals("", lines[lineIndex++]);
130+
assertEquals("", lines[lineIndex++]);
131+
for (int t = 0; t < numThreads; t++) {
132+
for (int s = 0; s < numStreams; s++) {
133+
for (int i = 0; i < numOperations; i++) {
134+
final String expected = str(t, streamLabels[s], i);
135+
final String actual = lines[lineIndex++];
136+
assertEquals(expected, actual);
137+
}
138+
}
139+
}
140+
assertTrue(lines[lineIndex++].startsWith("Benchmark took "));
141+
assertEquals(completenessMessage, lines[lineIndex++]);
142+
assertEquals("Goodbye cruel world!", lines[lineIndex++]);
143+
assertEquals("Hello world!", lines[lineIndex++]);
144+
assertEquals(lineIndex, lines.length);
145+
146+
System.out.println("Success! All output accounted for!");
147+
}
148+
catch (final BadLocationException exc) {
149+
exc.printStackTrace();
150+
}
151+
}
152+
});
153+
98154
}
99155

100156
// - Helper methods --

0 commit comments

Comments
 (0)