Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace ExpectedException in grpc-api and grpc-core #11962

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies {
testImplementation project(':grpc-core')
testImplementation project(':grpc-testing')
testImplementation libraries.guava.testlib
testImplementation libraries.truth

signature (libraries.signature.java) {
artifact {
Expand Down
35 changes: 14 additions & 21 deletions api/src/test/java/io/grpc/MetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.grpc;

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertArrayEquals;
Expand All @@ -24,6 +25,7 @@
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -37,9 +39,7 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.Locale;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand All @@ -49,9 +49,6 @@
@RunWith(JUnit4.class)
public class MetadataTest {

@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none();

private static final Metadata.BinaryMarshaller<Fish> FISH_MARSHALLER =
new Metadata.BinaryMarshaller<Fish>() {
@Override
Expand All @@ -65,7 +62,7 @@ public Fish parseBytes(byte[] serialized) {
}
};

private static class FishStreamMarsaller implements Metadata.BinaryStreamMarshaller<Fish> {
private static class FishStreamMarshaller implements Metadata.BinaryStreamMarshaller<Fish> {
@Override
public InputStream toStream(Fish fish) {
return new ByteArrayInputStream(FISH_MARSHALLER.toBytes(fish));
Expand All @@ -82,7 +79,7 @@ public Fish parseStream(InputStream stream) {
}

private static final Metadata.BinaryStreamMarshaller<Fish> FISH_STREAM_MARSHALLER =
new FishStreamMarsaller();
new FishStreamMarshaller();

/** A pattern commonly used to avoid unnecessary serialization of immutable objects. */
private static final class FakeFishStream extends InputStream {
Expand Down Expand Up @@ -121,10 +118,9 @@ public Fish parseStream(InputStream stream) {

@Test
public void noPseudoHeaders() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Invalid character");

Metadata.Key.of(":test-bin", FISH_MARSHALLER);
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> Metadata.Key.of(":test-bin", FISH_MARSHALLER));
assertThat(e).hasMessageThat().isEqualTo("Invalid character ':' in key name ':test-bin'");
}

@Test
Expand Down Expand Up @@ -186,8 +182,7 @@ public void testGetAllNoRemove() {
Iterator<Fish> i = metadata.getAll(KEY).iterator();
assertEquals(lance, i.next());

thrown.expect(UnsupportedOperationException.class);
i.remove();
assertThrows(UnsupportedOperationException.class, i::remove);
}

@Test
Expand Down Expand Up @@ -271,17 +266,15 @@ public void mergeExpands() {

@Test
public void shortBinaryKeyName() {
thrown.expect(IllegalArgumentException.class);

Metadata.Key.of("-bin", FISH_MARSHALLER);
assertThrows(IllegalArgumentException.class, () -> Metadata.Key.of("-bin", FISH_MARSHALLER));
}

@Test
public void invalidSuffixBinaryKeyName() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Binary header is named");

Metadata.Key.of("nonbinary", FISH_MARSHALLER);
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> Metadata.Key.of("nonbinary", FISH_MARSHALLER));
assertThat(e).hasMessageThat()
.isEqualTo("Binary header is named nonbinary. It must end with -bin");
}

@Test
Expand Down Expand Up @@ -415,7 +408,7 @@ public void streamedValueDifferentMarshaller() {
h.put(KEY_STREAMED, salmon);

// Get using a different marshaller instance.
Fish fish = h.get(copyKey(KEY_STREAMED, new FishStreamMarsaller()));
Fish fish = h.get(copyKey(KEY_STREAMED, new FishStreamMarshaller()));
assertEquals(salmon, fish);
}

Expand Down
6 changes: 0 additions & 6 deletions api/src/test/java/io/grpc/MethodDescriptorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import io.grpc.MethodDescriptor.Marshaller;
import io.grpc.MethodDescriptor.MethodType;
import io.grpc.testing.TestMethodDescriptors;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand All @@ -37,10 +35,6 @@
*/
@RunWith(JUnit4.class)
public class MethodDescriptorTest {
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule
public final ExpectedException thrown = ExpectedException.none();

@Test
public void createMethodDescriptor() {
MethodDescriptor<String, String> descriptor = MethodDescriptor.<String, String>newBuilder()
Expand Down
18 changes: 7 additions & 11 deletions api/src/test/java/io/grpc/ServerInterceptorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.collect.Iterables.getOnlyElement;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.AdditionalAnswers.delegatesTo;
import static org.mockito.ArgumentMatchers.same;
Expand All @@ -40,7 +41,6 @@
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;
import org.mockito.ArgumentMatchers;
Expand All @@ -55,10 +55,6 @@ public class ServerInterceptorsTest {
@Rule
public final MockitoRule mocks = MockitoJUnit.rule();

@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule
public final ExpectedException thrown = ExpectedException.none();

@Mock
private Marshaller<String> requestMarshaller;

Expand Down Expand Up @@ -111,21 +107,21 @@ public void makeSureExpectedMocksUnused() {
public void npeForNullServiceDefinition() {
ServerServiceDefinition serviceDef = null;
List<ServerInterceptor> interceptors = Arrays.asList();
thrown.expect(NullPointerException.class);
ServerInterceptors.intercept(serviceDef, interceptors);
assertThrows(NullPointerException.class,
() -> ServerInterceptors.intercept(serviceDef, interceptors));
}

@Test
public void npeForNullInterceptorList() {
thrown.expect(NullPointerException.class);
ServerInterceptors.intercept(serviceDefinition, (List<ServerInterceptor>) null);
assertThrows(NullPointerException.class,
() -> ServerInterceptors.intercept(serviceDefinition, (List<ServerInterceptor>) null));
}

@Test
public void npeForNullInterceptor() {
List<ServerInterceptor> interceptors = Arrays.asList((ServerInterceptor) null);
thrown.expect(NullPointerException.class);
ServerInterceptors.intercept(serviceDefinition, interceptors);
assertThrows(NullPointerException.class,
() -> ServerInterceptors.intercept(serviceDefinition, interceptors));
}

@Test
Expand Down
19 changes: 5 additions & 14 deletions api/src/test/java/io/grpc/ServerServiceDefinitionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand All @@ -52,9 +51,6 @@ public class ServerServiceDefinitionTest {
= ServerMethodDefinition.create(method1, methodHandler1);
private ServerMethodDefinition<String, Integer> methodDef2
= ServerMethodDefinition.create(method2, methodHandler2);
@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void noMethods() {
Expand Down Expand Up @@ -91,35 +87,30 @@ public void addMethod_duplicateName() {
ServiceDescriptor sd = new ServiceDescriptor(serviceName, method1);
ServerServiceDefinition.Builder ssd = ServerServiceDefinition.builder(sd)
.addMethod(method1, methodHandler1);
thrown.expect(IllegalStateException.class);
ssd.addMethod(diffMethod1, methodHandler2)
.build();
assertThrows(IllegalStateException.class, () -> ssd.addMethod(diffMethod1, methodHandler2));
}

@Test
public void buildMisaligned_extraMethod() {
ServiceDescriptor sd = new ServiceDescriptor(serviceName);
ServerServiceDefinition.Builder ssd = ServerServiceDefinition.builder(sd)
.addMethod(methodDef1);
thrown.expect(IllegalStateException.class);
ssd.build();
assertThrows(IllegalStateException.class, ssd::build);
}

@Test
public void buildMisaligned_diffMethodInstance() {
ServiceDescriptor sd = new ServiceDescriptor(serviceName, method1);
ServerServiceDefinition.Builder ssd = ServerServiceDefinition.builder(sd)
.addMethod(diffMethod1, methodHandler1);
thrown.expect(IllegalStateException.class);
ssd.build();
assertThrows(IllegalStateException.class, ssd::build);
}

@Test
public void buildMisaligned_missingMethod() {
ServiceDescriptor sd = new ServiceDescriptor(serviceName, method1);
ServerServiceDefinition.Builder ssd = ServerServiceDefinition.builder(sd);
thrown.expect(IllegalStateException.class);
ssd.build();
assertThrows(IllegalStateException.class, ssd::build);
}

@Test
Expand Down
51 changes: 24 additions & 27 deletions api/src/test/java/io/grpc/ServiceDescriptorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@

package io.grpc;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import com.google.common.truth.StringSubject;
import io.grpc.MethodDescriptor.MethodType;
import io.grpc.testing.TestMethodDescriptors;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

Expand All @@ -36,48 +37,45 @@
@RunWith(JUnit4.class)
public class ServiceDescriptorTest {

@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule
public final ExpectedException thrown = ExpectedException.none();

@Test
public void failsOnNullName() {
thrown.expect(NullPointerException.class);
thrown.expectMessage("name");

new ServiceDescriptor(null, Collections.<MethodDescriptor<?, ?>>emptyList());
List<MethodDescriptor<?, ?>> methods = Collections.emptyList();
NullPointerException e = assertThrows(NullPointerException.class,
() -> new ServiceDescriptor(null, methods));
assertThat(e).hasMessageThat().isEqualTo("name");
}

@Test
public void failsOnNullMethods() {
thrown.expect(NullPointerException.class);
thrown.expectMessage("methods");

new ServiceDescriptor("name", (Collection<MethodDescriptor<?, ?>>) null);
NullPointerException e = assertThrows(NullPointerException.class,
() -> new ServiceDescriptor("name", (Collection<MethodDescriptor<?, ?>>) null));
assertThat(e).hasMessageThat().isEqualTo("methods");
}

@Test
public void failsOnNullMethod() {
thrown.expect(NullPointerException.class);
thrown.expectMessage("method");

new ServiceDescriptor("name", Collections.<MethodDescriptor<?, ?>>singletonList(null));
List<MethodDescriptor<?, ?>> methods = Collections.singletonList(null);
NullPointerException e = assertThrows(NullPointerException.class,
() -> new ServiceDescriptor("name", methods));
assertThat(e).hasMessageThat().isEqualTo("method");
}

@Test
public void failsOnNonMatchingNames() {
List<MethodDescriptor<?, ?>> descriptors = Collections.<MethodDescriptor<?, ?>>singletonList(
MethodDescriptor.<Void, Void>newBuilder()
.setType(MethodType.UNARY)
.setFullMethodName(MethodDescriptor.generateFullMethodName("wrongservice", "method"))
.setFullMethodName(MethodDescriptor.generateFullMethodName("wrongService", "method"))
.setRequestMarshaller(TestMethodDescriptors.voidMarshaller())
.setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
.build());

thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("service names");

new ServiceDescriptor("name", descriptors);
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> new ServiceDescriptor("fooService", descriptors));
StringSubject error = assertThat(e).hasMessageThat();
error.contains("service names");
error.contains("fooService");
error.contains("wrongService");
}

@Test
Expand All @@ -96,10 +94,9 @@ public void failsOnNonDuplicateNames() {
.setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
.build());

thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("duplicate");

new ServiceDescriptor("name", descriptors);
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> new ServiceDescriptor("name", descriptors));
assertThat(e).hasMessageThat().isEqualTo("duplicate name name/method");
}

@Test
Expand Down
Loading