Java Code Examples for org.junit.rules.TemporaryFolder#delete()

The following examples show how to use org.junit.rules.TemporaryFolder#delete() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlockSyncTestUtils.java    From besu with Apache License 2.0 6 votes vote down vote up
public static List<Block> firstBlocks(final int count) {
  final List<Block> result = new ArrayList<>(count);
  final TemporaryFolder temp = new TemporaryFolder();
  try {
    temp.create();
    final Path blocks = temp.newFile().toPath();
    BlockTestUtil.write1000Blocks(blocks);
    try (final RawBlockIterator iterator =
        new RawBlockIterator(
            blocks, rlp -> BlockHeader.readFrom(rlp, new MainnetBlockHeaderFunctions()))) {
      for (int i = 0; i < count; ++i) {
        result.add(iterator.next());
      }
    }
  } catch (final IOException ex) {
    throw new IllegalStateException(ex);
  } finally {
    temp.delete();
  }
  return result;
}
 
Example 2
Source File: VerificationHost_s.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
public void tearDown() {
    stop();
    TemporaryFolder tempFolder = this.getTemporaryFolder();
    if (tempFolder != null) {
        tempFolder.delete();
    }
}
 
Example 3
Source File: VerificationHost_t.java    From gumtree-spoon-ast-diff with Apache License 2.0 5 votes vote down vote up
public void tearDown() {
    stop();
    TemporaryFolder tempFolder = this.getTemporaryFolder();
    if (tempFolder != null) {
        tempFolder.delete();
    }
}
 
Example 4
Source File: AsciidoctorTestObserver.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public void afterTestShutdownUnsharedTemporaryFolderInstance(@Observes After after) {
    TemporaryFolder temporaryFolder = scopedTemporaryFolder.get().getUnsharedTemporaryFolder();
    if (temporaryFolder != null) {
        temporaryFolder.delete();
        scopedTemporaryFolder.get().setUnsharedTemporaryFolder(null);
    }
}
 
Example 5
Source File: AsciidoctorTestObserver.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
public void afterTestClassShutdownSharedTemporaryFolderInstance(@Observes AfterClass afterClass) {
    TemporaryFolder temporaryFolder = scopedTemporaryFolder.get().getSharedTemporaryFolder();
    if (temporaryFolder != null) {
        temporaryFolder.delete();
        scopedTemporaryFolder.get().setSharedTemporaryFolder(null);
    }

}
 
Example 6
Source File: CxxPreprocessAndCompileIntegrationTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void sanitizeSymlinkedWorkingDirectory() throws IOException {
  TemporaryFolder folder = new TemporaryFolder();
  folder.create();

  // Setup up a symlink to our working directory.
  Path symlinkedRoot = folder.getRoot().toPath().resolve("symlinked-root");
  CreateSymlinksForTests.createSymLink(symlinkedRoot, tmp.getRoot());

  // Run the build, setting PWD to the above symlink.  Typically, this causes compilers to use
  // the symlinked directory, even though it's not the right project root.
  BuildTarget target = BuildTargetFactory.newInstance("//:simple#default,static");
  workspace
      .runBuckCommandWithEnvironmentOverridesAndContext(
          tmp.getRoot(),
          Optional.empty(),
          ImmutableMap.of("PWD", symlinkedRoot.toString()),
          "build",
          target.getFullyQualifiedName())
      .assertSuccess();

  // Verify that we still sanitized this path correctly.
  Path lib =
      workspace.getPath(
          BuildTargetPaths.getGenPath(
              workspace.getProjectFileSystem(), target, "%s/libsimple.a"));
  String contents = Files.asByteSource(lib.toFile()).asCharSource(Charsets.ISO_8859_1).read();
  assertFalse(lib.toString(), contents.contains(tmp.getRoot().toString()));
  assertFalse(lib.toString(), contents.contains(symlinkedRoot.toString()));

  folder.delete();
}
 
