Skip to content

Commit

Permalink
Got rid of unnecessary forget() method
Browse files Browse the repository at this point in the history
Signed-off-by: Kent Beck <kent@threeriversinstitute.org>
  • Loading branch information
David Saff authored and KentBeck committed Mar 10, 2009
1 parent d6c79c0 commit 95da392
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 91 deletions.
26 changes: 5 additions & 21 deletions src/main/java/org/junit/experimental/max/MaxCore.java
Expand Up @@ -18,34 +18,18 @@
import org.junit.runners.model.InitializationError;

public class MaxCore {
public static MaxCore createFresh() {
// TODO (Mar 2, 2009 11:38:46 PM): exposed implementation detail
File file= new File("MaxCore.ser");
if (file.exists())
file.delete();

try {
return new MaxCore();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
public static MaxCore forFolder(String storedResults) {
return new MaxCore(new File(storedResults + ".ser"));
}

public static MaxCore forFolder(String storedResults) {
public static MaxCore forFolder(File storedResults) {
return new MaxCore(storedResults);
}

public final MaxHistory fHistory;

private MaxCore(String folder) {
fHistory = MaxHistory.forFolder(folder);
}

private MaxCore() {
// TODO: ensure fresh
this("MaxCore");
public MaxCore(File storedResults) {
fHistory = MaxHistory.forFolder(storedResults);
}

public Result run(Class<?> testClass) {
Expand Down
51 changes: 27 additions & 24 deletions src/main/java/org/junit/experimental/max/MaxHistory.java
Expand Up @@ -2,7 +2,6 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
Expand All @@ -19,24 +18,36 @@
public class MaxHistory implements Serializable {
private static final long serialVersionUID= 1L;

public static MaxHistory forFolder(String folder) {
File serializedFile= new File(folder + ".ser");

public static MaxHistory forFolder(File storedResults) {
try {
if (serializedFile.exists())
return readHistory(folder);
if (storedResults.exists())
return readHistory(storedResults);
} catch (CouldNotReadCoreException e) {
e.printStackTrace();
serializedFile.delete();
storedResults.delete();
}
return new MaxHistory(folder);
return new MaxHistory(storedResults);
}

private static MaxHistory readHistory(String folder) throws CouldNotReadCoreException {

// public static MaxHistory forFolder(String folder) {
// File serializedFile= new File(folder + ".ser");
// try {
// if (serializedFile.exists())
// return readHistory(folder);
// } catch (CouldNotReadCoreException e) {
// e.printStackTrace();
// serializedFile.delete();
// }
// return new MaxHistory(folder);
// }

private static MaxHistory readHistory(File storedResults) throws CouldNotReadCoreException {
// TODO: rule of three
// TODO: Really?
ObjectInputStream stream;
try {
stream= new ObjectInputStream(new FileInputStream(folder + ".ser"));
stream= new ObjectInputStream(new FileInputStream(storedResults));
} catch (IOException e) {
throw new CouldNotReadCoreException(e);
}
Expand All @@ -56,27 +67,19 @@ private static MaxHistory readHistory(String folder) throws CouldNotReadCoreExce

public final Map<String, Long> fFailureTimestamps= new HashMap<String, Long>();

public final String fFolder;
public final File fFolder;

public MaxHistory(String folder) {
fFolder= folder;
// TODO Auto-generated constructor stub
public MaxHistory(File storedResults) {
fFolder= storedResults;
}


public String getFolder() {
public File getFile() {
return fFolder;
}

public void forget() {
// TODO (Mar 2, 2009 11:40:29 PM): doesn't delete fFolder + ".ser",
// which is where we're storing things.
new File(fFolder).delete();
}

void save() throws FileNotFoundException, IOException {
public void save() throws IOException {
ObjectOutputStream stream= new ObjectOutputStream(new FileOutputStream(
fFolder + ".ser"));
fFolder));
stream.writeObject(this);
stream.close();
}
Expand Down
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.util.List;

import junit.framework.TestCase;
Expand All @@ -14,15 +15,19 @@

public class JUnit38SortingTest {
private MaxCore fMax;
private File fMaxFile;

@Before
public void createMax() {
fMax= MaxCore.createFresh();
fMaxFile= new File("MaxCore.ser");
if (fMaxFile.exists())
fMaxFile.delete();
fMax= new MaxCore(fMaxFile);
}

@After
public void forgetMax() {
fMax.fHistory.forget();
fMaxFile.delete();
}

public static class JUnit4Test {
Expand Down
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -27,7 +28,7 @@
public class MaxComputerTest {
private static class MaxComputer extends Computer {
// TODO (Mar 2, 2009 11:21:28 PM): configure somehow
MaxHistory fMax= new MaxHistory("MaxCore");
MaxHistory fMax= new MaxHistory(new File("MaxCore.max"));

@Override
protected Runner getRunner(RunnerBuilder builder, Class<?> testClass)
Expand Down
59 changes: 16 additions & 43 deletions src/test/java/org/junit/tests/experimental/max/MaxStarterTest.java
Expand Up @@ -7,6 +7,7 @@
import static org.junit.Assert.fail;
import static org.junit.matchers.JUnitMatchers.containsString;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -15,7 +16,6 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.max.MaxCore;
import org.junit.experimental.max.MaxHistory;
import org.junit.internal.runners.JUnit38ClassRunner;
import org.junit.runner.Computer;
import org.junit.runner.Description;
Expand All @@ -31,15 +31,19 @@
public class MaxStarterTest {
private MaxCore fMax;

private File fMaxFile;

@Before
public void createMax() {
fMax= MaxCore.createFresh();
fMaxFile= new File("MaxCore.ser");
if (fMaxFile.exists())
fMaxFile.delete();
fMax= new MaxCore(fMaxFile);
}

@After
public void forgetMax() {
MaxHistory history= fMax.fHistory;
history.forget();
fMaxFile.delete();
}

public static class TwoTests {
Expand Down Expand Up @@ -128,48 +132,19 @@ public void slow() throws InterruptedException {
public void fast() {
fail();
}
}

@Test
public void rememberOldRuns() {
fMax.run(TwoUnEqualTests.class);
String storedResults= fMax.fHistory.getFolder();

MaxCore reincarnation= MaxCore.forFolder(storedResults);
try {
List<Failure> failures= reincarnation.run(TwoUnEqualTests.class)
.getFailures();
assertEquals("fast", failures.get(0).getDescription()
.getMethodName());
assertEquals("slow", failures.get(1).getDescription()
.getMethodName());
} finally {
reincarnation.fHistory.forget();
}
}

@Test
public void rememberOldRunsSqueeze() {
public void rememberOldRuns() {
// TODO (Mar 9, 2009 10:40:03 PM): Direct access to fHistory
fMax.run(TwoUnEqualTests.class);
String storedResults= fMax.fHistory.getFolder();

MaxCore reincarnation= MaxCore.forFolder(storedResults);
try {
List<Failure> failures= run(reincarnation)
.getFailures();
assertEquals("fast", failures.get(0).getDescription()
.getMethodName());
assertEquals("slow", failures.get(1).getDescription()
.getMethodName());
} finally {
reincarnation.fHistory.forget();
}
}

private Result run(MaxCore reincarnation) {
JUnitCore core= new JUnitCore();
return core.run(reincarnation.sortRequest(
Request.aClass(TwoUnEqualTests.class)).getRunner());
MaxCore reincarnation= MaxCore.forFolder(fMaxFile);
List<Failure> failures= reincarnation.run(TwoUnEqualTests.class)
.getFailures();
assertEquals("fast", failures.get(0).getDescription().getMethodName());
assertEquals("slow", failures.get(1).getDescription().getMethodName());
}

@Test
Expand Down Expand Up @@ -251,9 +226,7 @@ public String describe() {

@Test
public void testCountsStandUpToFiltration() {
// TODO (Nov 18, 2008 4:42:43 PM): DUP above
Class<AllTests> testClass= AllTests.class;
assertFilterLeavesTestUnscathed(testClass);
assertFilterLeavesTestUnscathed(AllTests.class);
}

private void assertFilterLeavesTestUnscathed(Class<?> testClass) {
Expand Down

0 comments on commit 95da392

Please sign in to comment.