forked from junit-pioneer/junit-pioneer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - use 'TestCases' over 'TestCase' as per junit-pioneer#552 (comment)
- Loading branch information
Showing
5 changed files
with
142 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 66 additions & 19 deletions
85
src/test/java/org/junitpioneer/jupiter/ResourcesDirTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,78 @@ | ||
/* | ||
* Copyright 2016-2021 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junitpioneer.jupiter; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import static java.util.Collections.singletonList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
import static org.junitpioneer.testkit.assertion.PioneerAssert.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junitpioneer.testkit.ExecutionResults; | ||
import org.junitpioneer.testkit.PioneerTestKit; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junitpioneer.testkit.assertion.PioneerAssert.assertThat; | ||
|
||
class ResourcesDirTests { | ||
|
||
@DisplayName("when a test class has a test method with a @Dir-annotated parameter") | ||
@Nested | ||
class WhenTestClassHasTestMethodWithDirParameterTests { | ||
|
||
@DisplayName("then the parameter is populated with a new readable and writeable temporary directory " | ||
+ "that lasts as long as the test") | ||
@Test | ||
void thenParameterIsPopulatedWithNewReadableAndWriteableTempDirThatLastsAsLongAsTheTest() { | ||
ExecutionResults executionResults = PioneerTestKit | ||
.executeTestClass(ResourcesTests.SingleTestMethodWithDirParameterTestCase.class); | ||
assertThat(executionResults).hasSingleSucceededTest(); | ||
assertThat(ResourcesTests.SingleTestMethodWithDirParameterTestCase.recordedPath).doesNotExist(); | ||
} | ||
|
||
} | ||
@DisplayName("when a test class has a test method with a @Dir-annotated parameter") | ||
@Nested | ||
class WhenTestClassHasTestMethodWithDirParameterTests { | ||
|
||
@DisplayName("then the parameter is populated with a new readable and writeable temporary directory " | ||
+ "that lasts as long as the test") | ||
@Test | ||
void thenParameterIsPopulatedWithNewReadableAndWriteableTempDirThatLastsAsLongAsTheTest() { | ||
ExecutionResults executionResults = PioneerTestKit | ||
.executeTestClass(SingleTestMethodWithDirParameterTestCases.class); | ||
assertThat(executionResults).hasSingleSucceededTest(); | ||
assertThat(SingleTestMethodWithDirParameterTestCases.recordedPath).doesNotExist(); | ||
} | ||
|
||
} | ||
|
||
static class SingleTestMethodWithDirParameterTestCases { | ||
|
||
static Path recordedPath; | ||
|
||
@Test | ||
void theTest(@Dir Path tempDir) { | ||
assertEmptyReadableWriteableTemporaryDirectory(tempDir); | ||
assertCanAddAndReadTextFile(tempDir); | ||
|
||
recordedPath = tempDir; | ||
} | ||
|
||
} | ||
|
||
private static void assertEmptyReadableWriteableTemporaryDirectory(Path tempDir) { | ||
assertThat(tempDir).isEmptyDirectory().startsWith(ROOT_TMP_DIR).isReadable().isWritable(); | ||
} | ||
|
||
private static void assertCanAddAndReadTextFile(Path tempDir) { | ||
try { | ||
Path testFile = Files.createTempFile(tempDir, "some-test-file", ".txt"); | ||
Files.write(testFile, singletonList("some-text")); | ||
assertThat(Files.readAllLines(testFile)).containsExactly("some-text"); | ||
} | ||
catch (IOException e) { | ||
fail(e); | ||
} | ||
} | ||
|
||
private static final Path ROOT_TMP_DIR = Paths.get(System.getProperty("java.io.tmpdir")); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.