001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.jellytools;
043:
044: /*
045: * JellyTestCase.java
046: *
047: * Created on June 26, 2002, 4:08 PM
048: */
049:
050: import java.awt.Component;
051: import java.awt.Container;
052: import java.awt.Toolkit;
053: import java.awt.Window;
054: import java.awt.event.AWTEventListener;
055: import java.awt.event.HierarchyEvent;
056: import java.awt.event.HierarchyListener;
057:
058: import java.io.File;
059: import java.io.PrintStream;
060: import javax.swing.JDialog;
061:
062: import org.netbeans.junit.*;
063:
064: import org.netbeans.jemmy.*;
065: import org.netbeans.jemmy.operators.*;
066: import org.netbeans.jemmy.operators.JDialogOperator;
067: import org.netbeans.jemmy.util.PNGEncoder;
068: import org.netbeans.jemmy.util.Dumper;
069:
070: /** JUnit test case with implemented Jemmy/Jelly2 support stuff
071: *
072: * @author <a href="mailto:adam.sotona@sun.com">Adam Sotona</a>
073: * @version 1.0
074: */
075: public class JellyTestCase extends NbTestCase {
076:
077: /** screen capture feature in case of failure is enabled by default
078: */
079: public boolean captureScreen = Boolean.valueOf(
080: System.getProperty("jemmy.screen.capture", "true"))
081: .booleanValue();
082:
083: /** screen XML dump feature in case of failure is disabled by default
084: */
085: public boolean dumpScreen = Boolean
086: .getBoolean("jemmy.screen.xmldump");
087:
088: /** closing all modal dialogs after each test case is disabled by default
089: */
090: public boolean closeAllModal = Boolean.valueOf(
091: System.getProperty("jelly.close.modal", "true"))
092: .booleanValue();
093: /** Wait 1000 ms before every test case */
094: public boolean waitNoEvent = Boolean.valueOf(
095: System.getProperty("jelly.wait.no.event", "true"))
096: .booleanValue();
097:
098: /** constructor required by JUnit
099: * @param testName method name to be used as testcase
100: */
101: public JellyTestCase(String testName) {
102: super (testName);
103: }
104:
105: /** Inits environment before test case is executed. It can be overridden
106: * in sub class but it is recommended to call super.initEnvironment() at
107: * the beginning.
108: * <br>
109: * Default initialization: output messages from jemmy are redirected
110: * to jemmy.log file in workdir; jemmy timeouts are loaded from
111: * org.netbeans.jellytools.timeouts and if system property jelly.timeouts_resource
112: * or jelly.timeouts_file are set, timeouts are loaded from specified
113: * resource/file;
114: */
115: protected void initEnvironment() {
116: // redirect log messages from jemmy to jemmy.log file in workdir
117: PrintStream jemmyLog = getLog("jemmy.log");
118: JemmyProperties.setCurrentOutput(new TestOut(System.in,
119: jemmyLog, jemmyLog));
120: // load timeouts
121: String timeoutsResource = System
122: .getProperty("jelly.timeouts_resource");
123: String timeoutsFile = System.getProperty("jelly.timeouts_file");
124: try {
125: JemmyProperties.getCurrentTimeouts().load(
126: getClass().getClassLoader().getResourceAsStream(
127: "org/netbeans/jellytools/timeouts"));
128: if (timeoutsResource != null
129: && !"".equals(timeoutsResource)) {
130: JemmyProperties.getCurrentTimeouts().load(
131: getClass().getClassLoader()
132: .getResourceAsStream(timeoutsResource));
133: } else if (timeoutsFile != null && !"".equals(timeoutsFile)) {
134: JemmyProperties.getCurrentTimeouts().load(timeoutsFile);
135: }
136: } catch (Exception e) {
137: throw new JemmyException(
138: "Initialization of timeouts failed.", e);
139: }
140: }
141:
142: /** Overriden method from JUnit framework execution to perform conditional
143: * screen shot and conversion from TimeoutExpiredException to AssertionFailedError. <br>
144: * Waits a second before test execution.
145: * @throws Throwable Throwable
146: */
147: public void runBare() throws Throwable {
148: initEnvironment();
149: // workaround for JDK bug 4924516 (see below)
150: Toolkit.getDefaultToolkit().addAWTEventListener(
151: distributingHierarchyListener,
152: HierarchyEvent.HIERARCHY_EVENT_MASK);
153: // wait
154: if (waitNoEvent) {
155: new EventTool().waitNoEvent(1000);
156: }
157: try {
158: super .runBare();
159: } catch (ThreadDeath td) {
160: // ThreadDead must be re-throwed immediately
161: throw td;
162: } catch (Throwable th) {
163: // suite is notified about test failure so it can do some debug actions
164: try {
165: failNotify(th);
166: } catch (Exception e3) {
167: }
168: // screen capture is performed when test fails and in dependency on system property
169: if (captureScreen) {
170: try {
171: PNGEncoder.captureScreen(getWorkDir()
172: .getAbsolutePath()
173: + File.separator + "screen.png");
174: } catch (Exception e1) {
175: }
176: }
177: // XML dump is performed when test fails and in dependency on system property
178: if (dumpScreen) {
179: try {
180: Dumper.dumpAll(getWorkDir().getAbsolutePath()
181: + File.separator + "screen.xml");
182: } catch (Exception e2) {
183: }
184: }
185: // closes all modal dialogs in dependency on systems property
186: if (closeAllModal)
187: try {
188: closeAllModal();
189: } catch (Exception e) {
190: }
191: if (th instanceof JemmyException) {
192: // all instancies of JemmyException are re-throwed as AssertionError (test failed)
193: throw new AssertionFailedErrorException(
194: th.getMessage(), th);
195: } else {
196: throw th;
197: }
198: } finally {
199: // workaround for JDK bug 4924516 (see below)
200: Toolkit.getDefaultToolkit().removeAWTEventListener(
201: distributingHierarchyListener);
202: }
203: }
204:
205: /** Method called in case of fail or error just after screen shot and XML dumps. <br>
206: * Override this method when you need to be notified about test failures or errors
207: * but avoid any exception to be throwed from this method.<br>
208: * super.failNotify() does not need to be called because it is empty.
209: * @param reason Throwable reason of current fail */
210: protected void failNotify(Throwable reason) {
211: }
212:
213: /** Closes all opened modal dialogs. Non-modal stay opened. */
214: public static void closeAllModal() {
215: JDialog dialog;
216: ComponentChooser chooser = new ComponentChooser() {
217: public boolean checkComponent(Component comp) {
218: return (comp instanceof JDialog && comp.isShowing() && ((JDialog) comp)
219: .isModal());
220: }
221:
222: public String getDescription() {
223: return ("Modal dialog");
224: }
225: };
226: while ((dialog = (JDialog) DialogWaiter.getDialog(chooser)) != null) {
227: closeDialogs(findBottomDialog(dialog, chooser), chooser);
228: }
229: }
230:
231: private static JDialog findBottomDialog(JDialog dialog,
232: ComponentChooser chooser) {
233: Window owner = dialog.getOwner();
234: if (chooser.checkComponent(owner)) {
235: return (findBottomDialog((JDialog) owner, chooser));
236: }
237: return (dialog);
238: }
239:
240: private static void closeDialogs(JDialog dialog,
241: ComponentChooser chooser) {
242: Window[] ownees = dialog.getOwnedWindows();
243: for (int i = 0; i < ownees.length; i++) {
244: if (chooser.checkComponent(ownees[i])) {
245: closeDialogs((JDialog) ownees[i], chooser);
246: }
247: }
248: new JDialogOperator(dialog).close();
249: }
250:
251: /** Finishes test with status Fail
252: * @param t Throwable reason of test failure
253: */
254: public void fail(Throwable t) {
255: t.printStackTrace(getLog());
256: throw new AssertionFailedErrorException(t);
257: }
258:
259: /*
260: * methods for managing failures of group of dependent tests
261: * Usage: each method involved in a group must start with
262: * startTest() call and when finished it must perform endTest().
263: * To clear the test status (new group of tests does not depend on
264: * previous tests), method must call clearTestStatus() prior to startTest()
265: * Example:
266: * public void myTest() {
267: * startTest();
268: * // do my stuff
269: * endTest();
270: * }
271: */
272:
273: /** private variable for holding state whether test was finished
274: */
275: private static boolean testStatus = true;
276:
277: /** Checks whether previus test finished correctly and
278: * sets test status to 'not finished' state
279: *
280: */
281: protected void startTest() {
282: if (!testStatus) {
283: fail("Depending on previous test, but it failed");
284: }
285: testStatus = false;
286: }
287:
288: /** Sets the test status to 'finished' state (test passed)
289: */
290: protected void endTest() {
291: testStatus = true;
292: }
293:
294: /** Clears test status (used when test does not depend on previous test)
295: */
296: protected void clearTestStatus() {
297: testStatus = true;
298: }
299:
300: /* Workaround for JDK bug http://developer.java.sun.com/developer/bugParade/bugs/4924516.html.
301: * Also see issue http://www.netbeans.org/issues/show_bug.cgi?id=32466.
302: * ------------------------------------------------------------------------------------------
303: * It can be removed when it is fixed (probably in JDK1.5.0). The following
304: * listener is added to Toolkit at runBare() method and removed when it finishes.
305: * It distributes HierarchyEvent to all listening components and its subcomponents.
306: */
307: private static final DistributingHierarchyListener distributingHierarchyListener = new DistributingHierarchyListener();
308:
309: private static class DistributingHierarchyListener implements
310: AWTEventListener {
311:
312: public DistributingHierarchyListener() {
313: }
314:
315: public void eventDispatched(java.awt.AWTEvent aWTEvent) {
316: HierarchyEvent hevt = null;
317: if (aWTEvent instanceof HierarchyEvent) {
318: hevt = (HierarchyEvent) aWTEvent;
319: }
320: if (hevt != null
321: && ((HierarchyEvent.SHOWING_CHANGED & hevt
322: .getChangeFlags()) != 0)) {
323: distributeShowingEvent(hevt.getComponent(), hevt);
324: }
325: }
326:
327: private static void distributeShowingEvent(Component c,
328: HierarchyEvent hevt) {
329: //HierarchyListener[] hierarchyListeners = c.getHierarchyListeners();
330: // Need to use component.getListeners because it is not synchronized
331: // and it not cause deadlock
332: HierarchyListener[] hierarchyListeners = (HierarchyListener[]) (c
333: .getListeners(HierarchyListener.class));
334: if (hierarchyListeners != null) {
335: for (int i = 0; i < hierarchyListeners.length; i++) {
336: hierarchyListeners[i].hierarchyChanged(hevt);
337: }
338: }
339: if (c instanceof Container) {
340: Container cont = (Container) c;
341: int n = cont.getComponentCount();
342: for (int i = 0; i < n; i++) {
343: distributeShowingEvent(cont.getComponent(i), hevt);
344: }
345: }
346: }
347: }
348: }
|