Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Improved tests for VersionInfoCommand
  • Loading branch information
pesse committed Jun 7, 2018
commit 32360871bb73977d629863abf6bf32329dc97c81
38 changes: 38 additions & 0 deletions src/test/java/org/utplsql/cli/VersionInfoCommandIT.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,61 @@
package org.utplsql.cli;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.utplsql.cli.util.SystemOutCapturer;

import java.io.IOException;
import java.util.Arrays;

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

public class VersionInfoCommandIT {

private SystemOutCapturer capturer;

@BeforeEach
public void setupCaptureSystemOut() {
capturer = new SystemOutCapturer();
}

private int getNonEmptyLines(String content) {
return (int) Arrays.stream(content.split("[\n|\r]"))
.filter(line -> !line.isEmpty())
.count();
}

private void assertNumberOfLines( int expected, String content ) {
int numOfLines = getNonEmptyLines(content);
assertEquals(expected, numOfLines, String.format("Expected output to have %n lines, but got %n", expected, numOfLines));
}
@Test
public void infoCommandRunsWithoutConnection() throws Exception {

capturer.start();

int result = TestHelper.runApp("info");

String output = capturer.stop();

assertEquals(0, result);
assertNumberOfLines(2, output);
}
@Test
public void infoCommandRunsWithConnection() throws Exception {

capturer.start();

int result = TestHelper.runApp("info", TestHelper.getConnectionString());

String output = capturer.stop();

assertEquals(0, result);
assertNumberOfLines(3, output);
}

@AfterEach
public void cleanupCaptureSystemOut() throws IOException {
capturer.stop();
}
}
74 changes: 74 additions & 0 deletions src/test/java/org/utplsql/cli/util/SystemOutCapturer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.utplsql.cli.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;

/** All credit to Manasjyoti Sharma: https://stackoverflow.com/a/30665299
*/
public class SystemOutCapturer {
private ByteArrayOutputStream baos;
private PrintStream previous;
private boolean capturing;

public void start() {
if (capturing) {
return;
}

capturing = true;
previous = System.out;
baos = new ByteArrayOutputStream();

OutputStream outputStreamCombiner =
new OutputStreamCombiner(Arrays.asList(previous, baos));
PrintStream custom = new PrintStream(outputStreamCombiner);

System.setOut(custom);
}

public String stop() {
if (!capturing) {
return "";
}

System.setOut(previous);

String capturedValue = baos.toString();

baos = null;
previous = null;
capturing = false;

return capturedValue;
}

private static class OutputStreamCombiner extends OutputStream {
private List<OutputStream> outputStreams;

public OutputStreamCombiner(List<OutputStream> outputStreams) {
this.outputStreams = outputStreams;
}

public void write(int b) throws IOException {
for (OutputStream os : outputStreams) {
os.write(b);
}
}

public void flush() throws IOException {
for (OutputStream os : outputStreams) {
os.flush();
}
}

public void close() throws IOException {
for (OutputStream os : outputStreams) {
os.close();
}
}
}
}