-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
851193b
commit 021f3c3
Showing
7 changed files
with
386 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
78 changes: 78 additions & 0 deletions
78
...acing-tracerresolver-itest/src/test/java/io/opentracing/contrib/tracerresolver/Mocks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...olver-itest/src/test/java/io/opentracing/contrib/tracerresolver/PriorityComparatorIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
...esolver-itest/src/test/java/io/opentracing/contrib/tracerresolver/TracerResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.