KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > EarProjectTest


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.modules.j2ee.earproject;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.lang.ref.Reference JavaDoc;
29 import java.lang.ref.WeakReference JavaDoc;
30 import java.net.URL JavaDoc;
31 import javax.xml.parsers.SAXParser JavaDoc;
32 import javax.xml.parsers.SAXParserFactory JavaDoc;
33 import junit.framework.Assert;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.api.project.ProjectManager;
36 import org.netbeans.api.project.ui.OpenProjects;
37 import org.netbeans.junit.NbTestCase;
38 import org.netbeans.modules.j2ee.dd.api.application.Application;
39 import org.netbeans.modules.j2ee.dd.api.application.DDProvider;
40 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
41 import org.netbeans.modules.j2ee.earproject.EarProject.ProjectOpenedHookImpl;
42 import org.netbeans.modules.j2ee.earproject.test.TestUtil;
43 import org.netbeans.modules.j2ee.earproject.ui.wizards.NewEarProjectWizardIteratorTest;
44 import org.netbeans.spi.project.ui.LogicalViewProvider;
45 import org.netbeans.spi.project.ui.ProjectOpenedHook;
46 import org.openide.filesystems.FileObject;
47 import org.openide.filesystems.FileUtil;
48 import org.openide.nodes.Node;
49 import org.xml.sax.SAXException JavaDoc;
50 import org.xml.sax.SAXParseException JavaDoc;
51 import org.xml.sax.helpers.DefaultHandler JavaDoc;
52
53 /**
54  * @author Martin Krauskopf
55  */

