From 44847bf4e9628aea2e2ae64abf8438082d3ebe30 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Fri, 20 Jan 2023 09:33:11 -0800 Subject: [PATCH] Upgrade JUnit to 4.13.2 ExpectedException is deprecated, so I fixed the new warnings. However, we are still using ExpectedException many places and had previously supressed the warning. See https://github.com/grpc/grpc-java/issues/7467 . I did not fix those existing instances that had suppressed the warning, since it is unrelated to the upgrade and we have been free to fix them at any time since we dropped Java 7. --- .../io/grpc/internal/ReadableBuffersTest.java | 9 +--- examples/android/clientcache/app/build.gradle | 2 +- examples/build.gradle | 2 +- examples/example-gauth/pom.xml | 2 +- examples/example-hostname/build.gradle | 2 +- examples/example-hostname/pom.xml | 2 +- examples/example-jwt-auth/build.gradle | 2 +- examples/example-jwt-auth/pom.xml | 2 +- examples/pom.xml | 2 +- gradle/libs.versions.toml | 2 +- .../java/io/grpc/netty/AdvancedTlsTest.java | 52 +++++++++---------- 11 files changed, 35 insertions(+), 44 deletions(-) diff --git a/core/src/test/java/io/grpc/internal/ReadableBuffersTest.java b/core/src/test/java/io/grpc/internal/ReadableBuffersTest.java index 0947f65da12..2bc5a8a3760 100644 --- a/core/src/test/java/io/grpc/internal/ReadableBuffersTest.java +++ b/core/src/test/java/io/grpc/internal/ReadableBuffersTest.java @@ -19,6 +19,7 @@ import static com.google.common.base.Charsets.UTF_8; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.mock; @@ -32,9 +33,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.InvalidMarkException; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -46,9 +45,6 @@ public class ReadableBuffersTest { private static final byte[] MSG_BYTES = "hello".getBytes(UTF_8); - @Rule - public final ExpectedException thrown = ExpectedException.none(); - @Test public void empty_returnsEmptyBuffer() { ReadableBuffer buffer = ReadableBuffers.empty(); @@ -216,8 +212,7 @@ public void bufferInputStream_markDiscardedAfterDetached() throws IOException { InputStream inputStream = ReadableBuffers.openStream(buffer, true); inputStream.mark(5); ((Detachable) inputStream).detach(); - thrown.expect(InvalidMarkException.class); - inputStream.reset(); + assertThrows(InvalidMarkException.class, () -> inputStream.reset()); } @Test diff --git a/examples/android/clientcache/app/build.gradle b/examples/android/clientcache/app/build.gradle index 8bc8093ee68..ae328200bab 100644 --- a/examples/android/clientcache/app/build.gradle +++ b/examples/android/clientcache/app/build.gradle @@ -59,7 +59,7 @@ dependencies { implementation 'io.grpc:grpc-stub:1.53.0-SNAPSHOT' // CURRENT_GRPC_VERSION implementation 'org.apache.tomcat:annotations-api:6.0.53' - testImplementation 'junit:junit:4.12' + testImplementation 'junit:junit:4.13.2' testImplementation 'com.google.truth:truth:1.0.1' testImplementation 'io.grpc:grpc-testing:1.53.0-SNAPSHOT' // CURRENT_GRPC_VERSION } diff --git a/examples/build.gradle b/examples/build.gradle index fb3ba35ba87..981417c0ed9 100644 --- a/examples/build.gradle +++ b/examples/build.gradle @@ -36,7 +36,7 @@ dependencies { runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}" testImplementation "io.grpc:grpc-testing:${grpcVersion}" - testImplementation "junit:junit:4.12" + testImplementation "junit:junit:4.13.2" testImplementation "org.mockito:mockito-core:3.4.0" } diff --git a/examples/example-gauth/pom.xml b/examples/example-gauth/pom.xml index 01c90471119..7c72d687f06 100644 --- a/examples/example-gauth/pom.xml +++ b/examples/example-gauth/pom.xml @@ -73,7 +73,7 @@ junit junit - 4.12 + 4.13.2 test diff --git a/examples/example-hostname/build.gradle b/examples/example-hostname/build.gradle index 47cd17e4c73..884bea95c55 100644 --- a/examples/example-hostname/build.gradle +++ b/examples/example-hostname/build.gradle @@ -31,7 +31,7 @@ dependencies { compileOnly "org.apache.tomcat:annotations-api:6.0.53" runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}" - testImplementation 'junit:junit:4.12' + testImplementation 'junit:junit:4.13.2' testImplementation "io.grpc:grpc-testing:${grpcVersion}" } diff --git a/examples/example-hostname/pom.xml b/examples/example-hostname/pom.xml index 1acbc769207..a0ea9d4e4dd 100644 --- a/examples/example-hostname/pom.xml +++ b/examples/example-hostname/pom.xml @@ -58,7 +58,7 @@ junit junit - 4.12 + 4.13.2 test diff --git a/examples/example-jwt-auth/build.gradle b/examples/example-jwt-auth/build.gradle index 5468b14d9f8..1c20213407c 100644 --- a/examples/example-jwt-auth/build.gradle +++ b/examples/example-jwt-auth/build.gradle @@ -37,7 +37,7 @@ dependencies { runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}" testImplementation "io.grpc:grpc-testing:${grpcVersion}" - testImplementation "junit:junit:4.12" + testImplementation "junit:junit:4.13.2" testImplementation "org.mockito:mockito-core:3.4.0" } diff --git a/examples/example-jwt-auth/pom.xml b/examples/example-jwt-auth/pom.xml index aef4d5ba068..abfdb0793df 100644 --- a/examples/example-jwt-auth/pom.xml +++ b/examples/example-jwt-auth/pom.xml @@ -71,7 +71,7 @@ junit junit - 4.12 + 4.13.2 test diff --git a/examples/pom.xml b/examples/pom.xml index 3a2d419e684..320b72b89f9 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -70,7 +70,7 @@ junit junit - 4.12 + 4.13.2 test diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 87c26378827..6e377bb4c3a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -39,7 +39,7 @@ hdrhistogram = "org.hdrhistogram:HdrHistogram:2.1.12" javax-annotation = "org.apache.tomcat:annotations-api:6.0.53" jetty-alpn-agent = "org.mortbay.jetty.alpn:jetty-alpn-agent:2.0.10" jsr305 = "com.google.code.findbugs:jsr305:3.0.2" -junit = "junit:junit:4.12" +junit = "junit:junit:4.13.2" mockito-android = "org.mockito:mockito-android:3.8.0" mockito-core = "org.mockito:mockito-core:3.3.3" netty-codec-http2 = { module = "io.netty:netty-codec-http2", version.ref = "netty" } diff --git a/netty/src/test/java/io/grpc/netty/AdvancedTlsTest.java b/netty/src/test/java/io/grpc/netty/AdvancedTlsTest.java index 98bb4ea81ed..fafe33c0ee2 100644 --- a/netty/src/test/java/io/grpc/netty/AdvancedTlsTest.java +++ b/netty/src/test/java/io/grpc/netty/AdvancedTlsTest.java @@ -16,8 +16,10 @@ package io.grpc.netty; +import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; import com.google.common.util.concurrent.MoreExecutors; @@ -56,9 +58,7 @@ import javax.net.ssl.SSLEngine; import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -89,9 +89,6 @@ public class AdvancedTlsTest { private PrivateKey serverKeyBad; private X509Certificate[] serverCertBad; - @Rule - public ExpectedException exceptionRule = ExpectedException.none(); - @Before public void setUp() throws NoSuchAlgorithmException, IOException, CertificateException, InvalidKeySpecException { @@ -427,24 +424,22 @@ public void onFileLoadingKeyManagerTrustManagerTest() throws Exception { @Test public void onFileReloadingKeyManagerBadInitialContentTest() throws Exception { - exceptionRule.expect(GeneralSecurityException.class); AdvancedTlsX509KeyManager keyManager = new AdvancedTlsX509KeyManager(); // We swap the order of key and certificates to intentionally create an exception. - Closeable keyShutdown = keyManager.updateIdentityCredentialsFromFile(serverCert0File, - serverKey0File, 100, TimeUnit.MILLISECONDS, executor); - keyShutdown.close(); + assertThrows(GeneralSecurityException.class, + () -> keyManager.updateIdentityCredentialsFromFile(serverCert0File, + serverKey0File, 100, TimeUnit.MILLISECONDS, executor)); } @Test public void onFileReloadingTrustManagerBadInitialContentTest() throws Exception { - exceptionRule.expect(GeneralSecurityException.class); AdvancedTlsX509TrustManager trustManager = AdvancedTlsX509TrustManager.newBuilder() .setVerification(Verification.CERTIFICATE_ONLY_VERIFICATION) .build(); // We pass in a key as the trust certificates to intentionally create an exception. - Closeable trustShutdown = trustManager.updateTrustCredentialsFromFile(serverKey0File, - 100, TimeUnit.MILLISECONDS, executor); - trustShutdown.close(); + assertThrows(GeneralSecurityException.class, + () -> trustManager.updateTrustCredentialsFromFile(serverKey0File, + 100, TimeUnit.MILLISECONDS, executor)); } @Test @@ -472,40 +467,38 @@ public void trustManagerCheckTrustedWithSocketTest() throws Exception { @Test public void trustManagerCheckClientTrustedWithoutParameterTest() throws Exception { - exceptionRule.expect(CertificateException.class); - exceptionRule.expectMessage( - "Not enough information to validate peer. SSLEngine or Socket required."); AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager.newBuilder() .setVerification(Verification.INSECURELY_SKIP_ALL_VERIFICATION).build(); - tm.checkClientTrusted(serverCert0, "RSA"); + CertificateException ex = + assertThrows(CertificateException.class, () -> tm.checkClientTrusted(serverCert0, "RSA")); + assertThat(ex).hasMessageThat() + .isEqualTo("Not enough information to validate peer. SSLEngine or Socket required."); } @Test public void trustManagerCheckServerTrustedWithoutParameterTest() throws Exception { - exceptionRule.expect(CertificateException.class); - exceptionRule.expectMessage( - "Not enough information to validate peer. SSLEngine or Socket required."); AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager.newBuilder() .setVerification(Verification.INSECURELY_SKIP_ALL_VERIFICATION).build(); - tm.checkServerTrusted(serverCert0, "RSA"); + CertificateException ex = + assertThrows(CertificateException.class, () -> tm.checkServerTrusted(serverCert0, "RSA")); + assertThat(ex).hasMessageThat() + .isEqualTo("Not enough information to validate peer. SSLEngine or Socket required."); } @Test public void trustManagerEmptyChainTest() throws Exception { - exceptionRule.expect(IllegalArgumentException.class); - exceptionRule.expectMessage( - "Want certificate verification but got null or empty certificates"); AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager.newBuilder() .setVerification(Verification.CERTIFICATE_ONLY_VERIFICATION) .build(); tm.updateTrustCredentials(caCert); - tm.checkClientTrusted(null, "RSA", (SSLEngine) null); + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, + () -> tm.checkClientTrusted(null, "RSA", (SSLEngine) null)); + assertThat(ex).hasMessageThat() + .isEqualTo("Want certificate verification but got null or empty certificates"); } @Test public void trustManagerBadCustomVerificationTest() throws Exception { - exceptionRule.expect(CertificateException.class); - exceptionRule.expectMessage("Bad Custom Verification"); AdvancedTlsX509TrustManager tm = AdvancedTlsX509TrustManager.newBuilder() .setVerification(Verification.CERTIFICATE_ONLY_VERIFICATION) .setSslSocketAndEnginePeerVerifier( @@ -523,7 +516,10 @@ public void verifyPeerCertificate(X509Certificate[] peerCertChain, String authTy } }).build(); tm.updateTrustCredentials(caCert); - tm.checkClientTrusted(serverCert0, "RSA", new Socket()); + CertificateException ex = assertThrows( + CertificateException.class, + () -> tm.checkClientTrusted(serverCert0, "RSA", new Socket())); + assertThat(ex).hasMessageThat().isEqualTo("Bad Custom Verification"); } private static class SimpleServiceImpl extends SimpleServiceGrpc.SimpleServiceImplBase {