/** as {@link #newTempFile(String, String)} using the class as the basis for a prefix */ public static File newTempFile(Class<?> clazz, String ext) { return newTempFile(JavaClassNames.cleanSimpleClassName(clazz), ext); }
@BeforeMethod(alwaysRun=true) public void setUp() throws Exception { globalPropertiesFile = Os.newTempFile(getClass(), ".global.properties"); localPropertiesFile = Os.newTempFile(getClass(), "local.properties"); }
/** writes given contents to a temporary file which will be deleted on exit, located under the given directory */ public static File writeToTempFile(InputStream is, File tempDir, String prefix, String suffix) { Preconditions.checkNotNull(is, "Input stream required to create temp file for %s*%s", prefix, suffix); mkdirs(tempDir); File tempFile = newTempFile(prefix, suffix); OutputStream out = null; try { out = new FileOutputStream(tempFile); ByteStreams.copy(is, out); } catch (IOException e) { throw Throwables.propagate(e); } finally { Streams.closeQuietly(is); Streams.closeQuietly(out); } return tempFile; }
@BeforeMethod(alwaysRun=true) public void setUp() throws Exception { context = null; globalPropertiesFile = Os.newTempFile(getClass(), "global.properties"); }
/** create a copy of the given ZIP as a JAR with the given manifest, returning the new temp file */ public File copyAddingManifest(File f, Manifest mf) { File f2 = Os.newTempFile(f.getName(), "zip"); ZipOutputStream zout = null; ZipFile zf = null; try { zout = new JarOutputStream(new FileOutputStream(f2), mf); writeZipEntriesFromFile(zout, f, Predicates.not(Predicates.equalTo(MANIFEST_PATH))); } catch (IOException e) { throw Exceptions.propagateAnnotated("Unable to read "+f+" when looking for manifest", e); } finally { Streams.closeQuietly(zf); Streams.closeQuietly(zout); } return f2; }
private File createEmptyJarFile() throws Exception { File result = Os.newTempFile("base", "jar"); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(result)); zos.close(); return result; }
/** Creates a temporary file with the given metadata */ public File createTempBundle(String nameHint, Manifest mf, Map<ZipEntry, InputStream> files) { File f2 = Os.newTempFile(nameHint, "zip"); ZipOutputStream zout = null; ZipFile zf = null; try { zout = mf!=null ? new JarOutputStream(new FileOutputStream(f2), mf) : new ZipOutputStream(new FileOutputStream(f2)); writeZipEntries(zout, files); } catch (IOException e) { throw Exceptions.propagate("Unable to read/write for "+nameHint, e); } finally { Streams.closeQuietly(zf); Streams.closeQuietly(zout); } return f2; }
@Override public Void call() throws Exception { File tmp = Os.newTempFile(getClass(), "txt"); try(Writer out = new FileWriter(tmp)) { out.write(TEST_FILE_CONTENT); } tmp.delete(); return null; } };
@BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception { super.setUp(); pseudoBrooklynPropertiesFile = Os.newTempFile("brooklynnode-test", ".properties"); pseudoBrooklynCatalogFile = Os.newTempFile("brooklynnode-test", ".catalog"); brooklynCatalogSourceFile = Os.newTempFile("brooklynnode-test-source", ".catalog"); loc = app.newLocalhostProvisioningLocation(); locs = ImmutableList.of(loc); }
public void logTailOfPostgresLog() { try { File file = Os.newTempFile("postgresql-"+getEntity().getId(), "log"); int result = getMachine().copyFrom(getLogFile(), file.getAbsolutePath()); if (result != 0) throw new IllegalStateException("Could not access log file " + getLogFile()); log.info("Saving {} contents as {}", getLogFile(), file); Streams.logStreamTail(log, "postgresql.log", Streams.byteArrayOfString(Files.toString(file, Charsets.UTF_8)), 1024); file.delete(); } catch (IOException ioe) { log.debug("Error reading copied log file: {}", ioe); } }
@BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception { super.setUp(); pseudoBrooklynPropertiesFile = Os.newTempFile("brooklynnode-test", ".properties"); pseudoBrooklynPropertiesFile.delete(); pseudoBrooklynCatalogFile = Os.newTempFile("brooklynnode-test", ".catalog"); pseudoBrooklynCatalogFile.delete(); loc = app.newLocalhostProvisioningLocation(); locs = ImmutableList.of(loc); }
@Test(groups = "Integration") public void testCopyFileTo() throws Exception { File dest = Os.newTempFile(getClass(), ".dest.tmp"); File src = Os.newTempFile(getClass(), ".src.tmp"); try { Files.write("abc", src, Charsets.UTF_8); host.copyTo(src, dest); assertEquals("abc", Files.readFirstLine(dest, Charsets.UTF_8)); } finally { src.delete(); dest.delete(); } }
@BeforeClass(alwaysRun=true) public void setUpClass() throws Exception { origZip = newZip(archiveContents); origJar = Os.newTempFile(ArchiveUtilsTest.class, ".jar"); Files.copy(origZip, origJar); }
@Test public void testReadRsaPassphraseKeyAndWriteWithoutPassphrase() throws Exception { KeyPair key = readPem("classpath://brooklyn/util/crypto/sample_rsa_passphrase.pem", "passphrase"); checkNonTrivial(key); File f = Os.newTempFile(getClass(), "brooklyn-sample_rsa_passphrase_without_passphrase.pem"); Files.write(SecureKeys.toPem(key), f, Charset.defaultCharset()); KeyPair key2 = readPem(f.toURI().toString(), null); checkNonTrivial(key2); Assert.assertEquals(key2.getPrivate().getEncoded(), key.getPrivate().getEncoded()); Assert.assertEquals(key2.getPublic().getEncoded(), key.getPublic().getEncoded()); }
@Override public String call() throws Exception { int result = -1; try { Preconditions.checkNotNull(getMachine(), "machine"); backingFile = Os.newTempFile("brooklyn-ssh-fetch-", FilenameUtils.getName(remoteFile)); backingFile.deleteOnExit(); result = getMachine().copyFrom(config.getAllConfig(), remoteFile, backingFile.getPath()); } catch (Exception e) { throw new IllegalStateException("SSH fetch "+getRemoteFile()+" from "+getMachine()+" returned threw exception, in "+Tasks.current()+": "+e, e); } if (result!=0) { throw new IllegalStateException("SSH fetch "+getRemoteFile()+" from "+getMachine()+" returned non-zero exit code "+result+", in "+Tasks.current()); } return FileUtils.readFileToString(backingFile); } }
@Test(groups="Integration") public void testNewTempFile() { int CREATE_CNT = 5000; Collection<File> folders = new ArrayList<File>(CREATE_CNT); try { for (int i = 0; i < CREATE_CNT; i++) { try { folders.add(Os.newTempFile(OsTest.class, "test")); } catch (IllegalStateException e) { log.warn("testNewTempFile failed at " + i + " iteration."); Exceptions.propagate(e); } } } finally { //cleanup for (File folder : folders) { folder.delete(); } } }
@Test(groups="Integration") public void testPutCreatesNewFile() throws Exception { File nonExistantFile = Os.newTempFile(getClass(), "txt"); nonExistantFile.delete(); StoreObjectAccessorLocking accessor = new StoreObjectAccessorLocking(new FileBasedStoreObjectAccessor(nonExistantFile, ".tmp")); try { accessor.put("abc"); assertEquals(Files.readLines(nonExistantFile, Charsets.UTF_8), ImmutableList.of("abc")); } finally { accessor.delete(); } }
destFile = Os.newTempFile(getClass(), "commoncommands-test-dest.txt"); sourceFile1 = Os.newTempFile(getClass(), "commoncommands-test.txt"); sourceFileUrl1 = sourceFile1.toURI().toString(); Files.write("mysource1".getBytes(), sourceFile1); sourceFile2 = Os.newTempFile(getClass(), "commoncommands-test2.txt"); sourceFileUrl2 = sourceFile2.toURI().toString(); Files.write("mysource2".getBytes(), sourceFile2); Files.write("mylocal1".getBytes(), localRepoEntityFile); tmpSudoersFile = Os.newTempFile(getClass(), "sudoers" + Identifiers.makeRandomId(8));
@Override protected StoreObjectAccessorWithLock newPersistenceStoreObjectAccessor() throws IOException { file = Os.newTempFile(getClass(), "txt"); return new StoreObjectAccessorLocking(new FileBasedStoreObjectAccessor(file, ".tmp")); }
static void installJavaScanningMoreEntitiesV2(ManagementContext mgmt, Object context) throws FileNotFoundException { // scanning bundle functionality added in 0.12.0, relatively new compared to non-osgi scanning TestResourceUnavailableException.throwIfResourceUnavailable(context.getClass(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH); TestResourceUnavailableException.throwIfResourceUnavailable(context.getClass(), OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_V2_PATH); CampYamlLiteTest.installWithoutCatalogBom(mgmt, OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH); BundleMaker bm = new BundleMaker(mgmt); File f = Os.newTempFile(context.getClass(), "jar"); Streams.copy(ResourceUtils.create(context).getResourceFromUrl(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_V2_PATH), new FileOutputStream(f)); f = bm.copyRemoving(f, MutableSet.of("catalog.bom")); f = bm.copyAdding(f, MutableMap.of(new ZipEntry("catalog.bom"), new ByteArrayInputStream( Strings.lines( "brooklyn.catalog:", " scanJavaAnnotations: true").getBytes() ) )); ((ManagementContextInternal)mgmt).getOsgiManager().get().install(new FileInputStream(f)).checkNoError(); }