KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > projects > TestCreateProject


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 /*
21  * TestCreateProject.java
22  * NetBeans JUnit based test
23  *
24  * Created on July 1, 2004, 1:58 PM
25  */

26
27 package projects;
28
29 import javax.swing.JList JavaDoc;
30 import junit.framework.*;
31
32 import org.netbeans.jellytools.Bundle;
33 import org.netbeans.jellytools.JellyTestCase;
34 import org.netbeans.jellytools.NbDialogOperator;
35 import org.netbeans.jellytools.NewProjectNameLocationStepOperator;
36 import org.netbeans.jellytools.NewProjectWizardOperator;
37 import org.netbeans.jellytools.ProjectsTabOperator;
38
39 import org.netbeans.jellytools.nodes.ProjectRootNode;
40 import org.netbeans.jemmy.JemmyProperties;
41 import org.netbeans.jemmy.TimeoutExpiredException;
42 import org.netbeans.jemmy.operators.JButtonOperator;
43
44 import org.netbeans.jemmy.operators.JCheckBoxOperator;
45 import org.netbeans.jemmy.operators.JFileChooserOperator;
46 import org.netbeans.jemmy.operators.JLabelOperator;
47 import org.netbeans.jemmy.operators.JListOperator;
48 import org.netbeans.jemmy.operators.JTextFieldOperator;
49
50 import org.netbeans.junit.*;
51 import org.netbeans.junit.ide.ProjectSupport;
52
53 /**
54  *
55  */

