Skip to content

Commit

Permalink
refactor(samples): restore stdout to original state in test (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
suraj-qlogic authored and minherz committed Nov 17, 2022
1 parent 5cbff02 commit 0daf86c
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
7 changes: 5 additions & 2 deletions asset/src/test/java/com/example/asset/Analyze.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class Analyze {

private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

private static final void deleteObjects(String bucketName, String objectName) {
Storage storage = StorageOptions.getDefaultInstance().getService();
Expand All @@ -70,13 +71,15 @@ private static final void deleteObjects(String bucketName, String objectName) {
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
bout.reset();
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
}

@Test
Expand Down
11 changes: 8 additions & 3 deletions asset/src/test/java/com/example/asset/ListAssets.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class ListAssets {
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
System.setOut(new PrintStream(bout));
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
bout.reset();
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
}

@Test
Expand Down
7 changes: 5 additions & 2 deletions asset/src/test/java/com/example/asset/QuickStartIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class QuickStartIT {
private static final String datasetName = RemoteBigQueryHelper.generateDatasetName();
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;
private BigQuery bigquery;

private static final void deleteObjects() {
Expand All @@ -72,13 +73,15 @@ public void setUp() {
}
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}

@After
public void tearDown() {
String consoleOutput = bout.toString();
System.setOut(null);
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
deleteObjects();
DatasetId datasetId = DatasetId.of(bigquery.getOptions().getProjectId(), datasetName);
bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
Expand Down
11 changes: 8 additions & 3 deletions asset/src/test/java/com/example/asset/RealTimeFeed.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class RealTimeFeed {
private final String[] assetNames = {UUID.randomUUID().toString()};
private static final ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;

private String getProjectNumber(String projectId) {
ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService();
Expand All @@ -71,13 +73,16 @@ public static void deleteTopic() throws Exception {
@Before
public void beforeTest() {
bout = new ByteArrayOutputStream();
System.setOut(new PrintStream(bout));
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
bout.reset();
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
}

@Test
Expand Down
7 changes: 5 additions & 2 deletions asset/src/test/java/com/example/asset/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class Search {
private static final String datasetName = RemoteBigQueryHelper.generateDatasetName();
private ByteArrayOutputStream bout;
private PrintStream out;
private PrintStream originalPrintStream;
private BigQuery bigquery;

@Before
Expand All @@ -51,13 +52,15 @@ public void setUp() {
}
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
originalPrintStream = System.out;
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
bout.reset();
// restores print statements in the original method
System.out.flush();
System.setOut(originalPrintStream);
DatasetId datasetId = DatasetId.of(bigquery.getOptions().getProjectId(), datasetName);
bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
}
Expand Down

0 comments on commit 0daf86c

Please sign in to comment.