KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import org.netbeans.jellytools.*;
24 import org.netbeans.jellytools.nodes.*;
25 import org.netbeans.jemmy.JemmyProperties;
26 import org.netbeans.jemmy.TestOut;
27 import org.netbeans.jemmy.operators.*;
28 import org.netbeans.test.java.Utilities;
29 import org.netbeans.test.java.gui.GuiUtilities;
30 import java.io.*;
31 import org.netbeans.jemmy.TimeoutExpiredException;
32 import org.netbeans.junit.AssertionFailedErrorException;
33 import org.netbeans.junit.NbTestSuite;
34 import org.netbeans.test.java.Common;
35
36 /**
37  * Tests the New File Wizard.
38  * @author Roman Strobl
39  */

40 public class NewFileWizardTest extends JellyTestCase {
41     
42     // default path to bundle file
43
private static final String JavaDoc JAVA_BUNDLE_PATH = "org.netbeans.modules.java.project.Bundle";
44     
45     // default timeout for actions in miliseconds
46
private static final int ACTION_TIMEOUT = 1000;
47     
48     // name of sample project
49
private static final String JavaDoc TEST_PROJECT_NAME = "TestProject";
50     
51     // name of sample package
52
private static final String JavaDoc TEST_PACKAGE_NAME = "test";
53     
54     // name of sample class
55
private static final String JavaDoc TEST_CLASS_NAME = "TestClass";
56     
57     // name of invalid package
58
private static final String JavaDoc TEST_PACKAGE_NAME_INVALID = "a/b";
59     
60     /**
61      * error log
62      */

63     protected static PrintStream err;
64     /**
65      * standard log
66      */

67     protected static PrintStream log;
68     
69     // workdir, default /tmp, changed to NBJUnit workdir during test
70
private String JavaDoc workDir = "/tmp";
71     
72     static String JavaDoc projectDir;
73     
74     /**
75      * Adds tests into the test suite.
76      * @return suite
77      */

78     public static NbTestSuite suite() {
79         NbTestSuite suite = new NbTestSuite();
80         suite.addTest(new NewFileWizardTest("testCreateProject"));
81         // test requires an opened project
82
suite.addTest(new NewFileWizardTest("testCreatePackage"));
83         // test requires an opened project and created package
84
suite.addTest(new NewFileWizardTest("testDeletePackage"));
85         // test requires an opened project
86
suite.addTest(new NewFileWizardTest("testDeleteProject"));
87         suite.addTest(new NewFileWizardTest("testNewFileWizardComplex"));
88         suite.addTest(new NewFileWizardTest("testCreatePackageFailure"));
89         suite.addTest(new NewFileWizardTest("testCreateInterface"));
90         suite.addTest(new NewFileWizardTest("testCreateAnnotation"));
91         suite.addTest(new NewFileWizardTest("testCreateEnum"));
92         suite.addTest(new NewFileWizardTest("testCreateException"));
93         suite.addTest(new NewFileWizardTest("testCreateJApplet"));
94         suite.addTest(new NewFileWizardTest("testCreateEmptyFile"));
95         suite.addTest(new NewFileWizardTest("testCreateMainClass"));
96         suite.addTest(new NewFileWizardTest("testCreatePackageInfo"));
97         suite.addTest(new NewFileWizardTest("testInvalidName"));
98         suite.addTest(new NewFileWizardTest("testExistingName"));
99         
100         return suite;
101     }
102     
103     /**
104      * Main method for standalone execution.
105      * @param args the command line arguments
106      */

107     public static void main(String JavaDoc[] args) {
108         junit.textui.TestRunner.run(suite());
109     }
110     
111     /**
112      * Sets up logging facilities.
113      */

114     public void setUp() {
115         System.out.println("######## "+getName()+" #######");
116         err = getLog();
117         log = getRef();
118         JemmyProperties.getProperties().setOutput(new TestOut(null,
119                 new PrintWriter(err, true), new PrintWriter(err, false), null));
120         try {
121             File wd = getWorkDir();
122             workDir = wd.toString();
123         } catch (IOException e) { }
124     }
125     
126     /**
127      * Creates a new instance of Main
128      * @param testName name of test
129      */

130     public NewFileWizardTest(String JavaDoc testName) {
131         super(testName);
132     }
133     
134     public void testCreateProject() {
135         projectDir = GuiUtilities.createProject(TEST_PROJECT_NAME, workDir);
136     }
137     
138     /**
139      * Tests creating a project.
140      */

141     public void testCreateProject(String JavaDoc projectName) {
142         projectDir = GuiUtilities.createProject(projectName, workDir);
143     }
144     
145     public void testDeleteProject() {
146         GuiUtilities.deleteProject(TEST_PROJECT_NAME, null, projectDir, false);
147     }
148         
149     /**
150      * Tests deleting a project including files on hard drive.
151      */

152     public void testDeleteProject(String JavaDoc projectName) {
153         GuiUtilities.deleteProject(projectName, null, projectDir, false);
154     }
155     
156     /**
157      * Tests creating of a package.
158      */

159     public void testCreatePackage() {
160         GuiUtilities.createPackage(TEST_PROJECT_NAME,TEST_PACKAGE_NAME);
161     }
162     
163     public void testCreatePackage(String JavaDoc projName,String JavaDoc packName) {
164         GuiUtilities.createPackage(projName,packName);
165     }
166     
167     /**
168      * Tests deleting of a package.
169      */

170     public void testDeletePackage() {
171         // delete a package
172
Node pn = new ProjectsTabOperator().getProjectRootNode(TEST_PROJECT_NAME);
173         pn.select();
174         
175         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
176                 "org.netbeans.modules.java.j2seproject.Bundle",
177                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME);
178         n.select();
179         n.performPopupAction(org.netbeans.jellytools.Bundle.getString(
180                 "org.netbeans.core.projects.Bundle", "LBL_action_delete"));
181         
182         // confirm
183
new NbDialogOperator(Bundle.getString("org.openide.explorer.Bundle",
184                 "MSG_ConfirmDeleteObjectTitle")).yes();
185         
186     }
187     
188     /**
189      * Tests New File wizard.
190      * - create test project
191      * - create test package
192      * - create test class through New File wizard (core of the test)
193      * - close opened file and project
194      * - delete the project incl. all files on disc
195      */

