KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > java > gui > wizards > JavaElementsTest


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.java.gui.wizards;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import org.netbeans.jellytools.EditorOperator;
27 import org.netbeans.jellytools.JellyTestCase;
28 import org.netbeans.jellytools.NbDialogOperator;
29 import org.netbeans.jellytools.ProjectsTabOperator;
30 import org.netbeans.jellytools.actions.OpenAction;
31 import org.netbeans.jellytools.actions.SaveAction;
32 import org.netbeans.jellytools.nodes.Node;
33 import org.netbeans.jemmy.EventTool;
34 import org.netbeans.jemmy.JemmyProperties;
35 import org.netbeans.jemmy.TestOut;
36 import org.netbeans.jemmy.operators.JButtonOperator;
37 import org.netbeans.jemmy.operators.JComboBoxOperator;
38 import org.netbeans.jemmy.operators.JTextFieldOperator;
39 import org.netbeans.junit.NbTestSuite;
40 import org.netbeans.test.java.Utilities;
41
42
43
44 /**
45  * Tests Java Elements.
46  * @author Roman Strobl
47  */

48 public class JavaElementsTest extends JellyTestCase {
49     
50     // default timeout for actions in miliseconds
51
private static final int ACTION_TIMEOUT = 5000;
52     
53     // name of sample project
54
private static final String JavaDoc TEST_PROJECT_NAME = "default";
55     
56     // path to sample files
57
private static final String JavaDoc TEST_PACKAGE_PATH =
58             "org.netbeans.test.java.gui.wizards";
59     
60     // name of sample package
61
private static final String JavaDoc TEST_PACKAGE_NAME = TEST_PACKAGE_PATH+".test";
62     
63     // name of sample class
64
private static final String JavaDoc TEST_CLASS_NAME = "TestClass";
65     
66     // name of sample field
67
private static final String JavaDoc TEST_FIELD_NAME = "testField";
68     
69     // name of sample parameter
70
private static final String JavaDoc TEST_PARAMETER_NAME = "testParameter";
71     
72     // name of sample method
73
private static final String JavaDoc TEST_METHOD_NAME = "testMethod";
74     
75     // name of sample inner class
76
private static final String JavaDoc TEST_INNER_CLASS_NAME = "testInnerClass";
77     
78     // name of sample inner interface
79
private static final String JavaDoc TEST_INNER_INTERFACE_NAME =
80             "testInnerInterface";
81     
82     /**
83      * error log
84      */

85     protected static PrintStream JavaDoc err;
86     /**
87      * standard log
88      */

89     protected static PrintStream JavaDoc log;
90     
91     // workdir, default /tmp, changed to NBJUnit workdir during test
92
private String JavaDoc workDir = "/tmp";
93     
94     private static String JavaDoc projectDir;
95     
96     /**
97      * Adds tests into the test suite.
98      * @return suite
99      */

100     public static NbTestSuite suite() {
101         NbTestSuite suite = new NbTestSuite();
102         suite.addTest(new JavaElementsTest("testNewInitializer"));
103         suite.addTest(new JavaElementsTest("testNewField"));
104         suite.addTest(new JavaElementsTest("testNewConstructor"));
105         suite.addTest(new JavaElementsTest("testNewMethod"));
106         suite.addTest(new JavaElementsTest("testNewInnerClass"));
107         suite.addTest(new JavaElementsTest("testNewInnerInterface"));
108         return suite;
109     }
110     
111     /**
112      * Main method for standalone execution.
113      * @param args the command line arguments
114      */

115     public static void main(String JavaDoc[] args) {
116         junit.textui.TestRunner.run(suite());
117     }
118     
119     /**
120      * Sets up logging facilities.
121      */

122     public void setUp() {
123         System.out.println("######## "+getName()+" #######");
124         err = getLog();
125         log = getRef();
126         JemmyProperties.getProperties().setOutput(new TestOut(null,
127                 new PrintWriter JavaDoc(err, true), new PrintWriter JavaDoc(err, false), null));
128         try {
129             File JavaDoc wd = getWorkDir();
130             workDir = wd.toString();
131         } catch (IOException JavaDoc e) { }
132     }
133     
134     /**
135      * Creates a new instance of JavaElementsTest
136      * @param testName name of test
137      */

138     public JavaElementsTest(String JavaDoc testName) {
139         super(testName);
140     }
141     
142     /**
143      * New initializer test.
144      */

145     public void testNewInitializer() {
146         Node pn = new ProjectsTabOperator().getProjectRootNode(
147                 TEST_PROJECT_NAME);
148         pn.select();
149         
150         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
151                 "org.netbeans.modules.java.j2seproject.Bundle",
152                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
153                 +TEST_CLASS_NAME);
154         
155         n.select();
156         new OpenAction().perform();
157         
158         new EventTool().waitNoEvent(ACTION_TIMEOUT);
159         
160         Node n2 = new Node(pn, org.netbeans.jellytools.Bundle.getString(
161                 "org.netbeans.modules.java.j2seproject.Bundle",
162                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
163                 +TEST_CLASS_NAME+"|"+TEST_CLASS_NAME);
164         n2.select();
165         
166         // add new static block
167
n2.performPopupAction(org.netbeans.jellytools.Bundle.getString(
168                 "org.netbeans.modules.java.ui.nodes.elements.Bundle", "LAB_Add")+"|"+
169                 org.netbeans.jellytools.Bundle.getString(
170                 "org.netbeans.modules.java.ui.nodes.elements.Bundle", "MENU_CREATE_BLOCK"));
171         
172         // check generated source
173
String JavaDoc text = new EditorOperator(TEST_CLASS_NAME).getText();
174         log(text);
175         // check if static block is present
176
/*int index = text.indexOf(org.netbeans.jellytools.Bundle.getString(
177                 "org.netbeans.modules.java.ui.nodes.elements.Bundle",
178                 "SHORT_initializerElementFormat"));*/

179         assertTrue("Static block not found.", text.matches("(?s).*static\\s*\\{\\s*\\}.*"));
180         //select again
181
n2.select();
182         Utilities.takeANap(5000); //wait
183
//new SaveAction().perform();
184
EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
185         String JavaDoc s = editor.getText();
186         editor.save();
187     }
188     
189     /**
190      * New field test.
191      */

192     public void testNewField() {
193         Node pn = new ProjectsTabOperator().getProjectRootNode(
194                 TEST_PROJECT_NAME);
195         pn.select();
196         
197         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
198                 "org.netbeans.modules.java.j2seproject.Bundle",
199                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
200                 +TEST_CLASS_NAME+"|"+TEST_CLASS_NAME);
201         n.select();
202         
203         // add new field
204
n.performPopupActionNoBlock(org.netbeans.jellytools.Bundle.getString(
205                 "org.netbeans.modules.java.ui.nodes.elements.Bundle", "LAB_Add")+"|"
206                 +org.netbeans.jellytools.Bundle.getString(
207                 "org.netbeans.modules.java.ui.nodes.elements.Bundle", "MENU_CREATE_VARIABLE"));
208         
209         NbDialogOperator nb = new NbDialogOperator(
210                 org.netbeans.jellytools.Bundle.getString(
211                 "org.netbeans.modules.java.ui.nodes.elements.Bundle",
212                 "TIT_NewField"));
213         
214         JTextFieldOperator tfp = new JTextFieldOperator(nb, 0);
215         tfp.setText(TEST_FIELD_NAME);
216         
217         nb.ok();
218         
219         // check generated source
220
String JavaDoc text = new EditorOperator(TEST_CLASS_NAME).getText();
221         log(text);
222         // check if static block is present
223
//int index = text.indexOf(TEST_FIELD_NAME);
224
assertTrue("New field not found.", text.matches("(?s).*int\\s+"+TEST_FIELD_NAME+".*"));
225         
226         //new SaveAction().perform();
227
EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
228         String JavaDoc s = editor.getText();
229         editor.save();
230         
231     }
232     
233     /**
234      * New constructor test.
235      */