Example 7
Source File: BlockchainSetupUtil.java    From besu with Apache License 2.0 4 votes vote down vote up
private static BlockchainSetupUtil create(
    final ChainResources chainResources,
    final ProtocolScheduleProvider protocolScheduleProvider,
    final ProtocolContextProvider protocolContextProvider,
    final EthScheduler scheduler) {
  final TemporaryFolder temp = new TemporaryFolder();
  try {
    temp.create();
    final String genesisJson = Resources.toString(chainResources.getGenesisURL(), Charsets.UTF_8);

    final GenesisConfigFile genesisConfigFile = GenesisConfigFile.fromConfig(genesisJson);
    final ProtocolSchedule protocolSchedule = protocolScheduleProvider.get(genesisConfigFile);

    final GenesisState genesisState = GenesisState.fromJson(genesisJson, protocolSchedule);
    final MutableBlockchain blockchain = createInMemoryBlockchain(genesisState.getBlock());
    final WorldStateArchive worldArchive = createInMemoryWorldStateArchive();
    final TransactionPool transactionPool = mock(TransactionPool.class);

    genesisState.writeStateTo(worldArchive.getMutable());
    final ProtocolContext protocolContext = protocolContextProvider.get(blockchain, worldArchive);

    final Path blocksPath = Path.of(chainResources.getBlocksURL().toURI());
    final List<Block> blocks = new ArrayList<>();
    final BlockHeaderFunctions blockHeaderFunctions =
        ScheduleBasedBlockHeaderFunctions.create(protocolSchedule);
    try (final RawBlockIterator iterator =
        new RawBlockIterator(
            blocksPath, rlp -> BlockHeader.readFrom(rlp, blockHeaderFunctions))) {
      while (iterator.hasNext()) {
        blocks.add(iterator.next());
      }
    }
    return new BlockchainSetupUtil(
        genesisState,
        blockchain,
        protocolContext,
        protocolSchedule,
        worldArchive,
        transactionPool,
        blocks,
        scheduler);
  } catch (final IOException | URISyntaxException ex) {
    throw new IllegalStateException(ex);
  } finally {
    temp.delete();
  }
}
 
Example 8
Source File: TaskStateManagerImplTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * This tests if the {@link TaskStateManager} properly returns the the subtask local state dir from the
 * corresponding {@link TaskLocalStateStoreImpl}.
 */
@Test
public void testForwardingSubtaskLocalStateBaseDirFromLocalStateStore() throws IOException {
	JobID jobID = new JobID(42L, 43L);
	AllocationID allocationID = new AllocationID(4711L, 23L);
	JobVertexID jobVertexID = new JobVertexID(12L, 34L);
	ExecutionAttemptID executionAttemptID = new ExecutionAttemptID(23L, 24L);
	TestCheckpointResponder checkpointResponderMock = new TestCheckpointResponder();

	Executor directExecutor = Executors.directExecutor();

	TemporaryFolder tmpFolder = new TemporaryFolder();

	try {
		tmpFolder.create();

		File[] allocBaseDirs = new File[]{tmpFolder.newFolder(), tmpFolder.newFolder(), tmpFolder.newFolder()};

		LocalRecoveryDirectoryProviderImpl directoryProvider =
			new LocalRecoveryDirectoryProviderImpl(allocBaseDirs, jobID, jobVertexID, 0);

		LocalRecoveryConfig localRecoveryConfig =
			new LocalRecoveryConfig(true, directoryProvider);

		TaskLocalStateStore taskLocalStateStore =
			new TaskLocalStateStoreImpl(jobID, allocationID, jobVertexID, 13, localRecoveryConfig, directExecutor);

		TaskStateManager taskStateManager = taskStateManager(
			jobID,
			executionAttemptID,
			checkpointResponderMock,
			null,
			taskLocalStateStore);

		LocalRecoveryConfig localRecoveryConfFromTaskLocalStateStore =
			taskLocalStateStore.getLocalRecoveryConfig();

		LocalRecoveryConfig localRecoveryConfFromTaskStateManager =
			taskStateManager.createLocalRecoveryConfig();


		for (int i = 0; i < 10; ++i) {
			Assert.assertEquals(allocBaseDirs[i % allocBaseDirs.length],
				localRecoveryConfFromTaskLocalStateStore.getLocalStateDirectoryProvider().allocationBaseDirectory(i));
			Assert.assertEquals(allocBaseDirs[i % allocBaseDirs.length],
				localRecoveryConfFromTaskStateManager.getLocalStateDirectoryProvider().allocationBaseDirectory(i));
		}

		Assert.assertEquals(
			localRecoveryConfFromTaskLocalStateStore.isLocalRecoveryEnabled(),
			localRecoveryConfFromTaskStateManager.isLocalRecoveryEnabled());
	} finally {
		tmpFolder.delete();
	}
}
 
