Example usage for java.nio.file Files createTempDirectory

List of usage examples for java.nio.file Files createTempDirectory

Introduction

In this page you can find the example usage for java.nio.file Files createTempDirectory.

Prototype

public static Path createTempDirectory(String prefix, FileAttribute<?>... attrs) throws IOException 

Source Link

Document

Creates a new directory in the default temporary-file directory, using the given prefix to generate its name.

Usage

From source file:com.spectralogic.ds3client.metadata.MetadataReceivedListenerImpl_Test.java

@Test
public void testGettingMetadataFailureHandlerWindows() throws IOException, InterruptedException {
    Assume.assumeTrue(Platform.isWindows());

    try {//from  ww  w .  j  ava 2s . c om
        final String tempPathPrefix = null;
        final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix);

        final String fileName = "Gracie.txt";

        final Path filePath = Files.createFile(Paths.get(tempDirectory.toString(), fileName));

        try {
            // get permissions
            final ImmutableMap.Builder<String, Path> fileMapper = ImmutableMap.builder();
            fileMapper.put(filePath.toString(), filePath);
            final Map<String, String> metadataFromFile = new MetadataAccessImpl(fileMapper.build())
                    .getMetadataValue(filePath.toString());

            FileUtils.deleteDirectory(tempDirectory.toFile());

            // put old permissions back
            final Metadata metadata = new MetadataImpl(new MockedHeadersReturningKeys(metadataFromFile));

            final AtomicInteger numTimesFailureHandlerCalled = new AtomicInteger(0);

            new MetadataReceivedListenerImpl(tempDirectory.toString(), new FailureEventListener() {
                @Override
                public void onFailure(final FailureEvent failureEvent) {
                    numTimesFailureHandlerCalled.incrementAndGet();
                    assertEquals(FailureEvent.FailureActivity.RestoringMetadata, failureEvent.doingWhat());
                }
            }, "localhost").metadataReceived(fileName, metadata);

            assertEquals(1, numTimesFailureHandlerCalled.get());
        } finally {
            FileUtils.deleteDirectory(tempDirectory.toFile());
        }
    } catch (final Throwable t) {
        fail("Throwing exceptions from metadata est verbotten");
    }
}

From source file:org.corehunter.tests.data.simple.SimpleFrequencyGenotypeDataTest.java

@Test
public void toCsvFileWithSelectedIds() throws IOException {
    expectedHeaders = HEADERS_UNIQUE_NAMES;
    expectedMarkerNames = MARKER_NAMES;//from w  w w .j  av  a2 s  .  c o  m
    expectedAlleleNames = ALLELE_NAMES;

    SimpleFrequencyGenotypeData genotypicData = new SimpleFrequencyGenotypeData(expectedHeaders,
            expectedMarkerNames, expectedAlleleNames, ALLELE_FREQUENCIES);

    Set<Integer> ids = genotypicData.getIDs();

    Path dirPath = Paths.get(TEST_OUTPUT);

    Files.createDirectories(dirPath);

    dirPath = Files.createTempDirectory(dirPath, "GenoFreqs-SelectedIds");

    // create solution
    SubsetSolution solution = new SubsetSolution(ids);
    for (int sel : SELECTION) {
        solution.select(sel);
    }

    Path path;

    // write with integer ids
    dataName = "with-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write frequency genotypes file (with solution) " + dataName);

    genotypicData.writeData(path, FileType.CSV, solution, true, false, true);

    assertTrue("Output is not correct!",
            FileUtils.contentEquals(
                    new File(SimpleDistanceMatrixDataTest.class
                            .getResource("/frequency_genotypes/out/sel-with-ids.csv").getPath()),
                    path.toFile()));

    // write without integer ids
    dataName = "no-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write frequency genotypes file (with solution) " + dataName);

    genotypicData.writeData(path, FileType.CSV, solution, true, false, false);

    assertTrue("Output is not correct!",
            FileUtils
                    .contentEquals(
                            new File(SimpleDistanceMatrixDataTest.class
                                    .getResource("/frequency_genotypes/out/sel-no-ids.csv").getPath()),
                            path.toFile()));

}

