Skip to content

Commit 2f136de

Browse files
Copilottrask
andcommitted
Remove final keyword from method parameters and convert more JUnit assertions to AssertJ
Co-authored-by: trask <[email protected]>
1 parent 902594d commit 2f136de

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

consistent-sampling/src/test/java/io/opentelemetry/contrib/sampler/consistent/ConsistentSamplerTest.java

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import static io.opentelemetry.contrib.sampler.consistent.OtelTraceState.getInvalidP;
99
import static io.opentelemetry.contrib.sampler.consistent.OtelTraceState.getInvalidR;
1010
import static org.assertj.core.api.Assertions.assertThat;
11-
import static org.junit.jupiter.api.Assertions.assertEquals;
12-
import static org.junit.jupiter.api.Assertions.assertFalse;
13-
import static org.junit.jupiter.api.Assertions.assertThrows;
11+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1412

1513
import io.opentelemetry.api.common.Attributes;
1614
import io.opentelemetry.api.trace.Span;
@@ -32,45 +30,45 @@ class ConsistentSamplerTest {
3230

3331
@Test
3432
void testGetSamplingRate() {
35-
assertThrows(
36-
IllegalArgumentException.class, () -> ConsistentSampler.getSamplingProbability(-1));
33+
assertThatThrownBy(() -> ConsistentSampler.getSamplingProbability(-1))
34+
.isInstanceOf(IllegalArgumentException.class);
3735
for (int i = 0; i < OtelTraceState.getMaxP() - 1; i += 1) {
38-
assertEquals(Math.pow(0.5, i), ConsistentSampler.getSamplingProbability(i));
36+
assertThat(ConsistentSampler.getSamplingProbability(i)).isEqualTo(Math.pow(0.5, i));
3937
}
40-
assertEquals(0., ConsistentSampler.getSamplingProbability(OtelTraceState.getMaxP()));
41-
assertThrows(
42-
IllegalArgumentException.class,
43-
() -> ConsistentSampler.getSamplingProbability(OtelTraceState.getMaxP() + 1));
38+
assertThat(ConsistentSampler.getSamplingProbability(OtelTraceState.getMaxP())).isEqualTo(0.);
39+
assertThatThrownBy(
40+
() -> ConsistentSampler.getSamplingProbability(OtelTraceState.getMaxP() + 1))
41+
.isInstanceOf(IllegalArgumentException.class);
4442
}
4543

4644
@Test
4745
void testGetLowerBoundP() {
48-
assertEquals(0, ConsistentSampler.getLowerBoundP(1.0));
49-
assertEquals(0, ConsistentSampler.getLowerBoundP(Math.nextDown(1.0)));
46+
assertThat(ConsistentSampler.getLowerBoundP(1.0)).isEqualTo(0);
47+
assertThat(ConsistentSampler.getLowerBoundP(Math.nextDown(1.0))).isEqualTo(0);
5048
for (int i = 1; i < OtelTraceState.getMaxP() - 1; i += 1) {
5149
double samplingProbability = Math.pow(0.5, i);
52-
assertEquals(i, ConsistentSampler.getLowerBoundP(samplingProbability));
53-
assertEquals(i - 1, ConsistentSampler.getLowerBoundP(Math.nextUp(samplingProbability)));
54-
assertEquals(i, ConsistentSampler.getLowerBoundP(Math.nextDown(samplingProbability)));
50+
assertThat(ConsistentSampler.getLowerBoundP(samplingProbability)).isEqualTo(i);
51+
assertThat(ConsistentSampler.getLowerBoundP(Math.nextUp(samplingProbability))).isEqualTo(i - 1);
52+
assertThat(ConsistentSampler.getLowerBoundP(Math.nextDown(samplingProbability))).isEqualTo(i);
5553
}
56-
assertEquals(OtelTraceState.getMaxP() - 1, ConsistentSampler.getLowerBoundP(Double.MIN_NORMAL));
57-
assertEquals(OtelTraceState.getMaxP() - 1, ConsistentSampler.getLowerBoundP(Double.MIN_VALUE));
58-
assertEquals(OtelTraceState.getMaxP(), ConsistentSampler.getLowerBoundP(0.0));
54+
assertThat(ConsistentSampler.getLowerBoundP(Double.MIN_NORMAL)).isEqualTo(OtelTraceState.getMaxP() - 1);
55+
assertThat(ConsistentSampler.getLowerBoundP(Double.MIN_VALUE)).isEqualTo(OtelTraceState.getMaxP() - 1);
56+
assertThat(ConsistentSampler.getLowerBoundP(0.0)).isEqualTo(OtelTraceState.getMaxP());
5957
}
6058

6159
@Test
6260
void testGetUpperBoundP() {
63-
assertEquals(0, ConsistentSampler.getUpperBoundP(1.0));
64-
assertEquals(1, ConsistentSampler.getUpperBoundP(Math.nextDown(1.0)));
61+
assertThat(ConsistentSampler.getUpperBoundP(1.0)).isEqualTo(0);
62+
assertThat(ConsistentSampler.getUpperBoundP(Math.nextDown(1.0))).isEqualTo(1);
6563
for (int i = 1; i < OtelTraceState.getMaxP() - 1; i += 1) {
6664
double samplingProbability = Math.pow(0.5, i);
67-
assertEquals(i, ConsistentSampler.getUpperBoundP(samplingProbability));
68-
assertEquals(i, ConsistentSampler.getUpperBoundP(Math.nextUp(samplingProbability)));
69-
assertEquals(i + 1, ConsistentSampler.getUpperBoundP(Math.nextDown(samplingProbability)));
65+
assertThat(ConsistentSampler.getUpperBoundP(samplingProbability)).isEqualTo(i);
66+
assertThat(ConsistentSampler.getUpperBoundP(Math.nextUp(samplingProbability))).isEqualTo(i);
67+
assertThat(ConsistentSampler.getUpperBoundP(Math.nextDown(samplingProbability))).isEqualTo(i + 1);
7068
}
71-
assertEquals(OtelTraceState.getMaxP(), ConsistentSampler.getUpperBoundP(Double.MIN_NORMAL));
72-
assertEquals(OtelTraceState.getMaxP(), ConsistentSampler.getUpperBoundP(Double.MIN_VALUE));
73-
assertEquals(OtelTraceState.getMaxP(), ConsistentSampler.getUpperBoundP(0.0));
69+
assertThat(ConsistentSampler.getUpperBoundP(Double.MIN_NORMAL)).isEqualTo(OtelTraceState.getMaxP());
70+
assertThat(ConsistentSampler.getUpperBoundP(Double.MIN_VALUE)).isEqualTo(OtelTraceState.getMaxP());
71+
assertThat(ConsistentSampler.getUpperBoundP(0.0)).isEqualTo(OtelTraceState.getMaxP());
7472
}
7573

7674
@Test
@@ -168,18 +166,18 @@ private static void assertConsistentSampling(
168166
SamplingResult samplingResult =
169167
sampler.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks);
170168

171-
assertEquals(expectSampled, getSampledFlag(samplingResult));
169+
assertThat(getSampledFlag(samplingResult)).isEqualTo(expectSampled);
172170
OptionalInt p = getP(samplingResult, parentContext);
173171
if (OtelTraceState.isValidP(expectedP)) {
174-
assertEquals(expectedP, p.getAsInt());
172+
assertThat(p.getAsInt()).isEqualTo(expectedP);
175173
} else {
176-
assertFalse(p.isPresent());
174+
assertThat(p.isPresent()).isFalse();
177175
}
178176
OptionalInt r = getR(samplingResult, parentContext);
179177
if (OtelTraceState.isValidR(expectedR)) {
180-
assertEquals(expectedR, r.getAsInt());
178+
assertThat(r.getAsInt()).isEqualTo(expectedR);
181179
} else {
182-
assertFalse(r.isPresent());
180+
assertThat(r.isPresent()).isFalse();
183181
}
184182
}
185183

