SpringSchemaResourceLoader extends SchemaResourceLoader { @Override public CompletableFuture<Resource> load(Context ctx, String href, String baseHref, Map<String, Object> args) throws ResourceLoadException { log.debug("Trying to load resource from location={}, with baseLocation={}", new Object[]{href, baseHref}); return AsyncUtils.initFuture().thenCompose(ignore -> { if (href != null) { if (CollectionUtils.isEmpty(loaders.entrySet())) { throw new ResourceLoadException("No loaders find, register loader"); } String fullLocation = PathUtils.resolveUrl(href, baseHref != null ? baseHref : StringUtils.EMPTY); ru.histone.v2.utils.URI uri = PathUtils.parseURI(fullLocation); try { URI loadUri; String scheme = uri.getScheme() == null ? SERVLET_CONTEXT_SCHEME : uri.getScheme(); switch (scheme) { case SERVLET_CONTEXT_SCHEME: loadUri = baseHref != null ? URI.create(baseHref).resolve(href) : URI.create(href); break; case DATA_SCHEME: loadUri = makeFullLocation(href, ""); break; default: loadUri = makeFullLocation(href, baseHref); break; } Loader loader = loaders.get(scheme); if (loader != null) { return loader.loadResource(ctx, loadUri, args); } } catch (IllegalAccessException e) { throw new ResourceLoadException(e.getMessage(), e); } throw new ResourceLoadException(String.format("Unsupported scheme for resource loading: '%s'", uri.getScheme())); } throw new ResourceLoadException("HREF is null. Unsupported value"); }); } SpringSchemaResourceLoader(); @Override CompletableFuture<Resource> load(Context ctx, String href, String baseHref, Map<String, Object> args);  }
@Test public void testLoad_noResourceLoaders() { SpringSchemaResourceLoader resourceLoader = new SpringSchemaResourceLoader(); try { resourceLoader.load(createCtx(), PATH_SERVLET_CONTEXT_RESOURCE, null, Collections.EMPTY_MAP).join(); fail("Exception should be thrown"); } catch (Exception ex) { assertThat(ex.getMessage(), is("ru.histone.v2.exceptions.ResourceLoadException: No loaders find, register loader")); } }
@Test public void testLoad_hrefIsNull_0() { SpringSchemaResourceLoader resourceLoader = new SpringSchemaResourceLoader(); try { resourceLoader.load(createCtx(), null, PATH_SERVLET_CONTEXT_RESOURCE, Collections.EMPTY_MAP).join(); fail("Exception should be thrown"); } catch (Exception ex) { assertThat(ex.getMessage(), is("ru.histone.v2.exceptions.ResourceLoadException: HREF is null. Unsupported value")); } }
@Test public void testLoad_hrefIsNull_1() { SpringSchemaResourceLoader resourceLoader = new SpringSchemaResourceLoader(); try { resourceLoader.load(createCtx(), null, null, Collections.EMPTY_MAP).join(); fail("Exception should be thrown"); } catch (Exception ex) { assertThat(ex.getMessage(), is("ru.histone.v2.exceptions.ResourceLoadException: HREF is null. Unsupported value")); } }
@Test public void testLoad() throws IOException { SpringSchemaResourceLoader resourceLoader = new SpringSchemaResourceLoader(); resourceLoader.addLoader(new ServletContextLoader(servletContextMock)); Resource resource = resourceLoader.load(createCtx(), PATH_SERVLET_CONTEXT_RESOURCE, null, null).join(); assertNotNull(resource); assertThat(resource.getBaseHref(), is(PATH_SERVLET_CONTEXT_RESOURCE)); assertThat(resource.getContent(), is(INPUT_STREAM_CONTENT)); assertThat(resource.getContentType(), is("TEXT")); }