Skip to content

Commit

Permalink
Integrationtest (#20)
Browse files Browse the repository at this point in the history
* Introduce modules hierarchy to enable integrationtest module from #17.

* Add module + 'mvn verify' mechanism for integration tests.

* Add integration test for missing `@Priority` annotation.

* Add integration test for missing GlobalTracer class.

* Add unit test for a registered GlobalTracer.
  • Loading branch information
sjoerdtalsma authored Aug 14, 2017
1 parent 851193b commit 021f3c3
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 0 deletions.
94 changes: 94 additions & 0 deletions opentracing-tracerresolver-itest/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2017 The OpenTracing Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-tracerresolver-parent</artifactId>
<version>0.1.2-SNAPSHOT</version>
</parent>

<!-- Artifact identification -->
<artifactId>opentracing-tracerresolver-itest</artifactId>
<name>Tracer resolver - integrationtest</name>

<properties>
<main.basedir>${project.parent.basedir}</main.basedir>
</properties>

<dependencies>
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-tracerresolver</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-mock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven-failsafe-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2017 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentracing.contrib.tracerresolver;

import io.opentracing.Tracer;
import io.opentracing.mock.MockTracer;

import java.util.ArrayList;
import java.util.List;

public final class Mocks {
static final List<Class<?>> calledConverterTypes = new ArrayList<Class<?>>();

public static class FallbackTracer extends MockTracer {
}

public static class ResolvedTracer extends MockTracer {
}

public static class MockTracerResolver extends TracerResolver {
@Override
protected Tracer resolve() {
return new ResolvedTracer();
}
}

public static class NullTracerResolver extends TracerResolver {
@Override
protected Tracer resolve() {
return null;
}
}

public static class ThrowingResolver extends TracerResolver {
@Override
protected Tracer resolve() {
throw new IllegalStateException("Can't resolve tracer (missing configuration?)");
}
}

public static class IdentityConverter implements TracerConverter {
@Override
public Tracer convert(Tracer existingTracer) {
calledConverterTypes.add(getClass());
return existingTracer;
}
}

public static class ThrowingConverter implements TracerConverter {
@Override
public Tracer convert(Tracer existingTracer) {
calledConverterTypes.add(getClass());
throw new UnsupportedOperationException("Conversion of " + existingTracer + " not supported.");
}
}

public static class ConvertToNull implements TracerConverter {
@Override
public Tracer convert(Tracer existingTracer) {
calledConverterTypes.add(getClass());
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2017 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentracing.contrib.tracerresolver;

import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;

/**
* This class tests that the {@link PriorityComparator} doesn't throw any exceptions
* when the {@literal @}Priority annotation is not available on the classpath.
*
* @author Sjoerd Talsma
*/
public class PriorityComparatorIT {

@Test(expected = ClassNotFoundException.class)
public void verifyMissingPriorityAnnotation() throws ClassNotFoundException {
Class.forName("javax.annotation.Priority");
fail("Priority annotation should not be on the classpath.");
}

@Test
public void testPrioritize() {
Iterable anyIterable = mock(Iterable.class);
Iterable prioritized = PriorityComparator.prioritize(anyIterable);
assertThat(prioritized, is(sameInstance(anyIterable))); // Return iterable as-is
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2017 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentracing.contrib.tracerresolver;

import io.opentracing.Tracer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.fail;

/**
* This test repeats the {@linkplain TracerResolver} unit-test, but without the <code>{@literal @}Priority</code>
* and {@code GlobalTracer} dependencies available on the classpath.
*
* @author Sjoerd Talsma
*/
public class TracerResolverTest {
private static final File SERVICES_DIR = new File("target/test-classes/META-INF/services/");

@After
public void cleanServiceFiles() throws IOException {
new File(SERVICES_DIR, TracerResolver.class.getName()).delete();
new File(SERVICES_DIR, Tracer.class.getName()).delete();
}

@Before
@After
public void resetTracerResolver() {
TracerResolver.reload();
}

@Test(expected = ClassNotFoundException.class)
public void verifyGlobalTracerAbsence() throws ClassNotFoundException {
Class.forName("io.opentracing.util.GlobalTracer");
fail("GlobalTracer should not be on the classpath for this test.");
}

@Test
public void testNothingRegistered() throws IOException {
assertThat(TracerResolver.resolveTracer(), is(nullValue()));
}

@Test
public void testResolveTracer() throws IOException {
writeServiceFile(TracerResolver.class, Mocks.MockTracerResolver.class);
assertThat(TracerResolver.resolveTracer(), is(instanceOf(Mocks.ResolvedTracer.class)));
}

@Test
public void testResolveFallback() throws IOException {
writeServiceFile(Tracer.class, Mocks.FallbackTracer.class);
assertThat(TracerResolver.resolveTracer(), is(instanceOf(Mocks.FallbackTracer.class)));
}

@Test
public void testResolverBeatsFallback() throws IOException {
writeServiceFile(Tracer.class, Mocks.FallbackTracer.class);
writeServiceFile(TracerResolver.class, Mocks.MockTracerResolver.class);
assertThat(TracerResolver.resolveTracer(), is(instanceOf(Mocks.ResolvedTracer.class)));
}

@Test
public void testFallbackWhenResolvingNull() throws IOException {
writeServiceFile(Tracer.class, Mocks.FallbackTracer.class);
writeServiceFile(TracerResolver.class, Mocks.NullTracerResolver.class);
assertThat(TracerResolver.resolveTracer(), is(instanceOf(Mocks.FallbackTracer.class)));
}

@Test
public void testResolvingNullWithoutFallback() throws IOException {
writeServiceFile(TracerResolver.class, Mocks.NullTracerResolver.class);
assertThat(TracerResolver.resolveTracer(), is(nullValue()));
}

@Test
public void testSkipResolverThrowingException() throws IOException {
writeServiceFile(TracerResolver.class, Mocks.ThrowingResolver.class, Mocks.MockTracerResolver.class);
assertThat(TracerResolver.resolveTracer(), is(instanceOf(Mocks.ResolvedTracer.class)));
}

static <SVC> void writeServiceFile(Class<SVC> service, Class<?>... implementations) throws IOException {
SERVICES_DIR.mkdirs();
File serviceFile = new File(SERVICES_DIR, service.getName());
if (serviceFile.isFile()) serviceFile.delete();
PrintWriter writer = new PrintWriter(new FileWriter(serviceFile));
try {
for (Class<?> implementation : implementations) {
writer.println(implementation.getName());
}
} finally {
writer.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
*/
package io.opentracing.contrib.tracerresolver;

import io.opentracing.NoopTracerFactory;
import io.opentracing.mock.MockTracer;
import io.opentracing.util.GlobalTracer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;

import static io.opentracing.contrib.tracerresolver.TracerResolverTest.writeServiceFile;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -29,6 +33,9 @@
public class TracerConverterTest {
private static final File SERVICES_DIR = new File("target/test-classes/META-INF/services/");

/**
* Clean up any service files we may have created during our tests.
*/
@After
public void cleanServiceFiles() throws IOException {
new File(SERVICES_DIR, TracerResolver.class.getName()).delete();
Expand All @@ -42,6 +49,18 @@ public void resetTracerResolver() {
TracerResolver.reload();
}

/**
* If a GlobalTracer is registered, converters are not applied.
* To test this, the globaltracer needs to be cleared before and after the tests.
*/
@Before
@After
public void clearGlobalTracer() throws NoSuchFieldException, IllegalAccessException {
Field globalTracer = GlobalTracer.class.getDeclaredField("tracer");
globalTracer.setAccessible(true);
globalTracer.set(null, NoopTracerFactory.create());
}

@Test
public void testConverterNotCalledWhenNothingRegistered() throws IOException {
writeServiceFile(TracerConverter.class, Mocks.IdentityConverter.class);
Expand Down Expand Up @@ -90,4 +109,13 @@ public void testconvertToNull() throws IOException {
assertThat("Prioritized converter", Mocks.calledConverterTypes, contains((Class) Mocks.Prio10_ConvertToNull.class));
}

@Test
public void testConvertersIgnoredWithExistingGlobalTracer() throws IOException {
writeServiceFile(TracerResolver.class, Mocks.MockTracerResolver.class);
writeServiceFile(TracerConverter.class, Mocks.IdentityConverter.class, Mocks.Prio5_ThrowingConverter.class);
GlobalTracer.register(new MockTracer());

assertThat("Resolved tracer", TracerResolver.resolveTracer(), is(sameInstance(GlobalTracer.get())));
assertThat("Called converter types", Mocks.calledConverterTypes, is(empty()));
}
}
Loading

0 comments on commit 021f3c3

Please sign in to comment.