Skip to content

Commit 3236087

Browse files
committed
Improved tests for VersionInfoCommand
1 parent 457625d commit 3236087

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,61 @@
11
package org.utplsql.cli;
22

3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.BeforeEach;
35
import org.junit.jupiter.api.Test;
6+
import org.utplsql.cli.util.SystemOutCapturer;
7+
8+
import java.io.IOException;
9+
import java.util.Arrays;
410

511
import static org.junit.jupiter.api.Assertions.assertEquals;
612

713
public class VersionInfoCommandIT {
814

15+
private SystemOutCapturer capturer;
16+
17+
@BeforeEach
18+
public void setupCaptureSystemOut() {
19+
capturer = new SystemOutCapturer();
20+
}
21+
22+
private int getNonEmptyLines(String content) {
23+
return (int) Arrays.stream(content.split("[\n|\r]"))
24+
.filter(line -> !line.isEmpty())
25+
.count();
26+
}
27+
28+
private void assertNumberOfLines( int expected, String content ) {
29+
int numOfLines = getNonEmptyLines(content);
30+
assertEquals(expected, numOfLines, String.format("Expected output to have %n lines, but got %n", expected, numOfLines));
31+
}
932
@Test
1033
public void infoCommandRunsWithoutConnection() throws Exception {
1134

35+
capturer.start();
36+
1237
int result = TestHelper.runApp("info");
1338

39+
String output = capturer.stop();
40+
1441
assertEquals(0, result);
42+
assertNumberOfLines(2, output);
1543
}
1644
@Test
1745
public void infoCommandRunsWithConnection() throws Exception {
1846

47+
capturer.start();
48+
1949
int result = TestHelper.runApp("info", TestHelper.getConnectionString());
2050

51+
String output = capturer.stop();
52+
2153
assertEquals(0, result);
54+
assertNumberOfLines(3, output);
55+
}
56+
57+
@AfterEach
58+
public void cleanupCaptureSystemOut() throws IOException {
59+
capturer.stop();
2260
}
2361
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.utplsql.cli.util;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.io.OutputStream;
6+
import java.io.PrintStream;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
/** All credit to Manasjyoti Sharma: https://stackoverflow.com/a/30665299
11+
*/
12+
public class SystemOutCapturer {
13+
private ByteArrayOutputStream baos;
14+
private PrintStream previous;
15+
private boolean capturing;
16+
17+
public void start() {
18+
if (capturing) {
19+
return;
20+
}
21+
22+
capturing = true;
23+
previous = System.out;
24+
baos = new ByteArrayOutputStream();
25+
26+
OutputStream outputStreamCombiner =
27+
new OutputStreamCombiner(Arrays.asList(previous, baos));
28+
PrintStream custom = new PrintStream(outputStreamCombiner);
29+
30+
System.setOut(custom);
31+
}
32+
33+
public String stop() {
34+
if (!capturing) {
35+
return "";
36+
}
37+
38+
System.setOut(previous);
39+
40+
String capturedValue = baos.toString();
41+
42+
baos = null;
43+
previous = null;
44+
capturing = false;
45+
46+
return capturedValue;
47+
}
48+
49+
private static class OutputStreamCombiner extends OutputStream {
50+
private List<OutputStream> outputStreams;
51+
52+
public OutputStreamCombiner(List<OutputStream> outputStreams) {
53+
this.outputStreams = outputStreams;
54+
}
55+
56+
public void write(int b) throws IOException {
57+
for (OutputStream os : outputStreams) {
58+
os.write(b);
59+
}
60+
}
61+
62+
public void flush() throws IOException {
63+
for (OutputStream os : outputStreams) {
64+
os.flush();
65+
}
66+
}
67+
68+
public void close() throws IOException {
69+
for (OutputStream os : outputStreams) {
70+
os.close();
71+
}
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)