KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > j2ee > J2eeProjectsTest


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;
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.api.project.Project;
29 import org.netbeans.api.project.ProjectManager;
30 import org.netbeans.jellytools.JellyTestCase;
31 import org.netbeans.junit.NbTestCase;
32 import org.netbeans.junit.NbTestSuite;
33 import org.netbeans.modules.j2ee.earproject.EarProject;
34 import org.netbeans.modules.j2ee.earproject.EarProjectType;
35 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties;
36 import org.netbeans.spi.project.AuxiliaryConfiguration;
37 import org.netbeans.spi.project.support.ant.AntProjectHelper;
38 import org.netbeans.spi.project.support.ant.ReferenceHelper;
39 import org.openide.filesystems.FileUtil;
40 import org.netbeans.test.j2ee.lib.J2eeProjectSupport;
41 import org.netbeans.test.j2ee.lib.Reporter;
42 import org.netbeans.test.j2ee.lib.RequiredFiles;
43
44
45 /**
46  * Test creation of new projects via API
47  * These tests are part of J2EE Functional test suite.
48  *
49  * @author jungi
50  * @see <a HREF="http://qa.netbeans.org/modules/j2ee/promo-f/testspec/j2ee-wizards-testspec.html">J2EE Wizards Test Specification</a>
51  */

