Skip to content

Commit 7fecf77

Browse files
suraj-qlogicShabirmean
authored andcommitted
refactor(samples): restore stdout to original state after test (#379)
1 parent 0218743 commit 7fecf77

12 files changed

+60
-13
lines changed

translate/snippets/src/test/java/com/example/translate/CreateGlossaryTests.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class CreateGlossaryTests {
4545

4646
private ByteArrayOutputStream bout;
4747
private PrintStream out;
48+
private PrintStream originalPrintStream;
4849

4950
private static void requireEnvVar(String varName) {
5051
assertNotNull(
@@ -62,15 +63,17 @@ public static void checkRequirements() {
6263
public void setUp() {
6364
bout = new ByteArrayOutputStream();
6465
out = new PrintStream(bout);
66+
originalPrintStream = System.out;
6567
System.setOut(out);
6668
}
6769

6870
@After
6971
public void tearDown() throws InterruptedException, ExecutionException, IOException {
7072
// Delete the created glossary
7173
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
72-
73-
System.setOut(null);
74+
// restores print statements in the original method
75+
System.out.flush();
76+
System.setOut(originalPrintStream);
7477
}
7578

7679
@Test

translate/snippets/src/test/java/com/example/translate/DeleteGlossaryTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class DeleteGlossaryTests {
4646

4747
private ByteArrayOutputStream bout;
4848
private PrintStream out;
49+
private PrintStream originalPrintStream;
4950

5051
private static void requireEnvVar(String varName) {
5152
assertNotNull(
@@ -71,12 +72,15 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
7172

7273
bout = new ByteArrayOutputStream();
7374
out = new PrintStream(bout);
75+
originalPrintStream = System.out;
7476
System.setOut(out);
7577
}
7678

7779
@After
7880
public void tearDown() {
79-
System.setOut(null);
81+
// restores print statements in the original method
82+
System.out.flush();
83+
System.setOut(originalPrintStream);
8084
}
8185

8286
@Test

translate/snippets/src/test/java/com/example/translate/DetectLanguageTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class DetectLanguageTests {
3737

3838
private ByteArrayOutputStream bout;
3939
private PrintStream out;
40+
private PrintStream originalPrintStream;
4041

4142
private static void requireEnvVar(String varName) {
4243
assertNotNull(
@@ -54,12 +55,15 @@ public static void checkRequirements() {
5455
public void setUp() {
5556
bout = new ByteArrayOutputStream();
5657
out = new PrintStream(bout);
58+
originalPrintStream = System.out;
5759
System.setOut(out);
5860
}
5961

6062
@After
6163
public void tearDown() {
62-
System.setOut(null);
64+
// restores print statements in the original method
65+
System.out.flush();
66+
System.setOut(originalPrintStream);
6367
}
6468

6569
@Test

translate/snippets/src/test/java/com/example/translate/GetGlossaryTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class GetGlossaryTests {
4545

4646
private ByteArrayOutputStream bout;
4747
private PrintStream out;
48+
private PrintStream originalPrintStream;
4849

4950
private static void requireEnvVar(String varName) {
5051
assertNotNull(
@@ -70,14 +71,17 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
7071

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() throws InterruptedException, ExecutionException, IOException {
7880
// Delete the created glossary
7981
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
80-
System.setOut(null);
82+
// restores print statements in the original method
83+
System.out.flush();
84+
System.setOut(originalPrintStream);
8185
}
8286

8387
@Test

translate/snippets/src/test/java/com/example/translate/GetSupportedLanguagesForTargetTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class GetSupportedLanguagesForTargetTests {
3737

3838
private ByteArrayOutputStream bout;
3939
private PrintStream out;
40+
private PrintStream originalPrintStream;
4041

4142
private static void requireEnvVar(String varName) {
4243
assertNotNull(
@@ -54,12 +55,15 @@ public static void checkRequirements() {
5455
public void setUp() {
5556
bout = new ByteArrayOutputStream();
5657
out = new PrintStream(bout);
58+
originalPrintStream = System.out;
5759
System.setOut(out);
5860
}
5961

6062
@After
6163
public void tearDown() {
62-
System.setOut(null);
64+
// restores print statements in the original method
65+
System.out.flush();
66+
System.setOut(originalPrintStream);
6367
}
6468

6569
@Test

translate/snippets/src/test/java/com/example/translate/GetSupportedLanguagesTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class GetSupportedLanguagesTests {
3737

3838
private ByteArrayOutputStream bout;
3939
private PrintStream out;
40+
private PrintStream originalPrintStream;
4041

4142
private static void requireEnvVar(String varName) {
4243
assertNotNull(
@@ -54,12 +55,15 @@ public static void checkRequirements() {
5455
public void setUp() {
5556
bout = new ByteArrayOutputStream();
5657
out = new PrintStream(bout);
58+
originalPrintStream = System.out;
5759
System.setOut(out);
5860
}
5961

6062
@After
6163
public void tearDown() {
62-
System.setOut(null);
64+
// restores print statements in the original method
65+
System.out.flush();
66+
System.setOut(originalPrintStream);
6367
}
6468

6569
@Test

translate/snippets/src/test/java/com/example/translate/ListGlossariesTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class ListGlossariesTests {
4646

4747
private ByteArrayOutputStream bout;
4848
private PrintStream out;
49+
private PrintStream originalPrintStream;
4950

5051
private static void requireEnvVar(String varName) {
5152
assertNotNull(
@@ -71,14 +72,17 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
7172

7273
bout = new ByteArrayOutputStream();
7374
out = new PrintStream(bout);
75+
originalPrintStream = System.out;
7476
System.setOut(out);
7577
}
7678

7779
@After
7880
public void tearDown() throws InterruptedException, ExecutionException, IOException {
7981
// Delete the created glossary
8082
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
81-
System.setOut(null);
83+
// restores print statements in the original method
84+
System.out.flush();
85+
System.setOut(originalPrintStream);
8286
}
8387

8488
@Test

translate/snippets/src/test/java/com/example/translate/QuickstartSampleIT.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,21 @@
3232
public class QuickstartSampleIT {
3333
private ByteArrayOutputStream bout;
3434
private PrintStream out;
35+
private PrintStream originalPrintStream;
3536

3637
@Before
3738
public void setUp() {
3839
bout = new ByteArrayOutputStream();
3940
out = new PrintStream(bout);
41+
originalPrintStream = System.out;
4042
System.setOut(out);
4143
}
4244

4345
@After
4446
public void tearDown() {
45-
System.setOut(null);
47+
// restores print statements in the original method
48+
System.out.flush();
49+
System.setOut(originalPrintStream);
4650
}
4751

4852
@Test

translate/snippets/src/test/java/com/example/translate/TranslateTextTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class TranslateTextTests {
3737

3838
private ByteArrayOutputStream bout;
3939
private PrintStream out;
40+
private PrintStream originalPrintStream;
4041

4142
private static void requireEnvVar(String varName) {
4243
assertNotNull(
@@ -54,12 +55,15 @@ public static void checkRequirements() {
5455
public void setUp() {
5556
bout = new ByteArrayOutputStream();
5657
out = new PrintStream(bout);
58+
originalPrintStream = System.out;
5759
System.setOut(out);
5860
}
5961

6062
@After
6163
public void tearDown() {
62-
System.setOut(null);
64+
// restores print statements in the original method
65+
System.out.flush();
66+
System.setOut(originalPrintStream);
6367
}
6468

6569
@Test

translate/snippets/src/test/java/com/example/translate/TranslateTextWithGlossaryAndModelTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class TranslateTextWithGlossaryAndModelTests {
4646

4747
private ByteArrayOutputStream bout;
4848
private PrintStream out;
49+
private PrintStream originalPrintStream;
4950

5051
private static void requireEnvVar(String varName) {
5152
assertNotNull(
@@ -71,6 +72,7 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
7172

7273
bout = new ByteArrayOutputStream();
7374
out = new PrintStream(bout);
75+
originalPrintStream = System.out;
7476
System.setOut(out);
7577
}
7678

@@ -79,7 +81,9 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept
7981
// Clean up
8082
// Delete the created glossary
8183
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
82-
System.setOut(null);
84+
// restores print statements in the original method
85+
System.out.flush();
86+
System.setOut(originalPrintStream);
8387
}
8488

8589
@Test

translate/snippets/src/test/java/com/example/translate/TranslateTextWithGlossaryTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class TranslateTextWithGlossaryTests {
4545

4646
private ByteArrayOutputStream bout;
4747
private PrintStream out;
48+
private PrintStream originalPrintStream;
4849

4950
private static void requireEnvVar(String varName) {
5051
assertNotNull(
@@ -70,6 +71,7 @@ public void setUp() throws InterruptedException, ExecutionException, IOException
7071

7172
bout = new ByteArrayOutputStream();
7273
out = new PrintStream(bout);
74+
originalPrintStream = System.out;
7375
System.setOut(out);
7476
}
7577

@@ -78,7 +80,9 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept
7880
// Clean up
7981
// Delete the created glossary
8082
DeleteGlossary.deleteGlossary(PROJECT_ID, GLOSSARY_ID);
81-
System.setOut(null);
83+
// restores print statements in the original method
84+
System.out.flush();
85+
System.setOut(originalPrintStream);
8286
}
8387

8488
@Test

translate/snippets/src/test/java/com/example/translate/TranslateTextWithModelTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class TranslateTextWithModelTests {
3838

3939
private ByteArrayOutputStream bout;
4040
private PrintStream out;
41+
private PrintStream originalPrintStream;
4142

4243
private static void requireEnvVar(String varName) {
4344
assertNotNull(
@@ -55,12 +56,15 @@ public static void checkRequirements() {
5556
public void setUp() {
5657
bout = new ByteArrayOutputStream();
5758
out = new PrintStream(bout);
59+
originalPrintStream = System.out;
5860
System.setOut(out);
5961
}
6062

6163
@After
6264
public void tearDown() {
63-
System.setOut(null);
65+
// restores print statements in the original method
66+
System.out.flush();
67+
System.setOut(originalPrintStream);
6468
}
6569

6670
@Test

0 commit comments

Comments
 (0)