/** * Package private constructor to build a factory out of a builder * * @param builder the builder * @see JsonSchemaFactoryBuilder#freeze() */ JsonSchemaFactory(final JsonSchemaFactoryBuilder builder) { reportProvider = builder.reportProvider; loadingCfg = builder.loadingCfg; validationCfg = builder.validationCfg; loader = new SchemaLoader(loadingCfg); final Processor<SchemaContext, ValidatorList> processor = buildProcessor(); validator = new JsonValidator(loader, new ValidationProcessor(validationCfg, processor), reportProvider); syntaxValidator = new SyntaxValidator(validationCfg); }
return new SchemaLoader(loadingConfigurationBuilder.freeze());
@Test public void URIsAreNormalizedBehindTheScenes() throws ProcessingException { final String location = "http://toto/a/../b"; final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .preloadSchema(location, JacksonUtils.nodeFactory().objectNode()) .freeze(); final SchemaLoader loader = new SchemaLoader(cfg); final SchemaTree tree = loader.get(URI.create(location)); assertEquals(tree.getLoadingRef().toURI(), URI.create("http://toto/b#")); }
@Test public void schemasAreFetchedOnceNotTwice() throws ProcessingException, IOException { final URI uri = URI.create("foo:/baz#"); final URIDownloader downloader = spy(new URIDownloader() { @Override public InputStream fetch(final URI source) throws IOException { return new ByteArrayInputStream(BYTES); } }); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foo", downloader).freeze(); final SchemaLoader loader = new SchemaLoader(cfg); loader.get(uri); loader.get(uri); verify(downloader, times(1)).fetch(uri); }
@Test public void preloadedSchemasAreNotFetchedAgain() throws ProcessingException, IOException { final String location = "http://foo.bar/baz#"; final URI uri = URI.create(location); final URIDownloader mock = mock(URIDownloader.class); final LoadingConfigurationBuilder builder = LoadingConfiguration .newBuilder().addScheme("http", mock) .preloadSchema(location, JacksonUtils.nodeFactory().objectNode()); LoadingConfiguration cfg; SchemaLoader registry; cfg = builder.freeze(); registry = new SchemaLoader(cfg); registry.get(uri); verify(mock, never()).fetch(uri); //even if cache is disabled cfg = builder.setCacheSize(0).freeze(); registry = new SchemaLoader(cfg); registry.get(uri); verify(mock, never()).fetch(uri); }
@Test public void NonAbsoluteURIsAreRefused() { final SchemaLoader loader = new SchemaLoader(); final URI target = URI.create("moo#"); try { loader.get(target); fail("No exception thrown!"); } catch (ProcessingException e) { assertMessage(e.getProcessingMessage()) .hasMessage(BUNDLE.printf("refProcessing.uriNotAbsolute", target)) .hasLevel(LogLevel.FATAL).hasField("uri", target); } }
@Test public void schemasCacheCanBeDisabled() throws ProcessingException, IOException { final URI uri = URI.create("foo:/baz#"); final URIDownloader downloader = spy(new URIDownloader() { @Override public InputStream fetch(final URI source) throws IOException { return new ByteArrayInputStream(BYTES); } }); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foo", downloader) .setCacheSize(0) .freeze(); final SchemaLoader loader = new SchemaLoader(cfg); loader.get(uri); loader.get(uri); verify(downloader, times(2)).fetch(uri); }
@Test public void schemasCacheCanBeDisabledViaCacheSize() throws ProcessingException, IOException { final URI uri = URI.create("foo:/baz#"); final URIDownloader downloader = spy(new URIDownloader() { @Override public InputStream fetch(final URI source) throws IOException { return new ByteArrayInputStream(BYTES); } }); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foo", downloader) .setCacheSize(0) .freeze(); final SchemaLoader loader = new SchemaLoader(cfg); loader.get(uri); loader.get(uri); verify(downloader, times(2)).fetch(uri); } }
final SchemaLoader loader = new SchemaLoader(cfg);