KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > NbModuleProjectGeneratorTest


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.apisupport.project;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import java.util.jar.Manifest JavaDoc;
32 import org.netbeans.api.project.Project;
33 import org.netbeans.api.project.ProjectManager;
34 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
35 import org.netbeans.modules.apisupport.project.suite.SuiteProjectGenerator;
36 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
37 import org.netbeans.spi.project.SubprojectProvider;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileUtil;
40
41 /**
42  * NbModuleProjectGenerator tests.
43  *
44  * @author Martin Krauskopf, Jesse Glick
45  */

46 public class NbModuleProjectGeneratorTest extends TestBase {
47     // TODO test suite module and also NetBeans CVS tree modules
48
// XXX also should test content of created files (XMLs, properties)
49

50     public NbModuleProjectGeneratorTest(String JavaDoc testName) {
51         super(testName);
52     }
53     
54     private static final String JavaDoc[] BASIC_CREATED_FILES = {
55         "build.xml",
56         "manifest.mf",
57         "nbproject/project.xml",
58         "nbproject/build-impl.xml",
59         "src/org/example/testModule/resources/Bundle.properties",
60         "src/org/example/testModule/resources/layer.xml",
61         "test/unit/src",
62     };
63     
64     private static final String JavaDoc[] STANDALONE_CREATED_FILES = {
65         "nbproject/platform.properties",
66     };
67     
68     private static final String JavaDoc[] SUITE_COMP_REL_CREATED_FILES = {
69         "nbproject/suite.properties",
70     };
71     
72     public void testCreateStandAloneModule() throws Exception JavaDoc {
73         File JavaDoc targetPrjDir = new File JavaDoc(getWorkDir(), "testModule");
74         NbModuleProjectGenerator.createStandAloneModule(
75                 targetPrjDir,
76                 "org.example.testModule", // cnb
77
"Testing Module", // display name
78
"org/example/testModule/resources/Bundle.properties",
79                 "org/example/testModule/resources/layer.xml",
80                 NbPlatform.PLATFORM_ID_DEFAULT); // platform id
81
FileObject fo = FileUtil.toFileObject(targetPrjDir);
82         // Make sure generated files are created too - simulate project opening.
83
NbModuleProject p = (NbModuleProject) ProjectManager.getDefault().findProject(fo);
84         assertNotNull("have a project in " + targetPrjDir, p);
85         p.open();
86         // check generated module
87
for (int i=0; i < BASIC_CREATED_FILES.length; i++) {
88             assertNotNull(BASIC_CREATED_FILES[i]+" file/folder cannot be found",
89                     fo.getFileObject(BASIC_CREATED_FILES[i]));
90         }
91         for (int i=0; i < STANDALONE_CREATED_FILES.length; i++) {
92             assertNotNull(STANDALONE_CREATED_FILES[i]+" file/folder cannot be found",
93                     fo.getFileObject(STANDALONE_CREATED_FILES[i]));
94         }
95     }
96     
97     public void testCreateSuiteComponentModule() throws Exception JavaDoc {
98         // create suite for the module being tested
99
File JavaDoc suiteDir = new File JavaDoc(getWorkDir(), "testSuite");
100         SuiteProjectGenerator.createSuiteProject(suiteDir, NbPlatform.PLATFORM_ID_DEFAULT);
101         FileObject fo = FileUtil.toFileObject(suiteDir);
102         Project suiteProject = ProjectManager.getDefault().findProject(fo);
103         assertNotNull("have a project in " + suiteDir, suiteProject);
104         SubprojectProvider spp = (SubprojectProvider) suiteProject.getLookup().lookup(SubprojectProvider.class);
105         assertNotNull("has a SubprojectProvider", spp);
106         
107         // create "relative" module in suite
108
File JavaDoc targetPrjDir = new File JavaDoc(suiteDir, "testModuleRel");
109         NbModuleProjectGenerator.createSuiteComponentModule(
110                 targetPrjDir,
111                 "org.example.testModuleRel", // cnb
112
"Testing Module", // display name
113
"org/example/testModule/resources/Bundle.properties",
114                 "org/example/testModule/resources/layer.xml",
115                 suiteDir); // platform id
116
fo = FileUtil.toFileObject(targetPrjDir);
117         // Make sure generated files are created too - simulate project opening.
118
NbModuleProject moduleProjectRel = (NbModuleProject) ProjectManager.getDefault().findProject(fo);
119         assertNotNull("have a project in " + targetPrjDir, moduleProjectRel);
120         moduleProjectRel.open();
121         // check generated module
122
for (int i=0; i < BASIC_CREATED_FILES.length; i++) {
123             assertNotNull(BASIC_CREATED_FILES[i]+" file/folder cannot be found",
124                     fo.getFileObject(BASIC_CREATED_FILES[i]));
125         }
126         for (int i=0; i < SUITE_COMP_REL_CREATED_FILES.length; i++) {
127             assertNotNull(SUITE_COMP_REL_CREATED_FILES[i]+" file/folder cannot be found",
128                     fo.getFileObject(SUITE_COMP_REL_CREATED_FILES[i]));
129         }
130         assertEquals("listed as the sole suite component", Collections.singleton(moduleProjectRel), spp.getSubprojects());
131         
132         // create "absolute" module in suite
133
targetPrjDir = new File JavaDoc(getWorkDir(), "testModuleAbs");
134         NbModuleProjectGenerator.createSuiteComponentModule(
135                 targetPrjDir,
136                 "org.example.testModuleAbs", // cnb
137
"Testing Module", // display name
138
"org/example/testModule/resources/Bundle.properties",
139                 "org/example/testModule/resources/layer.xml",
140                 suiteDir); // platform id
141
fo = FileUtil.toFileObject(targetPrjDir);
142         // Make sure generated files are created too - simulate project opening.
143
NbModuleProject moduleProjectAbs = (NbModuleProject) ProjectManager.getDefault().findProject(fo);
144         assertNotNull("have a project in " + targetPrjDir, moduleProjectAbs);
145         moduleProjectAbs.open();
146         // check generated module
147
for (int i=0; i < BASIC_CREATED_FILES.length; i++) {
148             assertNotNull(BASIC_CREATED_FILES[i]+" file/folder cannot be found",
149                     fo.getFileObject(BASIC_CREATED_FILES[i]));
150         }
151         assertEquals("now have two suite components", new HashSet JavaDoc(Arrays.asList(new Project[] {moduleProjectRel, moduleProjectAbs})), spp.getSubprojects());
152     }
153     
154     public void testCreateSuiteLibraryModule() throws Exception JavaDoc {
155         Map JavaDoc/*<String,String>*/ contents = new HashMap JavaDoc();
156         contents.put("lib/pkg/Clazz3.class", "");
157         contents.put("lib/pkg2/Clazz4.class", "");
158         contents.put("1.0/oldlib/Clazz5.class", ""); // #72669
159
File JavaDoc jar = new File JavaDoc(getWorkDir(), "some.jar");
160         createJar(jar, contents, new Manifest JavaDoc());
161         SuiteProject sweet = generateSuite("sweet");
162         File JavaDoc moduleDir = new File JavaDoc(getWorkDir(), "module");
163         NbModuleProjectGenerator.createSuiteLibraryModule(
164                 moduleDir, "module", "Module", "module/Bundle.properties",
165                 sweet.getProjectDirectoryFile(), null, new File JavaDoc[] {jar});
166         NbModuleProject p = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(moduleDir));
167         ManifestManager.PackageExport[] exports = new ProjectXMLManager(p).getPublicPackages();
168         Set JavaDoc/*<String>*/ packages = new TreeSet JavaDoc();
169         for (int i = 0; i < exports.length; i++) {
170             assertFalse(exports[i].isRecursive());
171             packages.add(exports[i].getPackage());
172         }
173         assertEquals(Arrays.asList(new String JavaDoc[] {"lib.pkg", "lib.pkg2"}), new ArrayList JavaDoc(packages));
174     }
175     
176     // XXX hmmm, don't know yet how to fully test this case since I don't want
177
// to touch the netbeans.org CVS tree. Probably somehow simulating
178
// netbeans.org CVS tree would help. I'll try to investigate it later.
179
// public void testCreateNetBeansModule() throws Exception {
180
// File prjDir = new File("/usr/share/java/netbeans-cvs-current/ide/projectimport/testModule");
181
// NbModuleProjectGenerator.createNetBeansOrgModule(
182
// prjDir,
183
// "org.example.testModule", // cnb
184
// "Testing Module", // display name
185
// "org/example/testModule/resources/Bundle.properties",
186
// "org/example/testModule/resources/layer.xml");
187
// }
188

189 }
190
Popular Tags