jmx-metrics/src/main/groovy/io/opentelemetry/contrib/jmxmetrics/GroovyRunner.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class GroovyRunner {
4444
List<String> scriptSources = new ArrayList<>();
4545
try {
4646
if (config.targetSystems.size() != 0) {
47-
for (final String target : config.targetSystems) {
47+
for (String target : config.targetSystems) {
4848
String systemResourcePath = "target-systems/" + target + ".groovy";
4949
scriptSources.add(getTargetSystemResourceAsString(systemResourcePath));
5050
}
@@ -59,7 +59,7 @@ public class GroovyRunner {
5959

6060
this.scripts = new ArrayList<>();
6161
try {
62-
for (final String source : scriptSources) {
62+
for (String source : scriptSources) {
6363
this.scripts.add(new GroovyShell().parse(source));
6464
}
6565
} catch (CompilationFailedException e) {
@@ -74,18 +74,18 @@ public class GroovyRunner {
7474
new OtelHelper(jmxClient, this.groovyMetricEnvironment, config.aggregateAcrossMBeans);
7575
binding.setVariable("otel", otelHelper);
7676

77-
for (final Script script : scripts) {
77+
for (Script script : scripts) {
7878
script.setBinding(binding);
7979
}
8080
}
8181

82-
private static String getFileAsString(final String fileName) throws IOException {
82+
private static String getFileAsString(String fileName) throws IOException {
8383
try (InputStream is = new FileInputStream(fileName)) {
8484
return getFileFromInputStream(is);
8585
}
8686
}
8787

88-
private String getTargetSystemResourceAsString(final String targetSystem) throws IOException {
88+
private String getTargetSystemResourceAsString(String targetSystem) throws IOException {
8989
URL res = getClass().getClassLoader().getResource(targetSystem);
9090
if (res == null) {
9191
throw new ConfigurationException("Failed to load " + targetSystem);

0 commit comments

Comments
 (0)