Example 9
Source File: TaskStateManagerImplTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * This tests if the {@link TaskStateManager} properly returns the the subtask local state dir from the
 * corresponding {@link TaskLocalStateStoreImpl}.
 */
@Test
public void testForwardingSubtaskLocalStateBaseDirFromLocalStateStore() throws IOException {
	JobID jobID = new JobID(42L, 43L);
	AllocationID allocationID = new AllocationID(4711L, 23L);
	JobVertexID jobVertexID = new JobVertexID(12L, 34L);
	ExecutionAttemptID executionAttemptID = new ExecutionAttemptID(23L, 24L);
	TestCheckpointResponder checkpointResponderMock = new TestCheckpointResponder();

	Executor directExecutor = Executors.directExecutor();

	TemporaryFolder tmpFolder = new TemporaryFolder();

	try {
		tmpFolder.create();

		File[] allocBaseDirs = new File[]{tmpFolder.newFolder(), tmpFolder.newFolder(), tmpFolder.newFolder()};

		LocalRecoveryDirectoryProviderImpl directoryProvider =
			new LocalRecoveryDirectoryProviderImpl(allocBaseDirs, jobID, jobVertexID, 0);

		LocalRecoveryConfig localRecoveryConfig =
			new LocalRecoveryConfig(true, directoryProvider);

		TaskLocalStateStore taskLocalStateStore =
			new TaskLocalStateStoreImpl(jobID, allocationID, jobVertexID, 13, localRecoveryConfig, directExecutor);

		TaskStateManager taskStateManager = taskStateManager(
			jobID,
			executionAttemptID,
			checkpointResponderMock,
			null,
			taskLocalStateStore);

		LocalRecoveryConfig localRecoveryConfFromTaskLocalStateStore =
			taskLocalStateStore.getLocalRecoveryConfig();

		LocalRecoveryConfig localRecoveryConfFromTaskStateManager =
			taskStateManager.createLocalRecoveryConfig();


		for (int i = 0; i < 10; ++i) {
			Assert.assertEquals(allocBaseDirs[i % allocBaseDirs.length],
				localRecoveryConfFromTaskLocalStateStore.getLocalStateDirectoryProvider().allocationBaseDirectory(i));
			Assert.assertEquals(allocBaseDirs[i % allocBaseDirs.length],
				localRecoveryConfFromTaskStateManager.getLocalStateDirectoryProvider().allocationBaseDirectory(i));
		}

		Assert.assertEquals(
			localRecoveryConfFromTaskLocalStateStore.isLocalRecoveryEnabled(),
			localRecoveryConfFromTaskStateManager.isLocalRecoveryEnabled());
	} finally {
		tmpFolder.delete();
	}
}
 
Example 10
Source File: ErrorConvertersTest.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
@Test
@Category(NeedsRunner.class)
public void testErrorLogs() throws IOException {
  TupleTag<String> errorTag = new TupleTag<String>("errors") {};
  TupleTag<String> goodTag = new TupleTag<String>("good") {};

  TemporaryFolder tmpFolder = new TemporaryFolder();
  tmpFolder.create();

  pipeline
      .apply(Create.of("Hello", "World", "Colin"))
      .apply(
          ParDo.of(
              new DoFn<String, String>() {
                @ProcessElement
                public void processElement(ProcessContext c) {
                  if (c.element().equals("Hello")) {
                    c.output(c.element());
                  } else {
                    c.output(errorTag, c.element());
                  }
                }
              })
              .withOutputTags(goodTag, TupleTagList.of(errorTag)))
      .apply(
          ErrorConverters.LogErrors.newBuilder()
              .setErrorWritePath(
                  tmpFolder.getRoot().getAbsolutePath() + "errors.txt")
              .setErrorTag(errorTag)
              .build());

  pipeline.run();

  // Read in tempfile data
  File file = new File(tmpFolder.getRoot().getAbsolutePath() + "errors.txt-00000-of-00001");
  String fileContents = Files.toString(file, Charsets.UTF_8);
  tmpFolder.delete();

  // Get the unique expected & received lines of text
  HashSet<String> expected = new HashSet<>();
  Collections.addAll(expected, "World", "Colin");

  HashSet<String> result = new HashSet<>();
  Collections.addAll(result, fileContents.split("\n"));

  assertThat(result).isEqualTo(expected);
}
 
