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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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",
Expand Down