001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project;
043:
044: import java.io.File;
045: import java.util.ArrayList;
046: import java.util.Arrays;
047: import java.util.Collections;
048: import java.util.HashMap;
049: import java.util.HashSet;
050: import java.util.Map;
051: import java.util.Set;
052: import java.util.TreeSet;
053: import java.util.jar.Manifest;
054: import org.netbeans.api.project.Project;
055: import org.netbeans.api.project.ProjectManager;
056: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
057: import org.netbeans.modules.apisupport.project.suite.SuiteProjectGenerator;
058: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
059: import org.netbeans.spi.project.SubprojectProvider;
060: import org.openide.filesystems.FileObject;
061: import org.openide.filesystems.FileUtil;
062:
063: /**
064: * NbModuleProjectGenerator tests.
065: *
066: * @author Martin Krauskopf, Jesse Glick
067: */
068: public class NbModuleProjectGeneratorTest extends TestBase {
069: // TODO test suite module and also NetBeans source tree modules
070: // XXX also should test content of created files (XMLs, properties)
071:
072: public NbModuleProjectGeneratorTest(String testName) {
073: super (testName);
074: }
075:
076: private static final String[] BASIC_CREATED_FILES = { "build.xml",
077: "manifest.mf", "nbproject/project.xml",
078: "nbproject/build-impl.xml",
079: "src/org/example/testModule/resources/Bundle.properties",
080: "src/org/example/testModule/resources/layer.xml",
081: "test/unit/src", };
082:
083: private static final String[] STANDALONE_CREATED_FILES = { "nbproject/platform.properties", };
084:
085: private static final String[] SUITE_COMP_REL_CREATED_FILES = { "nbproject/suite.properties", };
086:
087: public void testCreateStandAloneModule() throws Exception {
088: File targetPrjDir = new File(getWorkDir(), "testModule");
089: NbModuleProjectGenerator.createStandAloneModule(
090: targetPrjDir,
091: "org.example.testModule", // cnb
092: "Testing Module", // display name
093: "org/example/testModule/resources/Bundle.properties",
094: "org/example/testModule/resources/layer.xml",
095: NbPlatform.PLATFORM_ID_DEFAULT); // platform id
096: FileObject fo = FileUtil.toFileObject(targetPrjDir);
097: // Make sure generated files are created too - simulate project opening.
098: NbModuleProject p = (NbModuleProject) ProjectManager
099: .getDefault().findProject(fo);
100: assertNotNull("have a project in " + targetPrjDir, p);
101: p.open();
102: // check generated module
103: for (int i = 0; i < BASIC_CREATED_FILES.length; i++) {
104: assertNotNull(BASIC_CREATED_FILES[i]
105: + " file/folder cannot be found", fo
106: .getFileObject(BASIC_CREATED_FILES[i]));
107: }
108: for (int i = 0; i < STANDALONE_CREATED_FILES.length; i++) {
109: assertNotNull(STANDALONE_CREATED_FILES[i]
110: + " file/folder cannot be found", fo
111: .getFileObject(STANDALONE_CREATED_FILES[i]));
112: }
113: }
114:
115: public void testCreateSuiteComponentModule() throws Exception {
116: // create suite for the module being tested
117: File suiteDir = new File(getWorkDir(), "testSuite");
118: SuiteProjectGenerator.createSuiteProject(suiteDir,
119: NbPlatform.PLATFORM_ID_DEFAULT);
120: FileObject fo = FileUtil.toFileObject(suiteDir);
121: Project suiteProject = ProjectManager.getDefault().findProject(
122: fo);
123: assertNotNull("have a project in " + suiteDir, suiteProject);
124: SubprojectProvider spp = suiteProject.getLookup().lookup(
125: SubprojectProvider.class);
126: assertNotNull("has a SubprojectProvider", spp);
127:
128: // create "relative" module in suite
129: File targetPrjDir = new File(suiteDir, "testModuleRel");
130: NbModuleProjectGenerator.createSuiteComponentModule(
131: targetPrjDir,
132: "org.example.testModuleRel", // cnb
133: "Testing Module", // display name
134: "org/example/testModule/resources/Bundle.properties",
135: "org/example/testModule/resources/layer.xml", suiteDir); // platform id
136: fo = FileUtil.toFileObject(targetPrjDir);
137: // Make sure generated files are created too - simulate project opening.
138: NbModuleProject moduleProjectRel = (NbModuleProject) ProjectManager
139: .getDefault().findProject(fo);
140: assertNotNull("have a project in " + targetPrjDir,
141: moduleProjectRel);
142: moduleProjectRel.open();
143: // check generated module
144: for (int i = 0; i < BASIC_CREATED_FILES.length; i++) {
145: assertNotNull(BASIC_CREATED_FILES[i]
146: + " file/folder cannot be found", fo
147: .getFileObject(BASIC_CREATED_FILES[i]));
148: }
149: for (int i = 0; i < SUITE_COMP_REL_CREATED_FILES.length; i++) {
150: assertNotNull(SUITE_COMP_REL_CREATED_FILES[i]
151: + " file/folder cannot be found", fo
152: .getFileObject(SUITE_COMP_REL_CREATED_FILES[i]));
153: }
154: assertEquals("listed as the sole suite component", Collections
155: .singleton(moduleProjectRel), spp.getSubprojects());
156:
157: // create "absolute" module in suite
158: targetPrjDir = new File(getWorkDir(), "testModuleAbs");
159: NbModuleProjectGenerator.createSuiteComponentModule(
160: targetPrjDir,
161: "org.example.testModuleAbs", // cnb
162: "Testing Module", // display name
163: "org/example/testModule/resources/Bundle.properties",
164: "org/example/testModule/resources/layer.xml", suiteDir); // platform id
165: fo = FileUtil.toFileObject(targetPrjDir);
166: // Make sure generated files are created too - simulate project opening.
167: NbModuleProject moduleProjectAbs = (NbModuleProject) ProjectManager
168: .getDefault().findProject(fo);
169: assertNotNull("have a project in " + targetPrjDir,
170: moduleProjectAbs);
171: moduleProjectAbs.open();
172: // check generated module
173: for (int i = 0; i < BASIC_CREATED_FILES.length; i++) {
174: assertNotNull(BASIC_CREATED_FILES[i]
175: + " file/folder cannot be found", fo
176: .getFileObject(BASIC_CREATED_FILES[i]));
177: }
178: assertEquals("now have two suite components",
179: new HashSet<Project>(Arrays.asList(moduleProjectRel,
180: moduleProjectAbs)), spp.getSubprojects());
181: }
182:
183: public void testCreateSuiteLibraryModule() throws Exception {
184: Map<String, String> contents = new HashMap<String, String>();
185: contents.put("lib/pkg/Clazz3.class", "");
186: contents.put("lib/pkg2/Clazz4.class", "");
187: contents.put("1.0/oldlib/Clazz5.class", ""); // #72669
188: File jar = new File(getWorkDir(), "some.jar");
189: createJar(jar, contents, new Manifest());
190: SuiteProject sweet = generateSuite("sweet");
191: File moduleDir = new File(getWorkDir(), "module");
192: NbModuleProjectGenerator.createSuiteLibraryModule(moduleDir,
193: "module", "Module", "module/Bundle.properties", sweet
194: .getProjectDirectoryFile(), null,
195: new File[] { jar });
196: NbModuleProject p = (NbModuleProject) ProjectManager
197: .getDefault().findProject(
198: FileUtil.toFileObject(moduleDir));
199: Set<String> packages = new TreeSet<String>();
200: for (ManifestManager.PackageExport export : new ProjectXMLManager(
201: p).getPublicPackages()) {
202: assertFalse(export.isRecursive());
203: packages.add(export.getPackage());
204: }
205: assertEquals(Arrays.asList("lib.pkg", "lib.pkg2"),
206: new ArrayList<String>(packages));
207: }
208:
209: // XXX hmmm, don't know yet how to fully test this case since I don't want
210: // to touch the netbeans.org source tree. Probably somehow simulating
211: // netbeans.org source tree would help. I'll try to investigate it later.
212: // public void testCreateNetBeansModule() throws Exception {
213: // File prjDir = new File("/usr/share/java/netbeans-cvs-current/ide/projectimport/testModule");
214: // NbModuleProjectGenerator.createNetBeansOrgModule(
215: // prjDir,
216: // "org.example.testModule", // cnb
217: // "Testing Module", // display name
218: // "org/example/testModule/resources/Bundle.properties",
219: // "org/example/testModule/resources/layer.xml");
220: // }
221:
222: }
|