diff --git a/core/src/main/java/org/everit/json/schema/loader/ClassPathAwareSchemaClient.java b/core/src/main/java/org/everit/json/schema/loader/ClassPathAwareSchemaClient.java index 85e826b25..f709c6e4b 100644 --- a/core/src/main/java/org/everit/json/schema/loader/ClassPathAwareSchemaClient.java +++ b/core/src/main/java/org/everit/json/schema/loader/ClassPathAwareSchemaClient.java @@ -4,7 +4,9 @@ import static java.util.Collections.unmodifiableList; import static java.util.Objects.requireNonNull; +import java.io.IOException; import java.io.InputStream; +import java.io.UncheckedIOException; import java.util.List; import java.util.Optional; @@ -19,9 +21,17 @@ class ClassPathAwareSchemaClient implements SchemaClient { } @Override public InputStream get(String url) { - return handleProtocol(url) - .map(this::loadFromClasspath) - .orElseGet(() -> fallbackClient.get(url)); + Optional maybeString = handleProtocol(url); + if(maybeString.isPresent()) { + InputStream stream = this.loadFromClasspath(maybeString.get()); + if(stream != null) { + return stream; + } else { + throw new UncheckedIOException(new IOException(String.format("Could not find %s", url))); + } + } else { + return fallbackClient.get(url); + } } private InputStream loadFromClasspath(String str) { diff --git a/core/src/test/java/org/everit/json/schema/loader/ClassPathAwareSchemaClientTest.java b/core/src/test/java/org/everit/json/schema/loader/ClassPathAwareSchemaClientTest.java index f80bff48e..97d539b31 100644 --- a/core/src/test/java/org/everit/json/schema/loader/ClassPathAwareSchemaClientTest.java +++ b/core/src/test/java/org/everit/json/schema/loader/ClassPathAwareSchemaClientTest.java @@ -7,12 +7,15 @@ import static org.mockito.Mockito.when; import java.io.InputStream; +import java.io.UncheckedIOException; import org.everit.json.schema.ResourceLoader; import org.json.JSONObject; import org.json.JSONTokener; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import junitparams.JUnitParamsRunner; @@ -39,6 +42,18 @@ public void delegatesUnhandledProtocolsToFallback() { assertSame(expected, actual); } + @Rule + public ExpectedException exception = ExpectedException.none(); + @Test + public void throwsErrorOnMissingClasspathResource() { + exception.expect(UncheckedIOException.class); + exception.expectMessage("Could not find"); + + String url = "classpath:/bogus.json"; + ClassPathAwareSchemaClient subject = new ClassPathAwareSchemaClient(fallbackClient); + subject.get(url); + } + @Test @Parameters({ "classpath:/org/everit/jsonvalidator/constobject.json",