Skip to content

Commit dc771c0

Browse files
suraj-qlogicanguillanneuf
authored andcommitted
refactor(samples): restore stdout to original state in test (#500)
1 parent 413d931 commit dc771c0

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

asset/src/test/java/com/example/asset/Analyze.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class Analyze {
5050

5151
private ByteArrayOutputStream bout;
5252
private PrintStream out;
53+
private PrintStream originalPrintStream;
5354

5455
private static final void deleteObjects(String bucketName, String objectName) {
5556
Storage storage = StorageOptions.getDefaultInstance().getService();
@@ -70,13 +71,15 @@ private static final void deleteObjects(String bucketName, String objectName) {
7071
public void setUp() {
7172
bout = new ByteArrayOutputStream();
7273
out = new PrintStream(bout);
74+
originalPrintStream = System.out;
7375
System.setOut(out);
7476
}
7577

7678
@After
7779
public void tearDown() {
78-
System.setOut(null);
79-
bout.reset();
80+
// restores print statements in the original method
81+
System.out.flush();
82+
System.setOut(originalPrintStream);
8083
}
8184

8285
@Test

asset/src/test/java/com/example/asset/ListAssets.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,22 @@
3333
@SuppressWarnings("checkstyle:abbreviationaswordinname")
3434
public class ListAssets {
3535
private ByteArrayOutputStream bout;
36+
private PrintStream out;
37+
private PrintStream originalPrintStream;
3638

3739
@Before
3840
public void setUp() {
3941
bout = new ByteArrayOutputStream();
40-
System.setOut(new PrintStream(bout));
42+
out = new PrintStream(bout);
43+
originalPrintStream = System.out;
44+
System.setOut(out);
4145
}
4246

4347
@After
4448
public void tearDown() {
45-
System.setOut(null);
46-
bout.reset();
49+
// restores print statements in the original method
50+
System.out.flush();
51+
System.setOut(originalPrintStream);
4752
}
4853

4954
@Test

asset/src/test/java/com/example/asset/QuickStartIT.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class QuickStartIT {
4848
private static final String datasetName = RemoteBigQueryHelper.generateDatasetName();
4949
private ByteArrayOutputStream bout;
5050
private PrintStream out;
51+
private PrintStream originalPrintStream;
5152
private BigQuery bigquery;
5253

5354
private static final void deleteObjects() {
@@ -72,13 +73,15 @@ public void setUp() {
7273
}
7374
bout = new ByteArrayOutputStream();
7475
out = new PrintStream(bout);
76+
originalPrintStream = System.out;
7577
System.setOut(out);
7678
}
7779

7880
@After
7981
public void tearDown() {
80-
String consoleOutput = bout.toString();
81-
System.setOut(null);
82+
// restores print statements in the original method
83+
System.out.flush();
84+
System.setOut(originalPrintStream);
8285
deleteObjects();
8386
DatasetId datasetId = DatasetId.of(bigquery.getOptions().getProjectId(), datasetName);
8487
bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());

asset/src/test/java/com/example/asset/RealTimeFeed.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class RealTimeFeed {
4949
private final String[] assetNames = {UUID.randomUUID().toString()};
5050
private static final ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
5151
private ByteArrayOutputStream bout;
52+
private PrintStream out;
53+
private PrintStream originalPrintStream;
5254

5355
private String getProjectNumber(String projectId) {
5456
ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService();
@@ -71,13 +73,16 @@ public static void deleteTopic() throws Exception {
7173
@Before
7274
public void beforeTest() {
7375
bout = new ByteArrayOutputStream();
74-
System.setOut(new PrintStream(bout));
76+
out = new PrintStream(bout);
77+
originalPrintStream = System.out;
78+
System.setOut(out);
7579
}
7680

7781
@After
7882
public void tearDown() {
79-
System.setOut(null);
80-
bout.reset();
83+
// restores print statements in the original method
84+
System.out.flush();
85+
System.setOut(originalPrintStream);
8186
}
8287

8388
@Test

asset/src/test/java/com/example/asset/Search.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class Search {
4141
private static final String datasetName = RemoteBigQueryHelper.generateDatasetName();
4242
private ByteArrayOutputStream bout;
4343
private PrintStream out;
44+
private PrintStream originalPrintStream;
4445
private BigQuery bigquery;
4546

4647
@Before
@@ -51,13 +52,15 @@ public void setUp() {
5152
}
5253
bout = new ByteArrayOutputStream();
5354
out = new PrintStream(bout);
55+
originalPrintStream = System.out;
5456
System.setOut(out);
5557
}
5658

5759
@After
5860
public void tearDown() {
59-
System.setOut(null);
60-
bout.reset();
61+
// restores print statements in the original method
62+
System.out.flush();
63+
System.setOut(originalPrintStream);
6164
DatasetId datasetId = DatasetId.of(bigquery.getOptions().getProjectId(), datasetName);
6265
bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
6366
}

0 commit comments

Comments
 (0)