196     public void testNewFileWizardComplex() {
197         // create test project
198
testCreateProject(TEST_PROJECT_NAME);
199         
200         // create test package
201
testCreatePackage(TEST_PROJECT_NAME,TEST_PACKAGE_NAME);
202         
203         // select project node
204
Node pn = new ProjectsTabOperator().getProjectRootNode(TEST_PROJECT_NAME);
205         pn.select();
206         
207         // create test class
208
NewFileWizardOperator op = NewFileWizardOperator.invoke();
209         
210         op.selectCategory(Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes"));
211         op.selectFileType(Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Class.java"));
212         op.next();
213         
214         JTextFieldOperator tf = new JTextFieldOperator(op);
215         tf.setText(TEST_CLASS_NAME);
216         
217         op.finish();
218         
219         // check generated source
220
EditorOperator editor = new EditorOperator(TEST_CLASS_NAME);
221         String JavaDoc text = editor.getText();
222         
223         // check if class name is generated 4 times in the source code
224
int oldIndex = 0;
225         for (int i=0; i<4; i++) {
226             oldIndex = text.indexOf(TEST_CLASS_NAME, oldIndex);
227             if (oldIndex>-1) oldIndex++;
228         }
229         
230         assertTrue("Error in generated class "+TEST_CLASS_NAME+".java.",
231                 oldIndex!=-1);
232         
233         editor.close();
234         
235         // delete test package
236
testDeletePackage();
237         
238         // delete test project
239
testDeleteProject(TEST_PROJECT_NAME);
240         
241     }
242     
243     /**
244      * Negative test for creating of a package.
245      */

246     public void testCreatePackageFailure() {
247         NewFileWizardOperator op = NewFileWizardOperator.invoke();
248         
249         // wait till all fields are loaded
250
JDialogOperator jdo = new JDialogOperator(
251                 org.netbeans.jellytools.Bundle.getString(
252                 "org.netbeans.modules.project.ui.Bundle",
253                 "LBL_NewFileWizard_Title"));
254         JTreeOperator jto = new JTreeOperator(jdo, 0);
255         boolean exitLoop = false;
256         for (int i=0; i<10; i++) {
257             for (int j=0; j<jto.getChildCount(jto.getRoot()); j++) {
258                 if (jto.getChild(jto.getRoot(), j).toString()==
259                         Bundle.getString(JAVA_BUNDLE_PATH,
260                         "Templates/Classes")) {
261                     exitLoop = true;
262                     break;
263                 }
264             }
265             if (exitLoop) break;
266             Utilities.takeANap(1000);
267         }
268         
269         // choose package
270
op.selectCategory(Bundle.getString(JAVA_BUNDLE_PATH,
271                 "Templates/Classes"));
272         op.selectFileType(Bundle.getString(JAVA_BUNDLE_PATH,
273                 "Templates/Classes/Package"));
274         op.next();
275         
276         // try to set an invalid name
277
JTextFieldOperator tfp = new JTextFieldOperator(op, 0);
278         tfp.setText(TEST_PACKAGE_NAME_INVALID);
279         //for (int i=0; i<10; i++) {
280
// JButtonOperator jbo = new JButtonOperator(op,
281
// Bundle.getString("org.openide.Bundle", "CTL_FINISH"));
282
// if (!jbo.isEnabled()) break;
283
// Utilities.takeANap(1000);
284
//}
285
Utilities.takeANap(1000);
286         
287         // check finish button
288
//JButtonOperator jbo = new JButtonOperator(op,
289
// Bundle.getString("org.openide.Bundle", "CTL_FINISH"));
290

291         //this should be replaced with line above
292
JButtonOperator jbo = new JButtonOperator(op, "Finish");
293         
294         assertFalse("Finish button should be disabled for package with "
295                 +"invalid name.", jbo.isEnabled());
296         
297         new NbDialogOperator(Bundle.getString(
298                 "org.netbeans.modules.project.ui.Bundle",
299                 "LBL_NewProjectWizard_Subtitle")+" "
300                 +Bundle.getString(JAVA_BUNDLE_PATH,
301                 "Templates/Classes/Package")).cancel();
302     }
303     
304     public void testCreateInterface() {
305         String JavaDoc expected = "" +
306                 "/*\n"+
307                 " * MyIface.java\n"+
308                 " *\n"+
309                 " * Created on XXXXX xx, XXX, X:XX XX\n"+
310                 " *\n"+
311                 " * To change this template, choose Tools | Template Manager\n"+
312                 " * and open the template in the editor.\n"+
313                 " */\n"+
314                 "\n"+
315                 "package test;\n"+
316                 "\n"+
317                 "/**\n"+
318                 " *\n"+
319                 " * @author XXXXXXX\n"+
320                 " */\n"+
321                 "public interface MyIface {\n"+
322                 " \n"+
323                 "}\n";
324         createAndVerify("MyIface" ,
325                 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Interface.java"),
326                 Common.unify(expected),true);
327         
328     }
329     
330     public void testCreateAnnotation() {
331         String JavaDoc expected = "" +
332                 "/*\n"+
333                 " * MyAnnot.java\n"+
334                 " *\n"+
335                 " * Created on xxxxxxxxxxxxx\n"+
336                 " *\n"+
337                 " * To change this template, choose Tools | Template Manager\n"+
338                 " * and open the template in the editor.\n"+
339                 " */\n"+
340                 "\n"+
341                 "package test;\n"+
342                 "\n"+
343                 "/**\n"+
344                 " *\n"+
345                 " * @author xxxxxxx\n"+
346                 " */\n"+
347                 "public @interface MyAnnot {\n"+
348                 " \n"+
349                 "}\n";
350         createAndVerify("MyAnnot" ,
351                 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/AnnotationType.java"),
352                 Common.unify(expected),true);
353     }
354     
355     public void testCreateEnum() {
356         String JavaDoc expected = "" +
357                 "/*\n"+
358                 " * MyEnum.java\n"+
359                 " *\n"+
360                 " * Created on xxxxxxxxxxxxxxx\n"+
361                 " *\n"+
362                 " * To change this template, choose Tools | Template Manager\n"+
363                 " * and open the template in the editor.\n"+
364                 " */\n"+
365                 "\n"+
366                 "package test;\n"+
367                 "\n"+
368                 "/**\n"+
369                 " *\n"+
370                 " * @author xxxxxxxx\n"+
371                 " */\n"+
372                 "public enum MyEnum {\n"+
373                 " \n"+
374                 "}\n";
375         createAndVerify("MyEnum" ,
376                 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Enum.java"),
377                 Common.unify(expected),true);
378     }
379     
380     public void testCreateException() {
381         String JavaDoc expected = "" +
382                 "/*\n"+
383                 " * MyExp.java\n"+
384                 " *\n"+
385                 " * Created on November 3, 2006, 2:40 PM\n"+
386                 " *\n"+
387                 " * To change this template, choose Tools | Template Manager\n"+
388                 " * and open the template in the editor.\n"+
389                 " */\n"+
390                 "\n"+
391                 "package test;\n"+
392                 "\n"+
393                 "/**\n"+
394                 " *\n"+
395                 " * @author jp159440\n"+
396                 " */\n"+
397                 "public class MyExp extends java.lang.Exception {\n"+
398                 " \n"+
399                 " /**\n"+
400                 " * Creates a new instance of <code>MyExp</code> without detail message.\n"+
401                 " */\n"+
402                 " public MyExp() {\n"+
403                 " }\n"+
404                 " \n"+
405                 " \n"+
406                 " /**\n"+
407                 " * Constructs an instance of <code>MyExp</code> with the specified detail message.\n"+
408                 " * @param msg the detail message.\n"+
409                 " */\n"+
410                 " public MyExp(String msg) {\n"+
411                 " super(msg);\n"+
412                 " }\n"+
413                 "}\n";
414         
415         createAndVerify("MyExp" ,
416                 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Exception.java"),
417                 Common.unify(expected),true);
418     }
419     
420     
421     public void testCreateJApplet() {
422         String JavaDoc expected = "" +
423                 "/*\n"+
424                 " * MyJApplet.java\n"+
425                 " *\n"+
426                 " * Created on November 3, 2006, 2:45 PM\n"+
427                 " *\n"+
428                 " * To change this template, choose Tools | Template Manager\n"+
429                 " * and open the template in the editor.\n"+
430                 " */\n"+
431                 "\n"+
432                 "package test;\n"+
433                 "\n"+
434                 "/**\n"+
435                 " *\n"+
436                 " * @author jp159440\n"+
437                 " */\n"+
438                 "public class MyJApplet extends javax.swing.JApplet {\n"+
439                 " \n"+
440                 " /** Creates a new instance of MyJApplet */\n"+
441                 " public MyJApplet() {\n"+
442                 " }\n"+
443                 " \n"+
444                 "}\n";
445         
446         createAndVerify("MyJApplet" ,
447                 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/JApplet.java"),
448                 Common.unify(expected),true);
449     }
450     
451     public void testCreateEmptyFile() {
452         String JavaDoc expected = "" +
453                 "package test;\n"+
454                 "\n";
455         
456         createAndVerify("Empty" ,
457                 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Empty.java"),
458                 Common.unify(expected),true);
459     }
460     
461     public void testCreateMainClass() {
462         String JavaDoc expected = "" +
463                 "/*\n"+
464                 " * MyMain.java\n"+
465                 " *\n"+
466                 " * Created on November 3, 2006, 2:50 PM\n"+
467                 " *\n"+
468                 " * To change this template, choose Tools | Template Manager\n"+
469                 " * and open the template in the editor.\n"+
470                 " */\n"+
471                 "\n"+
472                 "package test;\n"+
473                 "\n"+
474                 "/**\n"+
475                 " *\n"+
476                 " * @author jp159440\n"+
477                 " */\n"+
478                 "public class MyMain {\n"+
479                 " \n"+
480                 " /** Creates a new instance of MyMain */\n"+
481                 " public MyMain() {\n"+
482                 " }\n"+
483                 " \n"+
484                 " /**\n"+
485                 " * @param args the command line arguments\n"+
486                 " */\n"+
487                 " public static void main(String[] args) {\n"+
488                 " // TODO code application logic here\n"+
489                 " }\n"+
490                 " \n"+
491                 "}\n";
492         
493         createAndVerify("MyMain" ,
494                 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Main.java"),
495                 Common.unify(expected),true);
496     }
497     
498     public void testCreatePackageInfo() {
499         String JavaDoc expected = "" +
500                 "/*\n"+
501                 " * package-info.java\n"+
502                 " *\n"+
503                 " * Created on November 3, 2006, 2:51 PM\n"+
504                 " *\n"+
505                 " * To change this template, choose Tools | Template Manager\n"+
506                 " * and open the template in the editor.\n"+
507                 " */\n"+
508                 "\n"+
509                 "package test;\n";
510         
511         createAndVerify("package-info" ,
512                 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/package-info.java"),
513                 Common.unify(expected),true);
514     }
515     
516     public void testInvalidName() {
517         String JavaDoc expected = "";
518         createAndVerify("Name.invalid" ,
519                 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Class.java"),
520                 Common.unify(expected),false);
521     }
522     
523     public void testExistingName() {
524         String JavaDoc expected = "";
525         createAndVerify("MyMain" ,
526                 Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes/Class.java"),
527                 Common.unify(expected),false);
528     }
529     
530     private void createIfNotOpened(String JavaDoc projName,String JavaDoc packName) {
531         Node pn = null;
532         try {
533             pn = new ProjectsTabOperator().getProjectRootNode(projName);
534         } catch(TimeoutExpiredException tee) {
535             System.out.println("Project is not opened, creating new one");
536             testCreateProject(projName);
537             pn = new ProjectsTabOperator().getProjectRootNode(projName);
538         }
539         Node n = null;
540         try {
541             n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
542                     "org.netbeans.modules.java.j2seproject.Bundle",
543                     "NAME_src.dir")+"|"+packName);
544         } catch (TimeoutExpiredException tee) {
545             System.out.println("Expected package not present, creating new one");
546             testCreatePackage(projName,packName);
547             n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
548                     "org.netbeans.modules.java.j2seproject.Bundle",
549                     "NAME_src.dir")+"|"+packName);
550         }
551         n.select();
552         
553     }
554     
555     /**
556      * Method for creation file from new file wizard
557      *
558      * @param name Name of new file
559      * @param type String expression of new file type
560      * @param expectedContent Expected content of the new file
561      * @param shouldPass Indicated it there is expected error in the wizard
562      */