56 public class TestCreateProject extends JellyTestCase {
57     
58     String JavaDoc standardLabel = Bundle.getStringTrimmed("org.netbeans.modules.java.j2seproject.ui.wizards.Bundle",
59             "Templates/Project/Standard");
60     String JavaDoc applicationLabel = Bundle.getStringTrimmed("org.netbeans.modules.java.j2seproject.ui.wizards.Bundle",
61             "Templates/Project/Standard/emptyJ2SE.xml");
62     String JavaDoc libraryLabel = Bundle.getStringTrimmed("org.netbeans.modules.java.j2seproject.ui.wizards.Bundle",
63             "Templates/Project/Standard/emptyJ2SElibrary.xml");
64     String JavaDoc existingLabel = Bundle.getStringTrimmed("org.netbeans.modules.java.j2seproject.ui.wizards.Bundle",
65             "Templates/Project/Standard/existingJ2SE.xml");
66  // String scanningLabel = Bundle.getStringTrimmed("org.netbeans.modules.javacore.Bundle",
67
//"TXT_ApplyingPathsTitle");
68
public TestCreateProject(java.lang.String JavaDoc testName) {
69         super(testName);
70     }
71     
72     public static void main(java.lang.String JavaDoc[] args) {
73         junit.textui.TestRunner.run(suite());
74     }
75     
76     public static Test suite() {
77         TestSuite suite = new NbTestSuite(TestCreateProject.class);
78         return suite;
79     }
80     
81     public void setUp() {
82         System.out.println("######## " + getName() + " #######"); // NOI18N
83
}
84     
85     // -------------------------------------------------------------------------
86

87     /**
88      * Test of creating default project (Set as Main Project, Main Class)
89      */

90     public void testCreateProject_1() throws Exception JavaDoc {
91         
92         String JavaDoc projName = "TestApp_1"; // NOI18N
93
String JavaDoc pkg = "org.netbeans"; // NOI18N
94
String JavaDoc mainClass = "MyMain" + projName; // NOI18N
95

96         createJavaApplication(projName, getWorkDir().getAbsolutePath(), true, pkg + "." + mainClass);
97         // Opening Projects
98
String JavaDoc openingProjectsTitle = Bundle.getString("org.netbeans.modules.project.ui.Bundle", "LBL_Opening_Projects_Progress");
99         try {
100             // wait at most 60 second until progress dialog dismiss
101
JemmyProperties.setCurrentTimeout("ComponentOperator.WaitStateTimeout", 60000);
102             new NbDialogOperator(openingProjectsTitle).waitClosed();
103         } catch (TimeoutExpiredException e) {
104             // ignore when progress dialog was closed before we started to wait for it
105
}
106         //JemmyProperties.setCurrentTimeout("ComponentOperator.WaitStateTimeout", 60000);
107
// wait project appear in projects view
108
new ProjectsTabOperator().getProjectRootNode(projName);
109         // wait classpath scanning finished
110
ProjectSupport.waitScanFinished();
111         //new ProjectsTabOperator().getProjectRootNode(projName);
112

113         // Verify that correct project has been created
114
TestProjectUtils.verifyProjectExists(projName);
115         
116         // Main class created and opened in editor
117
TestProjectUtils.verifyMainClassInEditor(mainClass + ".java");
118         
119         // Verify that created project can be built and run
120
TestProjectUtils.verifyProjectBuilds(projName);
121         
122     }
123     
124     
125     /**
126      * Test of creating project (Set as Main Project, no Main Class)
127      */

128     public void testCreateProject_2() throws Exception JavaDoc {
129         
130         String JavaDoc projName = "TestApp_2"; // NOI18N
131

132         createJavaApplication(projName, getWorkDir().getAbsolutePath(), true, null);
133         
134          // Opening Projects
135
String JavaDoc openingProjectsTitle = Bundle.getString("org.netbeans.modules.project.ui.Bundle", "LBL_Opening_Projects_Progress");
136         try {
137             // wait at most 60 second until progress dialog dismiss
138
JemmyProperties.setCurrentTimeout("ComponentOperator.WaitStateTimeout", 60000);
139             new NbDialogOperator(openingProjectsTitle).waitClosed();
140         } catch (TimeoutExpiredException e) {
141             // ignore when progress dialog was closed before we started to wait for it
142
}
143         //JemmyProperties.setCurrentTimeout("ComponentOperator.WaitStateTimeout", 60000);
144
// wait project appear in projects view
145
new ProjectsTabOperator().getProjectRootNode(projName);
146         // wait classpath scanning finished
147
ProjectSupport.waitScanFinished();
148         
149         //new ProjectsTabOperator().getProjectRootNode(projName);
150

151         // Verify that correct project has been created
152
TestProjectUtils.verifyProjectExists(projName);
153         
154         // Verify that created project can be built
155
TestProjectUtils.verifyProjectBuilds(projName);
156     }
157     
158     /**
159      * Test of creating project (Not set as Main Project, Main Class)
160      */

161     public void testCreateProject_3() throws Exception JavaDoc {
162         
163         String JavaDoc projName = "TestApp_3"; // NOI18N
164
String JavaDoc pkg = "org.netbeans"; // NOI18N
165
String JavaDoc mainClass = "MyMain" + projName; // NOI18N
166

167         createJavaApplication(projName, getWorkDir().getAbsolutePath(), false, pkg + "." + mainClass);
168         ProjectSupport.waitScanFinished();
169         
170         new ProjectsTabOperator().getProjectRootNode(projName);
171         
172         // Verify that correct project has been created
173
TestProjectUtils.verifyProjectExists(projName);
174         
175         // Main class created and opened in editor
176
TestProjectUtils.verifyMainClassInEditor(mainClass + ".java");
177         
178         // Verify that created project can be built and run
179
TestProjectUtils.verifyProjectBuilds(projName);
180         
181     }
182     
183     /**
184      * Test of creating project (Not set as Main Project, no Main Class)
185      */

186     public void testCreateProject_4() throws Exception JavaDoc {
187         
188         String JavaDoc projName = "TestApp_4"; // NOI18N
189

190         createJavaApplication(projName, getWorkDir().getAbsolutePath(), false, null);
191         ProjectSupport.waitScanFinished();
192         
193         new ProjectsTabOperator().getProjectRootNode(projName);
194         
195         // Verify that correct project has been created
196
TestProjectUtils.verifyProjectExists(projName);
197         
198         // Verify that created project can be built
199
TestProjectUtils.verifyProjectBuilds(projName);
200         
201     }
202     
203     /**
204      * Test of creating default project - Class Library
205      */

206     public void testCreateProject_5() throws Exception JavaDoc {
207         
208         String JavaDoc projName = "TestLib_1"; // NOI18N
209

210         createJavaLibrary(projName, getWorkDir().getAbsolutePath());
211         ProjectSupport.waitScanFinished();
212         
213         new ProjectsTabOperator().getProjectRootNode(projName);
214         
215         // Verify that correct project has been created
216
TestProjectUtils.verifyProjectExists(projName);
217         
218         // Verify that created project can be built
219
TestProjectUtils.verifyProjectBuilds(projName);
220         
221     }
222     
223     /**
224      * Test of creating project from existing sources
225      */

226     public void testCreateProject_6() throws Exception JavaDoc {
227         
228         String JavaDoc projName = "ExSources_1"; // NOI18N
229
createJavaApplicationWithExistingSources(projName, getWorkDir().getAbsolutePath(),
230                 getDataDir().getAbsolutePath() + java.io.File.separator + "srcroot1", null, true);
231         
232         ProjectSupport.waitScanFinished();
233         
234         ProjectRootNode prn = new ProjectsTabOperator().getProjectRootNode(projName);
235         prn.expand();
236         
237         // Verify that correct project has been created
238
TestProjectUtils.verifyProjectExists(projName);
239         
240         // Verify that created project can be built
241
TestProjectUtils.verifyProjectBuilds(projName);
242         
243     }
244     
245     // -------------------------------------------------------------------------
246

247     /**
248      * Creates New Java Application Project
249      * @param projName Name of the project
250      * @param projLocation Project folder location
251      * @param mainProj Set project as main
252      * @param mainClass if null then no main class created,
253      * if "" then default main class, otherwise given String represents main class incl. package
254      */

255     private void createJavaApplication(String JavaDoc projName, String JavaDoc projLocation, boolean mainProj, String JavaDoc mainClass) {
256         
257         // select the right template
258
NewProjectWizardOperator npwo = NewProjectWizardOperator.invoke();
259         npwo.selectCategory(standardLabel);
260         npwo.selectProject(applicationLabel);
261         npwo.next();
262         
263         // project name
264
NewProjectNameLocationStepOperator npnlso = new NewProjectNameLocationStepOperator();
265         npnlso.txtProjectName().clearText();
266         npnlso.txtProjectName().setText(projName);
267         
268         // project location
269
npnlso.txtProjectLocation().clearText();
270         npnlso.txtProjectLocation().setText(projLocation);
271         
272         // set project as main
273
JCheckBoxOperator mainProjOper = npnlso.cbSetAsMainProject();
274         if (!mainProj && mainProjOper.isSelected()) {
275             mainProjOper.doClick();
276         }
277         
278         // set project main class
279
//JCheckBoxOperator mainClassOper = npnlso.cbCreateMainClass(); // - FIX NEEDED!
280
JCheckBoxOperator mainClassOper = new JCheckBoxOperator(npnlso, 1);
281         JTextFieldOperator mainClassFieldOper = new JTextFieldOperator(npnlso, 3);
282         if (mainClass == null) {
283             if (mainClassOper.isSelected()) {
284                 mainClassOper.doClick();
285             }
286         } else {
287             if (!mainClassOper.isSelected()) {
288                 mainClassOper.doClick();
289             }
290             if (!mainClass.equals("")) {
291                 mainClassFieldOper.clearText();
292                 mainClassFieldOper.typeText(mainClass);
293             }
294         }
295         npnlso.finish();
296         String JavaDoc openingProjectsTitle = Bundle.getString("org.netbeans.modules.project.ui.Bundle", "LBL_Opening_Projects_Progress");
297         try {
298             // wait at most 60 second until progress dialog dismiss
299
JemmyProperties.setCurrentTimeout("ComponentOperator.WaitStateTimeout", 60000);
300             new NbDialogOperator(openingProjectsTitle).waitClosed();
301         } catch (TimeoutExpiredException e) {
302             // ignore when progress dialog was closed before we started to wait for it
303
}
304     }
305     
306     /**
307      * Creates New Java Class Library Project
308      * @param projName Name of the project
309      * @param projLocation Project folder location
310      */

311     private void createJavaLibrary(String JavaDoc projName, String JavaDoc projLocation) {
312         
313         // select the right template
314
NewProjectWizardOperator npwo = NewProjectWizardOperator.invoke();
315         npwo.selectCategory(standardLabel);
316         npwo.selectProject(libraryLabel);
317         npwo.next();
318         
319         // project name
320
NewProjectNameLocationStepOperator npnlso = new NewProjectNameLocationStepOperator();
321         npnlso.txtProjectName().clearText();
322         npnlso.txtProjectName().setText(projName);
323         
324         // project location
325
npnlso.txtProjectLocation().clearText();
326         npnlso.txtProjectLocation().setText(projLocation);
327         
328         npnlso.finish();
329         String JavaDoc openingProjectsTitle = Bundle.getString("org.netbeans.modules.project.ui.Bundle", "LBL_Opening_Projects_Progress");
330         try {
331             // wait at most 60 second until progress dialog dismiss
332
JemmyProperties.setCurrentTimeout("ComponentOperator.WaitStateTimeout", 60000);
333             new NbDialogOperator(openingProjectsTitle).waitClosed();
334         } catch (TimeoutExpiredException e) {
335             // ignore when progress dialog was closed before we started to wait for it
336
}
337     }
338     
339     /**
340      * Creates Java Application from existing sources
341      * @param projName Name of the project
342      * @param projLocation Project folder location
343      * @param srcPkgFolder Location of source folder
344      * @param testPkgFolder Location of test folder
345      * @param mainProj Set Project as main
346      */

347     private void createJavaApplicationWithExistingSources(String JavaDoc projName, String JavaDoc projLocation,
348             String JavaDoc srcPkgFolder, String JavaDoc testPkgFolder, boolean mainProj) {
349         
350         // select the right template
351
NewProjectWizardOperator npwo = NewProjectWizardOperator.invoke();
352         npwo.selectCategory(standardLabel);
353         npwo.selectProject(existingLabel);
354         npwo.next();
355         
356         // project name
357
NewProjectNameLocationStepOperator npnlso = new NewProjectNameLocationStepOperator();
358         npnlso.txtProjectName().setText(projName);
359         
360         // project folder
361
npnlso.txtProjectFolder().setText(projLocation);
362         
363         // set project as main
364
JCheckBoxOperator mainProjOper = npnlso.cbSetAsMainProject();
365         if (!mainProj && mainProjOper.isSelected()) {
366             mainProjOper.doClick();
367         }
368         
369         npnlso.next();
370         
371         // test packages folder
372
JLabelOperator location = new JLabelOperator(npnlso,org.netbeans.jellytools.Bundle.getStringTrimmed("org.netbeans.modules.java.j2seproject.ui.wizards.Bundle", "CTL_SourceRoots"));
373         JListOperator list;
374         if ( location.getLabelFor()!=null ) {
375             list = new JListOperator((JList JavaDoc)location.getLabelFor());
376             
377             if(list.getModel().getSize() == 0){
378                 JButtonOperator browseButton = new JButtonOperator(npnlso, org.netbeans.jellytools.Bundle.getStringTrimmed("org.netbeans.modules.java.j2seproject.ui.customizer.Bundle", "CTL_AddSourceRoot"));
379                 browseButton.push();
380                 JFileChooserOperator chooserOperator = new JFileChooserOperator();
381                 chooserOperator.chooseFile(srcPkgFolder);
382             }
383         }
384         
385         npnlso.finish();
386         String JavaDoc openingProjectsTitle = Bundle.getString("org.netbeans.modules.project.ui.Bundle", "LBL_Opening_Projects_Progress");
387         try {
388             // wait at most 60 second until progress dialog dismiss
389
JemmyProperties.setCurrentTimeout("ComponentOperator.WaitStateTimeout", 60000);
390             new NbDialogOperator(openingProjectsTitle).waitClosed();
391         } catch (TimeoutExpiredException e) {
392             // ignore when progress dialog was closed before we started to wait for it
393
}
394     }
395     
396 }
397
Popular Tags