KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > InteractiveTestCase


1 /*
2  *
3  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
4  * Santa Clara, California 95054, U.S.A. All rights reserved.
5  */

6
7 package org.jdesktop.swing;
8
9 import java.awt.BorderLayout JavaDoc;
10 import java.awt.Point JavaDoc;
11
12 import java.lang.reflect.Method JavaDoc;
13
14 import javax.swing.JComponent JavaDoc;
15 import javax.swing.JFrame JavaDoc;
16 import javax.swing.JScrollPane JavaDoc;
17 import javax.swing.JToolBar JavaDoc;
18
19 /**
20  * Base class for supporting inclusion of interactive tests into a JUnit test case.
21  * Note that the interactive tests are NOT executed by the JUnit framework and
22  * are not automated. They are typically used for visual inspection of features
23  * during development. It is convenient to include the interactive tests along with
24  * the automated JUnit tests since they may share resources and it keeps tests
25  * focused in a single place.
26  * <p>
27  * All interactive test methods should be prefixed with &quot;interactive&quot;,
28  * e.g. interactiveTestTableSorting().</p>
29  * <p>
30  * The test class's <code>main</code> method should be used to control which
31  * interactive tests should run. Use <code>runInteractiveTests()</code> method
32  * to run all interactive tests in the class.</p>
33  * <p>
34  * Ultimately we need to investigate moving to a mechanism which can help automate
35  * interactive tests. JFCUnit is being investigated. In the meantime, this
36  * is quick and dirty and cheap.
37  * </p>
38  * @author Amy Fowler
39  * @version 1.0
40  */

41 public abstract class InteractiveTestCase extends junit.framework.TestCase {
42     private Point JavaDoc frameLocation = new Point JavaDoc(0,0);
43
44     protected InteractiveTestCase(String JavaDoc testTitle) {
45         super(testTitle);
46     }
47
48     public JFrame JavaDoc wrapWithScrollingInFrame(JComponent JavaDoc component, String JavaDoc title) {
49         JScrollPane JavaDoc scroller = new JScrollPane JavaDoc(component);
50         return wrapInFrame(scroller, title);
51     }
52
53     public JFrame JavaDoc wrapInFrame(JComponent JavaDoc component, String JavaDoc title) {
54         JFrame JavaDoc frame = new JFrame JavaDoc(title);
55         JToolBar JavaDoc toolbar = new JToolBar JavaDoc();
56         frame.getContentPane().add(BorderLayout.CENTER, component);
57         frame.getContentPane().add(BorderLayout.NORTH, toolbar);
58         frame.pack();
59         frame.setLocation(frameLocation);
60         if (frameLocation.x == 0) {
61             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
62             frame.setTitle(title+" [close me and all tests will close]");
63         }
64         frameLocation.x += 30;
65         frameLocation.y += 30;
66         return frame;
67     }
68
69     /**
70      * Runs all tests whose method names match the specified regex pattern.
71      * @param regexPattern regular expression pattern used to match test method names
72      * @throws java.lang.Exception
73      */

74     public void runInteractiveTests(String JavaDoc regexPattern) throws java.lang.Exception JavaDoc {
75         setUp();
76         Class JavaDoc testClass = getClass();
77         Method JavaDoc methods[] = testClass.getMethods();
78
79         for (int i = 0; i < methods.length; i++) {
80             if (methods[i].getName().matches(regexPattern)) {
81                 try {
82                     methods[i].invoke(this, null);
83                 }
84                 catch (Exception JavaDoc e) {
85                     System.out.println("could not run interactive test: " +
86                                        methods[i].getName());
87                     e.printStackTrace();
88                 }
89             }
90         }
91         if (methods.length == 0) {
92             System.out.println("no test methods found matching the pattern: "+
93                                regexPattern);
94         }
95         tearDown();
96     }
97
98     /**
99      * Runs all test methods which are prefixed with &quot;interactive&quot;.
100      * @throws java.lang.Exception
101      */

102     public void runInteractiveTests() throws java.lang.Exception JavaDoc {
103         runInteractiveTests("interactive.*");
104     }
105
106
107 }
108
Popular Tags