KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.FileReader JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.SortedSet JavaDoc;
35 import java.util.TreeSet JavaDoc;
36 import java.util.jar.Manifest JavaDoc;
37 import org.netbeans.api.project.ProjectManager;
38 import org.netbeans.modules.apisupport.project.ui.customizer.ModuleDependency;
39 import org.netbeans.modules.apisupport.project.universe.LocalizedBundleInfo;
40 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
41 import org.netbeans.spi.project.support.ant.AntProjectHelper;
42 import org.netbeans.spi.project.support.ant.EditableProperties;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileUtil;
45
46 /**
47  * Tests {@link Util}.
48  *
49  * @author Martin Krauskopf
50  */

51 public class UtilTest extends TestBase {
52     
53     public UtilTest(String JavaDoc name) {
54         super(name);
55     }
56     
57     public void testNormalizeCNB() throws Exception JavaDoc {
58         assertEquals("space test", "spacetest", Util.normalizeCNB("space test"));
59         assertEquals("slash test", "slashtest", Util.normalizeCNB("slash\\test"));
60         assertEquals("lowercase test", "org.capital.test", Util.normalizeCNB("org.Capital.test"));
61         assertEquals("dot-space test", "org.example.package", Util.normalizeCNB("org...example ... package..."));
62         assertEquals("org.example.hmmmm.misc.test339", Util.normalizeCNB("org.example.hmMMm.misc. TEst3*3=9"));
63     }
64     
65     public void testFindLocalizedBundleInfoFromNetBeansOrgModule() throws Exception JavaDoc {
66         FileObject dir = nbCVSRoot().getFileObject("apisupport/project");
67         assertNotNull("have apisupport/project checked out", dir);
68         NbModuleProject apisupport = (NbModuleProject) ProjectManager.getDefault().findProject(dir);
69         LocalizedBundleInfo info = Util.findLocalizedBundleInfo(
70                 apisupport.getSourceDirectory(), apisupport.getManifest());
71         assertApiSupportInfo(info);
72     }
73     
74     public void testFindLocalizedBundleInfoFromSourceDirectory() throws Exception JavaDoc {
75         LocalizedBundleInfo info = Util.findLocalizedBundleInfo(file("apisupport/project"));
76         assertApiSupportInfo(info);
77     }
78     
79     public void testFindLocalizedBundleInfoFromSourceDirectory1() throws Exception JavaDoc {
80         LocalizedBundleInfo info = Util.findLocalizedBundleInfo(resolveEEPFile("suite3/dummy-project"));
81         assertNull(info);
82     }
83     
84     public void testFindLocalizedBundleInfoFromBinaryModule() throws Exception JavaDoc {
85         File JavaDoc apisupportF = file("nbbuild/netbeans/" + TestBase.CLUSTER_IDE + "/modules/org-netbeans-modules-apisupport-project.jar");
86         assertApiSupportInfo(Util.findLocalizedBundleInfoFromJAR(apisupportF));
87     }
88     
89     private void assertApiSupportInfo(LocalizedBundleInfo info) {
90         assertNotNull("info loaded", info);
91         // XXX ignore this for now, but be careful when editing the module's properties :)
92
assertEquals("display name", "NetBeans Module Projects", info.getDisplayName());
93         /* Too fragile:
94         assertEquals("category", "Developing NetBeans", info.getCategory());
95         assertEquals("short description", "Defines an Ant-based project type for NetBeans modules.", info.getShortDescription());
96         assertEquals("long description", "Defines a project type for NetBeans " +
97                 "modules, useful for developing plug-in extensions to NetBeans. " +
98                 "Provides the logical view for modules, supplies the classpath " +
99                 "used for code completion, integrates with the NetBeans build " +
100                 "system (using Ant), etc.", info.getLongDescription());
101          */

102     }
103     
104     /** cf. #64782 */
105     public void testFindLocalizedBundleInfoLocalization() throws Exception JavaDoc {
106         Locale JavaDoc orig = Locale.getDefault();
107         Locale.setDefault(Locale.JAPAN);
108         try {
109             clearWorkDir();
110             File JavaDoc dir = getWorkDir();
111             Manifest JavaDoc mani = new Manifest JavaDoc();
112             mani.getMainAttributes().putValue("OpenIDE-Module-Localizing-Bundle", "pack/age/Bundle.properties");
113             // Start with an unlocalized source project.
114
File JavaDoc src = new File JavaDoc(dir, "src");
115             File JavaDoc f = new File JavaDoc(src, "pack/age/Bundle.properties".replace('/', File.separatorChar));
116             f.getParentFile().mkdirs();
117             TestBase.dump(f, "OpenIDE-Module-Name=Foo\nOpenIDE-Module-Display-Category=Foo Stuff\nOpenIDE-Module-Short-Description=short\nOpenIDE-Module-Long-Description=Long...");
118             // XXX test also Util.findLocalizedBundleInfo(File)?
119
LocalizedBundleInfo info = Util.findLocalizedBundleInfo(FileUtil.toFileObject(src), mani);
120             assertEquals("Foo", info.getDisplayName());
121             assertEquals("Foo Stuff", info.getCategory());
122             assertEquals("short", info.getShortDescription());
123             assertEquals("Long...", info.getLongDescription());
124             // Now add some locale variants.
125
f = new File JavaDoc(src, "pack/age/Bundle_ja.properties".replace('/', File.separatorChar));
126             TestBase.dump(f, "OpenIDE-Module-Long-Description=Long Japanese text...");
127             f = new File JavaDoc(src, "pack/age/Bundle_ja_JP.properties".replace('/', File.separatorChar));
128             TestBase.dump(f, "OpenIDE-Module-Name=Foo Nihon");
129             info = Util.findLocalizedBundleInfo(FileUtil.toFileObject(src), mani);
130             assertEquals("Foo Nihon", info.getDisplayName());
131             assertEquals("Foo Stuff", info.getCategory());
132             assertEquals("short", info.getShortDescription());
133             assertEquals("Long Japanese text...", info.getLongDescription());
134             // Now try it on JAR files.
135
f = new File JavaDoc(dir, "noloc.jar");
136             createJar(f, Collections.singletonMap("pack/age/Bundle.properties", "OpenIDE-Module-Name=Foo"), mani);
137             info = Util.findLocalizedBundleInfoFromJAR(f);
138             assertEquals("Foo", info.getDisplayName());
139             assertNull(info.getShortDescription());
140             f = new File JavaDoc(dir, "internalloc.jar");
141             Map JavaDoc<String JavaDoc,String JavaDoc> contents = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
142             contents.put("pack/age/Bundle.properties", "OpenIDE-Module-Name=Foo\nOpenIDE-Module-Short-Description=short");
143             contents.put("pack/age/Bundle_ja_JP.properties", "OpenIDE-Module-Name=Foo Nihon");
144             createJar(f, contents, mani);
145             info = Util.findLocalizedBundleInfoFromJAR(f);
146             assertEquals("Foo Nihon", info.getDisplayName());
147             assertEquals("short", info.getShortDescription());
148             f = new File JavaDoc(dir, "externalloc.jar");
149             createJar(f, Collections.singletonMap("pack/age/Bundle.properties", "OpenIDE-Module-Name=Foo\nOpenIDE-Module-Short-Description=short"), mani);
150             File JavaDoc f2 = new File JavaDoc(dir, "locale" + File.separatorChar + "externalloc_ja.jar");
151             createJar(f2, Collections.singletonMap("pack/age/Bundle_ja.properties", "OpenIDE-Module-Short-Description=short Japanese"), new Manifest JavaDoc());
152             info = Util.findLocalizedBundleInfoFromJAR(f);
153             assertEquals("Foo", info.getDisplayName());
154             assertEquals("the meat of #64782", "short Japanese", info.getShortDescription());
155         } finally {
156             Locale.setDefault(orig);
157         }
158     }
159     
160     public void testLoadProperties() throws Exception JavaDoc {
161         File JavaDoc props = file(getWorkDir(), "testing.properties");
162         OutputStream JavaDoc propsOS = new FileOutputStream JavaDoc(props);
163         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(propsOS);
164         try {
165             pw.println("property1=some value");
166             pw.println("property2=other value");
167         } finally {
168             pw.close();
169         }
170         EditableProperties ep = Util.loadProperties(FileUtil.toFileObject(props));
171         assertEquals("property1", "some value", ep.getProperty("property1"));
172         assertEquals("property2", "other value", ep.getProperty("property2"));
173         try {
174             File JavaDoc notFile = file(getWorkDir(), "i_am_not_file");
175             notFile.mkdir();
176             Util.loadProperties(FileUtil.toFileObject(notFile));
177             fail("FileNotFoundException should be thrown");
178         } catch (FileNotFoundException JavaDoc fnfe) {
179             // fine expected exception has been thrown
180
}
181     }
182     
183     public void testStoreProperties() throws Exception JavaDoc {
184         FileObject propsFO = FileUtil.createData(FileUtil.toFileObject(getWorkDir()), "testing.properties");
185         EditableProperties props = Util.loadProperties(propsFO);
186         assertTrue("empty props", props.isEmpty());
187         props.setProperty("property1", "some value");
188         Util.storeProperties(propsFO, props);
189         
190         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(
191                 new FileReader JavaDoc(file(getWorkDir(), "testing.properties")));
192         try {
193             assertEquals("stored property", "property1=some value", reader.readLine());
194         } finally {
195             reader.close();
196         }
197     }
198     
199     // XXX testLoadManifest()
200
// XXX testStoreManifest()
201

202     public void testFindJavadoc() throws Exception JavaDoc {
203         File JavaDoc oneModuleDoc = new File JavaDoc(getWorkDir(), "org-example-module1");
204         assertTrue(oneModuleDoc.mkdir());
205         File JavaDoc index = new File JavaDoc(oneModuleDoc, "index.html");
206         assertTrue(index.createNewFile());
207         
208         NbModuleProject project = generateStandaloneModule("module1");
209         NbPlatform platform = project.getPlatform(false);
210         URL JavaDoc oneModuleDocURL = Util.urlForDir(oneModuleDoc);
211         platform.addJavadocRoot(oneModuleDocURL);
212         ModuleDependency md = new ModuleDependency(project.getModuleList().getEntry(project.getCodeNameBase()));
213         
214         URL JavaDoc url = Util.findJavadoc(md, platform);
215         assertNotNull("url was found", url);
216         
217         File JavaDoc nbDoc = new File JavaDoc(getWorkDir(), "nbDoc");
218         File JavaDoc moduleDoc = new File JavaDoc(nbDoc, "org-example-module1");
219         assertTrue(moduleDoc.mkdirs());
220         index = new File JavaDoc(moduleDoc, "index.html");
221         assertTrue(index.createNewFile());
222         
223         platform.addJavadocRoot(Util.urlForDir(nbDoc));
224         platform.removeJavadocRoots(new URL JavaDoc[] {oneModuleDocURL});
225         url = Util.findJavadoc(md, platform);
226         assertNotNull("url was found", url);
227     }
228     
229     public void testIsValidJavaFQN() throws Exception JavaDoc {
230         assertFalse(Util.isValidJavaFQN("a.b,c"));
231         assertFalse(Util.isValidJavaFQN(""));
232         assertFalse(Util.isValidJavaFQN("a.b.1"));
233         assertTrue(Util.isValidJavaFQN("a"));
234         assertTrue(Util.isValidJavaFQN("a.b.c1"));
235     }
236     
237     public void testIsValidSFSFolderName() throws Exception JavaDoc {
238         assertTrue(Util.isValidSFSPath("a"));
239         assertTrue(Util.isValidSFSPath("a/b/c"));
240         assertTrue(Util.isValidSFSPath("a/b/c_c/"));
241         assertTrue(Util.isValidSFSPath("/a/b/c_c"));
242         assertTrue(Util.isValidSFSPath("a/1a/b/c/1d_d/"));
243         assertTrue(Util.isValidSFSPath("_a/b/c_"));
244         assertFalse(Util.isValidSFSPath("a/b/c/dd+"));
245         assertFalse(Util.isValidSFSPath("a+b"));
246         assertFalse(Util.isValidSFSPath(""));
247         assertFalse(Util.isValidSFSPath(" "));
248     }
249     
250     public void testDisplayName_70363() throws Exception JavaDoc {
251         FileObject prjFO = TestBase.generateStandaloneModuleDirectory(getWorkDir(), "module");
252         FileUtil.moveFile(prjFO.getFileObject("src"), prjFO, "libsrc");
253         FileObject propsFO = FileUtil.createData(prjFO, AntProjectHelper.PROJECT_PROPERTIES_PATH);
254         EditableProperties ep = Util.loadProperties(propsFO);
255         ep.setProperty("src.dir", "libsrc");
256         Util.storeProperties(propsFO, ep);
257         LocalizedBundleInfo info = Util.findLocalizedBundleInfo(FileUtil.toFile(prjFO));
258         assertNotNull("localized info found", info);
259         assertEquals("has correct display name", "Testing Module", info.getDisplayName());
260     }
261     
262     public void testAddDependency() throws Exception JavaDoc {
263         NbModuleProject p = generateStandaloneModule("module");
264         assertEquals("no dependencies", 0, new ProjectXMLManager(p).getDirectDependencies().size());
265         assertTrue("successfully added", Util.addDependency(p, "org.openide.util"));
266         ProjectManager.getDefault().saveProject(p);
267         assertEquals("one dependency", 1, new ProjectXMLManager(p).getDirectDependencies().size());
268         assertFalse("does not exist", Util.addDependency(p, "org.openide.i_do_not_exist"));
269         ProjectManager.getDefault().saveProject(p);
270         assertEquals("still one dependency", 1, new ProjectXMLManager(p).getDirectDependencies().size());
271     }
272     
273     public void testScanProjectForPackageNames() throws Exception JavaDoc {
274         FileObject prjDir = generateStandaloneModuleDirectory(getWorkDir(), "module");
275         FileUtil.createData(prjDir, "src/a/b/c/Test.java");
276         SortedSet JavaDoc<String JavaDoc> packages = Util.scanProjectForPackageNames(FileUtil.toFile(prjDir));
277         assertEquals("one package", 1, packages.size());
278         assertEquals("a.b.c package", "a.b.c", packages.first());
279     }
280     
281     public void testScanJarForPackageNames() throws Exception JavaDoc {
282         Map JavaDoc<String JavaDoc,String JavaDoc> contents = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
283         contents.put("a/b/A12.class", "");
284         contents.put("a/b/c/B123.class", "");
285         File JavaDoc jar = new File JavaDoc(getWorkDir(), "some.jar");
286         createJar(jar, contents, new Manifest JavaDoc());
287         SortedSet JavaDoc<String JavaDoc> packages = new TreeSet JavaDoc<String JavaDoc>();
288         Util.scanJarForPackageNames(packages, jar);
289         assertEquals("two packages", 2, packages.size());
290         assertEquals("a.b package", "a.b", packages.first());
291         assertEquals("a.b.c package", "a.b.c", packages.last());
292     }
293     
294 }
295
Popular Tags