236     public void testNewConstructor() {
237         Node pn = new ProjectsTabOperator().getProjectRootNode(
238                 TEST_PROJECT_NAME);
239         pn.select();
240         
241         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
242                 "org.netbeans.modules.java.j2seproject.Bundle",
243                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
244                 +TEST_CLASS_NAME+"|"+TEST_CLASS_NAME);
245         n.select();
246         
247         // add a constructor
248
n.performPopupActionNoBlock(org.netbeans.jellytools.Bundle.getString(
249                 "org.netbeans.modules.java.ui.nodes.elements.Bundle", "LAB_Add")+"|"
250                 +org.netbeans.jellytools.Bundle.getString(
251                 "org.netbeans.modules.java.ui.nodes.elements.Bundle",
252                 "MENU_CREATE_CONSTRUCTOR"));
253         
254         NbDialogOperator nb = new NbDialogOperator(
255                 org.netbeans.jellytools.Bundle.getString(
256                 "org.netbeans.modules.java.ui.nodes.elements.Bundle",
257                 "TIT_NewConstructor"));
258         
259         JButtonOperator jbo = new JButtonOperator(nb,
260                 org.netbeans.jellytools.Bundle.getString(
261                 "org.netbeans.modules.java.ui.nodes.editors.Bundle",
262                 "CTL_Add"));
263         
264         jbo.pushNoBlock();
265         
266         // choose constructor parameters
267
NbDialogOperator nb2 = new NbDialogOperator(
268                 org.netbeans.jellytools.Bundle.getString(
269                 "org.netbeans.modules.java.ui.nodes.editors.Bundle",
270                 "LAB_EnterParameter"));
271         
272         JComboBoxOperator jco = new JComboBoxOperator(nb2);
273         jco.selectItem("String");
274         
275         JTextFieldOperator jtfo2 = new JTextFieldOperator(nb2, 1);
276         jtfo2.setText(TEST_PARAMETER_NAME);
277         
278         nb2.ok();
279         
280         nb.ok();
281         
282         // check generated source
283
String JavaDoc text = new EditorOperator(TEST_CLASS_NAME).getText();
284         log(text);
285         // check if static block is present
286
/*int index = text.indexOf(TEST_CLASS_NAME+"(String "
287                 +TEST_PARAMETER_NAME+")");
288         assertTrue("New constructor not found.", index>-1);*/