Example 11
Source File: ErrorConvertersTest.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
@Test
@Category(NeedsRunner.class)
public void testErrorLogs() throws IOException {
  TupleTag<String> errorTag = new TupleTag<String>("errors") {};
  TupleTag<String> goodTag = new TupleTag<String>("good") {};

  TemporaryFolder tmpFolder = new TemporaryFolder();
  tmpFolder.create();

  pipeline
      .apply(Create.of("Hello", "World", "Colin"))
      .apply(
          ParDo.of(
                  new DoFn<String, String>() {
                    @ProcessElement
                    public void processElement(ProcessContext c) {
                      if (c.element().equals("Hello")) {
                        c.output(c.element());
                      } else {
                        c.output(errorTag, c.element());
                      }
                    }
                  })
              .withOutputTags(goodTag, TupleTagList.of(errorTag)))
      .apply(
          ErrorConverters.LogErrors.newBuilder()
              .setErrorWritePath(
                  StaticValueProvider.of(tmpFolder.getRoot().getAbsolutePath() + "errors.txt"))
              .setErrorTag(errorTag)
              .build());

  pipeline.run();

  // Read in tempfile data
  File file = new File(tmpFolder.getRoot().getAbsolutePath() + "errors.txt-00000-of-00001");
  String fileContents = Files.toString(file, Charsets.UTF_8);
  tmpFolder.delete();

  // Get the unique expected & received lines of text
  HashSet<String> expected = new HashSet<>();
  Collections.addAll(expected, "World", "Colin");

  HashSet<String> result = new HashSet<>();
  Collections.addAll(result, fileContents.split("\n"));

  Assert.assertEquals(expected, result);
}
 
Example 12
Source File: TaskStateManagerImplTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * This tests if the {@link TaskStateManager} properly returns the the subtask local state dir from the
 * corresponding {@link TaskLocalStateStoreImpl}.
 */
@Test
public void testForwardingSubtaskLocalStateBaseDirFromLocalStateStore() throws IOException {
	JobID jobID = new JobID(42L, 43L);
	AllocationID allocationID = new AllocationID(4711L, 23L);
	JobVertexID jobVertexID = new JobVertexID(12L, 34L);
	ExecutionAttemptID executionAttemptID = new ExecutionAttemptID(23L, 24L);
	TestCheckpointResponder checkpointResponderMock = new TestCheckpointResponder();

	Executor directExecutor = Executors.directExecutor();

	TemporaryFolder tmpFolder = new TemporaryFolder();

	try {
		tmpFolder.create();

		File[] allocBaseDirs = new File[]{tmpFolder.newFolder(), tmpFolder.newFolder(), tmpFolder.newFolder()};

		LocalRecoveryDirectoryProviderImpl directoryProvider =
			new LocalRecoveryDirectoryProviderImpl(allocBaseDirs, jobID, jobVertexID, 0);

		LocalRecoveryConfig localRecoveryConfig =
			new LocalRecoveryConfig(true, directoryProvider);

		TaskLocalStateStore taskLocalStateStore =
			new TaskLocalStateStoreImpl(jobID, allocationID, jobVertexID, 13, localRecoveryConfig, directExecutor);

		TaskStateManager taskStateManager = taskStateManager(
			jobID,
			executionAttemptID,
			checkpointResponderMock,
			null,
			taskLocalStateStore);

		LocalRecoveryConfig localRecoveryConfFromTaskLocalStateStore =
			taskLocalStateStore.getLocalRecoveryConfig();

		LocalRecoveryConfig localRecoveryConfFromTaskStateManager =
			taskStateManager.createLocalRecoveryConfig();


		for (int i = 0; i < 10; ++i) {
			Assert.assertEquals(allocBaseDirs[i % allocBaseDirs.length],
				localRecoveryConfFromTaskLocalStateStore.getLocalStateDirectoryProvider().allocationBaseDirectory(i));
			Assert.assertEquals(allocBaseDirs[i % allocBaseDirs.length],
				localRecoveryConfFromTaskStateManager.getLocalStateDirectoryProvider().allocationBaseDirectory(i));
		}

		Assert.assertEquals(
			localRecoveryConfFromTaskLocalStateStore.isLocalRecoveryEnabled(),
			localRecoveryConfFromTaskStateManager.isLocalRecoveryEnabled());
	} finally {
		tmpFolder.delete();
	}
}