KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > wizard > NewProjectWizardsTest


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 package org.netbeans.test.j2ee.wizard;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28 import org.netbeans.jellytools.EditorWindowOperator;
29 import org.netbeans.jellytools.JellyTestCase;
30 import org.netbeans.jellytools.NewProjectNameLocationStepOperator;
31 import org.netbeans.jellytools.NewProjectWizardOperator;
32 import org.netbeans.jellytools.ProjectsTabOperator;
33 import org.netbeans.jellytools.nodes.Node;
34 import org.netbeans.jemmy.EventTool;
35 import org.netbeans.jemmy.operators.JComboBoxOperator;
36 import org.netbeans.junit.NbTestCase;
37 import org.netbeans.junit.NbTestSuite;
38 import org.netbeans.test.j2ee.lib.Reporter;
39 import org.netbeans.test.j2ee.lib.RequiredFiles;
40 import org.netbeans.test.j2ee.lib.J2eeProjectSupport;
41 import org.netbeans.test.j2ee.lib.Utils;
42
43 /**
44  * Test New File wizards in J2EE area. These tests are
45  * part of J2EE Functional test suite. Each test checks
46  * if all files were created (deployment descriptors,
47  * directories for sources etc.) and if project node
48  * is expanded after finishing New Project wizard.
49  *
50  * @author jungi
51  * @see <a HREF="http://qa.netbeans.org/modules/j2ee/promo-f/testspec/j2ee-wizards-testspec.html">J2EE Wizards Test Specification</a>
52  */

