Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions java-client/src/main/java/co/elastic/clients/json/JsonpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,24 @@

public class JsonpUtils {

private static JsonProvider systemJsonProvider = null;

/**
* Get a <code>JsonProvider</code> instance. This method first calls the standard `JsonProvider.provider()` that is based on
* the current thread's context classloader, and in case of failure tries to find a provider in other classloaders.
* the current thread's context classloader, and in case of failure tries to find a provider in other classloaders. The
* value is cached for subsequent calls.
*/
@AllowForbiddenApis("Implementation of the JsonProvider lookup")
public static JsonProvider provider() {
JsonProvider result = systemJsonProvider;
if (result == null) {
result = findProvider();
systemJsonProvider = result;
}
return result;
}

@AllowForbiddenApis("Implementation of the JsonProvider lookup")
static JsonProvider findProvider() {
RuntimeException exception;
try {
return JsonProvider.provider();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ public Enumeration<URL> getResources(String name) {
}
}

@Test
@AllowForbiddenApis("Testing JsonpUtil.provider()")
public void testProviderCache() {
// A new provider at each call
assertNotSame(JsonpUtils.findProvider(), JsonpUtils.findProvider());

// Result is cached
assertSame(JsonpUtils.provider(), JsonpUtils.provider());
}

@Test
public void testObjectToString() {
// Test that we call toString() on application classes.
Expand Down