KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > api > AppClientProjectGeneratorTest


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.clientproject.api;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.junit.NbTestCase;
26 import org.netbeans.modules.j2ee.clientproject.test.TestUtil;
27 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
28 import org.netbeans.spi.project.support.ant.AntProjectHelper;
29 import org.netbeans.spi.project.support.ant.EditableProperties;
30 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33
34 /**
35  *
36  * @author Lukas Jungmann
37  */

38 public class AppClientProjectGeneratorTest extends NbTestCase {
39     
40     private String JavaDoc serverID;
41     
42     private static final String JavaDoc[] createdFiles = {
43         "build.xml",
44         "nbproject/build-impl.xml",
45         "nbproject/genfiles.properties",
46         "nbproject/project.xml",
47         "nbproject/project.properties",
48         "nbproject/private/private.properties",
49         "src/conf",
50         "src/conf/MANIFEST.MF",
51         "src/java",
52         "test"
53     };
54     
55     private static final String JavaDoc[] createdFilesExtSources = {
56         "build.xml",
57         "nbproject/build-impl.xml",
58         "nbproject/genfiles.properties",
59         "nbproject/project.xml",
60         "nbproject/project.properties",
61         "nbproject/private/private.properties",
62     };
63     
64     private static final String JavaDoc[] createdProperties = {
65         "build.classes.dir",
66         "build.classes.excludes",
67         "build.dir",
68         "build.ear.classes.dir",
69         "build.generated.dir",
70         "build.sysclasspath",
71         "build.test.classes.dir",
72         "build.test.results.dir",
73         "debug.classpath",
74         "debug.test.classpath",
75         "dist.dir",
76         "dist.ear.jar",
77         "dist.jar",
78         "dist.javadoc.dir",
79         "j2ee.appclient.mainclass.args",
80         "j2ee.platform",
81         "j2ee.server.type",
82         "jar.compress",
83         "jar.name",
84         "javac.classpath",
85         "javac.compilerargs",
86         "javac.deprecation",
87         "javac.source",
88         "javac.target",
89         "javac.test.classpath",
90         "javadoc.additionalparam",
91         "javadoc.author",
92         "javadoc.encoding",
93         "javadoc.noindex",
94         "javadoc.nonavbar",
95         "javadoc.notree",
96         "javadoc.private",
97         "javadoc.splitindex",
98         "javadoc.use",
99         "javadoc.version",
100         "javadoc.windowtitle",
101         "main.class",
102         "manifest.file",
103         "meta.inf",
104         "platform.active",
105         "resource.dir",
106         "run.classpath",
107         "run.jvmargs",
108         "run.test.classpath",
109         "source.root",
110         "src.dir",
111         "test.src.dir"
112     };
113     
114     private static final String JavaDoc[] createdPropertiesExtSources = {
115         "build.classes.dir",
116         "build.classes.excludes",
117         "build.dir",
118         "build.ear.classes.dir",
119         "build.generated.dir",
120         "build.sysclasspath",
121         "build.test.classes.dir",
122         "build.test.results.dir",
123         "debug.classpath",
124         "debug.test.classpath",
125         "dist.dir",
126         "dist.ear.jar",
127         "dist.jar",
128         "dist.javadoc.dir",
129         "j2ee.appclient.mainclass.args",
130         "j2ee.platform",
131         "j2ee.server.type",
132         "jar.compress",
133         "jar.name",
134         "javac.classpath",
135         "javac.compilerargs",
136         "javac.deprecation",
137         "javac.source",
138         "javac.target",
139         "javac.test.classpath",
140         "javadoc.additionalparam",
141         "javadoc.author",
142         "javadoc.encoding",
143         "javadoc.noindex",
144         "javadoc.nonavbar",
145         "javadoc.notree",
146         "javadoc.private",
147         "javadoc.splitindex",
148         "javadoc.use",
149         "javadoc.version",
150         "javadoc.windowtitle",
151         "manifest.file",
152         "meta.inf",
153         "platform.active",
154         "resource.dir",
155         "run.classpath",
156         "run.jvmargs",
157         "run.test.classpath",
158         //"source.root",
159
"src.dir",
160         "test.src.dir"
161     };
162     
163     public AppClientProjectGeneratorTest(String JavaDoc name) {
164         super(name);
165     }
166     
167     protected void setUp() throws Exception JavaDoc {
168         super.setUp();
169         TestUtil.makeScratchDir(this);
170         serverID = TestUtil.registerSunAppServer(this);
171     }
172     
173     public void testCreateProject() throws Exception JavaDoc {
174         File JavaDoc root = new File JavaDoc(getWorkDir(), "projects");
175         File JavaDoc proj = new File JavaDoc(root, "TestCreateACProject");
176         AntProjectHelper aph = AppClientProjectGenerator.createProject(proj, "test-project",
177                 "test.MyMain", J2eeModule.JAVA_EE_5, serverID);
178         assertNotNull(aph);
179         FileObject fo = aph.getProjectDirectory();
180         for (int i=0; i<createdFiles.length; i++) {
181             assertNotNull(createdFiles[i]+" file/folder cannot be found", fo.getFileObject(createdFiles[i]));
182         }
183         EditableProperties props = aph.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
184         @SuppressWarnings JavaDoc("unchecked")
185         List JavaDoc<Object JavaDoc> l = new ArrayList JavaDoc<Object JavaDoc>(props.keySet());
186         for (int i=0; i<createdProperties.length; i++) {
187             assertNotNull(createdProperties[i]+" property cannot be found in project.properties", props.getProperty(createdProperties[i]));
188             l.remove(createdProperties[i]);
189         }
190         assertEquals("Found unexpected property: "+l,createdProperties.length, props.keySet().size());
191     }
192     
193     public void testImportProject() throws Exception JavaDoc {
194         File JavaDoc root = new File JavaDoc(getWorkDir(), "projects");
195         File JavaDoc proj = new File JavaDoc(root, "ProjectDir");
196         File JavaDoc rootToImport = TestUtil.copyFolder(getWorkDir(), new File JavaDoc(getDataDir(), "projects/importTest"));
197         File JavaDoc srcRoot = new File JavaDoc(rootToImport, "src/java");
198         File JavaDoc confRoot = new File JavaDoc(rootToImport, "src/conf");
199         File JavaDoc testRoot = new File JavaDoc(proj, "test");
200         AntProjectHelper helper = AppClientProjectGenerator.importProject(proj,
201                 "test-project-ext-src", new File JavaDoc[] {srcRoot}, new File JavaDoc[] {testRoot},
202                 confRoot, null, J2eeModule.JAVA_EE_5, serverID);
203         assertNotNull(helper);
204         FileObject importedDirFO = FileUtil.toFileObject(proj);
205         for (int i=0; i<createdFilesExtSources.length; i++) {
206             assertNotNull(createdFilesExtSources[i]+" file/folder cannot be found", importedDirFO.getFileObject(createdFilesExtSources[i]));
207         }
208         assertNotNull("MANIFEST.MF was created", new File JavaDoc(confRoot, "MANIFEST.MF"));
209         EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
210         @SuppressWarnings JavaDoc("unchecked")
211         List JavaDoc<Object JavaDoc> l = new ArrayList JavaDoc<Object JavaDoc>(props.keySet());
212         int extFileRefCount = 0;
213         for (int i=0; i<createdPropertiesExtSources.length; i++) {
214             String JavaDoc propName = createdPropertiesExtSources[i];
215             String JavaDoc propValue = props.getProperty(propName);
216             assertNotNull(propName+" property cannot be found in project.properties", propValue);
217             l.remove(propName);
218             if ("manifest.file".equals(propName)) {
219                 assertEquals("Invalid value of manifest.file property.", "${meta.inf}/MANIFEST.MF", propValue);
220             } else if ("src.dir".equals (propName)) {
221                 PropertyEvaluator eval = helper.getStandardPropertyEvaluator();
222                 //Remove the file.reference to the source.dir, it is implementation detail
223
//depending on the presence of the AlwaysRelativeCollocationQuery
224
assertTrue("Value of the external source dir should be file reference",propValue.startsWith("${file.reference."));
225                 if (l.remove (propValue.subSequence(2,propValue.length()-1))) {
226                     extFileRefCount++;
227                 }
228                 File JavaDoc file = helper.resolveFile(eval.evaluate(propValue));
229                 assertEquals("Invalid value of src.dir property.", srcRoot, file);
230             } else if ("test.src.dir".equals(propName)) {
231                 PropertyEvaluator eval = helper.getStandardPropertyEvaluator();
232                 //Remove the file.reference to the source.dir, it is implementation detail
233
//depending on the presence of the AlwaysRelativeCollocationQuery
234
assertTrue("Value of the external test dir should be file reference",propValue.startsWith("${file.reference."));
235                 if (l.remove (propValue.subSequence(2,propValue.length()-1))) {
236                     extFileRefCount++;
237                 }
238                 File JavaDoc file = helper.resolveFile(eval.evaluate(propValue));
239                 assertEquals("Invalid value of test.src.dir property.", testRoot, file);
240             }
241         }
242         assertEquals("Found unexpected property: "+l,createdPropertiesExtSources.length, props.keySet().size() - extFileRefCount);
243     }
244     
245     public void testSetPlatform() throws Exception JavaDoc {
246         File JavaDoc root = new File JavaDoc(getWorkDir(), "projects");
247         File JavaDoc proj = new File JavaDoc(root, "ImportProjectsNBDir");
248         File JavaDoc importRoot = new File JavaDoc(getDataDir(), "projects/importTest");
249         File JavaDoc srcRoot = new File JavaDoc(importRoot, "src/java");
250         File JavaDoc confRoot = new File JavaDoc(importRoot, "src/conf");
251         File JavaDoc libDir = new File JavaDoc(root, "libs");
252         libDir.mkdirs();
253         AntProjectHelper helper = AppClientProjectGenerator.importProject(proj,
254                 "test-project-ext-src2", new File JavaDoc[] {srcRoot}, new File JavaDoc[] {},
255                 confRoot, libDir, J2eeModule.JAVA_EE_5, serverID);
256         assertNotNull(helper);
257         EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
258         assertEquals("default_platform", ep.getProperty("platform.active"));
259         assertEquals("${default.javac.source}", ep.getProperty("javac.source"));
260         assertEquals("${default.javac.target}", ep.getProperty("javac.target"));
261         AppClientProjectGenerator.setPlatform(helper, "ExplicitPlatform", "1.4");
262         ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
263         assertEquals("ExplicitPlatform", ep.getProperty("platform.active"));
264         assertEquals("1.4", ep.getProperty("javac.source"));
265         assertEquals("1.4", ep.getProperty("javac.target"));
266     }
267     
268 }
269
Popular Tags