3030
3131package 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 ;
3337import java .util .concurrent .Future ;
3438
39+ import javax .swing .JTextPane ;
40+ import javax .swing .text .BadLocationException ;
41+ import javax .swing .text .Document ;
42+
3543import org .scijava .Context ;
3644import org .scijava .thread .ThreadService ;
3745import 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