Skip to content

Commit 2ad4eea

Browse files
author
Steven van Rossum
authored
Drop junit-dep dependency by removing system-rules usage (#35464)
* Drop junit-dep dependency by removing system-rules usage * Use explicit CharSet * Run spotless * Use encoding name instead of CharSet * Set line separator as scanner delimiter * Use hasItem matcher instead of contains matcher * Minimize scope of stdout redirection
1 parent 42f232b commit 2ad4eea

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

sdks/java/container/license_scripts/dep_urls_java.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ jackson-bom:
6161
'2.15.4':
6262
license: "https://raw.githubusercontent.com/FasterXML/jackson-bom/master/LICENSE"
6363
type: "Apache License 2.0"
64-
junit-dep:
65-
'4.11':
66-
license: "https://opensource.org/licenses/cpl1.0.txt"
67-
type: "Common Public License Version 1.0"
6864
org.eclipse.jgit:
6965
'4.4.1.201607150455-r':
7066
license: "https://www.eclipse.org/org/documents/edl-v10.html"

sdks/java/core/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ dependencies {
106106
implementation library.java.snake_yaml
107107
shadowTest library.java.everit_json_schema
108108
provided library.java.junit
109-
testImplementation "com.github.stefanbirkner:system-rules:1.19.0"
110109
provided library.java.hamcrest
111110
provided 'io.airlift:aircompressor:0.18'
112111
provided 'com.facebook.presto.hadoop:hadoop-apache2:3.2.0-1'
@@ -125,7 +124,6 @@ dependencies {
125124
shadowTest library.java.log4j2_api
126125
shadowTest library.java.jamm
127126
testRuntimeOnly library.java.slf4j_jdk14
128-
testImplementation "com.github.stefanbirkner:system-rules:1.19.0"
129127
}
130128

131129
project.tasks.compileTestJava {

sdks/java/core/src/test/java/org/apache/beam/sdk/fn/JvmInitializersTest.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,26 @@
1717
*/
1818
package org.apache.beam.sdk.fn;
1919

20+
import static org.hamcrest.MatcherAssert.assertThat;
21+
import static org.hamcrest.Matchers.hasItem;
2022
import static org.hamcrest.core.StringContains.containsString;
2123
import static org.junit.Assert.assertEquals;
2224
import static org.junit.Assert.assertTrue;
2325

2426
import com.google.auto.service.AutoService;
27+
import java.io.ByteArrayInputStream;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
30+
import java.io.PrintStream;
31+
import java.io.UnsupportedEncodingException;
32+
import java.util.Scanner;
2533
import org.apache.beam.sdk.harness.JvmInitializer;
2634
import org.apache.beam.sdk.options.PipelineOptions;
2735
import org.apache.beam.sdk.testing.ExpectedLogs;
2836
import org.apache.beam.sdk.testing.TestPipeline;
29-
import org.hamcrest.MatcherAssert;
3037
import org.junit.Before;
3138
import org.junit.Rule;
3239
import org.junit.Test;
33-
import org.junit.contrib.java.lang.system.SystemOutRule;
3440
import org.junit.runner.RunWith;
3541
import org.junit.runners.JUnit4;
3642

@@ -39,7 +45,6 @@
3945
public final class JvmInitializersTest {
4046

4147
@Rule public ExpectedLogs expectedLogs = ExpectedLogs.none(JvmInitializers.class);
42-
@Rule public SystemOutRule systemOutRule = new SystemOutRule().enableLog();
4348

4449
private static Boolean onStartupRan;
4550
private static Boolean beforeProcessingRan;
@@ -68,12 +73,23 @@ public void setUp() {
6873
}
6974

7075
@Test
71-
public void runOnStartup_runsInitializers() {
72-
JvmInitializers.runOnStartup();
76+
public void runOnStartup_runsInitializers() throws IOException, UnsupportedEncodingException {
77+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
78+
79+
PrintStream out = System.out;
80+
try (PrintStream ps = new PrintStream(baos, false, "UTF8")) {
81+
System.setOut(ps);
82+
JvmInitializers.runOnStartup();
83+
} finally {
84+
System.setOut(out);
85+
}
7386

7487
assertTrue(onStartupRan);
75-
MatcherAssert.assertThat(
76-
systemOutRule.getLog(), containsString("Running JvmInitializer#onStartup"));
88+
assertThat(
89+
() ->
90+
new Scanner(new ByteArrayInputStream(baos.toByteArray()), "UTF8")
91+
.useDelimiter(System.lineSeparator()),
92+
hasItem(containsString("Running JvmInitializer#onStartup")));
7793
}
7894

7995
@Test

0 commit comments

Comments
 (0)