/** takes {@link #getResourceFromUrl(String)} and reads fully, into a string */ public String getResourceAsString(String url) { try { return Streams.readFullyStringAndClose(getResourceFromUrl(url)); } catch (Exception e) { log.debug("ResourceUtils got error reading "+url+(context==null?"":" "+context)+" (rethrowing): "+e); throw Throwables.propagate(e); } }
private static void addPropertiesFromUrl(BrooklynPropertiesImpl p, String url, boolean warnIfNotFound) { if (url==null) return; try { p.addFrom(ResourceUtils.create(BrooklynPropertiesImpl.class).getResourceFromUrl(url)); } catch (Exception e) { if (warnIfNotFound) LOG.warn("Could not load {}; continuing", url); if (LOG.isTraceEnabled()) LOG.trace("Could not load "+url+"; continuing", e); } }
private static void addPropertiesFromUrl(BrooklynProperties p, String url, boolean warnIfNotFound) { if (url==null) return; try { p.addFrom(ResourceUtils.create(BrooklynProperties.class).getResourceFromUrl(url)); } catch (Exception e) { if (warnIfNotFound) LOG.warn("Could not load {}; continuing", url); if (LOG.isTraceEnabled()) LOG.trace("Could not load "+url+"; continuing", e); } }
@Test(expectedExceptions = RuntimeException.class) public void testFollowsProtoChange() throws Exception { utils.getResourceFromUrl(baseUrl + "/secure"); }
@Test(expectedExceptions = RuntimeException.class) public void testProtectedFailsWithoutCredentials() throws Exception { utils.getResourceFromUrl(baseUrl + "/auth"); }
@Test public void testGetResourceViaClasspathWithPrefix() throws Exception { InputStream stream = utils.getResourceFromUrl("classpath://brooklyn/config/sample.properties"); assertNotNull(stream); }
@Test public void testGetResourceViaClasspathWithoutPrefix() throws Exception { InputStream stream = utils.getResourceFromUrl("/brooklyn/config/sample.properties"); assertNotNull(stream); }
@Test(groups="Integration") public void testGetResourceViaSftp() throws Exception { InputStream stream = utils.getResourceFromUrl("sftp://localhost:"+tempFile.getAbsolutePath()); assertEquals(Streams.readFullyStringAndClose(stream), tempFileContents); }
@Test public void testRedirect() throws Exception { InputStream stream = utils.getResourceFromUrl(baseUrl + "/redirect"); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
@Test(expectedExceptions = RuntimeException.class) public void testCycleRedirect() throws Exception { InputStream stream = utils.getResourceFromUrl(baseUrl + "/cycle"); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
@Test public void testInvalidCredentialsNotPassed() throws Exception { String url = baseUrl + "/no_credentials?no:auth@needed"; InputStream stream = utils.getResourceFromUrl(url); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
@Test public void testGetEmpty() throws Exception { InputStream stream = utils.getResourceFromUrl(baseUrl + "/empty"); assertEquals(Streams.readFullyStringAndClose(stream), ""); }
@Test public void testGetResourceViaFileWithPrefix() throws Exception { // The correct format for file URLs is file:///<absolute path>. // On UNIX file:///tmp. // On Windows both file:/C:/temp and file:///C:/temp are supported by Java, // while Windows itself supports the latter only. // Note that file://C:/temp is *wrong*, because C: is interpreted as the host InputStream stream = utils.getResourceFromUrl(tempFile.toURI().toURL().toString()); assertEquals(Streams.readFullyStringAndClose(stream), tempFileContents); }
@Test public void testGet() throws Exception { InputStream stream = utils.getResourceFromUrl(baseUrl + "/simple"); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
@Test public void testGetProtected() throws Exception { String url = baseUrl.replace("http://", "http://test:test@") + "/auth"; InputStream stream = utils.getResourceFromUrl(url); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
@Test public void testGetProtectedEscape() throws Exception { String url = baseUrl.replace("http://", "http://test%40me%3A%2F:test@") + "/auth_escape"; InputStream stream = utils.getResourceFromUrl(url); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
@Test public void testGetProtectedEscape2() throws Exception { String url = baseUrl.replace("http://", "http://test%40me%3Atest@") + "/auth_escape2"; InputStream stream = utils.getResourceFromUrl(url); assertEquals(Streams.readFullyStringAndClose(stream), "OK"); }
private String getTestYaml() { final ResourceUtils resourceUtils = ResourceUtils.create(); final InputStream yaml = resourceUtils.getResourceFromUrl("classpath://test-highstate.yaml"); return Streams.readFullyStringAndClose(yaml); } }
@Override public void run() { try { byte[] bytes = Streams.readFullyAndClose(ResourceUtils.create(this).getResourceFromUrl(url)); // confirm this follow redirects! Assert.assertTrue(bytes.length > 100*1000, "download of "+url+" is suspect ("+Strings.makeSizeString(bytes.length)+")"); // (could also check it is a zip etc) } catch (Exception e) { throw Exceptions.propagate(e); } }}); }
@Test(groups={"Integration", "WIP", "Broken"}) public void testJavaWebWithMemberSpecRebind() throws Exception { Reader input = Streams.reader(new ResourceUtils(this).getResourceFromUrl("test-java-web-app-spec-and-db-with-function.yaml")); AssemblyTemplate at = platform.pdp().registerDeploymentPlan(input); Assembly assembly = at.getInstantiator().newInstance().instantiate(at, platform); final Application app = (Application) mgmt().getEntityManager().getEntity(assembly.getId()); Set<Task<?>> tasks = BrooklynTaskTags.getTasksInEntityContext(mgmt().getExecutionManager(), app); for (Task<?> t: tasks) t.blockUntilEnded(); Entities.dumpInfo(app); Application app2 = rebind(app); Assert.assertEquals(app2.getChildren().size(), 2); }