Originally designed by OkHttp team, derived from {@code okhttp3.internal.platform.Platform} + */ +public abstract class Platform { + private static final Platform PLATFORM = findPlatform(); + + Platform() { + } + + public RuntimeException uncheckedIOException(IOException e) { + return new RuntimeException(e); + } + + public static Platform get() { + return PLATFORM; + } + + /** Attempt to match the host runtime to a capable Platform implementation. */ + static Platform findPlatform() { + Platform jre8 = Jre8.buildIfSupported(); + + if (jre8 != null) return jre8; + + Platform jre7 = Jre7.buildIfSupported(); + + if (jre7 != null) return jre7; + + // compatible with JRE 6 + return Jre6.build(); + } + + static final class Jre8 extends Platform { + static Jre8 buildIfSupported() { + // Find JRE 8 new types + try { + Class.forName("java.io.UncheckedIOException"); + return new Jre8(); + } catch (ClassNotFoundException e) { + // pre JRE 8 + } + return null; + } + + @IgnoreJRERequirement @Override public RuntimeException uncheckedIOException(IOException e) { + return new java.io.UncheckedIOException(e); + } + } + + static final class Jre7 extends Platform { + static Jre7 buildIfSupported() { + // Find JRE 7 new types + try { + Class.forName("java.util.concurrent.ThreadLocalRandom"); + return new Jre7(); + } catch (ClassNotFoundException e) { + // pre JRE 7 + } + return null; + } + } + + static final class Jre6 extends Platform { + static Jre6 build() { + return new Jre6(); + } + } +} diff --git a/zipkin/src/test/java/zipkin/collector/CollectorTest.java b/zipkin/src/test/java/zipkin/collector/CollectorTest.java index d0392e1de97..6cbddb0cfa5 100644 --- a/zipkin/src/test/java/zipkin/collector/CollectorTest.java +++ b/zipkin/src/test/java/zipkin/collector/CollectorTest.java @@ -27,8 +27,8 @@ import zipkin.internal.v2.storage.SpanConsumer; import static java.util.Arrays.asList; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; diff --git a/zipkin/src/test/java/zipkin/internal/CollectorTest.java b/zipkin/src/test/java/zipkin/internal/CollectorTest.java index dcf5b4f1b92..8a3a7e41c1d 100644 --- a/zipkin/src/test/java/zipkin/internal/CollectorTest.java +++ b/zipkin/src/test/java/zipkin/internal/CollectorTest.java @@ -23,8 +23,8 @@ import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; diff --git a/zipkin/src/test/java/zipkin/internal/v2/CallTest.java b/zipkin/src/test/java/zipkin/internal/v2/CallTest.java index 1ced8d473ba..067c9f64e69 100644 --- a/zipkin/src/test/java/zipkin/internal/v2/CallTest.java +++ b/zipkin/src/test/java/zipkin/internal/v2/CallTest.java @@ -29,8 +29,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.isA; -import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Matchers.isA; +import static org.mockito.Matchers.isNull; import static org.mockito.Mockito.verify; public class CallTest { diff --git a/zipkin/src/test/java/zipkin/internal/v2/internal/PlatformTest.java b/zipkin/src/test/java/zipkin/internal/v2/internal/PlatformTest.java new file mode 100644 index 00000000000..8635ea056af --- /dev/null +++ b/zipkin/src/test/java/zipkin/internal/v2/internal/PlatformTest.java @@ -0,0 +1,62 @@ +/** + * Copyright 2015-2017 The OpenZipkin 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 zipkin.internal.v2.internal; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; + +@RunWith(PowerMockRunner.class) +// Added to declutter console: tells power mock not to mess with implicit classes we aren't testing +@PowerMockIgnore({"org.apache.logging.*", "javax.script.*"}) +@PrepareForTest(Platform.class) +public class PlatformTest { + + @Test public void findPlatform_jre8Wins() throws ClassNotFoundException { + mockStatic(Class.class); + when(Class.forName("java.io.UncheckedIOException")) + .thenReturn(null); + + assertThat(Platform.findPlatform()) + .isInstanceOf(Platform.Jre8.class); + } + + @Test public void findPlatform_fallsBackToJre7() throws ClassNotFoundException { + mockStatic(Class.class); + when(Class.forName("java.io.UncheckedIOException")) + .thenThrow(new ClassNotFoundException()); + when(Class.forName("java.util.concurrent.ThreadLocalRandom")) + .thenReturn(null); + + assertThat(Platform.findPlatform()) + .isInstanceOf(Platform.Jre7.class); + } + + @Test public void findPlatform_fallsBackToJre6() throws ClassNotFoundException { + mockStatic(Class.class); + when(Class.forName("java.io.UncheckedIOException")) + .thenThrow(new ClassNotFoundException()); + when(Class.forName("java.util.concurrent.ThreadLocalRandom")) + .thenThrow(new ClassNotFoundException()); + + assertThat(Platform.findPlatform()) + .isInstanceOf(Platform.Jre6.class); + } +}