52 public class J2eeProjectsTest extends JellyTestCase {
53     
54     private Reporter reporter;
55     
56     /**
57      * Creates a new instance of J2eeProjectsTest
58      */

59     public J2eeProjectsTest(String JavaDoc name) {
60         super(name);
61     }
62     
63     
64     public void setUp() throws Exception JavaDoc {
65         super.setUp();
66         reporter = Reporter.getReporter((NbTestCase)this);
67     }
68     
69     public void tearDown() throws Exception JavaDoc {
70         super.tearDown();
71         reporter.close();
72     }
73
74     /**
75      * Creates EJB Module project and checks if all files are created
76      * succesfully (including server specific deployment descriptor)
77      */

78     public void testCreateEjbProject() {
79         File JavaDoc wd = null;
80         try {
81             wd = getWorkDir();
82         } catch (IOException JavaDoc ioe) {
83             ioe.printStackTrace(reporter.getLogStream());
84         }
85         assertNotNull(wd);
86         RequiredFiles rf = null;
87         try {
88             rf = new RequiredFiles(new File JavaDoc(getDataDir(), "structures/ejbProject.str"));
89         } catch (IOException JavaDoc ioe) {
90             ioe.printStackTrace(reporter.getLogStream());
91         }
92         assertNotNull(rf);
93         Project p = (Project) J2eeProjectSupport.createProject(wd, "EJBModule", J2eeProjectSupport.EJB_PROJECT, null);
94         assertNotNull(p);
95         checkProjectStructure(p, rf);
96     }
97     
98     /**
99      * Creates Web Application project and checks if all files are created
100      * succesfully (including server specific deployment descriptor)
101      */

102     public void testCreateWebProject() {
103         File JavaDoc wd = null;
104         try {
105             wd = getWorkDir();
106         } catch (IOException JavaDoc ioe) {
107             ioe.printStackTrace(reporter.getLogStream());
108         }
109         assertNotNull(wd);
110         RequiredFiles rf = null;
111         try {
112             rf = new RequiredFiles(new File JavaDoc(getDataDir(), "structures/webProject.str"));
113         } catch (IOException JavaDoc ioe) {
114             ioe.printStackTrace(reporter.getLogStream());
115         }
116         assertNotNull(rf);
117         Project p = (Project) J2eeProjectSupport.createProject(wd, "WebModule", J2eeProjectSupport.WEB_PROJECT, null);
118         assertNotNull(p);
119         checkProjectStructure(p, rf);
120     }
121     
122     /**
123      * Creates empty Enterprise Application project and checks if all files
124      * are created succesfully (including server specific deployment descriptor)
125      */

126     public void testCreateEmptyJ2eeProject() {
127         File JavaDoc wd = null;
128         try {
129             wd = getWorkDir();
130         } catch (IOException JavaDoc ioe) {
131             ioe.printStackTrace(reporter.getLogStream());
132         }
133         assertNotNull(wd);
134         RequiredFiles rf = null;
135         try {
136             rf = new RequiredFiles(new File JavaDoc(getDataDir(), "structures/emptyJ2eeProject.str"));
137         } catch (IOException JavaDoc ioe) {
138             ioe.printStackTrace(reporter.getLogStream());
139         }
140         assertNotNull(rf);
141         Project p = (Project) J2eeProjectSupport.createProject(wd, "J2eePrj", J2eeProjectSupport.J2EE_PROJECT, null);
142         assertNotNull(p);
143         checkProjectStructure(p, rf);
144     }
145
146     /**
147      * Add war and jar to ear.
148      */

149     public void testAddModulesToJ2eeProject() {
150         EarProject earPrj = null;
151         Project warPrj = null;
152         Project ejbPrj = null;
153         ProjectManager pm = ProjectManager.getDefault();
154         try {
155             earPrj = (EarProject) pm.findProject(FileUtil.toFileObject(new File JavaDoc(getWorkDir(), "../testCreateEmptyJ2eeProject/J2eePrj").getCanonicalFile()));
156             warPrj = pm.findProject(FileUtil.toFileObject(new File JavaDoc(getWorkDir(), "../testCreateWebProject/WebModule").getCanonicalFile()));
157             ejbPrj = pm.findProject(FileUtil.toFileObject(new File JavaDoc(getWorkDir(), "../testCreateEjbProject/EJBModule").getCanonicalFile()));
158             AntProjectHelper h = earPrj.getUpdateHelper().getAntProjectHelper();
159             AuxiliaryConfiguration aux = h.createAuxiliaryConfiguration();
160             ReferenceHelper refHelper = new ReferenceHelper(h, aux, h.getStandardPropertyEvaluator());
161             EarProjectProperties epp = new EarProjectProperties(earPrj, refHelper, new EarProjectType());
162             epp.addJ2eeSubprojects(new Project[] {warPrj, ejbPrj});
163         } catch (IOException JavaDoc ioe) {
164             ioe.printStackTrace(reporter.getLogStream());
165             fail("IOEx while adding modules to EAR project.");
166         }
167     }
168     
169     protected void checkProjectStructure(Project p, RequiredFiles r) {
170         Set JavaDoc/*<String>*/ l = J2eeProjectSupport.getFileSet(p);
171         Set JavaDoc/*<String>*/ rf = r.getRequiredFiles();
172         reporter.ref("Project: " + p.toString());
173         reporter.ref("Expected: " + rf);
174         reporter.ref("Real: " + l);
175         assertTrue("Files: " + getDifference(l, rf) + " are missing in project: " + p.toString() , l.containsAll(rf));
176         rf = r.getRequiredFiles();
177         reporter.ref("Project: " + p.toString());
178         reporter.ref("Expected: " + rf);
179         reporter.ref("Real: " + l);
180         Set JavaDoc s = getDifference(rf, l);
181         assertTrue("Files: " + s + " are new in project: " + p.toString() , s.isEmpty());
182     }
183     
184     public static TestSuite suite() {
185         TestSuite suite = new NbTestSuite();
186         suite.addTest(new J2eeProjectsTest("testCreateEjbProject"));
187         suite.addTest(new J2eeProjectsTest("testCreateWebProject"));
188         suite.addTest(new J2eeProjectsTest("testCreateEmptyJ2eeProject"));
189         suite.addTest(new J2eeProjectsTest("testAddModulesToJ2eeProject"));
190         return suite;
191     }
192     
193     private Set JavaDoc getDifference(Set JavaDoc s1, Set JavaDoc s2) {
194         Set JavaDoc result = new HashSet JavaDoc();
195         s2.removeAll(s1);
196         for (Iterator JavaDoc i = s2.iterator(); i.hasNext();) {
197             String JavaDoc s = (String JavaDoc) i.next();
198             if (s.indexOf(".LCK") < 0) {
199                 result.add(s);
200             } else {
201                 reporter.log("Additional file: " + s);
202             }
203         }
204         return result;
205     }
206 }
207
Popular Tags