53 public class NewProjectWizardsTest extends JellyTestCase {
54     
55     private static final int EJB = 0;
56     private static final int WEB = 1;
57     private static final int WEB_JAKARTA = 2;
58     private static final int J2EE_DEFAULT = 3;
59     private static final int APP_CLIENT_DEFAULT = 4;
60     
61     private String JavaDoc projectLocation;
62     private String JavaDoc projectName;
63     private String JavaDoc version;
64     private Reporter reporter;
65
66     public NewProjectWizardsTest(String JavaDoc testName) {
67         this(testName, "1.4");
68     }
69     
70     public NewProjectWizardsTest(String JavaDoc testName, String JavaDoc version) {
71         super(testName);
72         this.version = version;
73     }
74     
75     public void setUp() throws Exception JavaDoc {
76         reporter = Reporter.getReporter((NbTestCase)this);
77     }
78     
79     public void tearDown() throws Exception JavaDoc {
80         reporter.close();
81     }
82     
83     public static void main(java.lang.String JavaDoc[] args) {
84         junit.textui.TestRunner.run(suite());
85     }
86     
87     public static Test suite() {
88         TestSuite suite = new NbTestSuite(NewProjectWizardsTest.class);
89         return suite;
90     }
91
92     /**
93      * Create EJB Module which name contains spaces
94      * and default project settings.
95      */

96     public void testDefaultNewEJBModWizard() throws Exception JavaDoc {
97         Utils.openOutputTab();
98         projectLocation = getWorkDir().getAbsolutePath();
99         projectName = "def EJB Mod" + version;
100         NewProjectWizardOperator wiz
101                 = WizardUtils.createNewProject("Enterprise", "EJB Module");
102         NewProjectNameLocationStepOperator op
103                 = WizardUtils.setProjectNameLocation(projectName,
104                 projectLocation);
105         WizardUtils.setJ2eeSpecVersion(op, WizardUtils.MODULE_EJB, version);
106         wiz.finish();
107         checkProjectStructure(EJB);
108         checkProjectNodes();
109     }
110     
111     /**
112      * Create EJB Module with default project settings.
113      */

114     public void testNewEJBModWizard() throws Exception JavaDoc {
115         projectLocation = getWorkDir().getAbsolutePath();
116         projectName = "BadModule" + version;
117         NewProjectWizardOperator wiz
118                 = WizardUtils.createNewProject("Enterprise", "EJB Module");
119         NewProjectNameLocationStepOperator op
120                 = WizardUtils.setProjectNameLocation(projectName,
121                 projectLocation);
122         WizardUtils.setJ2eeSpecVersion(op, WizardUtils.MODULE_EJB, version);
123         wiz.finish();
124         checkProjectStructure(EJB);
125         checkProjectNodes();
126     }
127     
128     
129     /**
130      * Create Enterprise Application Client project with default project
131      * settings.
132      */

133     public void testDefaultAppClientWizard() throws Exception JavaDoc {
134         projectLocation = getWorkDir().getAbsolutePath();
135         projectName = "App client" + version;
136         NewProjectWizardOperator wiz
137                 = WizardUtils.createNewProject("Enterprise", "Enterprise Application Client");
138         NewProjectNameLocationStepOperator op
139                 = WizardUtils.setProjectNameLocation(projectName,
140                 projectLocation);
141         WizardUtils.setJ2eeSpecVersion(op, WizardUtils.MODULE_CAR, version);
142         wiz.finish();
143         checkProjectStructure(APP_CLIENT_DEFAULT);
144         checkProjectNodes();
145     }
146     
147     /**
148      * Create Web Application which name contains spaces
149      * and default project settings.
150      */

151     public void testDefaultNewWebModWizard() throws Exception JavaDoc {
152         projectLocation = getWorkDir().getAbsolutePath();
153         projectName = "def Web app" + version;
154         NewProjectWizardOperator wiz
155                 = WizardUtils.createNewProject("Web", "Web Application");
156         NewProjectNameLocationStepOperator op
157                 = WizardUtils.setProjectNameLocation(projectName,
158                 projectLocation);
159         WizardUtils.setJ2eeSpecVersion(op, WizardUtils.MODULE_WAR, version);
160         wiz.finish();
161         Thread.sleep(1000);
162         checkProjectStructure(WEB);
163         checkProjectNodes();
164     }
165     
166     /**
167      * Create Web Application with Jakarta source structure.
168      */

169     public void testDefaultNewJakartaWebModWizard() throws Exception JavaDoc {
170         projectLocation = getWorkDir().getAbsolutePath();
171         projectName = "Jakarta Web App" + version;
172         NewProjectWizardOperator wiz
173                 = WizardUtils.createNewProject("Web", "Web Application");
174         NewProjectNameLocationStepOperator nlsop
175                 = WizardUtils.setProjectNameLocation(projectName,
176                 projectLocation);
177         JComboBoxOperator cbop = new JComboBoxOperator(nlsop, "BluePrints");
178         cbop.setSelectedIndex(1); //jakarta source structure
179
WizardUtils.setJ2eeSpecVersion(nlsop, WizardUtils.MODULE_WAR, version);
180         wiz.finish();
181         Thread.sleep(1000);
182         checkProjectStructure(WEB_JAKARTA);
183         checkProjectNodes();
184     }
185     
186     /**
187      * Create Enterprise Application project with default project
188      * settings (ejb and web module are as well ).
189      */

190     public void testDefaultNewJ2eeAppWizard() throws Exception JavaDoc {
191         projectLocation = getWorkDir().getAbsolutePath();
192         projectName = "def EAR app" + version;
193         NewProjectWizardOperator wiz
194                 = WizardUtils.createNewProject("Enterprise", "Enterprise Application");
195         NewProjectNameLocationStepOperator op
196                 = WizardUtils.setProjectNameLocation(projectName,
197                 projectLocation);
198         WizardUtils.setJ2eeSpecVersion(op, WizardUtils.MODULE_EAR, version);
199         wiz.finish();
200         checkProjectStructure(J2EE_DEFAULT);
201         Node root = checkProjectNodes();
202         Node modules = new Node(root, "Java EE Modules");
203         modules.expand();
204         String JavaDoc[] s = modules.getChildren();
205         assertEquals("Expected: \"def_EAR_app" + version + "-ejb.jar\", was: \"" + s[1]
206                 + "\"", "def_EAR_app" + version + "-ejb.jar", s[1]);
207         assertEquals("Expected: \"def_EAR_app" + version + "-war.war\", was: \"" + s[0]
208                 + "\"", "def_EAR_app" + version + "-war.war", s[0]);
209     }
210
211     private void checkProjectStructure(int prjType) {
212         RequiredFiles r = null;
213         switch (prjType) {
214             case EJB:
215                 r = readRF("structures/ejbProject.str");
216                 break;
217             case WEB:
218                 r = readRF("structures/webProject.str");
219                 break;
220             case WEB_JAKARTA:
221                 r = readRF("structures/webProjectJakarta.str");
222                 break;
223             case J2EE_DEFAULT:
224                 r = readRF("structures/defEAR.str");
225                 break;
226             case APP_CLIENT_DEFAULT:
227                r = readRF("structures/carProject.str");
228                break;
229             default:
230                 throw new IllegalArgumentException JavaDoc();
231         }
232         try {
233             Thread.currentThread().sleep(5000);
234         } catch (InterruptedException JavaDoc ex) {
235             //do nothing
236
}
237         Set JavaDoc/*<String>*/ l = J2eeProjectSupport.getFileSet(projectLocation + File.separatorChar + projectName);
238         Set JavaDoc/*<String>*/ rf = r.getRequiredFiles();
239         reporter.ref("Project: " + projectLocation);
240         reporter.ref("Expected: " + rf);
241         reporter.ref("Real: " + l);
242         if ((EJB == prjType) && ("5".equals(version))) {
243             Set JavaDoc result = getDifference(l, rf);
244             if (!result.remove("src" + File.separator + "conf" + File.separator + "ejb-jar.xml")) {
245                 fail("Files: " + result + " are missing in project at: " + projectLocation);
246             }
247         } else {
248             assertTrue("Files: " + getDifference(l, rf) + " are missing in project at: " + projectLocation , l.containsAll(rf));
249         }
250         rf = r.getRequiredFiles();
251         reporter.ref("Project: " + projectLocation);
252         reporter.ref("Expected: " + rf);
253         reporter.ref("Real: " + l);
254         Set JavaDoc s = getDifference(rf, l);
255         assertTrue("Files: " + s + " are new in project: " + projectLocation , s.isEmpty());
256     }
257     
258     public void closeProjects() {
259         ProjectsTabOperator pto = new ProjectsTabOperator();
260         pto.getProjectRootNode("def EJB Mod").performPopupAction("Close Project");
261         pto.getProjectRootNode("BadModule").performPopupAction("Close Project");
262         pto.getProjectRootNode("def EAR app").performPopupAction("Close Project");
263         pto.getProjectRootNode("def Web app").performPopupAction("Close Project");
264 // pto.getProjectRootNode("def App client").performPopupAction("Close Project");
265
//pto.getProjectRootNode("Jakarta Web App").performPopupAction("Close Project");
266
new EventTool().waitNoEvent(2500);
267     }
268     
269     private RequiredFiles readRF(String JavaDoc fileName) {
270         RequiredFiles rf = null;
271         try {
272             rf = new RequiredFiles(new File JavaDoc(getDataDir(), fileName));
273         } catch (IOException JavaDoc ioe) {
274             ioe.printStackTrace(reporter.getLogStream());
275         }
276         assertNotNull(rf);
277         return rf;
278     }
279     
280     private Set JavaDoc getDifference(Set JavaDoc s1, Set JavaDoc s2) {
281         Set JavaDoc result = new HashSet JavaDoc();
282         s2.removeAll(s1);
283         for (Iterator JavaDoc i = s2.iterator(); i.hasNext();) {
284             String JavaDoc s = (String JavaDoc) i.next();
285             if (s.indexOf(".LCK") < 0) {
286                 result.add(s);
287             } else {
288                 reporter.log("Additional file: " + s);
289             }
290         }
291         return result;
292     }
293     
294     private Node checkProjectNodes() {
295         Node node = new ProjectsTabOperator().getProjectRootNode(projectName);
296         try {
297             Thread.sleep(1500);
298         } catch (InterruptedException JavaDoc ie) {
299             //do nothing
300
}
301         assertTrue("Project " + projectName + " is not expanded", node.isExpanded());
302         return node;
303     }
304 }
305
Popular Tags