KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > utilities > testcase > 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.utilities.testcase;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import org.netbeans.jellytools.Bundle;
25 import org.netbeans.jellytools.NbDialogOperator;
26 import org.netbeans.jellytools.ProjectsTabOperator;
27 import org.netbeans.jellytools.actions.OpenAction;
28 import org.netbeans.jellytools.nodes.Node;
29 import org.netbeans.jemmy.EventTool;
30 import org.netbeans.jemmy.operators.JCheckBoxOperator;
31 import org.netbeans.jemmy.operators.JPopupMenuOperator;
32 import org.openide.loaders.DataObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.actions.SaveAllAction;
35
36 /**
37  * Utilities|Search helper class
38  * @author Max Sauer
39  */

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

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

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

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

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

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

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

162     public static void pushFindPopup(Node n) {
163         JPopupMenuOperator jpmo = n.callPopup();
164         jpmo.pushMenuNoBlock(Bundle.getString(UTILITIES_BUNDLE,
165                 "TEXT_TITLE_CUSTOMIZE"));
166     }
167     
168     /**
169      * Sets all checkboxes inside Junit create tests dialog to checked
170      */

171     public static void checkAllCheckboxes(NbDialogOperator ndo) {
172         for(int i = 0; i < 7; i++) {
173             new JCheckBoxOperator(ndo, i).setSelected(true);
174         }
175     }
176     
177     public static NbDialogOperator getFindDialog() {
178         Node pn = new ProjectsTabOperator().getProjectRootNode(
179                 Utilities.TEST_PROJECT_NAME);
180         pn.select();
181         Node n = new Node(pn, Utilities.SRC_PACKAGES_PATH);
182         n.select();
183         Utilities.takeANap(1000);
184         Utilities.pushFindPopup(n);
185         
186         return new NbDialogOperator(FIND_DIALOG);
187     }
188     
189     /**
190      * Sleeps for waitTimeout miliseconds to avoid incorrect test failures.
191      */

192     public static void takeANap(long waitTimeout) {
193         new org.netbeans.jemmy.EventTool().waitNoEvent(waitTimeout);
194     }
195 }
196
Popular Tags