⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testrunner.java

📁 JAVA 数学程序库 提供常规的数值计算程序包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package jmathlib.tools.junit.swingui;

import jmathlib.tools.junit.framework.*;
import jmathlib.tools.junit.runner.*;

import java.util.*;
import java.lang.reflect.*;
import java.net.URL;
import java.io.*;

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

/**
 * A Swing based user interface to run tests.
 * Enter the name of a class which either provides a static
 * suite method or is a subclass of TestCase.
 * <pre>
 * Synopsis: java junit.swingui.TestRunner [-noloading] [TestCase]
 * </pre>
 * TestRunner takes as an optional argument the name of the testcase class to be run.
 */
public class TestRunner extends BaseTestRunner implements TestRunContext {
	protected JFrame fFrame;
	private Thread fRunner;
	private TestResult fTestResult;
	
	private JComboBox fSuiteCombo;
	private ProgressBar fProgressIndicator;
	private DefaultListModel fFailures;
	private JLabel fLogo;
	private CounterPanel fCounterPanel;
	private JButton fRun;
	private JButton fQuitButton;
	private JButton fRerunButton;
	private StatusLine fStatusLine;
	private FailureDetailView fFailureView;
	private JTabbedPane fTestViewTab;
	private JCheckBox fUseLoadingRunner;
	private Vector fTestRunViews= new Vector(); // view associated with tab in tabbed pane
	private static Font PLAIN_FONT= StatusLine.PLAIN_FONT;
	private static Font BOLD_FONT= StatusLine.BOLD_FONT;
	private static final int GAP= 4;
	private static final int HISTORY_LENGTH= 5;

	private static final String TESTCOLLECTOR_KEY= "TestCollectorClass";
	private static final String FAILUREDETAILVIEW_KEY= "FailureViewClass";
		
	public TestRunner() {
	} 
	
	public static void main(String[] args) {
		new TestRunner().start(args);
	}
	 
	public static void run(Class test) {
		String args[]= { test.getName() };
		main(args);
	}
	
	public void addError(final Test test, final Throwable t) {
		SwingUtilities.invokeLater(
			new Runnable() {
				public void run() {
					fCounterPanel.setErrorValue(fTestResult.errorCount());
					appendFailure("Error", test, t);
				}
			}
		);
	}
	
	public void addFailure(final Test test, final AssertionFailedError t) {
		SwingUtilities.invokeLater(
			new Runnable() {
				public void run() {
					fCounterPanel.setFailureValue(fTestResult.failureCount());
					appendFailure("Failure", test, t);
				}
			}		
		);
	}
	
	public void startTest(Test test) {
		postInfo("Running: "+test);
	}
	
	public void endTest(Test test) {
		postEndTest(test);
	}

	private void postEndTest(final Test test) {
		synchUI();
		SwingUtilities.invokeLater(
			new Runnable() {
				public void run() {
					if (fTestResult != null) {
						fCounterPanel.setRunValue(fTestResult.runCount());
						fProgressIndicator.step(fTestResult.wasSuccessful());
					}
				}
			}
		);
	}

	public void setSuite(String suiteName) {
		fSuiteCombo.getEditor().setItem(suiteName);
	}

	private void addToHistory(final String suite) {
		for (int i= 0; i < fSuiteCombo.getItemCount(); i++) {
			if (suite.equals(fSuiteCombo.getItemAt(i))) {
				fSuiteCombo.removeItemAt(i);
				fSuiteCombo.insertItemAt(suite, 0);
				fSuiteCombo.setSelectedIndex(0);
				return;
			}
		}
		fSuiteCombo.insertItemAt(suite, 0);
		fSuiteCombo.setSelectedIndex(0);
		pruneHistory();
	}
	
	private void pruneHistory() {
		int historyLength= getPreference("maxhistory", HISTORY_LENGTH);
		if (historyLength < 1)
			historyLength= 1;
		for (int i= fSuiteCombo.getItemCount()-1; i > historyLength-1; i--) 
			fSuiteCombo.removeItemAt(i);
	}
	
	private void appendFailure(String kind, Test test, Throwable t) {
		fFailures.addElement(new TestFailure(test, t));
		if (fFailures.size() == 1) 
			revealFailure(test);
	}
	
	private void revealFailure(Test test) {
		for (Enumeration e= fTestRunViews.elements(); e.hasMoreElements(); ) {
			TestRunView v= (TestRunView) e.nextElement();
			v.revealFailure(test);
		}
	}
		
	protected void aboutToStart(final Test testSuite) {
		for (Enumeration e= fTestRunViews.elements(); e.hasMoreElements(); ) {
			TestRunView v= (TestRunView) e.nextElement();
			v.aboutToStart(testSuite, fTestResult);
		}
	}
	
	protected void runFinished(final Test testSuite) {
		SwingUtilities.invokeLater(
			new Runnable() {
				public void run() {
					for (Enumeration e= fTestRunViews.elements(); e.hasMoreElements(); ) {
						TestRunView v= (TestRunView) e.nextElement();
						v.runFinished(testSuite, fTestResult);
					}
				}
			}
		);
	}

	protected CounterPanel createCounterPanel() {
		return new CounterPanel();
	}
	