From source file:com.spectralogic.ds3client.integration.GetJobManagement_Test.java

@Test
public void createReadJobWithBigFile() throws IOException, URISyntaxException, NoSuchMethodException,
        IllegalAccessException, InvocationTargetException {
    final String tempPathPrefix = null;
    final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix);

    try {// w w  w  .j  av a  2 s. com
        final String DIR_NAME = "largeFiles/";
        final String FILE_NAME = "lesmis-copies.txt";

        final Path objPath = ResourceUtils.loadFileResource(DIR_NAME + FILE_NAME);
        final long bookSize = Files.size(objPath);
        final Ds3Object obj = new Ds3Object(FILE_NAME, bookSize);

        final Ds3ClientShim ds3ClientShim = new Ds3ClientShim((Ds3ClientImpl) client);

        final int maxNumBlockAllocationRetries = 1;
        final int maxNumObjectTransferAttempts = 3;
        final Ds3ClientHelpers ds3ClientHelpers = Ds3ClientHelpers.wrap(ds3ClientShim,
                maxNumBlockAllocationRetries, maxNumObjectTransferAttempts);

        final Ds3ClientHelpers.Job readJob = ds3ClientHelpers.startReadJob(BUCKET_NAME, Arrays.asList(obj));

        final AtomicBoolean dataTransferredEventReceived = new AtomicBoolean(false);
        final AtomicBoolean objectCompletedEventReceived = new AtomicBoolean(false);
        final AtomicBoolean checksumEventReceived = new AtomicBoolean(false);
        final AtomicBoolean metadataEventReceived = new AtomicBoolean(false);
        final AtomicBoolean waitingForChunksEventReceived = new AtomicBoolean(false);
        final AtomicBoolean failureEventReceived = new AtomicBoolean(false);

        readJob.attachDataTransferredListener(new DataTransferredListener() {
            @Override
            public void dataTransferred(final long size) {
                dataTransferredEventReceived.set(true);
                assertEquals(bookSize, size);
            }
        });
        readJob.attachObjectCompletedListener(new ObjectCompletedListener() {
            @Override
            public void objectCompleted(final String name) {
                objectCompletedEventReceived.set(true);
            }
        });
        readJob.attachChecksumListener(new ChecksumListener() {
            @Override
            public void value(final BulkObject obj, final ChecksumType.Type type, final String checksum) {
                checksumEventReceived.set(true);
                assertEquals("0feqCQBgdtmmgGs9pB/Huw==", checksum);
            }
        });
        readJob.attachMetadataReceivedListener(new MetadataReceivedListener() {
            @Override
            public void metadataReceived(final String filename, final Metadata metadata) {
                metadataEventReceived.set(true);
            }
        });
        readJob.attachWaitingForChunksListener(new WaitingForChunksListener() {
            @Override
            public void waiting(final int secondsToWait) {
                waitingForChunksEventReceived.set(true);
            }
        });
        readJob.attachFailureEventListener(new FailureEventListener() {
            @Override
            public void onFailure(final FailureEvent failureEvent) {
                failureEventReceived.set(true);
            }
        });

        final GetJobSpectraS3Response jobSpectraS3Response = ds3ClientShim
                .getJobSpectraS3(new GetJobSpectraS3Request(readJob.getJobId()));

        assertThat(jobSpectraS3Response.getMasterObjectListResult(), is(notNullValue()));

        readJob.transfer(new FileObjectGetter(tempDirectory));

        final File originalFile = ResourceUtils.loadFileResource(DIR_NAME + FILE_NAME).toFile();
        final File fileCopiedFromBP = Paths.get(tempDirectory.toString(), FILE_NAME).toFile();
        assertTrue(FileUtils.contentEquals(originalFile, fileCopiedFromBP));

        assertTrue(dataTransferredEventReceived.get());
        assertTrue(objectCompletedEventReceived.get());
        assertTrue(checksumEventReceived.get());
        assertTrue(metadataEventReceived.get());
        assertFalse(waitingForChunksEventReceived.get());
        assertFalse(failureEventReceived.get());
    } finally {
        FileUtils.deleteDirectory(tempDirectory.toFile());
    }
}

From source file:com.facebook.presto.hive.TestPrestoS3FileSystem.java

@Test
public void testCreateWithStagingDirectorySymlink() throws Exception {
    java.nio.file.Path tmpdir = Paths.get(StandardSystemProperty.JAVA_IO_TMPDIR.value());
    java.nio.file.Path staging = Files.createTempDirectory(tmpdir, "staging");
    java.nio.file.Path link = Paths.get(staging + ".symlink");
    // staging = /tmp/stagingXXX
    // link = /tmp/stagingXXX.symlink -> /tmp/stagingXXX

    try {//w  ww  .j  a  v  a 2s.  com
        try {
            Files.createSymbolicLink(link, staging);
        } catch (UnsupportedOperationException e) {
            throw new SkipException("Filesystem does not support symlinks", e);
        }

        try (PrestoS3FileSystem fs = new PrestoS3FileSystem()) {
            MockAmazonS3 s3 = new MockAmazonS3();
            Configuration conf = new Configuration();
            conf.set(PrestoS3FileSystem.S3_STAGING_DIRECTORY, link.toString());
            fs.initialize(new URI("s3n://test-bucket/"), conf);
            fs.setS3Client(s3);
            FSDataOutputStream stream = fs.create(new Path("s3n://test-bucket/test"));
            stream.close();
            assertTrue(Files.exists(link));
        }
    } finally {
        deleteRecursively(link.toFile());
        deleteRecursively(staging.toFile());
    }
}

From source file:org.duracloud.mill.dup.DuplicationTaskProcessor.java

/**
 * Allows for some simple testing of this class
 *//*from  www  .j a  v  a 2  s .c  o m*/
public static void main(String[] args) throws Exception {
    if (args.length != 6) {
        throw new RuntimeException(
                "6 arguments expected:\n " + "spaceId\n contentId\n sourceProviderUsername\n "
                        + "sourceProviderPassword\n destProviderUsername\n " + "destProviderPassword");
    }

    String spaceId = args[0];
    String contentId = args[1];
    String srcProvCredUser = args[2];
    String srcProvCredPass = args[3];
    String destProvCredUser = args[4];
    String destProvCredPass = args[5];

    System.out.println("Performing duplication check for content item " + contentId + " in space " + spaceId);

    DuplicationTask task = new DuplicationTask();
    task.setAccount("Dup Testing");
    task.setSpaceId(spaceId);
    task.setContentId(contentId);

    StorageProvider srcProvider = new S3StorageProvider(srcProvCredUser, srcProvCredPass);
    // Making an assumption here that the secondary provider is SDSC
    StorageProvider destProvider = new SDSCStorageProvider(destProvCredUser, destProvCredPass);

    File workDir = Files.createTempDirectory("dup-work", null).toFile();
    workDir.deleteOnExit();

    DuplicationTaskProcessor dupProcessor = new DuplicationTaskProcessor(task, srcProvider, destProvider,
            workDir);
    dupProcessor.execute();

    System.out.println("Duplication check completed successfully!");
    System.exit(0);
}

From source file:org.apache.beam.sdk.io.TextIOTest.java

License:asdf

private void runTestWrite(String[] elems, String header, String footer, int numShards) throws Exception {
    String outputName = "file.txt";
    Path baseDir = Files.createTempDirectory(tempFolder, "testwrite");
    String baseFilename = baseDir.resolve(outputName).toString();

    PCollection<String> input = p.apply(Create.of(Arrays.asList(elems)).withCoder(StringUtf8Coder.of()));

    TextIO.Write write = TextIO.write().to(baseFilename).withHeader(header).withFooter(footer);

    if (numShards == 1) {
        write = write.withoutSharding();
    } else if (numShards > 0) {
        write = write.withNumShards(numShards).withShardNameTemplate(ShardNameTemplate.INDEX_OF_MAX);
    }/*w  ww .  j  a  va 2s .  c o m*/

    input.apply(write);

    p.run();

    assertOutputFiles(elems, header, footer, numShards, baseDir, outputName,
            firstNonNull(write.getShardTemplate(), DefaultFilenamePolicy.DEFAULT_SHARD_TEMPLATE));
}

