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.BufferedReader;
045: import java.io.File;
046: import java.io.FileNotFoundException;
047: import java.io.FileOutputStream;
048: import java.io.FileReader;
049: import java.io.OutputStream;
050: import java.io.PrintWriter;
051: import java.net.URL;
052: import java.util.Collections;
053: import java.util.HashMap;
054: import java.util.Locale;
055: import java.util.Map;
056: import java.util.SortedSet;
057: import java.util.TreeSet;
058: import java.util.jar.Manifest;
059: import org.netbeans.api.project.ProjectManager;
060: import org.netbeans.modules.apisupport.project.ui.customizer.ModuleDependency;
061: import org.netbeans.modules.apisupport.project.universe.LocalizedBundleInfo;
062: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
063: import org.netbeans.spi.project.support.ant.AntProjectHelper;
064: import org.netbeans.spi.project.support.ant.EditableProperties;
065: import org.openide.filesystems.FileObject;
066: import org.openide.filesystems.FileUtil;
067:
068: /**
069: * Tests {@link Util}.
070: *
071: * @author Martin Krauskopf
072: */
073: public class UtilTest extends TestBase {
074:
075: public UtilTest(String name) {
076: super (name);
077: }
078:
079: public void testNormalizeCNB() throws Exception {
080: assertEquals("space test", "spacetest", Util
081: .normalizeCNB("space test"));
082: assertEquals("slash test", "slashtest", Util
083: .normalizeCNB("slash\\test"));
084: assertEquals("lowercase test", "org.capital.test", Util
085: .normalizeCNB("org.Capital.test"));
086: assertEquals("dot-space test", "org.example.package", Util
087: .normalizeCNB("org...example ... package..."));
088: assertEquals("org.example.hmmmm.misc.test339", Util
089: .normalizeCNB("org.example.hmMMm.misc. TEst3*3=9"));
090: }
091:
092: public void testFindLocalizedBundleInfoFromNetBeansOrgModule()
093: throws Exception {
094: FileObject dir = nbRoot().getFileObject("apisupport.project");
095: assertNotNull("have apisupport.project checked out", dir);
096: NbModuleProject apisupport = (NbModuleProject) ProjectManager
097: .getDefault().findProject(dir);
098: LocalizedBundleInfo info = Util.findLocalizedBundleInfo(
099: apisupport.getSourceDirectory(), apisupport
100: .getManifest());
101: assertApiSupportInfo(info);
102: }
103:
104: public void testFindLocalizedBundleInfoFromSourceDirectory()
105: throws Exception {
106: LocalizedBundleInfo info = Util
107: .findLocalizedBundleInfo(file("apisupport.project"));
108: assertApiSupportInfo(info);
109: }
110:
111: public void testFindLocalizedBundleInfoFromSourceDirectory1()
112: throws Exception {
113: LocalizedBundleInfo info = Util
114: .findLocalizedBundleInfo(resolveEEPFile("suite3/dummy-project"));
115: assertNull(info);
116: }
117:
118: public void testFindLocalizedBundleInfoFromBinaryModule()
119: throws Exception {
120: File apisupportF = file("nbbuild/netbeans/"
121: + TestBase.CLUSTER_APISUPPORT
122: + "/modules/org-netbeans-modules-apisupport-project.jar");
123: assertApiSupportInfo(Util
124: .findLocalizedBundleInfoFromJAR(apisupportF));
125: }
126:
127: private void assertApiSupportInfo(LocalizedBundleInfo info) {
128: assertNotNull("info loaded", info);
129: // XXX ignore this for now, but be careful when editing the module's properties :)
130: assertEquals("display name", "NetBeans Module Projects", info
131: .getDisplayName());
132: /* Too fragile:
133: assertEquals("category", "Developing NetBeans", info.getCategory());
134: assertEquals("short description", "Defines an Ant-based project type for NetBeans modules.", info.getShortDescription());
135: assertEquals("long description", "Defines a project type for NetBeans " +
136: "modules, useful for developing plug-in extensions to NetBeans. " +
137: "Provides the logical view for modules, supplies the classpath " +
138: "used for code completion, integrates with the NetBeans build " +
139: "system (using Ant), etc.", info.getLongDescription());
140: */
141: }
142:
143: /** cf. #64782 */
144: public void testFindLocalizedBundleInfoLocalization()
145: throws Exception {
146: Locale orig = Locale.getDefault();
147: Locale.setDefault(Locale.JAPAN);
148: try {
149: clearWorkDir();
150: File dir = getWorkDir();
151: Manifest mani = new Manifest();
152: mani.getMainAttributes().putValue(
153: "OpenIDE-Module-Localizing-Bundle",
154: "pack/age/Bundle.properties");
155: // Start with an unlocalized source project.
156: File src = new File(dir, "src");
157: File f = new File(src, "pack/age/Bundle.properties"
158: .replace('/', File.separatorChar));
159: f.getParentFile().mkdirs();
160: TestBase
161: .dump(
162: f,
163: "OpenIDE-Module-Name=Foo\nOpenIDE-Module-Display-Category=Foo Stuff\nOpenIDE-Module-Short-Description=short\nOpenIDE-Module-Long-Description=Long...");
164: // XXX test also Util.findLocalizedBundleInfo(File)?
165: LocalizedBundleInfo info = Util.findLocalizedBundleInfo(
166: FileUtil.toFileObject(src), mani);
167: assertEquals("Foo", info.getDisplayName());
168: assertEquals("Foo Stuff", info.getCategory());
169: assertEquals("short", info.getShortDescription());
170: assertEquals("Long...", info.getLongDescription());
171: // Now add some locale variants.
172: f = new File(src, "pack/age/Bundle_ja.properties".replace(
173: '/', File.separatorChar));
174: TestBase
175: .dump(f,
176: "OpenIDE-Module-Long-Description=Long Japanese text...");
177: f = new File(src, "pack/age/Bundle_ja_JP.properties"
178: .replace('/', File.separatorChar));
179: TestBase.dump(f, "OpenIDE-Module-Name=Foo Nihon");
180: info = Util.findLocalizedBundleInfo(FileUtil
181: .toFileObject(src), mani);
182: assertEquals("Foo Nihon", info.getDisplayName());
183: assertEquals("Foo Stuff", info.getCategory());
184: assertEquals("short", info.getShortDescription());
185: assertEquals("Long Japanese text...", info
186: .getLongDescription());
187: // Now try it on JAR files.
188: f = new File(dir, "noloc.jar");
189: createJar(f, Collections.singletonMap(
190: "pack/age/Bundle.properties",
191: "OpenIDE-Module-Name=Foo"), mani);
192: info = Util.findLocalizedBundleInfoFromJAR(f);
193: assertEquals("Foo", info.getDisplayName());
194: assertNull(info.getShortDescription());
195: f = new File(dir, "internalloc.jar");
196: Map<String, String> contents = new HashMap<String, String>();
197: contents
198: .put("pack/age/Bundle.properties",
199: "OpenIDE-Module-Name=Foo\nOpenIDE-Module-Short-Description=short");
200: contents.put("pack/age/Bundle_ja_JP.properties",
201: "OpenIDE-Module-Name=Foo Nihon");
202: createJar(f, contents, mani);
203: info = Util.findLocalizedBundleInfoFromJAR(f);
204: assertEquals("Foo Nihon", info.getDisplayName());
205: assertEquals("short", info.getShortDescription());
206: f = new File(dir, "externalloc.jar");
207: createJar(
208: f,
209: Collections
210: .singletonMap("pack/age/Bundle.properties",
211: "OpenIDE-Module-Name=Foo\nOpenIDE-Module-Short-Description=short"),
212: mani);
213: File f2 = new File(dir, "locale" + File.separatorChar
214: + "externalloc_ja.jar");
215: createJar(f2, Collections.singletonMap(
216: "pack/age/Bundle_ja.properties",
217: "OpenIDE-Module-Short-Description=short Japanese"),
218: new Manifest());
219: info = Util.findLocalizedBundleInfoFromJAR(f);
220: assertEquals("Foo", info.getDisplayName());
221: assertEquals("the meat of #64782", "short Japanese", info
222: .getShortDescription());
223: } finally {
224: Locale.setDefault(orig);
225: }
226: }
227:
228: public void testLoadProperties() throws Exception {
229: File props = file(getWorkDir(), "testing.properties");
230: OutputStream propsOS = new FileOutputStream(props);
231: PrintWriter pw = new PrintWriter(propsOS);
232: try {
233: pw.println("property1=some value");
234: pw.println("property2=other value");
235: } finally {
236: pw.close();
237: }
238: EditableProperties ep = Util.loadProperties(FileUtil
239: .toFileObject(props));
240: assertEquals("property1", "some value", ep
241: .getProperty("property1"));
242: assertEquals("property2", "other value", ep
243: .getProperty("property2"));
244: try {
245: File notFile = file(getWorkDir(), "i_am_not_file");
246: notFile.mkdir();
247: Util.loadProperties(FileUtil.toFileObject(notFile));
248: fail("FileNotFoundException should be thrown");
249: } catch (FileNotFoundException fnfe) {
250: // fine expected exception has been thrown
251: }
252: }
253:
254: public void testStoreProperties() throws Exception {
255: FileObject propsFO = FileUtil.createData(FileUtil
256: .toFileObject(getWorkDir()), "testing.properties");
257: EditableProperties props = Util.loadProperties(propsFO);
258: assertTrue("empty props", props.isEmpty());
259: props.setProperty("property1", "some value");
260: Util.storeProperties(propsFO, props);
261:
262: BufferedReader reader = new BufferedReader(new FileReader(file(
263: getWorkDir(), "testing.properties")));
264: try {
265: assertEquals("stored property", "property1=some value",
266: reader.readLine());
267: } finally {
268: reader.close();
269: }
270: }
271:
272: // XXX testLoadManifest()
273: // XXX testStoreManifest()
274:
275: public void testFindJavadoc() throws Exception {
276: File oneModuleDoc = new File(getWorkDir(),
277: "org-example-module1");
278: assertTrue(oneModuleDoc.mkdir());
279: File index = new File(oneModuleDoc, "index.html");
280: assertTrue(index.createNewFile());
281:
282: NbModuleProject project = generateStandaloneModule("module1");
283: NbPlatform platform = project.getPlatform(false);
284: URL oneModuleDocURL = Util.urlForDir(oneModuleDoc);
285: platform.addJavadocRoot(oneModuleDocURL);
286: ModuleDependency md = new ModuleDependency(project
287: .getModuleList().getEntry(project.getCodeNameBase()));
288:
289: URL url = Util.findJavadoc(md, platform);
290: assertNotNull("url was found", url);
291:
292: File nbDoc = new File(getWorkDir(), "nbDoc");
293: File moduleDoc = new File(nbDoc, "org-example-module1");
294: assertTrue(moduleDoc.mkdirs());
295: index = new File(moduleDoc, "index.html");
296: assertTrue(index.createNewFile());
297:
298: platform.addJavadocRoot(Util.urlForDir(nbDoc));
299: platform.removeJavadocRoots(new URL[] { oneModuleDocURL });
300: url = Util.findJavadoc(md, platform);
301: assertNotNull("url was found", url);
302: }
303:
304: public void testIsValidJavaFQN() throws Exception {
305: assertFalse(Util.isValidJavaFQN("a.b,c"));
306: assertFalse(Util.isValidJavaFQN(""));
307: assertFalse(Util.isValidJavaFQN("a.b.1"));
308: assertTrue(Util.isValidJavaFQN("a"));
309: assertTrue(Util.isValidJavaFQN("a.b.c1"));
310: }
311:
312: public void testIsValidSFSFolderName() throws Exception {
313: assertTrue(Util.isValidSFSPath("a"));
314: assertTrue(Util.isValidSFSPath("a/b/c"));
315: assertTrue(Util.isValidSFSPath("a/b/c_c/"));
316: assertTrue(Util.isValidSFSPath("/a/b/c_c"));
317: assertTrue(Util.isValidSFSPath("a/1a/b/c/1d_d/"));
318: assertTrue(Util.isValidSFSPath("_a/b/c_"));
319: assertFalse(Util.isValidSFSPath("a/b/c/dd+"));
320: assertFalse(Util.isValidSFSPath("a+b"));
321: assertFalse(Util.isValidSFSPath(""));
322: assertFalse(Util.isValidSFSPath(" "));
323: }
324:
325: public void testDisplayName_70363() throws Exception {
326: FileObject prjFO = TestBase.generateStandaloneModuleDirectory(
327: getWorkDir(), "module");
328: FileUtil.moveFile(prjFO.getFileObject("src"), prjFO, "libsrc");
329: FileObject propsFO = FileUtil.createData(prjFO,
330: AntProjectHelper.PROJECT_PROPERTIES_PATH);
331: EditableProperties ep = Util.loadProperties(propsFO);
332: ep.setProperty("src.dir", "libsrc");
333: Util.storeProperties(propsFO, ep);
334: LocalizedBundleInfo info = Util
335: .findLocalizedBundleInfo(FileUtil.toFile(prjFO));
336: assertNotNull("localized info found", info);
337: assertEquals("has correct display name", "Testing Module", info
338: .getDisplayName());
339: }
340:
341: public void testAddDependency() throws Exception {
342: NbModuleProject p = generateStandaloneModule("module");
343: assertEquals("no dependencies", 0, new ProjectXMLManager(p)
344: .getDirectDependencies().size());
345: assertTrue("successfully added", Util.addDependency(p,
346: "org.openide.util"));
347: ProjectManager.getDefault().saveProject(p);
348: assertEquals("one dependency", 1, new ProjectXMLManager(p)
349: .getDirectDependencies().size());
350: assertFalse("does not exist", Util.addDependency(p,
351: "org.openide.i_do_not_exist"));
352: ProjectManager.getDefault().saveProject(p);
353: assertEquals("still one dependency", 1,
354: new ProjectXMLManager(p).getDirectDependencies().size());
355: }
356:
357: public void testScanProjectForPackageNames() throws Exception {
358: FileObject prjDir = generateStandaloneModuleDirectory(
359: getWorkDir(), "module");
360: FileUtil.createData(prjDir, "src/a/b/c/Test.java");
361: SortedSet<String> packages = Util
362: .scanProjectForPackageNames(FileUtil.toFile(prjDir));
363: assertEquals("one package", 1, packages.size());
364: assertEquals("a.b.c package", "a.b.c", packages.first());
365: }
366:
367: public void testScanJarForPackageNames() throws Exception {
368: Map<String, String> contents = new HashMap<String, String>();
369: contents.put("a/b/A12.class", "");
370: contents.put("a/b/c/B123.class", "");
371: File jar = new File(getWorkDir(), "some.jar");
372: createJar(jar, contents, new Manifest());
373: SortedSet<String> packages = new TreeSet<String>();
374: Util.scanJarForPackageNames(packages, jar);
375: assertEquals("two packages", 2, packages.size());
376: assertEquals("a.b package", "a.b", packages.first());
377: assertEquals("a.b.c package", "a.b.c", packages.last());
378: }
379:
380: }
|