	protected JPanel createFailedPanel() {
		JPanel failedPanel= new JPanel(new GridLayout(0, 1, 0, 2));
		fRerunButton= new JButton("Run");
		fRerunButton.setEnabled(false);
		fRerunButton.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					rerun();
				}
			}
		);
		failedPanel.add(fRerunButton);
		return failedPanel;
	}
			
	protected FailureDetailView createFailureDetailView() {
		String className= BaseTestRunner.getPreference(FAILUREDETAILVIEW_KEY);
		if (className != null) {			
			Class viewClass= null;
			try {
				viewClass= Class.forName(className);
				return (FailureDetailView)viewClass.newInstance();
			} catch(Exception e) {
				JOptionPane.showMessageDialog(fFrame, "Could not create Failure DetailView - using default view");
			}
		}
		return new DefaultFailureDetailView();
	}

	/**
	 * Creates the JUnit menu. Clients override this
	 * method to add additional menu items.
	 */
	protected JMenu createJUnitMenu() {
		JMenu menu= new JMenu("JUnit");
		menu.setMnemonic('J');
		JMenuItem mi1= new JMenuItem("About...");
		mi1.addActionListener(
		    new ActionListener() {
		        public void actionPerformed(ActionEvent event) {
		            about();
		        }
		    }
		);
		mi1.setMnemonic('A');
		menu.add(mi1);
		
		menu.addSeparator();
		JMenuItem mi2= new JMenuItem(" Exit ");
		mi2.addActionListener(
		    new ActionListener() {
		        public void actionPerformed(ActionEvent event) {
		            terminate();
		        }
		    }
		);
		mi2.setMnemonic('x');
		menu.add(mi2);

		return menu;
	}
	
	protected JFrame createFrame(String title) {
		JFrame frame= new JFrame("JUnit");
		Image icon= loadFrameIcon();	
		if (icon != null)
			frame.setIconImage(icon);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));
		
		frame.addWindowListener(
			new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					terminate();
				}
			}
		);
		return frame;
	}
	
	protected JLabel createLogo() {
		JLabel label;
		Icon icon= getIconResource(BaseTestRunner.class, "logo.gif");
		if (icon != null) 
			label= new JLabel(icon);
		else
			label= new JLabel("JV");
		label.setToolTipText("JUnit Version xxx");
		return label;
	}
	
	protected void createMenus(JMenuBar mb) {
		mb.add(createJUnitMenu());
	}
		
	protected JCheckBox createUseLoaderCheckBox() {
		boolean useLoader= useReloadingTestSuiteLoader();
		JCheckBox box= new JCheckBox("Reload classes every run", useLoader);
		box.setToolTipText("Use a custom class loader to reload the classes for every run");
		if (inVAJava())
			box.setVisible(false);
		return box;
	}
	
	protected JButton createQuitButton() {
		 // spaces required to avoid layout flicker
		 // Exit is shorter than Stop that shows in the same column
		JButton quit= new JButton(" Exit "); 
		quit.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					terminate();
				}
			}
		);
		return quit;
	}
	
	protected JButton createRunButton() {
		JButton run= new JButton("Run");
		run.setEnabled(true);
		run.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					runSuite();
				}
			}
		);
		return run;
	}
	
	protected Component createBrowseButton() {
		JButton browse= new JButton("...");
		browse.setToolTipText("Select a Test class");
		browse.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					browseTestClasses();
				}
			}
		);
		return browse;		
	}
	
	protected StatusLine createStatusLine() {
		return new StatusLine(420);
	}
	
	protected JComboBox createSuiteCombo() {
		JComboBox combo= new JComboBox();
		combo.setEditable(true);
		combo.setLightWeightPopupEnabled(false);
		
		combo.getEditor().getEditorComponent().addKeyListener(
			new KeyAdapter() {
				public void keyTyped(KeyEvent e) {
					textChanged();
					if (e.getKeyChar() == KeyEvent.VK_ENTER)
						runSuite();
				}
			}
		);
		try {
			loadHistory(combo);
		} catch (IOException e) {
			// fails the first time
		}
		combo.addItemListener(
			new ItemListener() {
				public void itemStateChanged(ItemEvent event) {
					if (event.getStateChange() == ItemEvent.SELECTED) {
						textChanged();
					}
				}
			}
		);
		return combo;
	}
	
	protected JTabbedPane createTestRunViews() {
		JTabbedPane pane= new JTabbedPane(JTabbedPane.BOTTOM);

		FailureRunView lv= new FailureRunView(this);
		fTestRunViews.addElement(lv);
		lv.addTab(pane);
		
		TestHierarchyRunView tv= new TestHierarchyRunView(this);
		fTestRunViews.addElement(tv);
		tv.addTab(pane);
		
		pane.addChangeListener(
			new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					testViewChanged();
				}
			}
		);
		return pane;
	}
	
	public void testViewChanged() {
		TestRunView view= (TestRunView)fTestRunViews.elementAt(fTestViewTab.getSelectedIndex());
		view.activate();
	}
	
	protected TestResult createTestResult() {
		return new TestResult();
	}
	
	protected JFrame createUI(String suiteName) {	
		JFrame frame= createFrame("JUnit");	
		JMenuBar mb= new JMenuBar();
		createMenus(mb);
		frame.setJMenuBar(mb);
	
		JLabel suiteLabel= new JLabel("Test class name:");
		fSuiteCombo= createSuiteCombo();
		fRun= createRunButton();
		frame.getRootPane().setDefaultButton(fRun);
		Component browseButton= createBrowseButton();
		
		fUseLoadingRunner= createUseLoaderCheckBox();
		fProgressIndicator= new ProgressBar();
		fCounterPanel= createCounterPanel();
		
		JLabel failureLabel= new JLabel("Errors and Failures:");
		fFailures= new DefaultListModel();
		
		fTestViewTab= createTestRunViews();	
		JPanel failedPanel= createFailedPanel();
		
		fFailureView= createFailureDetailView();
		JScrollPane tracePane= new JScrollPane(fFailureView.getComponent(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -