KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > qa > form > ExtJellyTestCase


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.qa.form;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Date JavaDoc;
24 import org.netbeans.jellytools.EditorOperator;
25 import org.netbeans.jellytools.JellyTestCase;
26 import org.netbeans.jellytools.NbDialogOperator;
27 import org.netbeans.jellytools.NewFileNameLocationStepOperator;
28 import org.netbeans.jellytools.NewFileWizardOperator;
29 import org.netbeans.jellytools.ProjectsTabOperator;
30 import org.netbeans.jellytools.actions.Action;
31 import org.netbeans.jellytools.actions.ActionNoBlock;
32 import org.netbeans.jellytools.actions.CompileAction;
33 import org.netbeans.jellytools.actions.DeleteAction;
34 import org.netbeans.jellytools.actions.OpenAction;
35 import org.netbeans.jellytools.modules.form.FormDesignerOperator;
36 import org.netbeans.jellytools.nodes.Node;
37 import org.netbeans.jellytools.nodes.ProjectRootNode;
38 import org.netbeans.jemmy.operators.JTextFieldOperator;
39 import org.netbeans.jemmy.operators.Operator;
40
41 /**
42  * Class with helpers for easy creating jemmy/jelly tests
43  *
44  * @author Jiri Vagner
45  */

46 public abstract class ExtJellyTestCase extends JellyTestCase {
47     private static int MY_WAIT_MOMENT = 500;
48     
49     public static String JavaDoc TEST_PROJECT_NAME = "SampleProject"; // NOI18N
50
public static String JavaDoc TEST_PACKAGE_NAME = "data"; // NOI18N
51
public static String JavaDoc DELETE_OBJECT_CONFIRM = "Confirm Object Deletion"; // NOI18N
52

53     /* Skip file (JFrame,Frame, JDialog, ...) delete in the end of each test */
54     public Boolean JavaDoc DELETE_FILES = true;
55     
56     /** Constructor required by JUnit */
57     public ExtJellyTestCase(String JavaDoc testName) {
58         super(testName);
59     }
60     
61     /**
62      * Simple console println
63      */

64     public void p(String JavaDoc msg) {
65         System.out.println(msg);
66     }
67     
68     /**
69      * Simple console println
70      */

71     public void p(Boolean JavaDoc msg) {
72         p(String.valueOf(msg));
73     }
74     
75     /**
76      * Simple console println
77      */

78     public void p(int msg) {
79         p(String.valueOf(msg));
80     }
81     
82     /**
83      * Creates new file using NB New File Wizzard
84      * @return name of a new file
85      * @param project name of project to create file in
86      * @param packageName package for a new file
87      * @param category category from first step of new file wizzard
88      * @param fileType filetype from first step of new file wizzard
89      * @param name name prefix of a new file, timestamp will be added to avoid name clash
90      */

91     private String JavaDoc createFile(String JavaDoc project, String JavaDoc packageName, String JavaDoc category, String JavaDoc fileType, String JavaDoc name) {
92         return createFile(project, packageName, category, fileType, name, null);
93     }
94     
95     private String JavaDoc createFile(String JavaDoc project, String JavaDoc packageName, String JavaDoc category, String JavaDoc fileType, String JavaDoc name, String JavaDoc beanName) {
96         NewFileWizardOperator nfwo = NewFileWizardOperator.invoke();
97         nfwo.selectProject(project);
98         nfwo.selectCategory(category);
99         nfwo.selectFileType(fileType);
100         nfwo.next();
101
102         String JavaDoc fileName = name + String.valueOf(new Date JavaDoc().getTime());
103         
104         if (beanName == null) {
105             NewFileNameLocationStepOperator nfnlso = new NewFileNameLocationStepOperator();
106             nfnlso.txtObjectName().clearText();
107             nfnlso.txtObjectName().typeText(fileName);
108             nfnlso.setPackage(packageName);
109             nfnlso.finish();
110         } else {
111             NewBeanFormOperator nbfOp = new NewBeanFormOperator();
112             nbfOp.txtClassName().clearText();
113             nbfOp.txtClassName().typeText(fileName);
114
115             nbfOp.cboPackage().clearText();
116             nbfOp.typePackage(packageName);
117
118             nbfOp.next();
119             
120             NewBeanFormSuperclassOperator superOp = new NewBeanFormSuperclassOperator();
121             superOp.setSuperclass(beanName);
122             superOp.finish();
123         }
124         
125         // following code avoids issue nr. 60418
126
ProjectsTabOperator pto = new ProjectsTabOperator();
127         ProjectRootNode prn = pto.getProjectRootNode(project);
128         prn.select();
129         Node formnode = new Node(prn, "Source Packages|" + packageName + "|" + fileName); // NOI18N
130
formnode.select();
131         
132         OpenAction openAction = new OpenAction();
133         openAction.perform(formnode);
134         // end of issue code
135

136         return fileName;
137     }
138     
139     
140     /**
141      * Removes file from actual project and actual test package
142      */

143     public void removeFile(String JavaDoc fileName) {
144         if (DELETE_FILES) {
145             ProjectsTabOperator project = new ProjectsTabOperator();
146             Node node = new Node(project.tree(), TEST_PROJECT_NAME +
147                     "|Source Packages|" + TEST_PACKAGE_NAME + "|" + fileName + ".java"); // NOI18N
148
DeleteAction act = new DeleteAction();
149             act.performPopup(node);
150             new NbDialogOperator(DELETE_OBJECT_CONFIRM).yes();
151         }
152     }
153     
154     /**
155      * Adds new bean into palette
156      *
157      * @param beanFileName
158      */

159     public void addBean(String JavaDoc beanFileName) {
160         Node fileNode = openFile(beanFileName);
161         waitAMoment();
162         
163         new ActionNoBlock("Tools|Add To Palette...", null).perform(); // NOI18N
164

165         SelectPaletteCategoryOperator op = new SelectPaletteCategoryOperator();
166         op.lstPaletteCategories().selectItem(SelectPaletteCategoryOperator.ITEM_BEANS);
167         op.ok();
168         
169         CompileAction compAct = new CompileAction();
170         compAct.perform(fileNode);
171         waitAMoment();
172     }
173     
174     /**
175      * Opens file into nb editor
176      * @param fileName
177      * @return node
178      */

179     public Node openFile(String JavaDoc fileName) {
180         ProjectsTabOperator pto = new ProjectsTabOperator();
181         ProjectRootNode prn = pto.getProjectRootNode(TEST_PROJECT_NAME);
182         prn.select();
183         
184         String JavaDoc path = "Source Packages|" + TEST_PACKAGE_NAME + "|" + fileName; // NOI18N
185
//p(path);
186
Node formnode = new Node(prn, path); // NOI18N
187
formnode.setComparator(new Operator.DefaultStringComparator(true, false));
188         formnode.select();
189         
190         OpenAction openAction = new OpenAction();
191         openAction.perform(formnode);
192         
193         return formnode;
194     }
195     
196
197     public String JavaDoc createBeanFormFile(String JavaDoc beanClassName) {
198         return createFile( TEST_PROJECT_NAME, TEST_PACKAGE_NAME , "Java GUI Forms", "Bean Form", "MyBeanForm", beanClassName); // NOI18N
199
}
200     
201     /**
202      * Creates new JDialog file in project
203      * @return new file name
204      */

205     public String JavaDoc createJDialogFile() {
206         return createFile( TEST_PROJECT_NAME, TEST_PACKAGE_NAME , "Java GUI Forms", "JDialog Form", "MyJDialog"); // NOI18N
207
}
208     
209     /**
210      * Creates new JFrame file in project
211      * @return new file name
212      */

213     public String JavaDoc createJFrameFile() {
214         return createFile( TEST_PROJECT_NAME, TEST_PACKAGE_NAME , "Java GUI Forms", "JFrame Form", "MyJFrame"); // NOI18N
215
}
216     
217     /**
218      * Creates new AWT Frame file in project
219      * @return new file name
220      */

221     public String JavaDoc createFrameFile() {
222         return createFile( TEST_PROJECT_NAME, TEST_PACKAGE_NAME , "Java GUI Forms|AWT Forms", "Frame Form", "MyFrame"); // NOI18N
223
}
224     
225     /**
226      * Runs popoup command over node
227      * @param popup command, ex.: "Add|Swing|Label"
228      * @param node to run action on
229      */

230     public void runPopupOverNode(String JavaDoc actionName, Node node) {
231         Action act = new Action(null, actionName);
232         act.setComparator(new Operator.DefaultStringComparator(false, false));
233         act.perform(node);
234         // p(actionName);
235
}
236     
237     /**
238      * Runs popup commands over node
239      * @param array list of popup commands
240      * @param node to run actions on
241      */

242     public void runPopupOverNode(ArrayList JavaDoc<String JavaDoc> actionNames, Node node, Operator.DefaultStringComparator comparator) {
243         for (String JavaDoc actionName: actionNames) {
244             Action act = new Action(null, actionName);
245             act.setComparator(comparator);
246             act.perform(node);
247             // p(actionName);
248
}
249     }
250     
251     /**
252      * Runs popup commands over node
253      * @param array list of popup commands
254      * @param node to run actions on
255      */

256     public void runPopupOverNode(ArrayList JavaDoc<String JavaDoc> actionNames, Node node) {
257         runPopupOverNode(actionNames, node, new Operator.DefaultStringComparator(false, false));
258     }
259     
260     /**
261      * Find a substring in a string
262      * Test fail() method is called, when code string doesnt contain stringToFind.
263      * @param stringToFind string to find
264      * @param string to search
265      */

266     private void findStringInCode(String JavaDoc stringToFind, String JavaDoc code) {
267         if (!code.contains(stringToFind))
268             fail("Missing string \"" + stringToFind + "\" in code."); // NOI18N
269
}
270     
271     /**
272      * Find a strings in a code
273      * @param lines array list of strings to find
274      * @param designer operator "with text"
275      */

276     public void findInCode(ArrayList JavaDoc<String JavaDoc> lines, FormDesignerOperator designer) {
277         EditorOperator editor = designer.editor();
278         String JavaDoc code = editor.getText();
279         
280         for (String JavaDoc line : lines)
281             findStringInCode(line, code);
282         
283         designer.design();
284     }
285     
286     /**
287      * Find a string in a code
288      * @param lines array list of strings to find
289      * @param designer operator "with text"
290      */

291     public void findInCode(String JavaDoc stringToFind, FormDesignerOperator designer) {
292         EditorOperator editor = designer.editor();
293         findStringInCode(stringToFind, editor.getText());
294         designer.design();
295     }
296     
297     /**
298      * Miss a string in a code
299      * Test fail() method is called, when code contains stringToFind string
300      * @param stringToFind
301      * @param designer operator "with text"
302      */

303     public void missInCode(String JavaDoc stringToFind, FormDesignerOperator designer) {
304         EditorOperator editor = designer.editor();
305         
306         if (editor.getText().contains(stringToFind))
307             fail("String \"" + stringToFind + "\" found in code."); // NOI18N
308

309         designer.design();
310     }
311     
312     /**
313      * Calls Jelly waitNoEvent()
314      * @param quiet time (miliseconds)
315      */

316     public static void waitNoEvent(long waitTimeout) {
317         new org.netbeans.jemmy.EventTool().waitNoEvent(waitTimeout);
318     }
319     
320     /**
321      * Calls Jelly waitNoEvent() with MY_WAIT_MOMENT
322      */

323     public static void waitAMoment() {
324         waitNoEvent(MY_WAIT_MOMENT);
325     }
326 }
327
Popular Tags