-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for strict verification of resource deletion in Tempora…
…ryFolder rule with test cases
- Loading branch information
Showing
4 changed files
with
217 additions
and
10 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
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
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
94 changes: 94 additions & 0 deletions
94
src/test/java/org/junit/tests/experimental/rules/TemporaryFolderRuleAssuredDeletionTest.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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package org.junit.tests.experimental.rules; | ||
|
||
import static org.junit.Assert.assertThat; | ||
import static org.junit.experimental.results.PrintableResult.testResult; | ||
import static org.junit.experimental.results.ResultMatchers.failureCountIs; | ||
import static org.junit.experimental.results.ResultMatchers.isSuccessful; | ||
|
||
import java.io.IOException; | ||
|
||
import org.hamcrest.CoreMatchers; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.experimental.results.PrintableResult; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
public class TemporaryFolderRuleAssuredDeletionTest { | ||
|
||
public static class TemporaryFolderStub extends TemporaryFolder { | ||
public TemporaryFolderStub(BuilderStub builder) { | ||
super(builder); | ||
} | ||
|
||
/* | ||
* Don't need to create as we are overriding deletion | ||
*/ | ||
public void create() throws IOException { | ||
|
||
} | ||
|
||
/* | ||
* Simulates failure to clean-up temporary folder | ||
*/ | ||
protected boolean tryDelete() { | ||
return false; | ||
} | ||
} | ||
|
||
public static class BuilderStub extends TemporaryFolder.Builder { | ||
public TemporaryFolder build() { | ||
return new TemporaryFolderStub(this); | ||
} | ||
} | ||
|
||
public static class HasTempFolderWithAssuredDeletion { | ||
@Rule public TemporaryFolder folder = new BuilderStub().assureDeletion().build(); | ||
|
||
@Test | ||
public void test() { | ||
// no-op | ||
} | ||
} | ||
|
||
@Test | ||
public void testStrictVerificationFailure() { | ||
PrintableResult result = testResult(HasTempFolderWithAssuredDeletion.class); | ||
assertThat(result, failureCountIs(1)); | ||
assertThat(result.toString(), CoreMatchers.containsString("Unable to clean up temporary folder")); | ||
} | ||
|
||
public static class HasTempFolderWithoutAssuredDeletion { | ||
@Rule public TemporaryFolder folder = new BuilderStub().build(); | ||
|
||
@Test | ||
public void test() { | ||
// no-op | ||
} | ||
} | ||
|
||
@Test | ||
public void testStrictVerificationSuccess() { | ||
PrintableResult result = testResult(HasTempFolderWithoutAssuredDeletion.class); | ||
assertThat(result, isSuccessful()); | ||
} | ||
|
||
public static class HasTempFolderWithLazyAssuredDeletion { | ||
@Rule public TemporaryFolder folder = new BuilderStub().build(); | ||
|
||
@Test | ||
public void test1() { | ||
folder.assureDeletion(); | ||
} | ||
|
||
@Test | ||
public void test2() { | ||
|
||
} | ||
} | ||
|
||
@Test | ||
public void testStrictVerificationFailureForLazyAssurance() { | ||
PrintableResult result = testResult(HasTempFolderWithLazyAssuredDeletion.class); | ||
assertThat(result, failureCountIs(1)); | ||
} | ||
} |