public static String readFullyStringAndClose(InputStream is) { try { return readFullyString(is); } finally { Streams.closeQuietly(is); } }
private String readBom(URL bom) { try (final InputStream ins = bom.openStream()) { return Streams.readFullyString(ins); } catch (IOException e) { throw Exceptions.propagateAnnotated("Error loading Catalog BOM from " + bom, e); } }
protected void addMemento(BrooklynObjectType type, String label, String id) throws Exception { String mementoFilename = label+"-"+id; String memento = Streams.readFullyString(getClass().getResourceAsStream(mementoFilename)); File persistedFile = getPersistanceFile(type, id); Files.write(memento.getBytes(), persistedFile); }
protected void addMemento(BrooklynObjectType type, String label, String id) throws Exception { String mementoFilename = label+"-"+id; String memento = Streams.readFullyString(getClass().getResourceAsStream(mementoFilename)); File persistedFile = getPersistanceFile(type, id); Files.write(memento.getBytes(), persistedFile); }
protected void doAddEntityMemento(String label, String entityId) throws Exception { String mementoResourceName = "dsl-" + label + "-entity-" + entityId; String memento = Streams.readFullyString(getClass().getResourceAsStream(mementoResourceName)); File persistedEntityFile = new File(mementoDir, Os.mergePaths("entities", entityId)); Files.write(memento.getBytes(), persistedEntityFile); } }
String payloadStr = null; try { payloadStr = Streams.readFullyString(payload); } catch (Exception e) { Exceptions.propagateIfFatal(e);
protected String getEntityMementoContent() throws InterruptedException, TimeoutException, FileNotFoundException { RebindTestUtils.stopPersistence(newApp); String fn = mementoDir + File.separator + "entities" + File.separator + newApp.getApplicationId(); return Streams.readFullyString(new FileInputStream(new File(fn))); }
bomS = Streams.readFullyString(zf.getInputStream(bom)); } catch (IOException e) { throw new IllegalArgumentException("Error reading catalog.bom from ZIP/JAR archive: "+e);
protected Entity loadEntityMemento(String label, String entityId) throws Exception { String mementoResourceName = JavaClassNames.cleanSimpleClassName(this) + "-" + label + "-entity-" + entityId+".memento"; String memento = Streams.readFullyString(getClass().getResourceAsStream(mementoResourceName)); File persistedEntityFile = new File(mementoDir, Os.mergePaths("entities", entityId)); Files.write(memento.getBytes(), persistedEntityFile); return newApp = rebind(); }
protected void doReadConfigInheritance(String label, String entityId) throws Exception { String mementoFilename = "config-inheritance-"+label+"-"+entityId; origMemento = Streams.readFullyString(getClass().getResourceAsStream(mementoFilename)); File persistedEntityFile = new File(mementoDir, Os.mergePaths("entities", entityId)); Files.write(origMemento.getBytes(), persistedEntityFile); // we'll make assertions on what we've loaded rebind(); rebindedApp = (Application) newManagementContext.lookup(entityId); // we'll also make assertions on the contents written out RebindTestUtils.waitForPersisted(mgmt()); newMemento = Joiner.on("\n").join(Files.readLines(persistedEntityFile, Charsets.UTF_8)); }
private void assertJarContents(File f, Map<String, String> expectedContents, boolean othersAllowed) throws Exception { ZipFile zipFile = new ZipFile(f); expectedContents = MutableMap.copyOf(expectedContents); try { for (ZipEntry zipEntry: Collections.list(zipFile.entries())) { String expectedContent = expectedContents.remove(zipEntry.getName()); if (expectedContent==null) { if (!othersAllowed) { assertNotNull(expectedContent, "Unexpected item in ZIP: "+zipEntry.getName()); } } else { String entryContents = Streams.readFullyString(zipFile.getInputStream(zipEntry)); assertEquals(entryContents, expectedContent, "Contents not eas expectd for "+zipEntry.getName()); } } if (!expectedContents.isEmpty()) { Asserts.fail("ZIP did not contain expected contents: "+expectedContents.keySet()); } } finally { zipFile.close(); } }
/** Method to facilitate creation of memento files */ private void createMemento() throws Exception { setUp(); origApp = super.createApp(); // edit this, run this class's main method, then use the log output for your test case origApp.config().set(TEST_KEY, new VersionedName("foo", Version.parseVersion("1.0.0.foo"))); RebindTestUtils.stopPersistence(origApp); String fn = mementoDir + File.separator + "entities" + File.separator + origApp.getApplicationId(); log.info("Persisted to "+fn); String yyyyMM = Time.makeDateString(new Date(), "yyyy-MM"); log.info("Set up your tests by copying from the persistence dir "+mementoDir+"\n\n"+ "cp "+fn+" "+ "src/test/resources/"+getClass().getPackage().getName().replaceAll("\\.", "/")+"/"+ JavaClassNames.cleanSimpleClassName(this)+"-"+yyyyMM+"-entity-"+origApp.getApplicationId()+".memento\n"); String content = Streams.readFullyString(new FileInputStream(new File(fn))); log.info("Or paste the following contents there:\n"+content); log.info("Then add the apache comment header there, and write your test doing loadEntityMemento(\""+yyyyMM+"\", \""+origApp.getApplicationId()+"\")"); }
protected KubernetesMachineLocation createKubernetesResourceLocation(Entity entity, ConfigBag setup) { String resourceUri = entity.config().get(KubernetesResource.RESOURCE_FILE); InputStream resource = ResourceUtils.create(entity).getResourceFromUrl(resourceUri); String templateContents = Streams.readFullyString(resource); String processedContents = TemplateProcessor.processTemplateContents(templateContents, (EntityInternal) entity, setup.getAllConfig()); InputStream processedResource = Streams.newInputStreamWithContents(processedContents);