From source file:org.corehunter.tests.data.simple.SimplePhenotypeDataTest.java

@Test
public void toCsvFileWithAllIds() throws IOException {
    expectedHeaders = HEADERS_UNIQUE_NAMES;
    SimplePhenotypeData phenotypeData = new SimplePhenotypeData("Phenotype Data", OBJECT_FEATURES_MIN_MAX_COL,
            OBJECT_TABLE_AS_LIST);/*from   w w  w .  j  a  va2  s.c  o  m*/

    Set<Integer> ids = phenotypeData.getIDs();

    Path dirPath = Paths.get(TEST_OUTPUT);

    Files.createDirectories(dirPath);

    dirPath = Files.createTempDirectory(dirPath, "Phenotype-AllIds");

    // create solution
    SubsetSolution solution = new SubsetSolution(ids);
    for (int sel : SELECTION) {
        solution.select(sel);
    }

    Path path;

    // write with integer ids
    dataName = "with-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write phenotype file (with solution) " + dataName);

    phenotypeData.writeData(path, FileType.CSV, solution, true, true, true);

    assertTrue("Output is not correct!",
            FileUtils
                    .contentEquals(
                            new File(SimpleDistanceMatrixDataTest.class
                                    .getResource("/phenotypes/out/all-with-ids.csv").getPath()),
                            path.toFile()));

    // write with integer ids
    dataName = "no-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write phenotype file (with solution) " + dataName);

    phenotypeData.writeData(path, FileType.CSV, solution, false, true, true);

    assertTrue("Output is not correct!",
            FileUtils.contentEquals(new File(
                    SimpleDistanceMatrixDataTest.class.getResource("/phenotypes/out/all-no-ids.csv").getPath()),
                    path.toFile()));

}

From source file:org.corehunter.tests.data.simple.SimpleDefaultGenotypeDataTest.java

@Test
public void homozygousToCsvFileWithUnselectedIds() throws IOException {
    expectedHeaders = HEADERS_UNIQUE_NAMES;
    expectedMarkerNames = MARKER_NAMES_DEFAULT;
    expectedAlleleNames = ALLELE_NAMES_HOMOZYGOUS;

    SimpleDefaultGenotypeData genotypicData = new SimpleDefaultGenotypeData(NAME, HEADERS_UNIQUE_NAMES,
            MARKER_NAMES_DEFAULT, ALLELE_OBS_HOMOZYGOUS);

    Set<Integer> ids = genotypicData.getIDs();

    Path dirPath = Paths.get(TEST_OUTPUT);

    Files.createDirectories(dirPath);

    dirPath = Files.createTempDirectory(dirPath, "GenoHomozygous-UnselectedIds");

    // create solution
    SubsetSolution solution = new SubsetSolution(ids);
    for (int sel : SELECTION) {
        solution.select(sel);//ww w  .  jav a2 s  .c o m
    }

    Path path;

    // write with integer ids
    dataName = "with-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write default genotypes file (homozygous, with solution) " + dataName);

    genotypicData.writeData(path, FileType.CSV, solution, false, true, true);

    assertTrue("Output is not correct!",
            FileUtils.contentEquals(
                    new File(SimpleDistanceMatrixDataTest.class
                            .getResource("/homozygous_genotypes/out/unsel-with-ids.csv").getPath()),
                    path.toFile()));

    // write without integer ids
    dataName = "no-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write default genotypes file (homozygous, with solution) " + dataName);

    genotypicData.writeData(path, FileType.CSV, solution, false, true, false);

    assertTrue("Output is not correct!",
            FileUtils.contentEquals(
                    new File(SimpleDistanceMatrixDataTest.class
                            .getResource("/homozygous_genotypes/out/unsel-no-ids.csv").getPath()),
                    path.toFile()));

}

