Skip to content

Commit

Permalink
Kent & Erich patch swallowing in Merlin
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeck committed Mar 29, 2002
1 parent 69a8dad commit 0807a8c
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 74 deletions.
18 changes: 18 additions & 0 deletions .project
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>junit</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.team.cvs.core.cvsnature</nature>
</natures>
</projectDescription>
15 changes: 15 additions & 0 deletions junit/extensions/ActiveTestSuite.java
Expand Up @@ -10,6 +10,21 @@
*/
public class ActiveTestSuite extends TestSuite {
private volatile int fActiveTestDeathCount;

public ActiveTestSuite() {
}

public ActiveTestSuite(Class theClass) {
super(theClass);
}

public ActiveTestSuite(String name) {
super (name);
}

public ActiveTestSuite(Class theClass, String name) {
super(theClass, name);
}

public void run(TestResult result) {
fActiveTestDeathCount= 0;
Expand Down
2 changes: 1 addition & 1 deletion junit/framework/Assert.java
Expand Up @@ -277,7 +277,7 @@ static private void failNotSame(String message, Object expected, Object actual)
String formatted= "";
if (message != null)
formatted= message+" ";
fail(formatted+"expected same");
fail(formatted+"expected same:<"+expected+"> was not:<"+actual+">");
}

static private void failNotEquals(String message, Object expected, Object actual) {
Expand Down
5 changes: 3 additions & 2 deletions junit/framework/TestCase.java
Expand Up @@ -79,9 +79,9 @@ public abstract class TestCase extends Assert implements Test {

/**
* No-arg constructor to enable serialization. This method
* is not intended to be used by mere mortals.
* is not intended to be used by mere mortals without calling setName().
*/
TestCase() {
public TestCase() {
fName= null;
}
/**
Expand Down Expand Up @@ -139,6 +139,7 @@ public void runBare() throws Throwable {
* @exception Throwable if any exception is thrown
*/
protected void runTest() throws Throwable {
assertNotNull(fName);
Method runMethod= null;
try {
// use getMethod to get all public inherited
Expand Down
97 changes: 67 additions & 30 deletions junit/framework/TestSuite.java
Expand Up @@ -3,6 +3,7 @@
import java.util.Vector;
import java.util.Enumeration;
import java.io.PrintWriter;import java.io.StringWriter;import java.lang.reflect.*;
import java.lang.reflect.Constructor;

/**
* A <code>TestSuite</code> is a <code>Composite</code> of Tests.
Expand All @@ -29,25 +30,33 @@ public class TestSuite implements Test {
private Vector fTests= new Vector(10);
private String fName;

/**
/**
* Constructs an empty TestSuite.
*/
public TestSuite() {
}

/**
* Constructs a TestSuite from the given class with the given name.
* @see TestSuite#TestSuite(Class)
*/
public TestSuite(Class theClass, String name) {
this(theClass);
setName(name);
}

/**
* Constructs a TestSuite from the given class. Adds all the methods
* starting with "test" as test cases to the suite.
* Parts of this method was written at 2337 meters in the Hüffihütte,
* Kanton Uri
*/

public TestSuite(final Class theClass) {
fName= theClass.getName();
Constructor constructor= null;
try {
constructor= getConstructor(theClass);
getConstructor(theClass); // Avoid generating multiple error messages
} catch (NoSuchMethodException e) {
addTest(warning("Class "+theClass.getName()+" has no public constructor TestCase(String name)"));
addTest(warning("Class "+theClass.getName()+" has no public constructor TestCase(String name) or TestCase()"));
return;
}

Expand All @@ -61,19 +70,21 @@ public TestSuite(final Class theClass) {
while (Test.class.isAssignableFrom(superClass)) {
Method[] methods= superClass.getDeclaredMethods();
for (int i= 0; i < methods.length; i++) {
addTestMethod(methods[i], names, constructor);
addTestMethod(methods[i], names, theClass);
}
superClass= superClass.getSuperclass();
}
if (fTests.size() == 0)
addTest(warning("No tests found in "+theClass.getName()));
}

/**
* Constructs an empty TestSuite.
*/
public TestSuite(String name) {
fName= name;
setName(name);
}

/**
* Adds a test to the suite.
*/
Expand All @@ -88,28 +99,45 @@ public void addTestSuite(Class testClass) {
addTest(new TestSuite(testClass));
}

private void addTestMethod(Method m, Vector names, Constructor constructor) {
private void addTestMethod(Method m, Vector names, Class theClass) {
String name= m.getName();
if (names.contains(name))
return;
if (isPublicTestMethod(m)) {
names.addElement(name);

Object[] args= new Object[]{name};
try {
addTest((Test)constructor.newInstance(args));
} catch (InstantiationException e) {
addTest(warning("Cannot instantiate test case: "+name+" ("+exceptionToString(e)+")"));
} catch (InvocationTargetException e) {
addTest(warning("Exception in constructor: "+name+" ("+exceptionToString(e.getTargetException())+")"));
} catch (IllegalAccessException e) {
addTest(warning("Cannot access test case: "+name+" ("+exceptionToString(e)+")"));
}

} else { // almost a test method
if (! isPublicTestMethod(m)) {
if (isTestMethod(m))
addTest(warning("Test method isn't public: "+m.getName()));
return;
}

names.addElement(name);

addTest(createTest(theClass, name));
}

public Test createTest(Class theClass, String name) throws IllegalArgumentException {
Constructor c;
try {
c= getConstructor(theClass);
} catch (NoSuchMethodException e) {
return warning("Class "+theClass.getName()+" has no public constructor TestCase(String name) or TestCase()");
}
Object test;
try {
if (c.getParameterTypes().length == 0) {
test= c.newInstance(new Object[0]);
if (test instanceof TestCase)
((TestCase) test).setName(name);
} else {
test= c.newInstance(new Object[]{name});
}
} catch (InstantiationException e) {
return(warning("Cannot instantiate test case: "+name+" ("+exceptionToString(e)+")"));
} catch (InvocationTargetException e) {
return(warning("Exception in constructor: "+name+" ("+exceptionToString(e.getTargetException())+")"));
} catch (IllegalAccessException e) {
return(warning("Cannot access test case: "+name+" ("+exceptionToString(e)+")"));
}
return (Test) test;
}

/**
Expand All @@ -122,6 +150,7 @@ private String exceptionToString(Throwable t) {
return stringWriter.toString();

}

/**
* Counts the number of test cases that will be run by this test.
*/
Expand All @@ -133,28 +162,32 @@ public int countTestCases() {
}
return count;
}

/**
* Gets a constructor which takes a single String as
* its argument.
* its argument or a no arg constructor.
*/
private Constructor getConstructor(Class theClass) throws NoSuchMethodException {
Class[] args= { String.class };
return theClass.getConstructor(args);
try {
return theClass.getConstructor(args);
} catch (NoSuchMethodException e) {
// fall through
}
return theClass.getConstructor(new Class[0]);
}

/**
*/
private boolean isPublicTestMethod(Method m) {
return isTestMethod(m) && Modifier.isPublic(m.getModifiers());
}
/**
*/

private boolean isTestMethod(Method m) {
String name= m.getName();
Class[] parameters= m.getParameterTypes();
Class returnType= m.getReturnType();
return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE);
}

/**
* Runs the tests and collects their result in a TestResult.
*/
Expand All @@ -177,25 +210,29 @@ public void runTest(Test test, TestResult result) {
public Test testAt(int index) {
return (Test)fTests.elementAt(index);
}

/**
* Returns the number of tests in this suite
*/
public int testCount() {
return fTests.size();
}

/**
* Returns the tests as an enumeration
*/
public Enumeration tests() {
return fTests.elements();
}

/**
*/
public String toString() {
if (getName() != null)
return getName();
return super.toString();
}

/**
* Sets the name of the suite.
* @param name The name to set
Expand All @@ -216,7 +253,7 @@ public String getName() {
/**
* Returns a test which will fail and log a warning message.
*/
private Test warning(final String message) {
private Test warning(final String message) {
return new TestCase("warning") {
protected void runTest() {
fail(message);
Expand Down
45 changes: 35 additions & 10 deletions junit/runner/BaseTestRunner.java
Expand Up @@ -13,7 +13,7 @@
public abstract class BaseTestRunner implements TestListener {
public static final String SUITE_METHODNAME= "suite";

static Properties fPreferences;
private static Properties fPreferences;
static int fgMaxMessageLength= 500;
static boolean fgFilterStack= true;
boolean fLoading= true;
Expand All @@ -24,7 +24,34 @@ public abstract class BaseTestRunner implements TestListener {
public synchronized void startTest(Test test) {
testStarted(test.toString());
}

protected static void setPreferences(Properties preferences) {
fPreferences = preferences;
}

protected static Properties getPreferences() {
if (fPreferences == null) {
fPreferences= new Properties();
fPreferences.put("loading", "true");
fPreferences.put("filterstack", "true");
readPreferences();
}
return fPreferences;
}

public static void savePreferences() throws IOException {
FileOutputStream fos= new FileOutputStream(getPreferencesFile());
try {
getPreferences().store(fos, "");
} finally {
fos.close();
}
}

public void setPreference(String key, String value) {
getPreferences().setProperty(key, value);
}

public synchronized void endTest(Test test) {
testEnded(test.toString());
}
Expand Down Expand Up @@ -75,6 +102,10 @@ public Test getTest(String suiteClassName) {
clearStatus();
return new TestSuite(testClass);
}
if (! Modifier.isStatic(suiteMethod.getModifiers())) {
runFailed("Suite() method must be static");
return null;
}
Test test= null;
try {
test= (Test)suiteMethod.invoke(null, new Class[0]); // static method
Expand Down Expand Up @@ -190,8 +221,8 @@ private static void readPreferences() {
InputStream is= null;
try {
is= new FileInputStream(getPreferencesFile());
fPreferences= new Properties(fPreferences);
fPreferences.load(is);
setPreferences(new Properties(getPreferences()));
getPreferences().load(is);
} catch (IOException e) {
try {
if (is != null)
Expand All @@ -202,7 +233,7 @@ private static void readPreferences() {
}

public static String getPreference(String key) {
return fPreferences.getProperty(key);
return getPreferences().getProperty(key);
}

public static int getPreference(String key, int dflt) {
Expand Down Expand Up @@ -286,12 +317,6 @@ static boolean filterLine(String line) {
}

static {
fPreferences= new Properties();
//JDK 1.2 feature
//fPreferences.setProperty("loading", "true");
fPreferences.put("loading", "true");
fPreferences.put("filterstack", "true");
readPreferences();
fgMaxMessageLength= getPreference("maxmessage", fgMaxMessageLength);
}

Expand Down
4 changes: 4 additions & 0 deletions junit/samples/money/MoneyTest.java
Expand Up @@ -62,6 +62,10 @@ public void testMixedSimpleAdd() {
IMoney expected= MoneyBag.create(f12CHF, f7USD);
assertEquals(expected, f12CHF.add(f7USD));
}
public void testBagNotEquals() {
IMoney bag= MoneyBag.create(f12CHF, f7USD);
assertFalse(bag.equals(new Money(12, "DEM").add(f7USD)));
}
public void testMoneyBagEquals() {
assertTrue(!fMB1.equals(null));

Expand Down

0 comments on commit 0807a8c

Please sign in to comment.