2
package org.crosswire.bible.view.swing.test;
4
import java.awt.BorderLayout;
5
import java.awt.Component;
6
import java.awt.FlowLayout;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.io.PrintWriter;
10
import java.util.Hashtable;
12
import javax.swing.BorderFactory;
13
import javax.swing.JButton;
14
import javax.swing.JList;
15
import javax.swing.JPanel;
16
import javax.swing.JScrollPane;
17
import javax.swing.JSplitPane;
18
import javax.swing.JTextArea;
20
import org.crosswire.bible.control.test.TestList;
21
import org.crosswire.swing.DocumentWriter;
22
import org.crosswire.swing.EirPanel;
23
import org.crosswire.util.StringUtil;
24
import org.crosswire.util.TestBase;
27
* Tester creates a list of the tests that can be run on the system,
28
* allows the user to select which of the tests to run, and then
29
* kicks them off in a separate thread.
31
* <table border='1' cellPadding='3' cellSpacing='0' width="100%">
32
* <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
33
* Distribution Licence:<br />
34
* Project B is free software; you can redistribute it
35
* and/or modify it under the terms of the GNU General Public License,
36
* version 2 as published by the Free Software Foundation.<br />
37
* This program is distributed in the hope that it will be useful,
38
* but WITHOUT ANY WARRANTY; without even the implied warranty of
39
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40
* General Public License for more details.<br />
41
* The License is available on the internet
42
* <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
43
* <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
44
* MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
45
* The copyright to this program is held by it's authors.
46
* </font></td></tr></table>
47
* @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
48
* @see <{docs.Licence}>
51
public class TesterPane extends EirPanel
54
* Construct a Test tool, this simply calls jbInit
62
* Create the GUI components. Possible optimization - start the split
63
* pane with only the list showing, and then bring the text area into
64
* view when the results start appearing.
68
lst_tests.setVisibleRowCount(5);
69
scr_tests.getViewport().add(lst_tests, null);
70
pnl_tests.setBorder(BorderFactory.createTitledBorder("Available Tests"));
71
pnl_tests.setLayout(new BorderLayout());
72
pnl_tests.add(scr_tests, BorderLayout.CENTER);
74
txt_results.setRows(10);
75
txt_results.setEditable(false);
76
scr_results.getViewport().add(txt_results, null);
77
pnl_results.setBorder(BorderFactory.createTitledBorder("Test Results"));
78
pnl_results.setLayout(new BorderLayout());
79
pnl_results.add(scr_results, BorderLayout.CENTER);
81
spl_main.setContinuousLayout(true);
82
spl_main.setBorder(BorderFactory.createEmptyBorder());
83
spl_main.setOrientation(JSplitPane.VERTICAL_SPLIT);
84
spl_main.add(pnl_tests, JSplitPane.TOP);
85
spl_main.add(pnl_results, JSplitPane.BOTTOM);
87
btn_test.addActionListener(new ActionListener() {
88
public void actionPerformed(ActionEvent ev)
93
btn_test.setText("Run Tests");
94
btn_test.setMnemonic('R');
95
btn_clear.addActionListener(new ActionListener() {
96
public void actionPerformed(ActionEvent ev)
101
btn_clear.setText("Clear Results");
102
btn_clear.setMnemonic('C');
103
lay_buttons.setAlignment(FlowLayout.RIGHT);
104
pnl_buttons.setLayout(lay_buttons);
105
pnl_buttons.add(btn_test, null);
106
pnl_buttons.add(btn_clear, null);
108
this.setLayout(new BorderLayout());
109
this.add(spl_main, BorderLayout.CENTER);
110
this.add(pnl_buttons, BorderLayout.SOUTH);
114
* Show this Panel in a new dialog
116
public void showInDialog(Component parent)
118
showInDialog(parent, "Unit Tests", false);
122
* Run the tests selected in a separate thread, placing the results
123
* in the results window.
127
Object[] objs = lst_tests.getSelectedValues();
128
String[] selected = StringUtil.getStringArray(objs);
129
TestThread runnable = new TestThread(selected);
131
Thread work = new Thread(runnable);
132
work.setPriority(Thread.MIN_PRIORITY);
137
* Clear the test results window
141
txt_results.setText("");
144
/** The packages to be tested */
145
private Hashtable testers = TestList.getTesters();
147
/** The button bar at the bottom of the panel */
148
private JPanel pnl_buttons = new JPanel();
150
/** The layout for the button bar */
151
private FlowLayout lay_buttons = new FlowLayout();
153
/** The run tests button */
154
private JButton btn_test = new JButton();
156
/** The clear tests results button */
157
private JButton btn_clear = new JButton();
159
/** The central split pane */
160
private JSplitPane spl_main = new JSplitPane();
162
/** Panel holding the test scroller */
163
private JPanel pnl_tests = new JPanel();
165
/** Scroller for the test list */
166
private JScrollPane scr_tests = new JScrollPane();
168
/** The list of available test */
169
private JList lst_tests = new JList(TestList.getNames());
171
/** Panel holding the result scroller */
172
private JPanel pnl_results = new JPanel();
174
/** Scroller for the result text area */
175
private JScrollPane scr_results = new JScrollPane();
177
/** The text output from the tests */
178
private JTextArea txt_results = new JTextArea();
181
* A thread for running tests in
183
class TestThread implements Runnable
186
* Configure a TestThread with a set of tests to run
188
TestThread(String[] selected)
190
this.selected = selected;
194
* Run the configured tests
198
btn_test.setEnabled(false);
199
btn_clear.setEnabled(false);
201
PrintWriter out = new PrintWriter(new DocumentWriter(txt_results.getDocument()));
203
for (int i=0; i<selected.length; i++)
205
TestBase test = (TestBase) testers.get(selected[i]);
206
test.test(out, false);
209
btn_clear.setEnabled(true);
210
btn_test.setEnabled(true);
213
/** The tests to run */
214
private String[] selected;