ClassPathLoader implements Loader { @Override public CompletableFuture<Resource> loadResource(Context ctx, URI url, Map<String, Object> params) { if (!url.toString().matches("classpath:/*(.*)")) { return CompletableFuture.completedFuture(null); } Matcher matcher = SPLIT_SLASH_PATTERN.matcher(url.getSchemeSpecificPart()); if (matcher.find()) { String location = matcher.group(1); try (InputStream resourceStream = getResourceStream(location)) { if (resourceStream == null) { throw new ResourceLoadException(String.format("Error with ResourceInputStream for file '%s'", location)); } String data = IOUtils.toString(resourceStream); return CompletableFuture.completedFuture(new HistoneStringResource(data, url.toString(), ContentType.TEXT.getId())); } catch (IOException e) { throw new ResourceLoadException(String.format("Error with ResourceInputStream for file '%s'", location)); } } return CompletableFuture.completedFuture(null); }  @Override CompletableFuture<Resource> loadResource(Context ctx, URI url, Map<String, Object> params); @Override String getScheme(); static final String CLASSPATH_SCHEME; }
@Test @SuppressWarnings("unchecked") public void testLoadResource_badSchema_0() throws Exception { URI badSchemaURI = URI.create("file:/classpathResource.tpl"); Resource resource = (Resource) classPathLoader.loadResource(createCtx(), badSchemaURI, Collections.EMPTY_MAP).join(); assertNull(resource); }
@Test @SuppressWarnings("unchecked") public void testLoadResource_badSchema_1() throws Exception { URI badSchemaURI = URI.create("classpath/classpathResource.tpl"); Resource resource = (Resource) classPathLoader.loadResource(createCtx(), badSchemaURI, Collections.EMPTY_MAP).join(); assertNull(resource); }
@Test @SuppressWarnings("unchecked") public void testLoadResource_existing_0() throws Exception { URI existingResourceURI = URI.create("classpath:/classpath/classpathResource.tpl"); Resource existingResource = (Resource) classPathLoader.loadResource(createCtx(), existingResourceURI, Collections.EMPTY_MAP).join(); assertNotNull(existingResource); }
@Test @SuppressWarnings("unchecked") public void testLoadResource_existing_1() throws Exception { URI existingResourceURI = URI.create("classpath:classpath/classpathResource.tpl"); Resource existingResource = (Resource) classPathLoader.loadResource(createCtx(), existingResourceURI, Collections.EMPTY_MAP).join(); assertNotNull(existingResource); }
@Test @SuppressWarnings("unchecked") public void testLoadResource_notExisting() throws Exception { URI notExistingResourceURI = URI.create("classpath:/classpathResourceNotExist.tpl"); try { classPathLoader.loadResource(createCtx(), notExistingResourceURI, Collections.EMPTY_MAP).join(); fail("Exception should be thrown"); } catch (Exception ex) { assertThat(ex.getMessage(), is("Error with ResourceInputStream for file 'classpathResourceNotExist.tpl'")); } }