KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > J2SEProjectGeneratorTest


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.java.j2seproject;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import org.netbeans.junit.NbTestCase;
25 import org.netbeans.spi.project.support.ant.AntProjectHelper;
26 import org.netbeans.spi.project.support.ant.EditableProperties;
27 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.modules.SpecificationVersion;
31
32 /**
33  * Tests for J2SEProjectGenerator
34  *
35  * @author David Konecny
36  */

37 public class J2SEProjectGeneratorTest extends NbTestCase {
38     
39     public J2SEProjectGeneratorTest(String JavaDoc testName) {
40         super(testName);
41     }
42
43     private static final String JavaDoc[] createdFiles = {
44         "build.xml",
45         "nbproject/build-impl.xml",
46         "nbproject/project.xml",
47         "nbproject/project.properties",
48         "src",
49         "test",
50     };
51     
52     private static final String JavaDoc[] createdFilesExtSources = {
53         "build.xml",
54         "nbproject/build-impl.xml",
55         "nbproject/project.xml",
56         "nbproject/project.properties",
57         "nbproject/private/private.properties",
58     };
59
60     private static final String JavaDoc[] createdProperties = {
61         "build.classes.dir",
62         "build.classes.excludes",
63         "build.dir",
64         "build.generated.dir",
65         "build.sysclasspath",
66         "build.test.classes.dir",
67         "build.test.results.dir",
68         "debug.classpath",
69         "debug.test.classpath",
70         "dist.dir",
71         "dist.jar",
72         "dist.javadoc.dir",
73         "jar.compress",
74         "javac.classpath",
75         "javac.compilerargs",
76         "javac.deprecation",
77         "javac.source",
78         "javac.target",
79         "javac.test.classpath",
80         "javadoc.author",
81         "javadoc.encoding",
82         "javadoc.noindex",
83         "javadoc.nonavbar",
84         "javadoc.notree",
85         "javadoc.private",
86         "javadoc.splitindex",
87         "javadoc.use",
88         "javadoc.version",
89         "javadoc.windowtitle",
90         "javadoc.additionalparam",
91         "main.class",
92         "manifest.file",
93         "meta.inf.dir",
94         "platform.active",
95         "run.classpath",
96         "run.jvmargs",
97         "run.test.classpath",
98         "src.dir",
99         "test.src.dir",
100     };
101
102     private static final String JavaDoc[] createdPropertiesExtSources = {
103         "build.classes.dir",
104         "build.classes.excludes",
105         "build.dir",
106         "build.generated.dir",
107         "build.sysclasspath",
108         "build.test.classes.dir",
109         "build.test.results.dir",
110         "debug.classpath",
111         "debug.test.classpath",
112         "dist.dir",
113         "dist.jar",
114         "dist.javadoc.dir",
115         "jar.compress",
116         "javac.classpath",
117         "javac.compilerargs",
118         "javac.deprecation",
119         "javac.source",
120         "javac.target",
121         "javac.test.classpath",
122         "javadoc.author",
123         "javadoc.encoding",
124         "javadoc.noindex",
125         "javadoc.nonavbar",
126         "javadoc.notree",
127         "javadoc.private",
128         "javadoc.splitindex",
129         "javadoc.use",
130         "javadoc.version",
131         "javadoc.windowtitle",
132         "javadoc.additionalparam",
133         "main.class",
134         "manifest.file",
135         "meta.inf.dir",
136         "platform.active",
137         "run.classpath",
138         "run.jvmargs",
139         "run.test.classpath",
140         "src.dir",
141         "test.src.dir",
142     };
143
144     public void testCreateProject() throws Exception JavaDoc {
145         File JavaDoc proj = getWorkDir();
146         clearWorkDir();
147         J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); //NOI18N
148
AntProjectHelper aph = J2SEProjectGenerator.createProject(proj, "test-project", null, "manifest.mf");
149         J2SEProjectGenerator.setDefaultSourceLevel(null);
150         assertNotNull(aph);
151         FileObject fo = aph.getProjectDirectory();
152         for (int i=0; i<createdFiles.length; i++) {
153             assertNotNull(createdFiles[i]+" file/folder cannot be found", fo.getFileObject(createdFiles[i]));
154         }
155         EditableProperties props = aph.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
156         ArrayList JavaDoc l = new ArrayList JavaDoc(props.keySet());
157         for (int i=0; i<createdProperties.length; i++) {
158             assertNotNull(createdProperties[i]+" property cannot be found in project.properties", props.getProperty(createdProperties[i]));
159             l.remove(createdProperties[i]);
160         }
161         assertEquals("Found unexpected property: "+l,createdProperties.length, props.keySet().size());
162     }
163     
164     public void testCreateProjectFromExtSources () throws Exception JavaDoc {
165         File JavaDoc root = getWorkDir();
166         clearWorkDir();
167         File JavaDoc proj = new File JavaDoc (root, "ProjectDir");
168         proj.mkdir();
169         File JavaDoc srcRoot = new File JavaDoc (root, "src");
170         srcRoot.mkdir ();
171         File JavaDoc testRoot = new File JavaDoc (root, "test");
172         testRoot.mkdir ();
173         J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); //NOI18N
174
AntProjectHelper helper = J2SEProjectGenerator.createProject(proj, "test-project-ext-src", new File JavaDoc[] {srcRoot}, new File JavaDoc[] {testRoot}, "manifest.mf");
175         J2SEProjectGenerator.setDefaultSourceLevel(null); //NOI18N
176
assertNotNull (helper);
177         FileObject fo = FileUtil.toFileObject(proj);
178         for (int i=0; i<createdFilesExtSources.length; i++) {
179             assertNotNull(createdFilesExtSources[i]+" file/folder cannot be found", fo.getFileObject(createdFilesExtSources[i]));
180         }
181         EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
182         ArrayList JavaDoc l = new ArrayList JavaDoc(props.keySet());
183         int extFileRefCount = 0;
184         for (int i=0; i<createdPropertiesExtSources.length; i++) {
185             String JavaDoc propName = createdPropertiesExtSources[i];
186             String JavaDoc propValue = props.getProperty(propName);
187             assertNotNull(propName+" property cannot be found in project.properties", propValue);
188             l.remove(propName);
189             if ("manifest.file".equals (propName)) {
190                 assertEquals("Invalid value of manifest.file property.", "manifest.mf", propValue);
191             }
192             else if ("src.dir".equals (propName)) {
193                 PropertyEvaluator eval = helper.getStandardPropertyEvaluator();
194                 //Remove the file.reference to the source.dir, it is implementation detail
195
//depending on the presence of the AlwaysRelativeCollocationQuery
196
assertTrue("Value of the external source dir should be file reference",propValue.startsWith("${file.reference."));
197                 if (l.remove (propValue.subSequence(2,propValue.length()-1))) {
198                     extFileRefCount++;
199                 }
200                 File JavaDoc file = helper.resolveFile(eval.evaluate(propValue));
201                 assertEquals("Invalid value of src.dir property.", srcRoot, file);
202             }
203             else if ("test.src.dir".equals(propName)) {
204                 PropertyEvaluator eval = helper.getStandardPropertyEvaluator();
205                 //Remove the file.reference to the source.dir, it is implementation detail
206
//depending on the presence of the AlwaysRelativeCollocationQuery
207
assertTrue("Value of the external test dir should be file reference",propValue.startsWith("${file.reference."));
208                 if (l.remove (propValue.subSequence(2,propValue.length()-1))) {
209                     extFileRefCount++;
210                 }
211                 File JavaDoc file = helper.resolveFile(eval.evaluate(propValue));
212                 assertEquals("Invalid value of test.src.dir property.", testRoot, file);
213             }
214         }
215         assertEquals("Found unexpected property: "+l,createdPropertiesExtSources.length, props.keySet().size() - extFileRefCount);
216     }
217     
218 }
219
Popular Tags