56 public class EarProjectTest extends NbTestCase {
57     
58     private String JavaDoc serverID;
59     
60     public EarProjectTest(String JavaDoc testName) {
61         super(testName);
62     }
63     
64     protected void setUp() throws Exception JavaDoc {
65         super.setUp();
66         TestUtil.makeScratchDir(this);
67         serverID = TestUtil.registerSunAppServer(this);
68     }
69     
70     public void testEarWithoutDDOpening() throws Exception JavaDoc { // #75586
71
File JavaDoc prjDirF = new File JavaDoc(getWorkDir(), "TestEarProject_15");
72         EarProjectGenerator.createProject(prjDirF, "test-project",
73                 J2eeModule.JAVA_EE_5, serverID, "1.5");
74         File JavaDoc dirCopy = copyFolder(prjDirF);
75         File JavaDoc ddF = new File JavaDoc(dirCopy, "src/conf/application.xml");
76         assertTrue("has deployment descriptor", ddF.isFile());
77         ddF.delete(); // one of #75586 scenario
78
FileObject fo = FileUtil.toFileObject(dirCopy);
79         Project project = ProjectManager.getDefault().findProject(fo);
80         assertNotNull("project is found", project);
81         // tests #75586
82
EarProjectTest.openProject((EarProject) project);
83     }
84     
85     public void testThatMissingDDIsRegeneratedCorrectlyDuringOpening() throws Exception JavaDoc { // #81154
86
File JavaDoc earDirF = new File JavaDoc(getWorkDir(), "testEA");
87         String JavaDoc name = "Test EnterpriseApplication";
88         String JavaDoc j2eeLevel = J2eeModule.JAVA_EE_5;
89         String JavaDoc ejbName = "testEA-ejb";
90         String JavaDoc acName = "testEA-ac";
91         NewEarProjectWizardIteratorTest.generateEARProject(earDirF, name, j2eeLevel,
92                 serverID, null, ejbName, acName, null, null, null);
93         File JavaDoc dirCopy = copyFolder(earDirF);
94         File JavaDoc ddF = new File JavaDoc(dirCopy, "src/conf/application.xml");
95         assertTrue("has deployment descriptor", ddF.isFile());
96         validate(ddF);
97         ddF.delete(); // one of #81154 scenario
98
FileObject fo = FileUtil.toFileObject(dirCopy);
99         Project project = ProjectManager.getDefault().findProject(fo);
100         assertNotNull("project is found", project);
101         EarProjectTest.openProject((EarProject) project);
102         assertTrue("deployment descriptor was regenerated", ddF.isFile());
103         validate(ddF);
104         Application app = DDProvider.getDefault().getDDRoot(FileUtil.toFileObject(ddF));
105         assertSame("two modules", 2, app.getModule().length);
106     }
107     
108     public void testOpeningWihtoutPrivateMetadataAndSrcDirectory() throws Exception JavaDoc { // #83507
109
File JavaDoc earDirF = new File JavaDoc(getWorkDir(), "testEA");
110         String JavaDoc name = "Test EnterpriseApplication";
111         String JavaDoc j2eeLevel = J2eeModule.JAVA_EE_5;
112         String JavaDoc ejbName = "testEA-ejb";
113         String JavaDoc acName = "testEA-ac";
114         NewEarProjectWizardIteratorTest.generateEARProject(earDirF, name, j2eeLevel,
115                 serverID, null, ejbName, acName, null, null, null);
116         File JavaDoc dirCopy = copyFolder(earDirF);
117         TestUtil.deleteRec(new File JavaDoc(new File JavaDoc(dirCopy, "nbproject"), "private"));
118         TestUtil.deleteRec(new File JavaDoc(dirCopy, "src"));
119         TestUtil.deleteRec(new File JavaDoc(new File JavaDoc(new File JavaDoc(dirCopy, "testEA-ac"), "nbproject"), "private"));
120         TestUtil.deleteRec(new File JavaDoc(new File JavaDoc(new File JavaDoc(dirCopy, "testEA-ejb"), "nbproject"), "private"));
121         FileObject fo = FileUtil.toFileObject(dirCopy);
122         Project project = ProjectManager.getDefault().findProject(fo);
123         assertNotNull("project is found", project);
124         EarProjectTest.openProject((EarProject) project);
125     }
126     
127     public void testEarProjectIsGCed() throws Exception JavaDoc { // #83128
128
File JavaDoc prjDirF = new File JavaDoc(getWorkDir(), "testEA");
129         String JavaDoc name = "Test EnterpriseApplication";
130         String JavaDoc j2eeLevel = "1.4";
131         
132         // creates a project we will use for the import
133
NewEarProjectWizardIteratorTest.generateEARProject(prjDirF, name, j2eeLevel,
134                 serverID, null, null, null, null, null, null);
135         Project earProject = ProjectManager.getDefault().findProject(FileUtil.toFileObject(prjDirF));
136         EarProjectTest.openProject((EarProject) earProject);
137         Node rootNode = ((LogicalViewProvider) earProject.getLookup().lookup(LogicalViewProvider.class)).createLogicalView();
138         rootNode.getChildren().getNodes(true); // ping
139
Reference JavaDoc<Project> wr = new WeakReference JavaDoc<Project>(earProject);
140         OpenProjects.getDefault().close(new Project[] { earProject });
141         EarProjectTest.closeProject((EarProject) earProject);
142         rootNode = null;
143         earProject = null;
144         assertGC("project cannot be garbage collected", wr);
145     }
146     
147     /**
148      * Accessor method for those who wish to simulate open of a project and in
149      * case of suite for example generate the build.xml.
150      */

151     public static void openProject(final EarProject p) {
152         ProjectOpenedHookImpl hook = (ProjectOpenedHookImpl) p.getLookup().lookup(ProjectOpenedHook.class);
153         assertNotNull("has an OpenedHook", hook);
154         hook.projectOpened(); // protected but can use package-private access
155
}
156     
157     public static void closeProject(final EarProject p) {
158         ProjectOpenedHookImpl hook = (ProjectOpenedHookImpl) p.getLookup().lookup(ProjectOpenedHook.class);
159         assertNotNull("has an OpenedHook", hook);
160         hook.projectClosed(); // protected but can use package-private access
161
}
162     
163     /**
164      * Make a temporary copy of a whole folder into some new dir in the scratch area.
165      * Stolen from ant/freeform.
166      */

167     private File JavaDoc copyFolder(File JavaDoc d) throws IOException JavaDoc {
168         assert d.isDirectory();
169         File JavaDoc workdir = getWorkDir();
170         String JavaDoc name = d.getName();
171         while (name.length() < 3) {
172             name = name + "x";
173         }
174         File JavaDoc todir = workdir.createTempFile(name, null, workdir);
175         todir.delete();
176         doCopy(d, todir);
177         return todir;
178     }
179     
180     private static void doCopy(File JavaDoc from, File JavaDoc to) throws IOException JavaDoc {
181         if (from.isDirectory()) {
182             if (from.getName().equals("CVS")) {
183                 return;
184             }
185             to.mkdir();
186             String JavaDoc[] kids = from.list();
187             for (int i = 0; i < kids.length; i++) {
188                 doCopy(new File JavaDoc(from, kids[i]), new File JavaDoc(to, kids[i]));
189             }
190         } else {
191             assert from.isFile();
192             InputStream JavaDoc is = new FileInputStream JavaDoc(from);
193             try {
194                 OutputStream JavaDoc os = new FileOutputStream JavaDoc(to);
195                 try {
196                     FileUtil.copy(is, os);
197                 } finally {
198                     os.close();
199                 }
200             } finally {
201                 is.close();
202             }
203         }
204     }
205     
206     public static void validate(final File JavaDoc ddFile) throws Exception JavaDoc {
207         SAXParserFactory JavaDoc f = (SAXParserFactory JavaDoc) Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance();
208         if (f == null) {
209             System.err.println("Validation skipped because org.apache.xerces.jaxp.SAXParserFactoryImpl was not found on classpath");
210             return;
211         }
212         f.setNamespaceAware(true);
213         f.setValidating(true);
214         SAXParser JavaDoc p = f.newSAXParser();
215         URL JavaDoc schemaURL_1_4 = EarProjectTest.class.getResource("/org/netbeans/modules/j2ee/dd/impl/resources/application_1_4.xsd");
216         URL JavaDoc schemaURL_5 = EarProjectTest.class.getResource("/org/netbeans/modules/j2ee/dd/impl/resources/application_5.xsd");
217         assertNotNull("have access to schema", schemaURL_1_4);
218         assertNotNull("have access to schema", schemaURL_5);
219         p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
220                 "http://www.w3.org/2001/XMLSchema");
221         p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", new String JavaDoc[] {
222             schemaURL_1_4.toExternalForm(),
223             schemaURL_5.toExternalForm()
224         });
225         try {
226             p.parse(ddFile.toURI().toString(), new Handler JavaDoc());
227         } catch (SAXParseException JavaDoc e) {
228             fail("Validation of XML document " + ddFile + " against schema failed. Details: " +
229                     e.getSystemId() + ":" + e.getLineNumber() + ": " + e.getLocalizedMessage());
230         }
231     }
232     
233     public static void validate(FileObject ddFO) throws Exception JavaDoc {
234         Assert.assertNotNull(ddFO);
235         File JavaDoc ddF = FileUtil.toFile(ddFO);
236         Assert.assertNotNull(ddF);
237         validate(ddF);
238     }
239     
240     private static final class Handler extends DefaultHandler JavaDoc {
241         public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
242             throw e;
243         }
244         public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
245             throw e;
246         }
247         public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
248             throw e;
249         }
250     }
251     
252 }
253
Popular Tags