KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > junit > utils > Utilities


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.junit.utils;
21
22 import com.sun.org.apache.bcel.internal.generic.IFEQ;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import org.netbeans.jellytools.Bundle;
26 import org.netbeans.jellytools.NbDialogOperator;
27 import org.netbeans.jellytools.ProjectsTabOperator;
28 import org.netbeans.jellytools.actions.OpenAction;
29 import org.netbeans.jellytools.nodes.Node;
30 import org.netbeans.jemmy.EventTool;
31 import org.netbeans.jemmy.operators.JCheckBoxOperator;
32 import org.netbeans.jemmy.operators.JPopupMenuOperator;
33 import org.openide.loaders.DataObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.actions.SaveAllAction;
36
37 /**
38  *
39  * @author Max Sauer
40  */

41 public class Utilities {
42     /** name of sample project */
43     public static final String JavaDoc TEST_PROJECT_NAME = "JunitTestProject";
44     
45     /** name of sample class */
46     public static final String JavaDoc TEST_CLASS_NAME = "TestClass";
47     
48     /** label when deleting object */
49     public static final String JavaDoc CONFIRM_OBJECT_DELETION =
50             Bundle.getString("org.openide.explorer.Bundle",
51             "MSG_ConfirmDeleteObjectTitle");
52     
53     /** default path to bundle file */
54     public static final String JavaDoc JUNIT_BUNDLE = "org.netbeans.modules.junit.Bundle";
55     
56     /** 'Test Packages' string from j2se project bundle */
57     public static final String JavaDoc TEST_PACKAGES_PATH =
58             Bundle.getString("org.netbeans.modules.java.j2seproject.Bundle",
59             "NAME_test.src.dir");
60     
61     /** 'Run File' action label from j2se project bundle */
62     public static final String JavaDoc RUN_FILE =
63             Bundle.getString("org.netbeans.modules.java.j2seproject.Bundle",
64             "ACTION_run.single");
65     
66     /** Test project label (j2se project context menu) */
67     public static final String JavaDoc TEST_PROJECT =
68             Bundle.getString("org.netbeans.modules.java.j2seproject.ui.Bundle",
69             "LBL_TestAction_Name");
70     
71     /** 'Source Packages' string from j2se project bundle */
72     public static final String JavaDoc SRC_PACKAGES_PATH =
73             Bundle.getString("org.netbeans.modules.java.j2seproject.Bundle",
74             "NAME_src.dir");
75     
76     // default timeout for actions in miliseconds
77
public static final int ACTION_TIMEOUT = 1000;
78     
79
80     /**
81      * Saves all opened files
82      */

83     public static void saveAll() {
84         ((SaveAllAction) SaveAllAction.findObject(SaveAllAction.class, true)).performAction();
85     }
86     
87     /**
88      * Deletes a file
89      * @param the file to be deleted
90      */

91     public static void delete(File JavaDoc file) {
92         try {
93             DataObject.find(FileUtil.toFileObject(file)).delete();
94         } catch (IOException JavaDoc e) {
95         }
96     }
97     
98     /**
99      * Deletes a node (file, package)
100      * using pop-up menu
101      */

102     public static void deleteNode(String JavaDoc path) {
103         Node pn = new ProjectsTabOperator().getProjectRootNode(
104                 Utilities.TEST_PROJECT_NAME);
105         if(pn != null && pn.isPresent()) {
106             pn.select();
107             Node n = new Node(pn, path);
108             n.select();
109             JPopupMenuOperator jpmo = n.callPopup();
110             jpmo.pushMenu("Delete");
111             new NbDialogOperator(CONFIRM_OBJECT_DELETION).btYes().push(); //confirm
112
takeANap(500);
113         }
114     }
115     
116     /**
117      * Recursively deletes a directory
118      */

119     public static void deleteDirectory(File JavaDoc path) {
120         if(path.exists()) {
121             File JavaDoc[] f = path.listFiles();
122             for(int i = 0; i < f.length ; i++) {
123                 if (f[i].isDirectory())
124                     deleteDirectory(f[i]);
125                 else
126                     f[i].delete();
127             }
128         }
129         path.delete();
130     }
131     
132     /**
133      * Opens a file from TEST_PROJECT_NAME
134      * @param Filename the file to be opened
135      */

136     public static Node openFile(String JavaDoc path) {
137         Node pn = new ProjectsTabOperator().getProjectRootNode(
138                 Utilities.TEST_PROJECT_NAME);
139         pn.select();
140         Node n = new Node(pn, path);
141         n.select();
142         new OpenAction().perform();
143         new EventTool().waitNoEvent(ACTION_TIMEOUT);
144         return n;
145     }
146     
147     /**
148      * Test whole project (presses 'Test Project from explorer's context menu
149      */

150     public static void testWholeProject() {
151         Node n = new ProjectsTabOperator().getProjectRootNode(
152                 Utilities.TEST_PROJECT_NAME);
153         n.callPopup().pushMenu(TEST_PROJECT);
154     }
155     
156     /**
157      * Pushes Tools|Create Junit tests over a node
158      * @param n the node where the action will be invoked
159      */

160     public static void pushCreateTestsPopup(Node n) {
161         JPopupMenuOperator jpmo = n.callPopup();
162         String JavaDoc[] path = {"Tools", Bundle.getString(Utilities.JUNIT_BUNDLE,
163                 "LBL_Action_CreateTest")};
164                 jpmo.pushMenu(path);
165     }
166     
167     /**
168      * Sets all checkboxes inside Junit create tests dialog to checked
169      */

170     public static void checkAllCheckboxes(NbDialogOperator ndo) {
171         for(int i = 0; i < 7; i++) {
172             new JCheckBoxOperator(ndo, i).setSelected(true);
173         }
174     }
175     
176     /**
177      * Sleeps for waitTimeout miliseconds to avoid incorrect test failures.
178      */

179     public static void takeANap(long waitTimeout) {
180         new org.netbeans.jemmy.EventTool().waitNoEvent(waitTimeout);
181     }
182 }
183
Popular Tags