Skip to content

Commit

Permalink
changed name collision mechanism to use ordinal numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejmikosik committed Dec 5, 2014
1 parent b02879f commit e8627ca
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 54 deletions.
11 changes: 5 additions & 6 deletions main/java/org/testanza/BodyTester.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package org.testanza;

import static java.lang.System.identityHashCode;
import junit.framework.Test;
import junit.framework.TestCase;

public abstract class BodyTester<T> implements Tester<T> {
private static final Namer namer = new Namer();

public Test test(final T item) {
return new TestCase(uniqueName(item)) {
TestCase test = new TestCase(name(item)) {
protected void runTest() throws Throwable {
body(item);
}
};
}

private String uniqueName(final T item) {
return name(item) + " #" + identityHashCode(item);
namer.makeNameUnique(test);
return test;
}

protected abstract String name(T item);
Expand Down
27 changes: 27 additions & 0 deletions main/java/org/testanza/Namer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.testanza;

import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.WeakHashMap;

import junit.framework.TestCase;

public class Namer {
private final Map<String, WeakReference<TestCase>> names = new WeakHashMap<String, WeakReference<TestCase>>();

public synchronized void makeNameUnique(TestCase test) {
String name = test.getName();
if (!names.containsKey(name)) {
names.put(name, new WeakReference<TestCase>(test));
} else if (!test.equals(names.get(name).get())) {
for (int i = 1;; i++) {
String newName = name + " #" + i;
if (!names.containsKey(newName)) {
names.put(newName, new WeakReference<TestCase>(test));
test.setName(newName);
break;
}
}
}
}
}
47 changes: 0 additions & 47 deletions test/org/testanza/describe_BodyTest.java

This file was deleted.

52 changes: 52 additions & 0 deletions test/org/testanza/describe_name_collisions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.testanza;

import static org.testanza.describe_testanza.verify;
import junit.framework.Test;
import junit.framework.TestCase;

public class describe_name_collisions {
private static String testName = "testName";
private static Object item = newObject("item");
private static Object otherItem = newObject("otherItem");
private static Tester<Object> tester;
private static Test test, otherTest;

public static void uses_original_name_if_no_collision() {
testName = "uses_original_name_if_no_collision";
tester = new NoBodyTester<Object>() {
protected String name(Object i) {
return testName;
}
};
test = tester.test(item);
verify(nameOf(test).equals(testName));
}

public static void fixes_colliding_name() {
testName = "fixes_colliding_name";
tester = new NoBodyTester<Object>() {
protected String name(Object i) {
return testName;
}
};
test = tester.test(item);
otherTest = tester.test(otherItem);
verify(!nameOf(test).equals(nameOf(otherTest)));
}

private static String nameOf(Test testCase) {
return ((TestCase) testCase).getName();
}

private static Object newObject(final String name) {
return new Object() {
public String toString() {
return name;
}
};
}

private static abstract class NoBodyTester<T> extends BodyTester<T> {
protected void body(T t) throws Throwable {}
}
}
2 changes: 1 addition & 1 deletion test/org/testanza/describe_testanza.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
public class describe_testanza {
public static void main(String[] args) throws Throwable {
runTestsIn(describe_TestMembers_hasModifier.class);
runTestsIn(describe_BodyTest.class);
runTestsIn(describe_TestBuilder.class);
runTestsIn(describe_name_collisions.class);
System.out.println("successful");
}

Expand Down

0 comments on commit e8627ca

Please sign in to comment.