-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Platform for wrapping IOExceptions properly (#1713)
This is used for pending work in SpanStore v2. Note: this downgrades mockito as it conflicts with powermock and spring boot versions.
- Loading branch information
1 parent
88f1f29
commit ab09853
Showing
10 changed files
with
191 additions
and
12 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
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
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
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
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
87 changes: 87 additions & 0 deletions
87
zipkin/src/main/java/zipkin/internal/v2/internal/Platform.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,87 @@ | ||
/** | ||
* 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 java.io.IOException; | ||
import org.jvnet.animal_sniffer.IgnoreJRERequirement; | ||
|
||
/** | ||
* Provides access to platform-specific features. | ||
* | ||
* <p>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(); | ||
} | ||
} | ||
} |
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
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
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
62 changes: 62 additions & 0 deletions
62
zipkin/src/test/java/zipkin/internal/v2/internal/PlatformTest.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,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); | ||
} | ||
} |