001 /******************************************************************************
002 * Copyright (C) MActor Developers. All rights reserved. *
003 * ---------------------------------------------------------------------------*
004 * This file is part of MActor. *
005 * *
006 * MActor is free software; you can redistribute it and/or modify *
007 * it under the terms of the GNU General Public License as published by *
008 * the Free Software Foundation; either version 2 of the License, or *
009 * (at your option) any later version. *
010 * *
011 * MActor is distributed in the hope that it will be useful, *
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014 * GNU General Public License for more details. *
015 * *
016 * You should have received a copy of the GNU General Public License *
017 * along with MActor; if not, write to the Free Software *
018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019 ******************************************************************************/
020 package org.mactor.ui.gui.testrunner;
021
022 import java.awt.BorderLayout;
023 import java.awt.FlowLayout;
024 import java.util.LinkedList;
025 import java.util.List;
026 import java.util.Map;
027 import java.util.Map.Entry;
028
029 import javax.swing.AbstractAction;
030 import javax.swing.JButton;
031 import javax.swing.JLabel;
032 import javax.swing.JPanel;
033 import javax.swing.JScrollPane;
034 import javax.swing.JSplitPane;
035 import javax.swing.SwingUtilities;
036
037 import org.mactor.framework.MactorException;
038 import org.mactor.framework.TestFeedbackListener;
039 import org.mactor.framework.TestRunner;
040 import org.mactor.framework.data.DataTable;
041 import org.mactor.framework.spec.TestSpec;
042 import org.mactor.ui.gui.GuiUtil;
043 import org.mactor.ui.gui.Stoppable;
044 import org.mactor.ui.gui.project.ProjectTreeNode;
045 import org.mactor.ui.gui.project.editors.ParamsPanel;
046 import org.mactor.ui.gui.project.editors.SimpleFormPanel;
047 import org.mactor.ui.gui.testrunner.RunningTestTreePanel.RunningTestModel;
048
049 public class SingelTestPanel extends JPanel implements Stoppable {
050 RunningTestTreePanel rtPanel;
051 TestRunner tr;
052 ParamsPanel pPanel = new ParamsPanel();
053 RunningTestModel rtModel = new RunningTestModel();
054 TestFeedbackListener tfl = new TestFeedbackListener() {
055 public void onNodeEvent(final org.mactor.framework.TestEvent event, org.mactor.framework.TestContext context) {
056 if (event.isStartEventType() || event.isTestCompleteEvent()) {
057 SwingUtilities.invokeLater(new Runnable() {
058 public void run() {
059 if (event.isTestCompleteEvent()) {
060 runTest.setEnabled(true);
061 stopTest.setEnabled(false);
062 }
063 rtModel.addEvent(event);
064 rtPanel.setModel(rtModel);
065 };
066 });
067 }
068 };
069 public void onTestRunCompleted(String testRunInstanceId, int succededCount, int failedCount) {
070 // TODO Auto-generated method stub
071 }
072 };
073 JButton runTest = new JButton(new AbstractAction("Run Test") {
074 public void actionPerformed(java.awt.event.ActionEvent e) {
075 rtModel.reset();
076 rtPanel.setModel(rtModel);
077 try {
078 tr = new TestRunner(1, test, getDataTable(), tfl);
079 tr.start();
080 runTest.setEnabled(false);
081 stopTest.setEnabled(true);
082 } catch (MactorException me) {
083 GuiUtil.showGuiError(SingelTestPanel.this, me);
084 }
085 };
086 });
087 JButton stopTest = new JButton(new AbstractAction("Stop Test") {
088 public void actionPerformed(java.awt.event.ActionEvent e) {
089 stop();
090 };
091 });
092 JButton addRowButton = new JButton(new AbstractAction("Add Param") {
093 public void actionPerformed(java.awt.event.ActionEvent e) {
094 pPanel.addNewRow();
095 };
096 });
097 DataTable getDataTable() {
098 DataTable dt = new DataTable();
099 Map<String, String> params = pPanel.getParams();
100 if (params.size() == 0) {
101 dt.addColumn("dummy_");
102 dt.addRow(new String[] { "" });
103 } else {
104 List<String> values = new LinkedList<String>();
105 for (Entry<String, String> e : params.entrySet()) {
106 dt.addColumn(e.getKey());
107 values.add(e.getValue());
108 }
109 dt.addRow(values);
110 }
111 return dt;
112 }
113 TestSpec test;
114 public SingelTestPanel(ProjectTreeNode node) throws MactorException {
115 super(new BorderLayout());
116 this.test = TestSpec.loadFromFile(node.getName());
117 SimpleFormPanel sf = new SimpleFormPanel();
118 rtPanel = new RunningTestTreePanel(node);
119 sf.add(new JLabel("Params:"));
120 sf.add(pPanel);
121 sf.add(addRowButton);
122 JPanel p1 = new JPanel(new BorderLayout());
123 p1.add(sf, BorderLayout.NORTH);
124 JScrollPane sp1 = new JScrollPane(p1);
125 JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sp1, rtPanel);
126 sp.setDividerLocation(400);
127 add(sp, BorderLayout.CENTER);
128 JPanel buttonPanel = new JPanel(new FlowLayout());
129 buttonPanel.add(runTest);
130 buttonPanel.add(stopTest);
131 stopTest.setEnabled(false);
132 add(buttonPanel, BorderLayout.NORTH);
133 }
134 public void stop() {
135 if (tr != null) {
136 tr.stop();
137 tr = null;
138 }
139 runTest.setEnabled(false);
140 stopTest.setEnabled(true);
141 }
142 }