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-2007 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: /*
043: * Utils.java
044: *
045: * Created on October 24, 2005, 2:37 PM
046: *
047: * This Utils used for helper functions aren't directly related with uml (screenshots, logs etc)
048: *
049: */
050:
051: package org.netbeans.test.umllib.util;
052:
053: import java.awt.AWTException;
054: import java.awt.Component;
055: import java.awt.Container;
056: import java.awt.Dimension;
057: import java.awt.Rectangle;
058: import java.awt.Toolkit;
059: import java.awt.event.KeyEvent;
060: import java.awt.image.BufferedImage;
061: import java.io.File;
062: import java.lang.reflect.Method;
063: import java.lang.reflect.Modifier;
064: import java.util.Date;
065: import org.netbeans.jellytools.MainWindowOperator;
066: import org.netbeans.jemmy.JemmyProperties;
067: import org.netbeans.jemmy.operators.JButtonOperator;
068: import org.netbeans.jemmy.operators.JDialogOperator;
069: import org.netbeans.jemmy.operators.JMenuBarOperator;
070: import org.netbeans.jemmy.operators.JMenuItemOperator;
071: import org.netbeans.jemmy.operators.JProgressBarOperator;
072: import org.netbeans.test.umllib.exceptions.UMLCommonException;
073: import org.netbeans.test.umllib.testcases.UMLTestCase;
074:
075: public class Utils {
076:
077: public static final long MAX_WAIT_TIME = 300000;
078: public static String WORK_DIR = System.getProperty("xtest.workdir");
079:
080: private static String DEFAULT_SCREENSHOT_PREFIX = "beforeTearDown";
081:
082: /**
083: * screenshot is created in directory accessible from xtest result
084: * call the method from your test methods or teardown/setup (i.e/ from your test class)
085: * method do nothing if there is problems with java.awt.Robot initialization or with file writing
086: * method with default prefix for screenshot file
087: * @param testClassName
088: * @param lastTestCase - last testase method name (if you want to place scrrenshot to methods's reprort directory)
089: */
090: public static void makeScreenShot(String testClassName,
091: String lastTestCase) {
092: makeScreenShotCustom(testClassName, lastTestCase,
093: DEFAULT_SCREENSHOT_PREFIX);
094: }
095:
096: /**
097: * screenshot is created in directory accessible from xtest result
098: * call the method from your test methods or teardown/setup (i.e/ from your test class)
099: * method do nothing if there is problems with java.awt.Robot initialization or with file writing
100: * by default only ide window is catched
101: * @param testClassName
102: * @param lastTestCase - last testase method name (if you want to place scrrenshot to methods's reprort directory)
103: * @param customPrefix part of screnshot file name
104: */
105:
106: public static void makeScreenShotCustom(String testClassName,
107: String lastTestCase, String customPrefix) {
108: makeScreenShotCustom(testClassName, lastTestCase, customPrefix,
109: false);
110: }
111:
112: /**
113: * screenshot is created in directory accessible from xtest result
114: * call the method from your test methods or teardown/setup (i.e/ from your test class)
115: * method do nothing if there is problems with java.awt.Robot initialization or with file writing
116: * @param testClassName
117: * @param lastTestCase - last testase method name (if you want to place scrrenshot to methods's reprort directory)
118: * @param customPrefix part of screnshot file name
119: */
120:
121: public static void makeScreenShotCustom(String testClassName,
122: String lastTestCase, String customPrefix, boolean fullscreen) {
123: //
124: String workdir = System.getProperty("xtest.workdir");
125: String path = workdir + "/user/" + testClassName + "/"
126: + lastTestCase + "/" + customPrefix
127: + new Date().getTime() + ".png";
128: //initially limited 1.5 support (workaround for calls from 1.6 java)
129: //but start check if test name valid in all cases
130: Class testClass = null;
131: Method[] ms = null;
132: Method m = null;
133: //most simple, should start with "test"
134: if (lastTestCase != null && lastTestCase.startsWith("test")) {
135: try {
136: testClass = Class.forName(testClassName);
137: } catch (ClassNotFoundException ex) {
138: ex.printStackTrace();
139: }
140: if (testClass != null) {
141: ms = testClass.getMethods();
142: for (int i = 0; i < ms.length; i++) {
143: int md = ms[i].getModifiers();
144: //test methods can be only public
145: //no parameters,
146: if (Modifier.isPublic(md)
147: && ms[i].getParameterAnnotations().length == 0
148: && ms[i].getReturnType().getName().indexOf(
149: "void") > -1) {
150: //we got it
151: m = ms[i];
152: break;
153: }
154: }
155: }
156: }
157: //path correction if didn't find such test method'
158: if (m == null)
159: path = workdir + "/user/" + testClassName + "/"
160: + customPrefix + new Date().getTime() + ".png";
161: java.awt.Robot rbt = null;
162: try {
163: rbt = new java.awt.Robot();
164: } catch (AWTException ex) {
165: ex.printStackTrace();
166: }
167: if (rbt != null) {
168: Rectangle bounds = null;
169: if (!fullscreen) {
170: bounds = MainWindowOperator.getDefault().getBounds();
171: } else {
172: Dimension size = Toolkit.getDefaultToolkit()
173: .getScreenSize();
174: bounds = new Rectangle(size);
175: }
176: BufferedImage img = rbt.createScreenCapture(bounds);
177: File saveTo = new File(path);
178: try {
179: javax.imageio.ImageIO.write(img, "png", saveTo);
180: } catch (Exception ex) {
181: ex.printStackTrace();
182: }
183: }
184:
185: }
186:
187: /**
188: * make scrrenshot and stores according to last test case name
189: * should be called from test class
190: * method with default prefix for screenshot file
191: * @param lastTestCase
192: */
193: public static void makeScreenShot(String lastTestCase) {
194: makeScreenShot(lastTestCase, false);
195: }
196:
197: /**
198: * make scrrenshot and stores according to last test case name
199: * should be called from test class
200: * method with default prefix for screenshot file
201: * @param lastTestCase
202: */
203: public static void makeScreenShot(String lastTestCase,
204: boolean fullscreen) {
205: makeScreenShotCustom(lastTestCase, DEFAULT_SCREENSHOT_PREFIX,
206: fullscreen);
207: }
208:
209: /**
210: * make scrrenshot and stores according to last test case name
211: * should be called from test class
212: * @param lastTestCase
213: * @param customPrefix part of screnshot file name
214: */
215: public static void makeScreenShotCustom(String lastTestCase,
216: String customPrefix) {
217: makeScreenShotCustom(lastTestCase, customPrefix, false);
218: }
219:
220: /**
221: * make scrrenshot and stores according to last test case name
222: * should be called from test class
223: * @param lastTestCase
224: * @param customPrefix part of screnshot file name
225: */
226: public static void makeScreenShotCustom(String lastTestCase,
227: String customPrefix, boolean fullscreen) {
228: String clName = UMLTestCase
229: .getCurrentClassNameWithCheck(lastTestCase);
230: if (clName != null)
231: makeScreenShotCustom(clName, lastTestCase, customPrefix);
232: }
233:
234: /**
235: * screenshot is created in directory accessible from xtest result
236: * call the method from any method but test method should be in stack
237: * screenshot is paced in report directory fro last executed test* method
238: * method do nothing if there is problems with java.awt.Robot initialization or there is no 'test*' method in trace or with file writing
239: * method with default prefix for screenshot file
240: */
241: public static void makeScreenShot() {
242: makeScreenShot(false);
243: makeScreenShotCustom(DEFAULT_SCREENSHOT_PREFIX);
244: }
245:
246: /**
247: * screenshot is created in directory accessible from xtest result
248: * call the method from any method but test method should be in stack
249: * screenshot is paced in report directory fro last executed test* method
250: * method do nothing if there is problems with java.awt.Robot initialization or there is no 'test*' method in trace or with file writing
251: * method with default prefix for screenshot file
252: */
253: public static void makeScreenShot(boolean fullscreen) {
254: makeScreenShotCustom(DEFAULT_SCREENSHOT_PREFIX, fullscreen);
255: }
256:
257: /**
258: * screenshot is created in directory accessible from xtest result
259: * call the method from any method but test method should be in stack
260: * screenshot is paced in report directory fro last executed test* method
261: * method do nothing if there is problems with java.awt.Robot initialization or there is no 'test*' method in trace or with file writing
262: * @param customPrefix part of screnshot file name
263: */
264: public static void makeScreenShotCustom(String customPrefix) {
265: makeScreenShotCustom(customPrefix, false);
266: }
267:
268: /**
269: * screenshot is created in directory accessible from xtest result
270: * call the method from any method but test method should be in stack
271: * screenshot is paced in report directory fro last executed test* method
272: * method do nothing if there is problems with java.awt.Robot initialization or there is no 'test*' method in trace or with file writing
273: * @param customPrefix part of screnshot file name
274: */
275: public static void makeScreenShotCustom(String customPrefix,
276: boolean fullscreen) {
277: String workdir = System.getProperty("xtest.workdir");
278: StackTraceElement[] els = Thread.currentThread()
279: .getStackTrace();
280: String[] ret = UMLTestCase.getCurrentTestNamesWithCheck();
281:
282: if (ret == null || ret[1] == null) {
283: return;
284: }
285:
286: makeScreenShotCustom(ret[0], ret[1], customPrefix, fullscreen);
287: }
288:
289: public static void waitScanningClassPath() {
290:
291: try {
292: Thread.sleep(3000);
293: } catch (Exception e) {
294: }
295:
296: long waitTime = 50;
297: long waitCount = MAX_WAIT_TIME / waitTime;
298:
299: for (long time = 0; time < waitCount; time++) {
300: try {
301: Thread.sleep(waitTime);
302: } catch (Exception e) {
303: }
304:
305: Object scanning = JProgressBarOperator
306: .findJProgressBar((Container) MainWindowOperator
307: .getDefault().getSource());
308: if (scanning == null) {
309: return;
310: }
311: }
312: throw new UMLCommonException("Scaning isn't finished in "
313: + MAX_WAIT_TIME + " ms");
314: }
315:
316: public static void saveAll() {
317: try {
318: //================ Save All Action =================
319: //SaveAllAction saveAllAction = new SaveAllAction();
320: //if(saveAllAction.isEnabled()){
321: // saveAllAction.performMenu();
322: //}
323: JMenuBarOperator mm = new JMenuBarOperator(
324: MainWindowOperator.getDefault());
325: JMenuItemOperator it = mm.showMenuItem("File|Save All");
326: if (it.isEnabled())
327: it.pushNoBlock();
328: else
329: mm.pushKey(KeyEvent.VK_ESCAPE);
330:
331: } catch (Exception e) {
332: System.out.println("Exception in tearDown mwthod: "
333: + e.getMessage());
334: }
335: }
336:
337: /**
338: *
339: * @param dialogName
340: * @param dialogButton
341: */
342: public static void closeDialog(String dialogName,
343: String dialogButton) {
344: final String name = dialogName;
345: final String button = dialogButton;
346: new Thread(new Runnable() {
347: //java.awt.EventQueue.invokeLater(new Runnable() {
348: public void run() {
349: long timeoutValDlg = JemmyProperties
350: .getCurrentTimeout("DialogWaiter.WaitDialogTimeout");
351: try {
352: Thread.sleep(3000);
353: JemmyProperties.setCurrentTimeout(
354: "DialogWaiter.WaitDialogTimeout", 10000);
355: JDialogOperator dialog = new JDialogOperator(name);
356: new JButtonOperator(dialog, button).pushNoBlock();
357: } catch (Exception e) {
358: } finally {
359: JemmyProperties.setCurrentTimeout(
360: "DialogWaiter.WaitDialogTimeout",
361: timeoutValDlg);
362: }
363: }
364: //});
365: }).start();
366: }
367:
368: /**
369: *
370: * @param dialogName
371: * @param dialogButton
372: */
373: public static void closeTwoDialogs(String dialogName,
374: String dialogButton) {
375: final String name = dialogName;
376: final String button = dialogButton;
377: new Thread(new Runnable() {
378: //java.awt.EventQueue.invokeLater(new Runnable() {
379: public void run() {
380: long timeoutValDlg = JemmyProperties
381: .getCurrentTimeout("DialogWaiter.WaitDialogTimeout");
382: try {
383: Thread.sleep(3000);
384: JemmyProperties.setCurrentTimeout(
385: "DialogWaiter.WaitDialogTimeout", 10000);
386: JDialogOperator dialog = new JDialogOperator(name);
387: new JButtonOperator(dialog, button).pushNoBlock();
388: closeDialog(name, button);
389: } catch (Exception e) {
390: } finally {
391: JemmyProperties.setCurrentTimeout(
392: "DialogWaiter.WaitDialogTimeout",
393: timeoutValDlg);
394: }
395: }
396: //});
397: }).start();
398: }
399:
400: public static void closeSaveDialog() {
401: closeTwoDialogs(LabelsAndTitles.DIALOG_TITLE_SAVE,
402: LabelsAndTitles.DIALOG_BUTTON_SAVE_ALL);
403: }
404:
405: public static void closeExitDialog() {
406: closeDialog(LabelsAndTitles.DIALOG_TITLE_EXIT_IDE,
407: LabelsAndTitles.DIALOG_BUTTON_EXIT_IDE);
408: }
409:
410: public static void tearDown() {
411: releaseModificators();
412: saveAll();
413: closeSaveDialog();
414: closeExitDialog();
415: }
416:
417: private static void releaseModificators() {
418: java.awt.Robot rbt = null;
419: try {
420: rbt = new java.awt.Robot();
421: } catch (AWTException ex) {
422: ex.printStackTrace();
423: }
424: if (rbt != null) {
425: rbt.keyRelease(KeyEvent.VK_SHIFT);
426: rbt.keyRelease(KeyEvent.VK_CONTROL);
427: rbt.keyRelease(KeyEvent.VK_ALT);
428: }
429: }
430:
431: public static void showIDE() {
432: showComponents(MainWindowOperator.getDefault().getSource());
433: }
434:
435: /**
436: *
437: * @param comp
438: */
439: public static void showComponents(Component comp) {
440: showComponents("", comp);
441: }
442:
443: /**
444: *
445: * @param blank
446: * @param comp
447: */
448: public static void showComponents(String blank, Component comp) {
449: showClassHierarchy(blank, comp);
450:
451: if (comp instanceof Container) {
452: Container cont = (Container) comp;
453: Component[] comps = cont.getComponents();
454:
455: for (Component c : comps) {
456: showComponents(blank + " ", c);
457: }
458:
459: }
460:
461: }
462:
463: /**
464: *
465: * @param obj
466: */
467: public static void showClassHierarchy(Object obj) {
468: showClassHierarchy("", obj);
469: }
470:
471: /**
472: *
473: * @param blank
474: * @param obj
475: */
476: public static void showClassHierarchy(String blank, Object obj) {
477: showClassHierarchy(blank + " ", obj.getClass());
478: System.out.println(blank + obj);
479: }
480:
481: /**
482: *
483: * @param cls
484: */
485: protected static void showClassHierarchy(Class cls) {
486: showClassHierarchy("", cls);
487: }
488:
489: /**
490: *
491: * @param blank
492: * @param cls
493: */
494: protected static void showClassHierarchy(String blank, Class cls) {
495:
496: Class super Class = cls.getSuperclass();
497: if (super Class != null) {
498: showClassHierarchy(blank + " ", super Class);
499: }
500:
501: Class[] interfaces = cls.getInterfaces();
502: if (interfaces != null) {
503: for (Class i : interfaces) {
504: showClassHierarchy(blank + " ", i);
505: }
506: }
507:
508: System.out.println(blank + "\"" + cls.getName() + "\"");
509:
510: }
511:
512: }
|