563     private void createAndVerify(String JavaDoc name, String JavaDoc type, String JavaDoc expectedContent, boolean shouldPass) {
564         createIfNotOpened(TEST_PROJECT_NAME, TEST_PACKAGE_NAME);
565         // select project node
566
Node pn = new ProjectsTabOperator().getProjectRootNode(TEST_PROJECT_NAME);
567         pn.select();
568         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
569                 "org.netbeans.modules.java.j2seproject.Bundle",
570                 "NAME_src.dir")+"|"+TEST_PACKAGE_NAME);
571         n.select();
572         // create test class
573
NewFileWizardOperator op = NewFileWizardOperator.invoke();
574         
575         op.selectCategory(Bundle.getString(JAVA_BUNDLE_PATH,"Templates/Classes"));
576         op.selectFileType(type);
577         op.next();
578         JTextFieldOperator tf = new JTextFieldOperator(op);
579         tf.setText(name);
580         if(!shouldPass) {
581             Utilities.takeANap(1000);
582             JButtonOperator jbo = new JButtonOperator(op,"Finish");
583             assertFalse("Finish button should be disabled", jbo.isEnabled());
584             // closing wizard
585
new NbDialogOperator(Bundle.getString(
586                     "org.netbeans.modules.project.ui.Bundle",
587                     "LBL_NewProjectWizard_Subtitle")+" "
588                     +type).cancel();
589             return;
590         }
591         op.finish();
592         // check generated source
593
EditorOperator editor = new EditorOperator(name);
594         String JavaDoc text = Common.unify(editor.getText());
595         assertEquals("File doesnt have expected content",expectedContent,text);
596         editor.close();
597     }
598     
599 }
600
Popular Tags