View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2005-2006 Christian W. Hargraves (engrean@hotmail.com)
4   
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon.ui;
20  
21  import net.sf.jameleon.ExecuteTestCase;
22  import net.sf.jameleon.bean.TestCase;
23  import net.sf.jameleon.event.DataDrivableEventHandler;
24  import net.sf.jameleon.event.FunctionEventHandler;
25  import net.sf.jameleon.event.TestCaseEventHandler;
26  import net.sf.jameleon.util.Configurator;
27  import net.sf.jameleon.util.JameleonDefaultValues;
28  import net.sf.jameleon.util.TemplateProcessor;
29  import org.apache.commons.jelly.JellyException;
30  
31  import javax.swing.*;
32  import javax.swing.event.HyperlinkEvent;
33  import javax.swing.event.HyperlinkListener;
34  import javax.swing.text.html.HTMLEditorKit;
35  import javax.swing.tree.TreePath;
36  import java.awt.*;
37  import java.awt.event.*;
38  import java.io.File;
39  import java.io.IOException;
40  import java.lang.reflect.Method;
41  import java.net.MalformedURLException;
42  import java.net.URL;
43  import java.util.HashMap;
44  
45  public class TestCasePane extends JPanel {
46  
47      protected JEditorPane tcDocsPane = new JEditorPane();
48      protected JButton executeButton = new JButton();
49      protected JButton stopExecutionButton = new JButton();
50      protected JButton debugButton = new JButton();
51      protected JButton resultsButton = new JButton("View All Results");
52      protected JTextArea tcSourcePane = new JTextArea();
53      protected TestCaseTree tcTree;
54      protected TestCaseResultsPane resultsPane;
55      protected JTabbedPane tabbedPane;
56      protected JFrame parent;
57      protected boolean stopExecution = false;
58      protected BasicHtmlBrowser bugBrowser = new BasicHtmlBrowser("Bug Info");
59      protected BasicHtmlBrowser resultsBrowser = new BasicHtmlBrowser("Jameleon Test Run Results");
60      private static final String DOCS = "Test Case Docs";
61      private static final String SOURCE = "Test Case Source";
62      private static final String RESULTS = "Test Case Results";
63      public static final int DOCS_INDEX = 0;
64      public static final int SOURCE_INDEX = 1;
65      public static final int RESULTS_INDEX = 2;
66      public static final int SRC_TAB_SIZE = 2;
67  
68      public TestCasePane(JFrame parent) {
69          super(new BorderLayout());
70          this.parent = parent;
71          resultsPane = new TestCaseResultsPane(parent);
72          resultsPane.setSourceArea(tcSourcePane);
73          genUI();
74      }
75  
76      public JFrame getJameleonUI(){
77          return parent;
78      }
79  
80      public void setTestCaseTree(TestCaseTree tcTree) {
81          this.tcTree = tcTree;
82          resultsPane.setTestCaseTree(tcTree);
83      }
84  
85      /***
86       * @return ImageIcon, or null if the path was invalid.
87       */
88      protected ImageIcon createImageIcon(String path, String description) {
89          URL imgURL = this.getClass().getResource(path);
90          ImageIcon icon = null;
91          if (imgURL != null) {
92              icon = new ImageIcon(imgURL, description);
93          } else {
94              System.err.println("Couldn't find file: " + path);
95          }
96          return icon;
97      }
98  
99      protected void genUI() {
100         executeButton.setMnemonic(KeyEvent.VK_E);
101         debugButton.setMnemonic(KeyEvent.VK_D);
102         stopExecutionButton.setMnemonic(KeyEvent.VK_S);
103         tcDocsPane.setEditable(false);
104         tcDocsPane.setEditorKit(new HTMLEditorKit());
105         tcDocsPane.addHyperlinkListener(new HyperlinkListener(){
106                 public void hyperlinkUpdate(HyperlinkEvent e) {
107                     if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
108                         bugBrowser.goToUrl(e.getURL());
109                     }
110                 }
111                 });
112         tcSourcePane.setEditable(false);
113         tcSourcePane.setTabSize(SRC_TAB_SIZE);
114         tcSourcePane.getCaret().setSelectionVisible(true);
115         ExecuteButtonAction buttonListener = new ExecuteButtonAction(this);
116         executeButton.addActionListener(buttonListener);
117         executeButton.setIcon(createImageIcon("/icons/running.gif", "Run Test(s)"));
118         executeButton.setToolTipText("Run Test(s)");
119 
120         stopExecutionButton.setIcon(createImageIcon("/icons/stop.gif", "Stop Execution"));
121         stopExecutionButton.setToolTipText("Stop test case execution");
122         stopExecutionButton.addActionListener(new ActionListener(){
123                     public void actionPerformed(ActionEvent e) {
124                         stopExecution = true;
125                     }
126                 });
127 
128         debugButton.addActionListener(buttonListener);
129         debugButton.setIcon(createImageIcon("/icons/debug.gif", "Debug Test(s)"));
130         debugButton.setToolTipText("Debug Test(s)");
131         debugButton.setName("debug button");
132 
133         resultsButton.addActionListener(new ActionListener(){
134             public void actionPerformed(ActionEvent e) {
135                 String baseDir = Configurator.getInstance().getValue("baseDir", JameleonDefaultValues.BASE_DIR.getPath());
136                 String resDir = Configurator.getInstance().getValue("resultsDir", JameleonDefaultValues.RESULTS_DIR);
137                 String results = JameleonDefaultValues.TEST_RUN_SUMMARY_FILE_NAME;
138                 File resultsFile = new File(baseDir + File.separator + resDir + File.separator + results);
139                 try {
140                     resultsBrowser.goToUrl( resultsFile.toURI().toURL() );
141                 } catch (MalformedURLException e1) {
142                     e1.printStackTrace();
143                 }
144             }
145         });
146 
147         resultsButton.setToolTipText("View the HTML Test Run Summary Results");
148         resultsButton.setName("test run results button");
149 
150         JPanel northP = new JPanel(new SpringLayout());
151         northP.add(executeButton);
152         northP.add(stopExecutionButton);
153         northP.add(debugButton);
154         northP.add(resultsButton);
155 
156         SpringUtilities.makeCompactGrid(northP,
157                                         1, 4,   //rows, cols
158                                         6, 6,   //initX, initY
159                                         6, 6);  //xPad, yPad
160                                         
161         add(northP, BorderLayout.NORTH);
162         tabbedPane = new JTabbedPane(JTabbedPane.BOTTOM);
163 
164         tabbedPane.add(DOCS, new JScrollPane(tcDocsPane));
165         tabbedPane.add(SOURCE, new JScrollPane(tcSourcePane));
166         tabbedPane.add(RESULTS, resultsPane);
167 
168         add(tabbedPane, BorderLayout.CENTER);
169     }
170 
171     public JTabbedPane getTabbedPane(){
172         return tabbedPane;
173     }
174 
175     public void setTestCaseInfo(TestCase tc) {
176         TemplateProcessor xfrmr = new TemplateProcessor("TestCaseDocsTemplate.txt");
177         HashMap vars = new HashMap();
178         vars.put("tc", tc);
179         String bugUrl = Configurator.getInstance().getValue("bugTrackerUrl");
180         if (bugUrl != null) {
181             vars.put("bugTrackerUrl", bugUrl);
182         }
183         vars.put("printFileName", Boolean.FALSE);
184         String contents = xfrmr.transformToString(vars);
185         tcDocsPane.setText(contents);
186         tcDocsPane.setCaretPosition(0);
187         if (tabbedPane.getSelectedIndex() == RESULTS_INDEX) {
188             tabbedPane.setSelectedIndex(DOCS_INDEX);
189         }
190     }
191 
192     public void setTestCaseSource(String contents) {
193         tcSourcePane.setText(contents);
194         tcSourcePane.setCaretPosition(0);
195     }
196 
197     protected class ExecuteButtonAction implements ActionListener {
198 
199         TestCasePane tcPane;
200         private boolean isExecuting;
201 
202         protected ExecuteButtonAction(TestCasePane tcPane) {
203             this.tcPane = tcPane;
204         }
205 
206         /***
207          * Invoked when an action occurs.
208          */
209         public void actionPerformed(ActionEvent e) {
210             resultsPane.resetTable();
211             JButton button = (JButton)e.getSource();
212             final boolean debug = "debug button".equals(button.getName());
213             final TreePath[] selectedTcs = tcTree.getSelectionPaths();
214             tabbedPane.setSelectedIndex(RESULTS_INDEX);
215             
216             resultsPane.setVisible(true);
217             stopExecution = false;
218             resultsPane.proceedExecution();
219 
220             Thread t = new Thread() {
221                 public void run() {
222                     ExecuteTestCase executor = new ExecuteTestCase();
223                     executor.registerEventListeners();
224                     for (int i = 0; i < selectedTcs.length && !stopExecution; i++) {
225                         Object obj = selectedTcs[i].getLastPathComponent();
226                         if (obj instanceof TCTreeNode) {
227                             TCTreeNode tn = (TCTreeNode)obj;
228                             FileNode fn = (FileNode)tn.getUserObject();
229                             if (!fn.isDir()) {
230 
231                                 if (debug) {
232                                     tabbedPane.setSelectedIndex(SOURCE_INDEX);
233                                 }
234                                 executeTestCase(fn, debug, executor);
235                                 while (isExecuting) {
236                                     if (stopExecution) {                                        
237                                         resultsPane.stopExecution();
238                                         this.interrupt();
239                                     }
240                                     try{
241                                         Thread.sleep(100);
242                                     }catch(InterruptedException ie){
243                                         //Apparently people don't like a stack trace printing out to the screen.
244                                         //ie.printStackTrace();
245                                     }
246                                 }
247                                 if (debug) {
248                                     tabbedPane.setSelectedIndex(RESULTS_INDEX);
249                                 }
250                             }
251                         }
252                     }
253                     executor.deregisterEventListeners();
254                 }
255             };
256 
257             t.setContextClassLoader(Utils.createClassLoader());
258             t.start();
259         }
260 
261 
262         protected Thread executeTestCase(final FileNode fn, final boolean debug, final ExecuteTestCase executor) {
263             isExecuting = true;
264             Thread t = new Thread(){
265                     public void run() {
266                         tcTree.setTestCaseSource(fn.getFile(), false);
267                         TestCaseEventHandler tcEventHandler = TestCaseEventHandler.getInstance();
268                         FunctionEventHandler fEventHandler = FunctionEventHandler.getInstance();
269                         DataDrivableEventHandler ddEventHandler = DataDrivableEventHandler.getInstance();
270                         try {
271                             if (debug) {
272                                 resultsPane.setDebug(true);
273                             }
274                             fEventHandler.addFunctionListener(resultsPane);
275                             tcEventHandler.addTestCaseListener(resultsPane);
276                             ddEventHandler.addDataDrivableListener(resultsPane);
277 
278                             try {
279                                 executor.runScript(fn.getFile());
280                             } catch (IOException ioe) {
281                                 ioe.printStackTrace();
282                             } catch (JellyException je) {
283                                 //ignore this for now.
284                                 //TODO: find a better way of handling this.
285                             }
286                         } finally {
287                             resultsPane.setDebug(false);
288                             fEventHandler.removeFunctionListener(resultsPane);
289                             tcEventHandler.removeTestCaseListener(resultsPane);
290                             ddEventHandler.removeDataDrivableListener(resultsPane);
291                             isExecuting = false;
292                         }
293                     }
294                     };
295             t.start();
296             return t;
297         }
298 
299     }
300 
301     protected class JiffiePluginAction implements ActionListener {
302 
303         private Object jiffieUI;
304 
305         /***
306          * Invoked when an action occurs.
307          */
308         public void actionPerformed(ActionEvent ae) {
309             if (jiffieUI == null) {
310                 try {
311 
312                     final Class clss = Class.forName("net.sf.jameleon.plugin.jiffie.ui.IEActionPointGenerator");
313                     jiffieUI = clss.newInstance();
314 
315                     Thread t = new Thread() {
316                         public void run() {
317                             try {
318                                 Class[] args = {WindowAdapter.class};
319                                 Method setCloseListener = clss.getDeclaredMethod("setExternalCloseListener", args);
320                                 Object[] ms = {
321                                     new WindowAdapter() {
322                                         public void windowClosing(WindowEvent e) {
323                                             jiffieUI = null;
324                                         }
325                                     }
326                                 };
327                                 setCloseListener.invoke(jiffieUI, ms);
328                                 Method genGUI = clss.getDeclaredMethod("genGUI", new Class[0]);
329                                 genGUI.invoke(jiffieUI, new Object[0]);
330                             } catch (Exception e) {
331                                 e.printStackTrace();
332                             }
333                         }
334                     };
335                     t.start();
336                 } catch (Exception ex) {
337                     ex.printStackTrace();
338                 }
339             }
340         }
341 
342     }
343 
344 }