Skip to content

Commit 120bf51

Browse files
authored
Merge pull request #332 from rjruizes/master
error handling for classpath schema client
2 parents 721c1bc + e669256 commit 120bf51

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

core/src/main/java/org/everit/json/schema/loader/ClassPathAwareSchemaClient.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import static java.util.Collections.unmodifiableList;
55
import static java.util.Objects.requireNonNull;
66

7+
import java.io.IOException;
78
import java.io.InputStream;
9+
import java.io.UncheckedIOException;
810
import java.util.List;
911
import java.util.Optional;
1012

@@ -19,9 +21,17 @@ class ClassPathAwareSchemaClient implements SchemaClient {
1921
}
2022

2123
@Override public InputStream get(String url) {
22-
return handleProtocol(url)
23-
.map(this::loadFromClasspath)
24-
.orElseGet(() -> fallbackClient.get(url));
24+
Optional<String> maybeString = handleProtocol(url);
25+
if(maybeString.isPresent()) {
26+
InputStream stream = this.loadFromClasspath(maybeString.get());
27+
if(stream != null) {
28+
return stream;
29+
} else {
30+
throw new UncheckedIOException(new IOException(String.format("Could not find %s", url)));
31+
}
32+
} else {
33+
return fallbackClient.get(url);
34+
}
2535
}
2636

2737
private InputStream loadFromClasspath(String str) {

core/src/test/java/org/everit/json/schema/loader/ClassPathAwareSchemaClientTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
import static org.mockito.Mockito.when;
88

99
import java.io.InputStream;
10+
import java.io.UncheckedIOException;
1011

1112
import org.everit.json.schema.ResourceLoader;
1213
import org.json.JSONObject;
1314
import org.json.JSONTokener;
1415
import org.junit.Before;
16+
import org.junit.Rule;
1517
import org.junit.Test;
18+
import org.junit.rules.ExpectedException;
1619
import org.junit.runner.RunWith;
1720

1821
import junitparams.JUnitParamsRunner;
@@ -39,6 +42,18 @@ public void delegatesUnhandledProtocolsToFallback() {
3942
assertSame(expected, actual);
4043
}
4144

45+
@Rule
46+
public ExpectedException exception = ExpectedException.none();
47+
@Test
48+
public void throwsErrorOnMissingClasspathResource() {
49+
exception.expect(UncheckedIOException.class);
50+
exception.expectMessage("Could not find");
51+
52+
String url = "classpath:/bogus.json";
53+
ClassPathAwareSchemaClient subject = new ClassPathAwareSchemaClient(fallbackClient);
54+
subject.get(url);
55+
}
56+
4257
@Test
4358
@Parameters({
4459
"classpath:/org/everit/jsonvalidator/constobject.json",

0 commit comments

Comments
 (0)