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

Migrate tests to JUnit5 #1489

Merged
merged 3 commits into from
Jul 31, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.lang.syntax.elements.ClassesShouldConjunction;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class InternalApiProtectionTest {
class InternalApiProtectionTest {

private static final String OTEL_BASE_PACKAGE = "io.opentelemetry";
private static final JavaClasses ALL_OTEL_CLASSES =
new ClassFileImporter().importPackages(OTEL_BASE_PACKAGE);

@Test
public void contrib_should_not_use_internal_api() {
void contrib_should_not_use_internal_api() {
ClassesShouldConjunction contribRule =
noClasses()
.that()
Expand Down
62 changes: 26 additions & 36 deletions api/src/test/java/io/opentelemetry/OpenTelemetryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
package io.opentelemetry;

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

import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.ContextPropagators;
Expand Down Expand Up @@ -54,34 +55,27 @@
import java.io.Writer;
import java.net.URL;
import javax.annotation.Nullable;
import org.junit.After;
import org.junit.BeforeClass;
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.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

@RunWith(JUnit4.class)
public class OpenTelemetryTest {
class OpenTelemetryTest {

@Rule public final ExpectedException thrown = ExpectedException.none();

@BeforeClass
public static void beforeClass() {
@BeforeAll
static void beforeClass() {
OpenTelemetry.reset();
}

@After
public void after() {
@AfterEach
void after() {
OpenTelemetry.reset();
System.clearProperty(TracerProviderFactory.class.getName());
System.clearProperty(MeterProviderFactory.class.getName());
System.clearProperty(CorrelationContextManagerFactory.class.getName());
}

@Test
public void testDefault() {
void testDefault() {
assertThat(OpenTelemetry.getTracerProvider()).isInstanceOf(DefaultTracerProvider.class);
assertThat(OpenTelemetry.getTracerProvider())
.isSameInstanceAs(OpenTelemetry.getTracerProvider());
Expand All @@ -96,7 +90,7 @@ public void testDefault() {
}

@Test
public void testTracerLoadArbitrary() throws IOException {
void testTracerLoadArbitrary() throws IOException {
File serviceFile =
createService(
TracerProviderFactory.class,
Expand All @@ -113,7 +107,7 @@ public void testTracerLoadArbitrary() throws IOException {
}

@Test
public void testTracerSystemProperty() throws IOException {
void testTracerSystemProperty() throws IOException {
File serviceFile =
createService(
TracerProviderFactory.class,
Expand All @@ -130,14 +124,13 @@ public void testTracerSystemProperty() throws IOException {
}

@Test
public void testTracerNotFound() {
void testTracerNotFound() {
System.setProperty(TracerProviderFactory.class.getName(), "io.does.not.exists");
thrown.expect(IllegalStateException.class);
OpenTelemetry.getTracer("testTracer");
assertThrows(IllegalStateException.class, () -> OpenTelemetry.getTracer("testTracer"));
}

@Test
public void testMeterLoadArbitrary() throws IOException {
void testMeterLoadArbitrary() throws IOException {
File serviceFile =
createService(
MeterProviderFactory.class,
Expand All @@ -154,7 +147,7 @@ public void testMeterLoadArbitrary() throws IOException {
}

@Test
public void testMeterSystemProperty() throws IOException {
void testMeterSystemProperty() throws IOException {
File serviceFile =
createService(
MeterProviderFactory.class,
Expand All @@ -171,14 +164,13 @@ public void testMeterSystemProperty() throws IOException {
}

@Test
public void testMeterNotFound() {
void testMeterNotFound() {
System.setProperty(MeterProviderFactory.class.getName(), "io.does.not.exists");
thrown.expect(IllegalStateException.class);
OpenTelemetry.getMeterProvider();
assertThrows(IllegalStateException.class, () -> OpenTelemetry.getMeterProvider());
}

@Test
public void testCorrelationContextManagerLoadArbitrary() throws IOException {
void testCorrelationContextManagerLoadArbitrary() throws IOException {
File serviceFile =
createService(
CorrelationContextManagerFactory.class,
Expand All @@ -197,7 +189,7 @@ public void testCorrelationContextManagerLoadArbitrary() throws IOException {
}

@Test
public void testCorrelationContextManagerSystemProperty() throws IOException {
void testCorrelationContextManagerSystemProperty() throws IOException {
File serviceFile =
createService(
CorrelationContextManagerFactory.class,
Expand All @@ -217,23 +209,21 @@ public void testCorrelationContextManagerSystemProperty() throws IOException {
}

@Test
public void testCorrelationContextManagerNotFound() {
void testCorrelationContextManagerNotFound() {
System.setProperty(CorrelationContextManagerFactory.class.getName(), "io.does.not.exists");
thrown.expect(IllegalStateException.class);
OpenTelemetry.getCorrelationContextManager();
assertThrows(IllegalStateException.class, () -> OpenTelemetry.getCorrelationContextManager());
}

@Test
public void testPropagatorsSet() {
void testPropagatorsSet() {
ContextPropagators propagators = DefaultContextPropagators.builder().build();
OpenTelemetry.setPropagators(propagators);
assertThat(OpenTelemetry.getPropagators()).isEqualTo(propagators);
}

@Test
public void testPropagatorsSetNull() {
thrown.expect(NullPointerException.class);
OpenTelemetry.setPropagators(null);
void testPropagatorsSetNull() {
assertThrows(NullPointerException.class, () -> OpenTelemetry.setPropagators(null));
}

private static File createService(Class<?> service, Class<?>... impls) throws IOException {
Expand Down
16 changes: 6 additions & 10 deletions api/src/test/java/io/opentelemetry/common/AttributeValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
import static com.google.common.truth.Truth.assertThat;

import com.google.common.testing.EqualsTester;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.Test;

/** Unit tests for {@link AttributeValue}. */
@RunWith(JUnit4.class)
public class AttributeValueTest {
class AttributeValueTest {

@Test
public void attributeValue_EqualsAndHashCode() {
void attributeValue_EqualsAndHashCode() {
EqualsTester tester = new EqualsTester();
tester.addEqualityGroup(
AttributeValue.stringAttributeValue("MyStringAttributeValue"),
Expand Down Expand Up @@ -65,7 +61,7 @@ public void attributeValue_EqualsAndHashCode() {
}

@Test
public void doNotCrashOnNull() {
void doNotCrashOnNull() {
AttributeValue.stringAttributeValue(null);
AttributeValue.arrayAttributeValue((String[]) null);
AttributeValue.arrayAttributeValue((Boolean[]) null);
Expand All @@ -74,7 +70,7 @@ public void doNotCrashOnNull() {
}

@Test
public void attributeValue_ToString() {
void attributeValue_ToString() {
AttributeValue attribute = AttributeValue.stringAttributeValue("MyStringAttributeValue");
assertThat(attribute.toString()).contains("MyStringAttributeValue");
attribute = AttributeValue.booleanAttributeValue(true);
Expand All @@ -100,7 +96,7 @@ public void attributeValue_ToString() {
}

@Test
public void arrayAttributeValue_nullValuesWithinArray() {
void arrayAttributeValue_nullValuesWithinArray() {
AttributeValue attribute;

attribute = AttributeValue.arrayAttributeValue("string", null, "", "string");
Expand Down
27 changes: 12 additions & 15 deletions api/src/test/java/io/opentelemetry/common/AttributesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.Test;

/** Unit tests for {@link Attributes}s. */
public class AttributesTest {
@Rule public final ExpectedException thrown = ExpectedException.none();
class AttributesTest {

@Test
public void forEach() {
void forEach() {
final Map<String, AttributeValue> entriesSeen = new HashMap<>();

Attributes attributes =
Expand All @@ -50,7 +47,7 @@ public void forEach() {
}

@Test
public void forEach_singleAttribute() {
void forEach_singleAttribute() {
final Map<String, AttributeValue> entriesSeen = new HashMap<>();

Attributes attributes = Attributes.of("key", stringAttributeValue("value"));
Expand All @@ -59,15 +56,15 @@ public void forEach_singleAttribute() {
}

@Test
public void forEach_empty() {
void forEach_empty() {
final AtomicBoolean sawSomething = new AtomicBoolean(false);
Attributes emptyAttributes = Attributes.empty();
emptyAttributes.forEach((key, value) -> sawSomething.set(true));
assertThat(sawSomething.get()).isFalse();
}

@Test
public void orderIndependentEquality() {
void orderIndependentEquality() {
Attributes one =
Attributes.of(
"key1", stringAttributeValue("value1"),
Expand Down Expand Up @@ -103,7 +100,7 @@ public void orderIndependentEquality() {
}

@Test
public void deduplication() {
void deduplication() {
Attributes one =
Attributes.of(
"key1", stringAttributeValue("value1"),
Expand All @@ -114,15 +111,15 @@ public void deduplication() {
}

@Test
public void emptyAndNullKey() {
void emptyAndNullKey() {
Attributes noAttributes =
Attributes.of("", stringAttributeValue("empty"), null, stringAttributeValue("null"));

assertThat(noAttributes.size()).isEqualTo(0);
}

@Test
public void builder() {
void builder() {
Attributes attributes =
Attributes.newBuilder()
.setAttribute("string", "value1")
Expand All @@ -142,7 +139,7 @@ public void builder() {
}

@Test
public void builder_arrayTypes() {
void builder_arrayTypes() {
Attributes attributes =
Attributes.newBuilder()
.setAttribute("string", "value1", "value2")
Expand All @@ -163,13 +160,13 @@ public void builder_arrayTypes() {
}

@Test
public void get_Null() {
void get_Null() {
assertThat(Attributes.empty().get("foo")).isNull();
assertThat(Attributes.of("key", stringAttributeValue("value")).get("foo")).isNull();
}

@Test
public void get() {
void get() {
assertThat(Attributes.of("key", stringAttributeValue("value")).get("key"))
.isEqualTo(stringAttributeValue("value"));
assertThat(Attributes.of("key", stringAttributeValue("value")).get("value")).isNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import org.junit.jupiter.api.Test;

class ImmutableKeyValuePairsTest {

public class ImmutableKeyValuePairsTest {
@Test
public void toStringIsCorrect() {
void toStringIsCorrect() {
assertThat(new TestPairs(Collections.emptyList()).toString()).isEqualTo("{}");
assertThat(new TestPairs(Arrays.asList("one", 55)).toString()).isEqualTo("{one=55}");
assertThat(new TestPairs(Arrays.asList("one", 55, "two", "b")).toString())
Expand Down
Loading