From source file:org.corehunter.tests.data.simple.SimpleBiAllelicGenotypeDataTest.java

@Test
public void toCsvFileWithUnselectedIds() throws IOException {
    expectedHeaders = HEADERS_UNIQUE_NAMES;
    expectedMarkerNames = MARKER_NAMES;/*w  w  w . ja v a 2s.  c  o m*/

    SimpleBiAllelicGenotypeData genotypicData = new SimpleBiAllelicGenotypeData(expectedHeaders,
            expectedMarkerNames, ALLELE_SCORES_BIALLELIC);

    Set<Integer> ids = genotypicData.getIDs();

    Path dirPath = Paths.get(TEST_OUTPUT);

    Files.createDirectories(dirPath);

    dirPath = Files.createTempDirectory(dirPath, "GenoBiallelic-UnselectedIds");

    // create solution
    SubsetSolution solution = new SubsetSolution(ids);
    for (int sel : SELECTION) {
        solution.select(sel);
    }

    Path path;

    // write allele score format with integer ids
    dataName = "bi-with-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write biallelic genotypes file (with solution) " + dataName);

    genotypicData.writeData(path, FileType.CSV, solution, false, true, true);

    assertTrue("Output file is not correct!",
            FileUtils.contentEquals(
                    new File(SimpleDistanceMatrixDataTest.class
                            .getResource("/biallelic_genotypes/out/unsel-bi-with-ids.csv").getPath()),
                    path.toFile()));

    // write allele score format without integer ids
    dataName = "bi-no-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write biallelic genotypes file (with solution) " + dataName);

    genotypicData.writeData(path, FileType.CSV, solution, false, true, false);

    assertTrue("Output file is not correct!",
            FileUtils.contentEquals(
                    new File(SimpleDistanceMatrixDataTest.class
                            .getResource("/biallelic_genotypes/out/unsel-bi-no-ids.csv").getPath()),
                    path.toFile()));

}

From source file:org.corehunter.tests.data.simple.SimpleFrequencyGenotypeDataTest.java

@Test
public void toCsvFileWithUnselectedIds() throws IOException {
    expectedHeaders = HEADERS_UNIQUE_NAMES;
    expectedMarkerNames = MARKER_NAMES;/* w w  w.j  av a  2 s  .com*/
    expectedAlleleNames = ALLELE_NAMES;

    SimpleFrequencyGenotypeData genotypicData = new SimpleFrequencyGenotypeData(expectedHeaders,
            expectedMarkerNames, expectedAlleleNames, ALLELE_FREQUENCIES);

    Set<Integer> ids = genotypicData.getIDs();

    Path dirPath = Paths.get(TEST_OUTPUT);

    Files.createDirectories(dirPath);

    dirPath = Files.createTempDirectory(dirPath, "GenoFreqs-UnselectedIds");

    // create solution
    SubsetSolution solution = new SubsetSolution(ids);
    for (int sel : SELECTION) {
        solution.select(sel);
    }

    Path path;

    // write with integer ids
    dataName = "with-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write frequency genotypes file (with solution) " + dataName);

    genotypicData.writeData(path, FileType.CSV, solution, false, true, true);

    assertTrue("Output is not correct!",
            FileUtils.contentEquals(
                    new File(SimpleDistanceMatrixDataTest.class
                            .getResource("/frequency_genotypes/out/unsel-with-ids.csv").getPath()),
                    path.toFile()));

    // write without integer ids
    dataName = "no-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write frequency genotypes file (with solution) " + dataName);

    genotypicData.writeData(path, FileType.CSV, solution, false, true, false);

    assertTrue("Output is not correct!",
            FileUtils.contentEquals(
                    new File(SimpleDistanceMatrixDataTest.class
                            .getResource("/frequency_genotypes/out/unsel-no-ids.csv").getPath()),
                    path.toFile()));

}