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.Dimension;
024    import java.awt.FlowLayout;
025    import java.util.Calendar;
026    import java.util.HashMap;
027    import java.util.LinkedList;
028    import java.util.List;
029    
030    import javax.swing.AbstractAction;
031    import javax.swing.JButton;
032    import javax.swing.JPanel;
033    import javax.swing.JScrollPane;
034    import javax.swing.JTable;
035    import javax.swing.SwingUtilities;
036    import javax.swing.table.AbstractTableModel;
037    
038    import org.mactor.framework.MactorException;
039    import org.mactor.framework.MockRunner;
040    import org.mactor.framework.TestEvent;
041    import org.mactor.framework.TestFeedbackListener;
042    import org.mactor.framework.spec.MockBatterySpec;
043    import org.mactor.framework.spec.TestSpec;
044    import org.mactor.ui.gui.GuiUtil;
045    import org.mactor.ui.gui.Stoppable;
046    import org.mactor.ui.gui.project.ProjectTreeNode;
047    
048    public class MockBatteryPanel extends JPanel implements Stoppable {
049            JTable table;
050            MockRunner mr;
051            int testThreadCount;
052            MockStatusTableModel model;
053            List<TestSpec> tests = new LinkedList<TestSpec>();
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                                                    model.setLastEvent(event);
060                                            };
061                                    });
062                            }
063                    };
064                    public void onTestRunCompleted(String testRunInstanceId, int succededCount, int failedCount) {
065                            // TODO Auto-generated method stub
066                    }
067            };
068            JButton startButton = new JButton(new AbstractAction("Start Mock") {
069                    public void actionPerformed(java.awt.event.ActionEvent e) {
070                            try {
071                                    mr = new MockRunner(testThreadCount, tests, tfl);
072                                    mr.start();
073                                    startButton.setEnabled(false);
074                                    stopButton.setEnabled(true);
075                            } catch (MactorException me) {
076                                    GuiUtil.showGuiError(MockBatteryPanel.this, me);
077                            }
078                    };
079            });
080            JButton stopButton = new JButton(new AbstractAction("Stop Mock") {
081                    public void actionPerformed(java.awt.event.ActionEvent e) {
082                            mr.stop();
083                            startButton.setEnabled(true);
084                            stopButton.setEnabled(false);
085                    };
086            });
087            public MockBatteryPanel(ProjectTreeNode node) throws MactorException {
088                    super(new BorderLayout());
089                    MockBatterySpec spec = MockBatterySpec.loadFromFile(node.getName());
090                    testThreadCount = spec.getThreadCount();
091                    for (String t : spec.getTests())
092                            tests.add(TestSpec.loadFromFile(t));
093                    model = new MockStatusTableModel(tests);
094                    table = new JTable(model);
095                    stopButton.setEnabled(false);
096                    JPanel buttonPanel = new JPanel(new FlowLayout());
097                    buttonPanel.add(startButton);
098                    buttonPanel.add(stopButton);
099                    add(buttonPanel, BorderLayout.NORTH);
100                    JScrollPane sp = new JScrollPane(table);
101                    sp.setPreferredSize(new Dimension(800, 200));
102                    add(sp, BorderLayout.CENTER);
103            }
104            public void stop() {
105                    if (mr != null)
106                            mr.stop();
107            }
108            private class MockStatusTableModel extends AbstractTableModel {
109                    final String[] cols = new String[] { "Test Name", "Last Event Time", "Last Node", "Last Message" };
110                    TestSnapshotInfo[] ts;
111                    HashMap<TestSpec, TestSnapshotInfo> dic = new HashMap<TestSpec, TestSnapshotInfo>();
112                    public MockStatusTableModel(List<TestSpec> tests) {
113                            int i = 0;
114                            ts = new TestSnapshotInfo[tests.size()];
115                            for (TestSpec t : tests) {
116                                    ts[i] = new TestSnapshotInfo(i, t.getName());
117                                    dic.put(t, ts[i]);
118                                    i++;
119                            }
120                            System.out.println(ts.length);
121                    }
122                    @Override
123                    public String getColumnName(int index) {
124                            return cols[index];
125                    }
126                    public int getColumnCount() {
127                            return cols.length;
128                    }
129                    public int getRowCount() {
130                            return ts.length;
131                    }
132                    @Override
133                    public Class<?> getColumnClass(int arg0) {
134                            return String.class;
135                    }
136                    public Object getValueAt(int row, int col) {
137                            if (col == 0)
138                                    return ts[row].getTestName();
139                            else if (col == 1)
140                                    return ts[row].getLastEventTime() + "";
141                            else if (col == 2)
142                                    return ts[row].getLastNodeName();
143                            else if (col == 3)
144                                    return ts[row].getLastMessage();
145                            throw new RuntimeException("No such row!");
146                    }
147                    public void setLastEvent(TestEvent event) {
148                            TestSnapshotInfo t = dic.get(event.getTestSpec());
149                            if (t != null) {
150                                    if (event.isStartEventType()) {
151                                            t.setEventInfo(event.getTime(), event.getNode().getName(), event.getOutputText());
152                                            super.fireTableRowsUpdated(t.getIndex(), t.getIndex());
153                                    }
154                                    if (event.isTestCompleteEvent()) {
155                                            t.setEventInfo(event.getTime(), "Test Completed", event.getOutputText());
156                                    }
157                                    super.fireTableRowsUpdated(t.getIndex(), t.getIndex());
158                            } else {
159                                    System.out.println("test not found:" + event.getTestSpec());
160                            }
161                    }
162                    public void clearResults() {
163                            for (TestSnapshotInfo t : ts)
164                                    t.setEventInfo(null, null, null);
165                            super.fireTableDataChanged();
166                    }
167            }
168            private class TestSnapshotInfo {
169                    private String testName;
170                    private Calendar lastEventTime;
171                    private String lastNodeName;
172                    private String lastMessage;
173                    private int index;
174                    public TestSnapshotInfo(int index, String testName) {
175                            this.testName = testName;
176                            this.index = index;
177                    }
178                    public void setEventInfo(Calendar eventTime, String nodeName, String message) {
179                            this.lastEventTime = eventTime;
180                            this.lastNodeName = nodeName;
181                            this.lastMessage = message;
182                    }
183                    public String getLastEventTime() {
184                            return GuiUtil.format(lastEventTime);
185                    }
186                    public String getLastMessage() {
187                            return lastMessage;
188                    }
189                    public String getLastNodeName() {
190                            return lastNodeName;
191                    }
192                    public String getTestName() {
193                            return testName;
194                    }
195                    public int getIndex() {
196                            return index;
197                    }
198            }
199    }