289         assertTrue("New constructor not found.", text.matches("(?s).*"+TEST_CLASS_NAME+"\\s*\\(String\\s+"+TEST_PARAMETER_NAME+"\\s*\\).*"));
290         //new SaveAction().perform();
291
EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
292         String JavaDoc s = editor.getText();
293         editor.save();
294     }
295     
296     /**
297      * New method test.
298      */

299     public void testNewMethod() {
300         Node pn = new ProjectsTabOperator().getProjectRootNode(
301                 TEST_PROJECT_NAME);
302         pn.select();
303         
304         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
305                 "org.netbeans.modules.java.j2seproject.Bundle",
306                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
307                 +TEST_CLASS_NAME+"|"+TEST_CLASS_NAME);
308         n.select();
309         
310         // open new method
311
n.performPopupActionNoBlock(org.netbeans.jellytools.Bundle.getString(
312                 "org.netbeans.modules.java.ui.nodes.elements.Bundle", "LAB_Add")+"|"
313                 +org.netbeans.jellytools.Bundle.getString(
314                 "org.netbeans.modules.java.ui.nodes.elements.Bundle",
315                 "MENU_CREATE_METHOD"));
316         
317         NbDialogOperator nb = new NbDialogOperator(
318                 org.netbeans.jellytools.Bundle.getString(
319                 "org.netbeans.modules.java.ui.nodes.elements.Bundle",
320                 "TIT_NewMethod"));
321         
322         JButtonOperator jbo = new JButtonOperator(nb,
323                 org.netbeans.jellytools.Bundle.getString(
324                 "org.netbeans.modules.java.ui.nodes.editors.Bundle",
325                 "CTL_Add"));
326         jbo.pushNoBlock();
327         
328         // choose parameters
329
NbDialogOperator nb2 = new NbDialogOperator(
330                 org.netbeans.jellytools.Bundle.getString(
331                 "org.netbeans.modules.java.ui.nodes.editors.Bundle",
332                 "LAB_EnterParameter"));
333         
334         JComboBoxOperator jco = new JComboBoxOperator(nb2);
335         jco.selectItem("String");
336         
337         JTextFieldOperator jtfo2 = new JTextFieldOperator(nb2, 1);
338         jtfo2.setText(TEST_PARAMETER_NAME);
339         
340         nb2.ok();
341         
342         JTextFieldOperator jtfo3 = new JTextFieldOperator(nb, 0);
343         jtfo3.setText(TEST_METHOD_NAME);
344         
345         nb.ok();
346         
347         // check generated source
348
String JavaDoc text = new EditorOperator(TEST_CLASS_NAME).getText();
349         log(text);
350         // check if new method is present
351
/*int index = text.indexOf(TEST_METHOD_NAME+"(String "
352                 +TEST_PARAMETER_NAME+")");
353         assertTrue("New method not found.", index>-1);*/

