Skip to content

Commit

Permalink
add tests for newInstance methods on JavaxJsonAdaptation
Browse files Browse the repository at this point in the history
  • Loading branch information
ceharris committed Sep 14, 2018
1 parent e33b332 commit 5dfcb2b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
*/
public abstract class JavaxJsonAdaptation implements JsonAdaptation<JsonValue> {

private static final String JSR_374_ADAPTATION = "Jsr374Adaptation";
private static final String JSR_353_ADAPTATION = "Jsr353Adaptation";
static final String JSR_374_ADAPTATION = "Jsr374Adaptation";
static final String JSR_353_ADAPTATION = "Jsr353Adaptation";

@Override
public Class<?> arrayType() {
Expand Down Expand Up @@ -67,17 +67,12 @@ public static JavaxJsonAdaptation newInstance() {
* provided by OSGi.
*
* @param classLoader the class loader to use to find the JSON-P API and
* implementation classes
* implementation classes
* @return adaptation instance for JSON-P data types
* @throws RuntimeException if the adaptation class cannot be instantiated
*/
public static JavaxJsonAdaptation newInstance(ClassLoader classLoader) {
try {
Json.class.getMethod("createValue", String.class);
return newInstance(classLoader, JSR_374_ADAPTATION);
} catch (NoSuchMethodException ex) {
return newInstance(classLoader, JSR_353_ADAPTATION);
}
return newInstance(classLoader, determineProvider("createValue", String.class));
}

/**
Expand All @@ -88,7 +83,7 @@ public static JavaxJsonAdaptation newInstance(ClassLoader classLoader) {
* @return adaptation instance
* @throws RuntimeException an instance of the specified type cannot be instantiated
*/
private static JavaxJsonAdaptation newInstance(ClassLoader classLoader,
static JavaxJsonAdaptation newInstance(ClassLoader classLoader,
String providerName) {
try {
return (JavaxJsonAdaptation) classLoader.loadClass(
Expand All @@ -99,6 +94,22 @@ private static JavaxJsonAdaptation newInstance(ClassLoader classLoader,
}
}

/**
* Determine the name of the adaptation provider class based on the availability
* of a particular sentinel method in the {@link Json} class.
* @param sentinelMethodName name of the method whose presence is to be checked
* @param argTypes argument types for the sentinel method
* @return adaptation provider name
*/
static String determineProvider(String sentinelMethodName, Class<?>... argTypes) {
try {
Json.class.getMethod(sentinelMethodName, argTypes);
return JSR_374_ADAPTATION;
} catch (NoSuchMethodException ex) {
return JSR_353_ADAPTATION;
}
}

/**
* Constructs the fully-qualified class name for a given named provider class.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void testAdaptJsonScalars(JavaxJsonAdaptation adaptation) {
assertNull(adaptation.adapt(JsonValue.NULL));
assertEquals(true, adaptation.adapt(JsonValue.TRUE));
assertEquals(false, adaptation.adapt(JsonValue.FALSE));
assertEquals("value", adaptation.adapt(Json.createValue("value")));
assertEquals(BigInteger.ONE, adaptation.adapt(Json.createValue(1)));
assertEquals(BigInteger.ONE, adaptation.adapt(Json.createValue(1L)));
assertEquals(BigInteger.ONE, adaptation.adapt(Json.createValue(BigInteger.ONE)));
Expand Down Expand Up @@ -112,6 +113,7 @@ public void testInvertIntrinsics(JavaxJsonAdaptation adaptation) {
assertEquals(Json.createValue("value"), adaptation.invert("value"));
assertEquals(Json.createValue(1), adaptation.invert(1));
assertEquals(Json.createValue(1L), adaptation.invert(1L));
assertEquals(Json.createValue(1.0), adaptation.invert(1.0));
assertEquals(Json.createValue(BigInteger.ONE), adaptation.invert(BigInteger.ONE));
assertEquals(Json.createValue(BigDecimal.ONE), adaptation.invert(BigDecimal.ONE));
}
Expand All @@ -130,4 +132,39 @@ public void testInvertArrayAdapter(JavaxJsonAdaptation adaptation) {
assertSame(array, adaptation.invert(new JavaxJsonArrayAdapter(array)));
}

@Test
public void testNewInstanceDefaultClassLoader() {
final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(null);
assertTrue(JavaxJsonAdaptation.newInstance() instanceof Jsr374Adaptation);
Thread.currentThread().setContextClassLoader(tccl);
}

@Test
public void testNewInstanceThreadContextClassLoader() {
final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
assertTrue(JavaxJsonAdaptation.newInstance() instanceof Jsr374Adaptation);
Thread.currentThread().setContextClassLoader(tccl);
}

@Test
public void testNewInstanceWithClassLoader() {
assertTrue(JavaxJsonAdaptation.newInstance(getClass().getClassLoader())
instanceof Jsr374Adaptation);
}

@Test
public void testDetermineProvider() {
assertEquals(JavaxJsonAdaptation.JSR_374_ADAPTATION,
JavaxJsonAdaptation.determineProvider("createValue", String.class));
assertEquals(JavaxJsonAdaptation.JSR_353_ADAPTATION,
JavaxJsonAdaptation.determineProvider(".noSuchMethod"));
}

@Test(expected = RuntimeException.class)
public void testInstanceWhenCannotInstantiate() {
JavaxJsonAdaptation.newInstance(getClass().getClassLoader(), ".noSuchProviderName");
}

}

0 comments on commit 5dfcb2b

Please sign in to comment.