Skip to content

Commit f063db7

Browse files
committed
Code style: no multi catch in tests
1 parent 64623f9 commit f063db7

12 files changed

Lines changed: 25 additions & 27 deletions

File tree

.github/agents/knowledge/testing-general-patterns.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
- JUnit 5, AssertJ assertions (not JUnit `assertEquals`/`assertTrue`).
1111
- Test classes and methods should be package-private (no `public`).
1212

13+
## Catch Style
14+
15+
- When multiple caught exceptions in a test have identical handling, prefer a single
16+
catch of their nearest common parent type instead of a multi-catch.
17+
- This preference applies even when the nearest common parent is `Exception` or
18+
`Throwable`.
19+
1320
## Span Attribute Assertions
1421

1522
- Use `span.hasAttributesSatisfyingExactly(...)` with `equalTo(...)`/`satisfies(...)` for

examples/distro/smoke-tests/src/test/java/com/example/javaagent/smoketest/SmokeTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import static java.util.concurrent.TimeUnit.SECONDS;
1010
import static java.util.stream.Collectors.toList;
1111

12-
import com.fasterxml.jackson.core.JsonProcessingException;
1312
import com.fasterxml.jackson.databind.ObjectMapper;
14-
import com.google.protobuf.InvalidProtocolBufferException;
1513
import com.google.protobuf.util.JsonFormat;
1614
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
1715
import io.opentelemetry.proto.trace.v1.Span;
@@ -163,7 +161,7 @@ protected Collection<ExportTraceServiceRequest> waitForTraces()
163161
// TODO: Register parser into object mapper to avoid de -> re -> deserialize.
164162
try {
165163
JsonFormat.parser().merge(OBJECT_MAPPER.writeValueAsString(it), builder);
166-
} catch (InvalidProtocolBufferException | JsonProcessingException e) {
164+
} catch (IOException e) {
167165
e.printStackTrace();
168166
}
169167
return builder.build();

examples/extension/src/test/java/com/example/javaagent/smoketest/IntegrationTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import static java.util.concurrent.TimeUnit.SECONDS;
1010
import static java.util.stream.Collectors.toList;
1111

12-
import com.fasterxml.jackson.core.JsonProcessingException;
1312
import com.fasterxml.jackson.databind.ObjectMapper;
14-
import com.google.protobuf.InvalidProtocolBufferException;
1513
import com.google.protobuf.util.JsonFormat;
1614
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
1715
import io.opentelemetry.proto.trace.v1.Span;
@@ -189,7 +187,7 @@ protected Collection<ExportTraceServiceRequest> waitForTraces()
189187
ExportTraceServiceRequest.Builder builder = ExportTraceServiceRequest.newBuilder();
190188
try {
191189
JsonFormat.parser().merge(OBJECT_MAPPER.writeValueAsString(it), builder);
192-
} catch (InvalidProtocolBufferException | JsonProcessingException e) {
190+
} catch (IOException e) {
193191
e.printStackTrace();
194192
}
195193
return builder.build();

instrumentation/hystrix-1.4/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/hystrix/HystrixTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
2222
import io.opentelemetry.sdk.trace.data.StatusData;
2323
import java.util.concurrent.BlockingQueue;
24-
import java.util.concurrent.ExecutionException;
2524
import java.util.concurrent.LinkedBlockingQueue;
2625
import java.util.function.Function;
2726
import java.util.stream.Stream;
@@ -133,7 +132,7 @@ private static Stream<Arguments> provideCommandActionArguments() {
133132
cmd -> {
134133
try {
135134
return cmd.queue().get();
136-
} catch (InterruptedException | ExecutionException e) {
135+
} catch (Exception e) {
137136
throw new RuntimeException(e);
138137
}
139138
})),

instrumentation/jaxrs-client/jaxrs-client-2.0-testing/src/test/java/io/opentelemetry/javaagent/instrumentation/jaxrsclient/ResteasySingleConnection.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import static java.util.concurrent.TimeUnit.MILLISECONDS;
1010

1111
import io.opentelemetry.instrumentation.testing.junit.http.SingleConnection;
12-
import java.net.MalformedURLException;
1312
import java.net.URI;
14-
import java.net.URISyntaxException;
1513
import java.net.URL;
1614
import java.util.Map;
1715
import java.util.Objects;
@@ -44,7 +42,7 @@ public int doRequest(String path, Map<String, String> headers) throws ExecutionE
4442
URI uri;
4543
try {
4644
uri = new URL("http", host, port, path).toURI();
47-
} catch (MalformedURLException | URISyntaxException e) {
45+
} catch (Exception e) {
4846
throw new ExecutionException(e);
4947
}
5048