354         assertTrue("New method not found.", text.matches("(?s).*"+TEST_METHOD_NAME+"\\s*\\(String\\s+"+TEST_PARAMETER_NAME+"\\s*\\).*"));
355         //new SaveAction().perform();
356
EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
357         String JavaDoc s = editor.getText();
358         editor.save();
359     }
360     
361     /**
362      * New inner class test.
363      */

364     public void testNewInnerClass() {
365         Node pn = new ProjectsTabOperator().getProjectRootNode(
366                 TEST_PROJECT_NAME);
367         pn.select();
368         
369         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
370                 "org.netbeans.modules.java.j2seproject.Bundle",
371                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
372                 +TEST_CLASS_NAME+"|"+TEST_CLASS_NAME);
373         n.select();
374         
375         // open new inner class
376
n.performPopupActionNoBlock(org.netbeans.jellytools.Bundle.getString(
377                 "org.netbeans.modules.java.ui.nodes.elements.Bundle", "LAB_Add")+"|"
378                 +org.netbeans.jellytools.Bundle.getString(
379                 "org.netbeans.modules.java.ui.nodes.elements.Bundle",
380                 "MENU_CREATE_INNERCLASS"));
381         
382         NbDialogOperator nb = new NbDialogOperator(
383                 org.netbeans.jellytools.Bundle.getString(
384                 "org.netbeans.modules.java.ui.nodes.elements.Bundle",
385                 "TIT_NewInnerClass"));
386         
387         // choose parameters
388
JTextFieldOperator jtfo3 = new JTextFieldOperator(nb, 0);
389         jtfo3.setText(TEST_INNER_CLASS_NAME);
390         
391         nb.ok();
392         
393         // check generated source
394
String JavaDoc text = new EditorOperator(TEST_CLASS_NAME).getText();
395         log(text);
396         // check if inner class is present
397
//int index = text.indexOf(TEST_CLASS_NAME);
398
assertTrue("New inner class not found.", text.matches("(?s).*class\\s+"+TEST_INNER_CLASS_NAME+"\\s+.*"));
399         
400         //new SaveAction().perform();
401
EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
402         String JavaDoc s = editor.getText();
403         editor.save();
404     }
405     
406     /**
407      * New inner interface test.
408      */

409     public void testNewInnerInterface() {
410         Node pn = new ProjectsTabOperator().getProjectRootNode(
411                 TEST_PROJECT_NAME);
412         pn.select();
413         
414         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
415                 "org.netbeans.modules.java.j2seproject.Bundle",
416                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME+"|"
417                 +TEST_CLASS_NAME+"|"+TEST_CLASS_NAME);
418         n.select();
419         
420         // open new inner interface
421
n.performPopupActionNoBlock(org.netbeans.jellytools.Bundle.getString(
422                 "org.netbeans.modules.java.ui.nodes.elements.Bundle", "LAB_Add")+"|"
423                 +org.netbeans.jellytools.Bundle.getString(
424                 "org.netbeans.modules.java.ui.nodes.elements.Bundle",
425                 "MENU_CREATE_INNERINTERFACE"));
426         
427         NbDialogOperator nb = new NbDialogOperator(
428                 org.netbeans.jellytools.Bundle.getString(
429                 "org.netbeans.modules.java.ui.nodes.elements.Bundle",
430                 "TIT_NewInnerInterface"));
431         
432         // choose parameters
433
JTextFieldOperator jtfo3 = new JTextFieldOperator(nb, 0);
434         jtfo3.setText(TEST_INNER_INTERFACE_NAME);
435         
436         nb.ok();
437         
438         // check generated source
439
String JavaDoc text = new EditorOperator(TEST_CLASS_NAME).getText();
440         log(text);
441         // check if inner interface is present
442
//int index = text.indexOf(TEST_INNER_INTERFACE_NAME);
443
assertTrue("New inner interface not found.", text.matches("(?s).*interface\\s+"+TEST_INNER_INTERFACE_NAME+"\\s+.*"));
444         
445         //new SaveAction().perform();
446
EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
447         String JavaDoc s = editor.getText();
448         editor.save();
449     }
450     
451 }
452
Popular Tags