ClassPathAwareSchemaClient implements SchemaClient { @Override public InputStream get(String 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); } } ClassPathAwareSchemaClient(SchemaClient fallbackClient); @Override InputStream get(String url);  }
@Test public void delegatesUnhandledProtocolsToFallback() { InputStream expected = ResourceLoader.DEFAULT.getStream("arraytestcases.json"); when(fallbackClient.get("http: ClassPathAwareSchemaClient subject = new ClassPathAwareSchemaClient(fallbackClient); InputStream actual = subject.get("http: assertSame(expected, actual); }
@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", "classpath: "classpath:org/everit/jsonvalidator/constobject.json" }) public void success(String url) { ClassPathAwareSchemaClient subject = new ClassPathAwareSchemaClient(fallbackClient); JSONObject actual = new JSONObject(new JSONTokener(subject.get(url))); assertThat(actual, sameJsonAs(EXPECTED)); }