Skip to content

Commit

Permalink
finished support for no-arg constructor.
Browse files Browse the repository at this point in the history
tweaks to the Swing UI
  • Loading branch information
egamma committed Mar 30, 2002
1 parent b6a023c commit 86b71cb
Show file tree
Hide file tree
Showing 49 changed files with 218 additions and 309 deletions.
3 changes: 2 additions & 1 deletion .project
Expand Up @@ -15,4 +15,5 @@
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.team.cvs.core.cvsnature</nature> <nature>org.eclipse.team.cvs.core.cvsnature</nature>
</natures> </natures>
</projectDescription> </projectDescription>

20 changes: 8 additions & 12 deletions junit/awtui/TestRunner.java
@@ -1,14 +1,12 @@
package junit.awtui; package junit.awtui;


import junit.framework.*;
import junit.runner.*;

import java.util.Vector;
import java.lang.reflect.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.awt.image.*; import java.awt.image.ImageProducer;
import java.io.*; import java.util.Vector;

import junit.framework.*;
import junit.runner.*;


/** /**
* An AWT based user interface to run tests. * An AWT based user interface to run tests.
Expand Down Expand Up @@ -342,12 +340,10 @@ private void rerunTest(Test test) {
return; return;
} }
Test reloadedTest= null; Test reloadedTest= null;
TestCase rerunTest= (TestCase)test;
try { try {
Class reloadedTestClass= getLoader().reload(test.getClass()); Class reloadedTestClass= getLoader().reload(test.getClass());
Class[] classArgs= { String.class }; reloadedTest= TestSuite.createTest(reloadedTestClass, rerunTest.getName());
Constructor constructor= reloadedTestClass.getConstructor(classArgs);
Object[] args= new Object[]{((TestCase)test).getName()};
reloadedTest= (Test)constructor.newInstance(args);
} catch(Exception e) { } catch(Exception e) {
showInfo("Could not reload "+ test.toString()); showInfo("Could not reload "+ test.toString());
return; return;
Expand Down
Binary file removed junit/awtui/logo.gif
Binary file not shown.
Binary file removed junit/awtui/smalllogo.gif
Binary file not shown.
22 changes: 10 additions & 12 deletions junit/framework/TestSuite.java
Expand Up @@ -54,7 +54,7 @@ public TestSuite(Class theClass, String name) {
public TestSuite(final Class theClass) { public TestSuite(final Class theClass) {
fName= theClass.getName(); fName= theClass.getName();
try { try {
getConstructor(theClass); // Avoid generating multiple error messages getTestConstructor(theClass); // Avoid generating multiple error messages
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
addTest(warning("Class "+theClass.getName()+" has no public constructor TestCase(String name) or TestCase()")); addTest(warning("Class "+theClass.getName()+" has no public constructor TestCase(String name) or TestCase()"));
return; return;
Expand Down Expand Up @@ -108,27 +108,25 @@ private void addTestMethod(Method m, Vector names, Class theClass) {
addTest(warning("Test method isn't public: "+m.getName())); addTest(warning("Test method isn't public: "+m.getName()));
return; return;
} }

names.addElement(name); names.addElement(name);

addTest(createTest(theClass, name)); addTest(createTest(theClass, name));
} }


public Test createTest(Class theClass, String name) throws IllegalArgumentException { static public Test createTest(Class theClass, String name) {
Constructor c; Constructor constructor;
try { try {
c= getConstructor(theClass); constructor= getTestConstructor(theClass);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
return warning("Class "+theClass.getName()+" has no public constructor TestCase(String name) or TestCase()"); return warning("Class "+theClass.getName()+" has no public constructor TestCase(String name) or TestCase()");
} }
Object test; Object test;
try { try {
if (c.getParameterTypes().length == 0) { if (constructor.getParameterTypes().length == 0) {
test= c.newInstance(new Object[0]); test= constructor.newInstance(new Object[0]);
if (test instanceof TestCase) if (test instanceof TestCase)
((TestCase) test).setName(name); ((TestCase) test).setName(name);
} else { } else {
test= c.newInstance(new Object[]{name}); test= constructor.newInstance(new Object[]{name});
} }
} catch (InstantiationException e) { } catch (InstantiationException e) {
return(warning("Cannot instantiate test case: "+name+" ("+exceptionToString(e)+")")); return(warning("Cannot instantiate test case: "+name+" ("+exceptionToString(e)+")"));
Expand All @@ -143,7 +141,7 @@ public Test createTest(Class theClass, String name) throws IllegalArgumentExcept
/** /**
* Converts the stack trace into a string * Converts the stack trace into a string
*/ */
private String exceptionToString(Throwable t) { private static String exceptionToString(Throwable t) {
StringWriter stringWriter= new StringWriter(); StringWriter stringWriter= new StringWriter();
PrintWriter writer= new PrintWriter(stringWriter); PrintWriter writer= new PrintWriter(stringWriter);
t.printStackTrace(writer); t.printStackTrace(writer);
Expand All @@ -167,7 +165,7 @@ public int countTestCases() {
* Gets a constructor which takes a single String as * Gets a constructor which takes a single String as
* its argument or a no arg constructor. * its argument or a no arg constructor.
*/ */
private Constructor getConstructor(Class theClass) throws NoSuchMethodException { public static Constructor getTestConstructor(Class theClass) throws NoSuchMethodException {
Class[] args= { String.class }; Class[] args= { String.class };
try { try {
return theClass.getConstructor(args); return theClass.getConstructor(args);
Expand Down Expand Up @@ -253,7 +251,7 @@ public String getName() {
/** /**
* Returns a test which will fail and log a warning message. * Returns a test which will fail and log a warning message.
*/ */
private Test warning(final String message) { private static Test warning(final String message) {
return new TestCase("warning") { return new TestCase("warning") {
protected void runTest() { protected void runTest() {
fail(message); fail(message);
Expand Down
7 changes: 2 additions & 5 deletions junit/runner/LoadingTestCollector.java
Expand Up @@ -59,14 +59,11 @@ boolean hasSuiteMethod(Class testClass) {
} }


boolean hasPublicConstructor(Class testClass) { boolean hasPublicConstructor(Class testClass) {
Class[] args= { String.class };
Constructor c= null;
try { try {
c= testClass.getConstructor(args); TestSuite.getTestConstructor(testClass);
} catch(Exception e) { } catch(NoSuchMethodException e) {
return false; return false;
} }
return true; return true;
} }

} }
26 changes: 13 additions & 13 deletions junit/runner/Version.java
@@ -1,14 +1,14 @@
package junit.runner; package junit.runner;

/**
* This class defines the current version of JUnit
*/
public class Version {
private Version() {
// don't instantiate
}


/** public static String id() {
* This class defines the current version of JUnit return "3.7";
*/ }
public class Version { }
private Version() {
// don't instantiate
}

public static String id() {
return "@version@";
}
}
4 changes: 1 addition & 3 deletions junit/samples/SimpleTest.java
Expand Up @@ -9,9 +9,7 @@
public class SimpleTest extends TestCase { public class SimpleTest extends TestCase {
protected int fValue1; protected int fValue1;
protected int fValue2; protected int fValue2;
public SimpleTest(String name) {
super(name);
}
protected void setUp() { protected void setUp() {
fValue1= 2; fValue1= 2;
fValue2= 3; fValue2= 3;
Expand Down
3 changes: 0 additions & 3 deletions junit/samples/VectorTest.java
Expand Up @@ -12,9 +12,6 @@ public class VectorTest extends TestCase {
protected Vector fEmpty; protected Vector fEmpty;
protected Vector fFull; protected Vector fFull;


public VectorTest(String name) {
super(name);
}
public static void main (String[] args) { public static void main (String[] args) {
junit.textui.TestRunner.run (suite()); junit.textui.TestRunner.run (suite());
} }
Expand Down
6 changes: 3 additions & 3 deletions junit/samples/money/IMoney.java
Expand Up @@ -13,12 +13,12 @@ interface IMoney {
* Adds a simple Money to this money. This is a helper method for * Adds a simple Money to this money. This is a helper method for
* implementing double dispatch * implementing double dispatch
*/ */
IMoney addMoney(Money m); public abstract IMoney addMoney(Money m);
/** /**
* Adds a MoneyBag to this money. This is a helper method for * Adds a MoneyBag to this money. This is a helper method for
* implementing double dispatch * implementing double dispatch
*/ */
IMoney addMoneyBag(MoneyBag s); public abstract IMoney addMoneyBag(MoneyBag s);
/** /**
* Tests whether this money is zero * Tests whether this money is zero
*/ */
Expand All @@ -38,5 +38,5 @@ interface IMoney {
/** /**
* Append this to a MoneyBag m. * Append this to a MoneyBag m.
*/ */
void appendTo(MoneyBag m); public abstract void appendTo(MoneyBag m);
} }
3 changes: 0 additions & 3 deletions junit/samples/money/MoneyTest.java
Expand Up @@ -11,9 +11,6 @@ public class MoneyTest extends TestCase {
private IMoney fMB1; private IMoney fMB1;
private IMoney fMB2; private IMoney fMB2;


public MoneyTest(String name) {
super(name);
}
public static void main(String args[]) { public static void main(String args[]) {
junit.textui.TestRunner.run(MoneyTest.class); junit.textui.TestRunner.run(MoneyTest.class);
} }
Expand Down
10 changes: 5 additions & 5 deletions junit/swingui/AboutDialog.java
Expand Up @@ -2,8 +2,8 @@


import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import javax.swing.*;


import javax.swing.*;
import junit.runner.*; import junit.runner.*;


/** /**
Expand All @@ -19,15 +19,15 @@ public AboutDialog(JFrame parent) {
setTitle("About"); setTitle("About");
setLocationRelativeTo(parent); setLocationRelativeTo(parent);


JButton button= new JButton("Close"); JButton close= new JButton("Close");
button.addActionListener( close.addActionListener(
new ActionListener() { new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
dispose(); dispose();
} }
} }
); );

getRootPane().setDefaultButton(close);
JLabel label1= new JLabel("JUnit"); JLabel label1= new JLabel("JUnit");
label1.setFont(new Font("dialog", Font.PLAIN, 36)); label1.setFont(new Font("dialog", Font.PLAIN, 36));


Expand All @@ -53,7 +53,7 @@ public void actionPerformed(ActionEvent e) {
constraintsButton1.gridwidth = 2; constraintsButton1.gridheight = 1; constraintsButton1.gridwidth = 2; constraintsButton1.gridheight = 1;
constraintsButton1.anchor = GridBagConstraints.CENTER; constraintsButton1.anchor = GridBagConstraints.CENTER;
constraintsButton1.insets= new Insets(8, 0, 8, 0); constraintsButton1.insets= new Insets(8, 0, 8, 0);
getContentPane().add(button, constraintsButton1); getContentPane().add(close, constraintsButton1);


GridBagConstraints constraintsLogo1= new GridBagConstraints(); GridBagConstraints constraintsLogo1= new GridBagConstraints();
constraintsLogo1.gridx = 2; constraintsLogo1.gridy = 0; constraintsLogo1.gridx = 2; constraintsLogo1.gridy = 0;
Expand Down
24 changes: 13 additions & 11 deletions junit/swingui/CounterPanel.java
@@ -1,9 +1,8 @@
package junit.swingui; package junit.swingui;


import java.awt.GridLayout;

import javax.swing.*; import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.*;


/** /**
* A panel with test run counters * A panel with test run counters
Expand All @@ -12,23 +11,26 @@ public class CounterPanel extends JPanel {
private JTextField fNumberOfErrors; private JTextField fNumberOfErrors;
private JTextField fNumberOfFailures; private JTextField fNumberOfFailures;
private JTextField fNumberOfRuns; private JTextField fNumberOfRuns;
private Icon fFailureIcon= TestRunner.getIconResource(getClass(), "icons/failure.gif");
private Icon fErrorIcon= TestRunner.getIconResource(getClass(), "icons/error.gif");

private int fTotal; private int fTotal;


public CounterPanel() { public CounterPanel() {
super(new GridLayout(2, 3)); super(new GridLayout(1, 6));
fNumberOfErrors= createOutputField(8);
fNumberOfFailures= createOutputField(5);
fNumberOfRuns= createOutputField(5);
add(new JLabel("Runs:")); add(new JLabel("Runs:"));
add(new JLabel("Errors:"));
add(new JLabel("Failures: "));
fNumberOfErrors= createOutputField();
fNumberOfFailures= createOutputField();
fNumberOfRuns= createOutputField();
add(fNumberOfRuns); add(fNumberOfRuns);
add(new JLabel("Errors:", fErrorIcon, SwingConstants.LEFT));
add(fNumberOfErrors); add(fNumberOfErrors);
add(new JLabel("Failures: ", fFailureIcon, SwingConstants.LEFT));
add(fNumberOfFailures); add(fNumberOfFailures);
} }


private JTextField createOutputField() { private JTextField createOutputField(int width) {
JTextField field= new JTextField("0", 10); JTextField field= new JTextField("0", width);
field.setHorizontalAlignment(JTextField.LEFT); field.setHorizontalAlignment(JTextField.LEFT);
field.setFont(StatusLine.BOLD_FONT); field.setFont(StatusLine.BOLD_FONT);
field.setEditable(false); field.setEditable(false);
Expand Down

0 comments on commit 86b71cb

Please sign in to comment.