-
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.
changed name collision mechanism to use ordinal numbers
- Loading branch information
1 parent
b02879f
commit e8627ca
Showing
5 changed files
with
85 additions
and
54 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
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,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 {} | ||
} | ||
} |
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