instrumentation/jmx-metrics/library/src/test/java/io/opentelemetry/instrumentation/jmx/internal/engine/MetricAggregationTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
import java.util.Locale;
2323
import java.util.concurrent.atomic.AtomicInteger;
2424
import java.util.function.Consumer;
25-
import javax.management.InstanceNotFoundException;
26-
import javax.management.MBeanRegistrationException;
25+
import javax.management.JMException;
2726
import javax.management.MBeanServer;
2827
import javax.management.MBeanServerFactory;
2928
import javax.management.MalformedObjectNameException;
@@ -85,7 +84,7 @@ void after() throws Exception {
8584
instance -> {
8685
try {
8786
theServer.unregisterMBean(instance.getObjectName());
88-
} catch (InstanceNotFoundException | MBeanRegistrationException e) {
87+
} catch (JMException e) {
8988
throw new RuntimeException(e);
9089
}
9190
});

instrumentation/jmx-metrics/library/src/test/java/io/opentelemetry/instrumentation/jmx/rules/TargetSystemTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.util.List;
3636
import java.util.Map;
3737
import java.util.concurrent.BlockingQueue;
38-
import java.util.concurrent.ExecutionException;
3938
import java.util.concurrent.LinkedBlockingDeque;
4039
import org.junit.jupiter.api.AfterAll;
4140
import org.junit.jupiter.api.AfterEach;
@@ -115,7 +114,7 @@ private static void stop(GenericContainer<?> container) {
115114
static void afterAll() {
116115
try {
117116
otlpServer.stop().get();
118-
} catch (InterruptedException | ExecutionException e) {
117+
} catch (Exception e) {
119118
throw new RuntimeException(e);
120119
}
121120
}

instrumentation/log4j/log4j-appender-1.2/javaagent/src/test/java/io/opentelemetry/instrumentation/log4j/appender/v1_2/Log4j1Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Log4j1Test {
4949
Field java1 = Loader.class.getDeclaredField("java1");
5050
java1.setAccessible(true);
5151
java1.set(null, false);
52-
} catch (NoSuchFieldException | IllegalAccessException e) {
52+
} catch (ReflectiveOperationException e) {
5353
throw new RuntimeException(e);
5454
}
5555
}

instrumentation/reactor/reactor-3.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/reactor/v3_1/ContextPropagationOperatorInstrumentationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ private static MethodHandle getContextWriteMethod(Class<?> type) {
4040
MethodHandles.Lookup lookup = MethodHandles.publicLookup();
4141
try {
4242
return lookup.findVirtual(type, "contextWrite", methodType(type, Function.class));
43-
} catch (NoSuchMethodException | IllegalAccessException e) {
43+
} catch (ReflectiveOperationException e) {
4444
// ignore
4545
}
4646
try {
4747
return lookup.findVirtual(type, "subscriberContext", methodType(type, Function.class));
48-
} catch (NoSuchMethodException | IllegalAccessException e) {
48+
} catch (ReflectiveOperationException e) {
4949
// ignore
5050
}
5151
return null;

instrumentation/reactor/reactor-3.1/library/src/test/java/io/opentelemetry/instrumentation/reactor/v3_1/ReactorCoreTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ private static MethodHandle getContextWriteMethod(Class<?> type) {
5353
MethodHandles.Lookup lookup = MethodHandles.publicLookup();
5454
try {
5555
return lookup.findVirtual(type, "contextWrite", methodType(type, Function.class));
56-
} catch (NoSuchMethodException | IllegalAccessException e) {
56+
} catch (ReflectiveOperationException e) {
5757
// ignore
5858
}
5959
try {
6060
return lookup.findVirtual(type, "subscriberContext", methodType(type, Function.class));
61-
} catch (NoSuchMethodException | IllegalAccessException e) {
61+
} catch (ReflectiveOperationException e) {
6262
throw new RuntimeException(e);
6363
}
6464
}
@@ -540,10 +540,10 @@ private static <T> Flux<T> retryWhen(Flux<T> flux) {
540540
Method retryWhenMethod = Flux.class.getMethod("retryWhen", retryClass);
541541
Method retrySpecMethod = retryClass.getMethod("indefinitely");
542542
return (Flux<T>) retryWhenMethod.invoke(flux, retrySpecMethod.invoke(retryClass));
543-
} catch (ClassNotFoundException | NoSuchMethodException exception) {
543+
} catch (ReflectiveOperationException e) {
544544
// ignore
545-
} catch (Exception exception) {
546-
throw new IllegalStateException(exception);
545+
} catch (Exception e) {
546+
throw new IllegalStateException(e);
547547
}
548548
throw new IllegalStateException("Could not find retryWhen method");
549549
}

0 commit comments

Comments
 (0)