KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbfreeform > EJBProjectGeneratorTest


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.ejbfreeform;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.jar.JarEntry JavaDoc;
32 import java.util.jar.JarOutputStream JavaDoc;
33 import javax.xml.parsers.SAXParser JavaDoc;
34 import javax.xml.parsers.SAXParserFactory JavaDoc;
35 import org.netbeans.api.project.Project;
36 import org.netbeans.api.project.ProjectInformation;
37 import org.netbeans.api.project.ProjectManager;
38 import org.netbeans.api.project.ProjectUtils;
39 import org.netbeans.modules.ant.freeform.spi.support.Util;
40 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar;
41 import org.netbeans.modules.java.freeform.JavaProjectGenerator;
42 import org.netbeans.spi.project.support.ant.AntProjectHelper;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileUtil;
45 import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
46 import org.netbeans.modules.project.ant.AntBasedProjectFactorySingleton;
47 import org.netbeans.spi.project.AuxiliaryConfiguration;
48 import org.w3c.dom.Element JavaDoc;
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  * Tests for EJBProjectGenerator.
55  *
56  * @author David Konecny, Lukas Jungmann
57  */

58 public class EJBProjectGeneratorTest extends TestBase {
59     
60     private File JavaDoc lib1;
61     private File JavaDoc lib2;
62     private File JavaDoc src;
63     private File JavaDoc test;
64     private File JavaDoc conf;
65     
66     protected void setUp() throws Exception JavaDoc {
67         super.setUp();
68     }
69     
70     public EJBProjectGeneratorTest(java.lang.String JavaDoc testName) {
71         super(testName);
72     }
73     
74     private AntProjectHelper createEmptyProject(String JavaDoc projectFolder, String JavaDoc projectName, boolean notSoEmpty) throws Exception JavaDoc {
75         File JavaDoc base = new File JavaDoc(getWorkDir(), projectFolder);
76         base.mkdir();
77         File JavaDoc antScript = new File JavaDoc(base, "build.xml");
78         antScript.createNewFile();
79         src = new File JavaDoc(base, "src");
80         src.mkdir();
81         conf = new File JavaDoc(base, "conf");
82         conf.mkdir();
83         new File JavaDoc(conf, "ejb-jar.xml").createNewFile();
84         test = new File JavaDoc(base, "test");
85         test.mkdir();
86         File JavaDoc libs = new File JavaDoc(base, "libs");
87         libs.mkdir();
88         lib1 = new File JavaDoc(libs, "some.jar");
89         createRealJarFile(lib1);
90         lib2 = new File JavaDoc(libs, "some2.jar");
91         createRealJarFile(lib2);
92         EJBProjectGenerator.EJBModule ejbMod = new EJBProjectGenerator.EJBModule();
93         ejbMod.j2eeSpecLevel = "1.4";
94         ejbMod.classpath = "";
95         ejbMod.configFiles = conf.getAbsolutePath();
96         List JavaDoc mods = new ArrayList JavaDoc();
97         mods.add(ejbMod);
98         AntProjectHelper helper = FreeformProjectGenerator.createProject(base, base, projectName, null);
99         EJBProjectGenerator.putEJBModules(helper, Util.getAuxiliaryConfiguration(helper), mods);
100         EJBProjectGenerator.putServerID(helper, "GENERIC");
101         
102         putSrcRoot(helper); //workaround for issue 71363
103

104         List JavaDoc l = new ArrayList JavaDoc();
105         String JavaDoc s = conf.getAbsolutePath();
106         l.add(s);
107         l.add(conf.getName());
108         EJBProjectGenerator.putEJBSourceFolder(helper, l);
109         EJBProjectGenerator.putEJBNodeView(helper, l);
110         
111         if (notSoEmpty) {
112             ArrayList JavaDoc sources = new ArrayList JavaDoc();
113             JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder();
114             sf.label = "src";
115             sf.type = "java";
116             sf.style = "packages";
117             sf.location = src.getAbsolutePath();
118             sources.add(sf);
119             sf = new JavaProjectGenerator.SourceFolder();
120             sf.label = "test";
121             sf.type = "java";
122             sf.style = "packages";
123             sf.location = test.getAbsolutePath();
124             sources.add(sf);
125             JavaProjectGenerator.putSourceFolders(helper, sources, "java");
126             JavaProjectGenerator.putSourceViews(helper, sources, "packages");
127             
128             ArrayList JavaDoc compUnits = new ArrayList JavaDoc();
129             JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit();
130             JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP();
131             cp.classpath = lib1.getAbsolutePath();
132             cp.mode = "compile";
133             cu.classpath = Collections.singletonList(cp);
134             cu.sourceLevel = "1.4";
135             cu.packageRoots = Collections.singletonList(src.getAbsolutePath());
136             compUnits.add(cu);
137             cu = new JavaProjectGenerator.JavaCompilationUnit();
138             cp = new JavaProjectGenerator.JavaCompilationUnit.CP();
139             cp.classpath = lib2.getAbsolutePath();
140             cp.mode = "compile";
141             cu.classpath = Collections.singletonList(cp);
142             cu.sourceLevel = "1.4";
143             cu.packageRoots = Collections.singletonList(test.getAbsolutePath());
144             cu.isTests = true;
145             compUnits.add(cu);
146             JavaProjectGenerator.putJavaCompilationUnits(helper, Util.getAuxiliaryConfiguration(helper), compUnits);
147         }
148         
149         return helper;
150     }
151     
152     public void testEJBModules() throws Exception JavaDoc {
153         AntProjectHelper helper = createEmptyProject("proj1", "proj-1", false);
154         FileObject base = helper.getProjectDirectory();
155         Project p = ProjectManager.getDefault().findProject(base);
156         assertNotNull("Project was not created", p);
157 // ProjectManager.getDefault().saveProject(p);
158
validate(p);
159         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
160         ProjectInformation pi = ProjectUtils.getInformation(p);
161         assertEquals("Project name was not set", "proj-1", pi.getName());
162         EjbJar e = EjbJar.getEjbJar(FileUtil.toFileObject(conf));
163         assertNotNull("EjbJar not found", e);
164         assertEquals("Incorrect ejb-jar.xml.", FileUtil.toFileObject(new File JavaDoc(conf, "ejb-jar.xml")),
165                 e.getDeploymentDescriptor());
166         assertEquals("Incorrect J2EE spec. version.", "1.4", e.getJ2eePlatformVersion());
167         Element JavaDoc ejb = ((AuxiliaryConfiguration) p.getLookup().lookup(AuxiliaryConfiguration.class))
168         .getConfigurationFragment("ejb-data", EJBProjectNature.NS_EJB, true);
169         assertNotNull(ejb);
170         List JavaDoc/*<Element>*/ ejbModules = Util.findSubElements(ejb);
171         assertEquals("One ejb-module element should be found.", 1, ejbModules.size());
172         Element JavaDoc module = (Element JavaDoc) ejbModules.get(0);
173         assertEquals("Classpath element is not empty.", "" , Util.findText(Util.findElement(module, "classpath", EJBProjectNature.NS_EJB)));
174     }
175     
176     public void testSourceFolders() throws Exception JavaDoc {
177         AntProjectHelper helper = createEmptyProject("proj-2", "proj-2", true);
178         FileObject base = helper.getProjectDirectory();
179         Project p = ProjectManager.getDefault().findProject(base);
180 // ProjectManager.getDefault().saveProject(p);
181
assertNotNull("Project was not created", p);
182         validate(p);
183         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
184         ProjectInformation pi = ProjectUtils.getInformation(p);
185         assertEquals("Project name was not set", "proj-2", pi.getName());
186         EjbJar e = EjbJar.getEjbJar(FileUtil.toFileObject(conf));
187         assertNotNull("EjbJar not found", e);
188         Set JavaDoc srcRoots = new HashSet JavaDoc();
189         srcRoots.add(FileUtil.toFileObject(src));
190         Set JavaDoc testRoots = new HashSet JavaDoc();
191         testRoots.add(FileUtil.toFileObject(test));
192         //in current implementation EjbJar.getJavaSources() returns
193
//all source roots (java+tests)
194
FileObject[] fos = e.getJavaSources();
195         Set JavaDoc s = new HashSet JavaDoc();
196         for (int i = 0; i < fos.length; i++) {
197             s.add(fos[i]);
198         }
199         assertTrue("There are missing java src roots", s.containsAll(srcRoots));
200     }
201     
202     // create real Jar otherwise FileUtil.isArchiveFile returns false for it
203
public void createRealJarFile(File JavaDoc f) throws Exception JavaDoc {
204         OutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
205         try {
206             JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(os);
207             JarEntry JavaDoc entry = new JarEntry JavaDoc("foo.txt");
208             jos.putNextEntry(entry);
209             jos.flush();
210             jos.close();
211         } finally {
212             os.close();
213         }
214     }
215     
216     public static void validate(Project proj) throws Exception JavaDoc {
217         File JavaDoc projF = FileUtil.toFile(proj.getProjectDirectory());
218         File JavaDoc xml = new File JavaDoc(new File JavaDoc(projF, "nbproject"), "project.xml");
219         SAXParserFactory JavaDoc f = (SAXParserFactory JavaDoc)Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance();
220         if (f == null) {
221             System.err.println("Validation skipped because org.apache.xerces.jaxp.SAXParserFactoryImpl was not found on classpath");
222             return;
223         }
224         f.setNamespaceAware(true);
225         f.setValidating(true);
226         SAXParser JavaDoc p = f.newSAXParser();
227         p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
228                 "http://www.w3.org/2001/XMLSchema");
229         p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", getSchemas());
230         try {
231             p.parse(xml.toURI().toString(), new Handler JavaDoc());
232         } catch (SAXParseException JavaDoc e) {
233             assertTrue("Validation of XML document "+xml+" against schema failed. Details: "+
234                     e.getSystemId() + ":" + e.getLineNumber() + ": " + e.getLocalizedMessage(), false);
235         }
236     }
237     
238     private static String JavaDoc[] getSchemas() throws Exception JavaDoc {
239         return new String JavaDoc[] {
240             FreeformProjectGenerator.class.getResource("resources/freeform-project-general.xsd").toExternalForm(),
241             JavaProjectGenerator.class.getResource("resources/freeform-project-java.xsd").toExternalForm(),
242             JavaProjectGenerator.class.getResource("resources/freeform-project-java-2.xsd").toExternalForm(),
243             EJBProjectGenerator.class.getResource("resources/freeform-project-ejb.xsd").toExternalForm(),
244             AntBasedProjectFactorySingleton.class.getResource("project.xsd").toExternalForm(),
245         };
246     }
247     
248     private static final class Handler extends DefaultHandler JavaDoc {
249         public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
250             throw e;
251         }
252         public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
253             throw e;
254         }
255         public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
256             throw e;
257         }
258     }
259     
260     // Issue 71363 WA
261
private void putSrcRoot(AntProjectHelper helper) {
262         JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder();
263         sf.label = "src";
264         sf.type = "java";
265         sf.style = "packages";
266         sf.location = src.getAbsolutePath();
267         ArrayList JavaDoc sources = new ArrayList JavaDoc();
268         sources.add(sf);
269         JavaProjectGenerator.putSourceFolders(helper, sources, null);
270         JavaProjectGenerator.putSourceViews(helper, sources, null);
271         ArrayList JavaDoc compUnits = new ArrayList JavaDoc();
272         JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit();
273         JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP();
274         cp.classpath = lib1.getAbsolutePath();
275         cp.mode = "compile";
276         cu.classpath = Collections.singletonList(cp);
277         cu.sourceLevel = "1.4";
278         cu.packageRoots = Collections.singletonList(src.getAbsolutePath());
279         compUnits.add(cu);
280         JavaProjectGenerator.putJavaCompilationUnits(helper, Util.getAuxiliaryConfiguration(helper), compUnits);
281     }
282     
283 }
284
Popular Tags