1 19 20 package org.netbeans.modules.apisupport.project; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.FileReader ; 27 import java.io.OutputStream ; 28 import java.io.PrintWriter ; 29 import java.net.URL ; 30 import java.util.Collections ; 31 import java.util.HashMap ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 import java.util.SortedSet ; 35 import java.util.TreeSet ; 36 import java.util.jar.Manifest ; 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 51 public class UtilTest extends TestBase { 52 53 public UtilTest(String name) { 54 super(name); 55 } 56 57 public void testNormalizeCNB() throws Exception { 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 { 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 { 75 LocalizedBundleInfo info = Util.findLocalizedBundleInfo(file("apisupport/project")); 76 assertApiSupportInfo(info); 77 } 78 79 public void testFindLocalizedBundleInfoFromSourceDirectory1() throws Exception { 80 LocalizedBundleInfo info = Util.findLocalizedBundleInfo(resolveEEPFile("suite3/dummy-project")); 81 assertNull(info); 82 } 83 84 public void testFindLocalizedBundleInfoFromBinaryModule() throws Exception { 85 File 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 assertEquals("display name", "NetBeans Module Projects", info.getDisplayName()); 93 102 } 103 104 105 public void testFindLocalizedBundleInfoLocalization() throws Exception { 106 Locale orig = Locale.getDefault(); 107 Locale.setDefault(Locale.JAPAN); 108 try { 109 clearWorkDir(); 110 File dir = getWorkDir(); 111 Manifest mani = new Manifest (); 112 mani.getMainAttributes().putValue("OpenIDE-Module-Localizing-Bundle", "pack/age/Bundle.properties"); 113 File src = new File (dir, "src"); 115 File f = new File (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 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 f = new File (src, "pack/age/Bundle_ja.properties".replace('/', File.separatorChar)); 126 TestBase.dump(f, "OpenIDE-Module-Long-Description=Long Japanese text..."); 127 f = new File (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 f = new File (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 (dir, "internalloc.jar"); 141 Map <String ,String > contents = new HashMap <String ,String >(); 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 (dir, "externalloc.jar"); 149 createJar(f, Collections.singletonMap("pack/age/Bundle.properties", "OpenIDE-Module-Name=Foo\nOpenIDE-Module-Short-Description=short"), mani); 150 File f2 = new File (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 ()); 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 { 161 File props = file(getWorkDir(), "testing.properties"); 162 OutputStream propsOS = new FileOutputStream (props); 163 PrintWriter pw = new PrintWriter (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 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 fnfe) { 179 } 181 } 182 183 public void testStoreProperties() throws Exception { 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 reader = new BufferedReader ( 191 new FileReader (file(getWorkDir(), "testing.properties"))); 192 try { 193 assertEquals("stored property", "property1=some value", reader.readLine()); 194 } finally { 195 reader.close(); 196 } 197 } 198 199 202 public void testFindJavadoc() throws Exception { 203 File oneModuleDoc = new File (getWorkDir(), "org-example-module1"); 204 assertTrue(oneModuleDoc.mkdir()); 205 File index = new File (oneModuleDoc, "index.html"); 206 assertTrue(index.createNewFile()); 207 208 NbModuleProject project = generateStandaloneModule("module1"); 209 NbPlatform platform = project.getPlatform(false); 210 URL oneModuleDocURL = Util.urlForDir(oneModuleDoc); 211 platform.addJavadocRoot(oneModuleDocURL); 212 ModuleDependency md = new ModuleDependency(project.getModuleList().getEntry(project.getCodeNameBase())); 213 214 URL url = Util.findJavadoc(md, platform); 215 assertNotNull("url was found", url); 216 217 File nbDoc = new File (getWorkDir(), "nbDoc"); 218 File moduleDoc = new File (nbDoc, "org-example-module1"); 219 assertTrue(moduleDoc.mkdirs()); 220 index = new File (moduleDoc, "index.html"); 221 assertTrue(index.createNewFile()); 222 223 platform.addJavadocRoot(Util.urlForDir(nbDoc)); 224 platform.removeJavadocRoots(new URL [] {oneModuleDocURL}); 225 url = Util.findJavadoc(md, platform); 226 assertNotNull("url was found", url); 227 } 228 229 public void testIsValidJavaFQN() throws Exception { 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 { 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 { 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 { 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 { 274 FileObject prjDir = generateStandaloneModuleDirectory(getWorkDir(), "module"); 275 FileUtil.createData(prjDir, "src/a/b/c/Test.java"); 276 SortedSet <String > 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 { 282 Map <String ,String > contents = new HashMap <String ,String >(); 283 contents.put("a/b/A12.class", ""); 284 contents.put("a/b/c/B123.class", ""); 285 File jar = new File (getWorkDir(), "some.jar"); 286 createJar(jar, contents, new Manifest ()); 287 SortedSet <String > packages = new TreeSet <String >(); 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 |