Skip to content

Commit

Permalink
Added TemporaryFile and ExternalResource interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
David Saff committed Jun 4, 2009
1 parent 9ee2808 commit 20dd074
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 9 deletions.
6 changes: 0 additions & 6 deletions .project
Expand Up @@ -10,15 +10,9 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.architexa.diagrams.jdt.builder.AtxaJDTBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.team.cvs.core.cvsnature</nature>
<nature>com.architexa.diagrams.jdt.builder.AtxaJDTBuilderProjectNature</nature>
</natures>
</projectDescription>
47 changes: 45 additions & 2 deletions doc/ReleaseNotes4.7.txt
Expand Up @@ -3,8 +3,51 @@
### Interceptors ###

- Interceptors allow very flexible addition or redefinition of the behavior
of each test method in a test class. For example, this class will
keep a log of each passing and failing test:
of each test method in a test class. Testers can reuse or extend one of the
provided Interceptors below, or write their own.

- The TemporaryFolder Interceptor allows creation of files and folders
that are guaranteed to be deleted when the test method finishes
(whether it passes or fails):

public static class HasTempFolder {
@Interceptor
public TemporaryFolder folder= new TemporaryFolder();

@Test
public void testUsingTempFolder() throws IOException {
File createdFile= folder.newFile("myfile.txt");
File createdFolder= folder.newFolder("subfolder");
// ...
}
}

- ExternalResource is a base class for Interceptors (like TemporaryFolder)
that set up an external resource before a test (a file, socket, server,
database connection, etc.), and tear it down afterward:

public static class UsesExternalResource {
@Interceptor public ExternalResource resource = new ExternalResource() {
@Override
protected void before() throws Throwable {
callSequence += "before ";
};

@Override
protected void after() {
callSequence += "after ";
};
};

@Test public void testFoo() {
callSequence += "test ";
}
}

- TestWatchman is a base class for Interceptors that take note
of the testing action, without modifying it.
For example, this class will keep a log of each passing and failing
test:

public static class WatchmanTest {
private static String watchedLog;
Expand Down
@@ -0,0 +1,32 @@
package org.junit.experimental.interceptor;

import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

public class ExternalResource implements StatementInterceptor {
public final Statement intercept(final Statement base, FrameworkMethod method) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
// TODO (Jun 3, 2009 11:49:23 PM): should we replicate
// @Before/@After semantics?
// 1. after() run even if before() fails.
// 2. exception in after() _adds_ to failure in base.
before();
try {
base.evaluate();
} finally {
after();
}
}
};
}

protected void before() throws Throwable {
// do nothing
}

protected void after() {
// do nothing
}
}
@@ -0,0 +1,56 @@
/**
*
*/
package org.junit.experimental.interceptor;

import java.io.File;
import java.io.IOException;

public class TemporaryFolder extends ExternalResource {
private File folder;

@Override
protected void before() throws Throwable {
create();
}

@Override
protected void after() {
delete();
}

// testing purposes only
public void create() throws IOException {
folder= File.createTempFile("junit", "");
folder.delete();
folder.mkdir();
}

public File newFile(String fileName) throws IOException {
File file= new File(folder, fileName);
file.createNewFile();
return file;
}

public File newFolder(String folderName) {
File file= new File(folder, folderName);
file.mkdir();
return file;
}

public File getRoot() {
return folder;
}

public void delete() {
recursiveDelete(folder);
}

private void recursiveDelete(File file) {
File[] files= file.listFiles();
if (files != null)
for (File each : files)
recursiveDelete(each);
file.delete();
}
}
6 changes: 5 additions & 1 deletion src/test/java/org/junit/tests/AllTests.java
Expand Up @@ -16,8 +16,10 @@
import org.junit.tests.experimental.ExperimentalTests;
import org.junit.tests.experimental.MatcherTest;
import org.junit.tests.experimental.interceptor.ExpectedExceptionInterceptorTest;
import org.junit.tests.experimental.interceptor.ExternalResourceInterceptorTest;
import org.junit.tests.experimental.interceptor.InterceptorTest;
import org.junit.tests.experimental.interceptor.NameInterceptorTest;
import org.junit.tests.experimental.interceptor.TempFolderInterceptorTest;
import org.junit.tests.experimental.interceptor.TimeoutInterceptorTest;
import org.junit.tests.experimental.max.JUnit38SortingTest;
import org.junit.tests.experimental.max.MaxComputerTest;
Expand Down Expand Up @@ -127,7 +129,9 @@
ParallelMethodTest.class,
ParentRunnerTest.class,
NameInterceptorTest.class,
ExpectedExceptionInterceptorTest.class
ExpectedExceptionInterceptorTest.class,
TempFolderInterceptorTest.class,
ExternalResourceInterceptorTest.class
})
public class AllTests {
public static Test suite() {
Expand Down
@@ -0,0 +1,37 @@
package org.junit.tests.experimental.interceptor;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
import org.junit.Test;
import org.junit.experimental.interceptor.ExternalResource;
import org.junit.experimental.interceptor.Interceptor;

public class ExternalResourceInterceptorTest {
private static String callSequence;

public static class UsesExternalResource {
@Interceptor public ExternalResource resource = new ExternalResource() {
@Override
protected void before() throws Throwable {
callSequence += "before ";
};

@Override
protected void after() {
callSequence += "after ";
};
};

@Test public void testFoo() {
callSequence += "test ";
}
}

@Test public void externalResourceGeneratesCorrectSequence() {
callSequence= "";
assertThat(testResult(UsesExternalResource.class), isSuccessful());
assertEquals("before test after ", callSequence);
}
}
@@ -0,0 +1,71 @@
package org.junit.tests.experimental.interceptor;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;

import java.io.File;
import java.io.IOException;

import org.junit.Test;
import org.junit.experimental.interceptor.Interceptor;
import org.junit.experimental.interceptor.TemporaryFolder;

public class TempFolderInterceptorTest {
private static File createdFile;

public static class HasTempFolder {
@Interceptor
public TemporaryFolder folder= new TemporaryFolder();

@Test
public void testUsingTempFolder() throws IOException {
createdFile= folder.newFile("myfile.txt");
assertTrue(createdFile.exists());
}
}

@Test
public void tempFolderIsDeleted() {
assertThat(testResult(HasTempFolder.class), isSuccessful());
assertFalse(createdFile.exists());
}

public static class CreatesSubFolder {
@Interceptor
public TemporaryFolder folder= new TemporaryFolder();

@Test
public void testUsingTempFolder() throws IOException {
createdFile= folder.newFolder("subfolder");
new File(createdFile, "a.txt").createNewFile();
assertTrue(createdFile.exists());
}
}

@Test
public void subFolderIsDeleted() {
assertThat(testResult(CreatesSubFolder.class), isSuccessful());
assertFalse(createdFile.exists());
}

@Test
public void recursiveDeleteFolderWithOneElement() throws IOException {
TemporaryFolder folder= new TemporaryFolder();
folder.create();
File file= folder.newFile("a");
folder.delete();
assertFalse(file.exists());
assertFalse(folder.getRoot().exists());
}

@Test
public void recursiveDeleteFolderWithZeroElements() throws IOException {
TemporaryFolder folder= new TemporaryFolder();
folder.create();
folder.delete();
assertFalse(folder.getRoot().exists());
}
}

0 comments on commit 20dd074

Please sign in to comment.