1 19 20 package org.netbeans.modules.apisupport.project.layers; 21 22 import java.io.File ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Locale ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.jar.Manifest ; 31 import org.netbeans.api.project.ProjectManager; 32 import org.netbeans.modules.apisupport.project.CreatedModifiedFiles; 33 import org.netbeans.modules.apisupport.project.NbModuleProject; 34 import org.netbeans.modules.apisupport.project.NbModuleProjectGenerator; 35 import org.netbeans.modules.apisupport.project.TestBase; 36 import org.netbeans.modules.apisupport.project.suite.SuiteProjectGenerator; 37 import org.netbeans.modules.apisupport.project.universe.NbPlatform; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileSystem; 40 import org.openide.filesystems.FileUtil; 41 import org.openide.loaders.DataObject; 42 43 47 public class LayerUtilsTest extends LayerTestBase { 48 49 public LayerUtilsTest(String name) { 50 super(name); 51 } 52 53 protected void setUp() throws Exception { 54 super.setUp(); 55 TestBase.initializeBuildProperties(getWorkDir(), getDataDir()); 56 } 57 58 public void testLayerHandle() throws Exception { 59 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module"); 60 LayerUtils.LayerHandle handle = LayerUtils.layerForProject(project); 61 FileObject expectedLayerXML = project.getProjectDirectory().getFileObject("src/org/example/module/resources/layer.xml"); 62 assertNotNull(expectedLayerXML); 63 FileObject layerXML = handle.getLayerFile(); 64 assertNotNull("layer.xml already exists", layerXML); 65 assertEquals("right layer file", expectedLayerXML, layerXML); 66 FileSystem fs = handle.layer(true); 67 assertEquals("initially empty", 0, fs.getRoot().getChildren().length); 68 long initialSize = layerXML.getSize(); 69 fs.getRoot().createData("foo"); 70 assertEquals("not saved yet", initialSize, layerXML.getSize()); 71 fs = handle.layer(true); 72 assertNotNull("still have in-memory mods", fs.findResource("foo")); 73 fs.getRoot().createData("bar"); 74 handle.save(); 75 assertTrue("now it is saved", layerXML.getSize() > initialSize); 76 String xml = 77 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 78 "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n" + 79 "<filesystem>\n" + 80 " <file name=\"bar\"/>\n" + 81 " <file name=\"foo\"/>\n" + 82 "</filesystem>\n"; 83 assertEquals("right contents too", xml, TestBase.slurp(layerXML)); 84 } 86 87 public void testLayerAutoSave() throws Exception { 88 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module"); 89 LayerUtils.LayerHandle handle = LayerUtils.layerForProject(project); 90 FileSystem fs = handle.layer(true); 91 handle.setAutosave(true); 92 FileObject foo = fs.getRoot().createData("foo"); 93 FileObject layerXML = handle.getLayerFile(); 94 String xml = 95 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 96 "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n" + 97 "<filesystem>\n" + 98 " <file name=\"foo\"/>\n" + 99 "</filesystem>\n"; 100 assertEquals("saved automatically", xml, TestBase.slurp(layerXML)); 101 foo.setAttribute("a", Boolean.TRUE); 102 xml = 103 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 104 "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n" + 105 "<filesystem>\n" + 106 " <file name=\"foo\">\n" + 107 " <attr name=\"a\" boolvalue=\"true\"/>\n" + 108 " </file>\n" + 109 "</filesystem>\n"; 110 assertEquals("saved automatically from an attribute change too", xml, TestBase.slurp(layerXML)); 111 } 112 113 117 public void testSystemFilesystemStandaloneProject() throws Exception { 118 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module"); 119 LayerUtils.LayerHandle handle = LayerUtils.layerForProject(project); 120 FileObject layerXML = handle.getLayerFile(); 121 String xml = 122 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 123 "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n" + 124 "<filesystem>\n" + 125 " <file name=\"foo\"/>\n" + 126 "</filesystem>\n"; 127 TestBase.dump(layerXML, xml); 128 long start = System.currentTimeMillis(); 129 FileSystem fs = LayerUtils.getEffectiveSystemFilesystem(project); 130 System.err.println("LayerUtils.getEffectiveSystemFilesystem ran in " + (System.currentTimeMillis() - start) + "msec"); 131 assertFalse("can write to it", fs.isReadOnly()); 132 assertNotNull("have stuff from the platform", fs.findResource("Menu/File")); 133 assertNotNull("have stuff from my own layer", fs.findResource("foo")); 134 fs.getRoot().createData("quux"); 135 handle.save(); 136 xml = 137 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 138 "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n" + 139 "<filesystem>\n" + 140 " <file name=\"foo\"/>\n" + 141 " <file name=\"quux\"/>\n" + 142 "</filesystem>\n"; 143 assertEquals("new layer stored", xml, TestBase.slurp(layerXML)); 144 } 145 146 public void testSystemFilesystemSuiteComponentProject() throws Exception { 147 File suiteDir = new File (getWorkDir(), "testSuite"); 148 SuiteProjectGenerator.createSuiteProject(suiteDir, NbPlatform.PLATFORM_ID_DEFAULT); 149 File module1Dir = new File (suiteDir, "testModule1"); 150 NbModuleProjectGenerator.createSuiteComponentModule( 151 module1Dir, 152 "test.module1", 153 "module1", 154 "test/module1/resources/Bundle.properties", 155 "test/module1/resources/layer.xml", 156 suiteDir); 157 NbModuleProject module1 = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(module1Dir)); 158 LayerUtils.LayerHandle handle = LayerUtils.layerForProject(module1); 159 FileUtil.createData(handle.layer(true).getRoot(), "random/stuff"); 160 handle.save(); 161 File module2Dir = new File (suiteDir, "testModule2"); 162 NbModuleProjectGenerator.createSuiteComponentModule( 163 module2Dir, 164 "test.module2", 165 "module2", 166 "test/module2/resources/Bundle.properties", 167 "test/module2/resources/layer.xml", 168 suiteDir); 169 NbModuleProject module2 = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(module2Dir)); 170 handle = LayerUtils.layerForProject(module2); 171 FileObject layerXML = handle.getLayerFile(); 172 String xml = 173 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 174 "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n" + 175 "<filesystem>\n" + 176 " <file name=\"existing\"/>\n" + 177 "</filesystem>\n"; 178 TestBase.dump(layerXML, xml); 179 FileSystem fs = LayerUtils.getEffectiveSystemFilesystem(module2); 180 assertFalse("can write to it", fs.isReadOnly()); 181 assertNotNull("have stuff from the platform", fs.findResource("Menu/File")); 182 assertNotNull("have stuff from my own layer", fs.findResource("existing")); 183 assertNotNull("have stuff from other modules in the same suite", fs.findResource("random/stuff")); 184 fs.getRoot().createData("new"); 185 handle.save(); 186 xml = 187 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 188 "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n" + 189 "<filesystem>\n" + 190 " <file name=\"existing\"/>\n" + 191 " <file name=\"new\"/>\n" + 192 "</filesystem>\n"; 193 assertEquals("new layer stored", xml, TestBase.slurp(layerXML)); 194 } 195 196 public void testSystemFilesystemLocalizedNames() throws Exception { 197 File suiteDir = new File (getWorkDir(), "testSuite"); 198 SuiteProjectGenerator.createSuiteProject(suiteDir, NbPlatform.PLATFORM_ID_DEFAULT); 199 File module1Dir = new File (suiteDir, "testModule1"); 200 NbModuleProjectGenerator.createSuiteComponentModule( 201 module1Dir, 202 "test.module1", 203 "module1", 204 "test/module1/resources/Bundle.properties", 205 "test/module1/resources/layer.xml", 206 suiteDir); 207 NbModuleProject module1 = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(module1Dir)); 208 CreatedModifiedFiles cmf = new CreatedModifiedFiles(module1); 209 cmf.add(cmf.createLayerEntry("foo", null, null, "Foo", null)); 210 cmf.run(); 211 File module2Dir = new File (suiteDir, "testModule2"); 212 NbModuleProjectGenerator.createSuiteComponentModule( 213 module2Dir, 214 "test.module2", 215 "module2", 216 "test/module2/resources/Bundle.properties", 217 "test/module2/resources/layer.xml", 218 suiteDir); 219 NbModuleProject module2 = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(module2Dir)); 220 cmf = new CreatedModifiedFiles(module2); 221 cmf.add(cmf.createLayerEntry("bar", null, null, "Bar", null)); 222 cmf.add(cmf.createLayerEntry("test-module2-MyAction.instance", null, null, null, null)); 223 cmf.add(cmf.createLayerEntry("test-module2-some-action.instance", null, null, null, Collections.<String ,Object >singletonMap("instanceClass", "test.module2.SomeAction"))); 224 cmf.add(cmf.createLayerEntry("test-module2-another-action.instance", null, null, null, Collections.<String ,Object >singletonMap("instanceCreate", "newvalue:test.module2.AnotherAction"))); 225 cmf.add(cmf.createLayerEntry("test-module2-factory-action.instance", null, null, null, Collections.<String ,Object >singletonMap("instanceCreate", "methodvalue:test.module2.FactoryAction.create"))); 226 cmf.add(cmf.createLayerEntry("sep-42.instance", null, null, null, Collections.<String ,Object >singletonMap("instanceClass", "javax.swing.JSeparator"))); 227 cmf.add(cmf.createLayerEntry("link-to-standard.shadow", null, null, null, Collections.<String ,Object >singletonMap("originalFile", "Actions/System/org-openide-actions-OpenAction.instance"))); 228 cmf.add(cmf.createLayerEntry("link-to-custom.shadow", null, null, null, Collections.<String ,Object >singletonMap("originalFile", "test-module2-MyAction.instance"))); 229 File dummyDir = new File (getWorkDir(), "dummy"); 230 dummyDir.mkdir(); 231 cmf.add(cmf.createLayerEntry("link-to-url.shadow", null, null, null, Collections.<String ,Object >singletonMap("originalFile", dummyDir.toURI().toURL()))); 232 cmf.run(); 233 FileSystem fs = LayerUtils.getEffectiveSystemFilesystem(module2); 234 assertDisplayName(fs, "right display name for platform file", "Menu/RunProject", "Run"); 235 assertDisplayName(fs, "label for file in suite", "foo", "Foo"); 236 assertDisplayName(fs, "label for file in this project", "bar", "Bar"); 237 assertDisplayName(fs, "right display name for well-known action", "Menu/File/org-openide-actions-SaveAction.instance", "Save"); 238 assertDisplayName(fs, "label for simple instance", "test-module2-MyAction.instance", "<instance of MyAction>"); 239 assertDisplayName(fs, "label for instanceClass", "test-module2-some-action.instance", "<instance of SomeAction>"); 240 assertDisplayName(fs, "label for newvalue instanceCreate", "test-module2-another-action.instance", "<instance of AnotherAction>"); 241 assertDisplayName(fs, "label for methodvalue instanceCreate", "test-module2-factory-action.instance", "<instance from FactoryAction.create>"); 242 assertDisplayName(fs, "label for menu separator", "sep-42.instance", "<separator>"); 243 assertDisplayName(fs, "link to standard menu item", "link-to-standard.shadow", "Open"); 244 assertDisplayName(fs, "link to custom menu item", "link-to-custom.shadow", "<instance of MyAction>"); 245 DataObject.find(fs.findResource("link-to-url.shadow")).getNodeDelegate().getDisplayName(); 250 } 251 252 public void testSystemFilesystemLocalizedNamesI18N() throws Exception { 253 Locale orig = Locale.getDefault(); 254 try { 255 Locale.setDefault(Locale.JAPAN); 256 File platformDir = new File (getWorkDir(), "testPlatform"); 257 Manifest mf = new Manifest (); 258 mf.getMainAttributes().putValue("OpenIDE-Module", "platform.module"); 259 mf.getMainAttributes().putValue("OpenIDE-Module-Layer", "platform/module/layer.xml"); 260 Map contents = new HashMap (); 261 contents.put("platform/module/Bundle.properties", "folder/file=English"); 262 contents.put("platform/module/layer.xml", "<filesystem><folder name=\"folder\"><file name=\"file\"><attr name=\"SystemFileSystem.localizingBundle\" stringvalue=\"platform.module.Bundle\"/></file></folder></filesystem>"); 263 TestBase.createJar(new File (platformDir, "cluster/modules/platform-module.jar".replace('/', File.separatorChar)), contents, mf); 264 mf = new Manifest (); 265 contents = new HashMap (); 266 contents.put("platform/module/Bundle_ja.properties", "folder/file=Japanese"); 267 TestBase.createJar(new File (platformDir, "cluster/modules/locale/platform-module_ja.jar".replace('/', File.separatorChar)), contents, mf); 268 TestBase.createJar(new File (new File (new File (platformDir, "platform"), "core"), "core.jar"), Collections.EMPTY_MAP, new Manifest ()); 270 NbPlatform.addPlatform("testplatform", platformDir, "Test Platform"); 271 File suiteDir = new File (getWorkDir(), "testSuite"); 272 SuiteProjectGenerator.createSuiteProject(suiteDir, "testplatform"); 273 File moduleDir = new File (suiteDir, "testModule"); 274 NbModuleProjectGenerator.createSuiteComponentModule( 275 moduleDir, 276 "test.module", 277 "module", 278 "test/module/resources/Bundle.properties", 279 "test/module/resources/layer.xml", 280 suiteDir); 281 NbModuleProject module = (NbModuleProject) ProjectManager.getDefault().findProject(FileUtil.toFileObject(moduleDir)); 282 FileSystem fs = LayerUtils.getEffectiveSystemFilesystem(module); 283 assertDisplayName(fs, "#64779: localized platform filename", "folder/file", "Japanese"); 284 } finally { 285 Locale.setDefault(orig); 286 } 287 } 288 289 public void testSystemFilesystemNetBeansOrgProject() throws Exception { 290 FileObject nbcvsroot = FileUtil.toFileObject(new File (System.getProperty("test.nbcvsroot"))); 291 NbModuleProject p = (NbModuleProject) ProjectManager.getDefault().findProject(nbcvsroot.getFileObject("beans")); 292 FileSystem fs = LayerUtils.getEffectiveSystemFilesystem(p); 293 assertDisplayName(fs, "right display name for netbeans.org standard file", "Menu/RunProject", "Run"); 294 assertNull("not loading files from extra modules", fs.findResource("Templates/Documents/docbook-article.xml")); 295 FileObject docbook = nbcvsroot.getFileObject("contrib/docbook"); 296 if (docbook == null) { 297 System.err.println("Skipping part of testSystemFilesystemNetBeansOrgProject since contrib is not checked out"); 298 return; 299 } 300 p = (NbModuleProject) ProjectManager.getDefault().findProject(docbook); 301 fs = LayerUtils.getEffectiveSystemFilesystem(p); 302 assertDisplayName(fs, "right display name for file from extra module", "Templates/Documents/docbook-article.xml", "DocBook Article"); 303 } 304 305 308 private static void assertDisplayName(FileSystem fs, String message, String path, String label) throws Exception { 309 FileObject file = fs.findResource(path); 310 assertNotNull("found " + path, file); 311 assertEquals(message, label, DataObject.find(file).getNodeDelegate().getDisplayName()); 312 } 313 314 public void testMasks() throws Exception { 315 NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module"); 316 FileSystem fs = LayerUtils.getEffectiveSystemFilesystem(project); 317 Set optionInstanceNames = new HashSet (); 318 FileObject toolsMenu = fs.findResource("Menu/Tools"); 319 assertNotNull(toolsMenu); 320 FileObject[] kids = toolsMenu.getChildren(); 321 for (int i = 0; i < kids.length; i++) { 322 String name = kids[i].getNameExt(); 323 if (name.indexOf("Options") != -1) { 324 optionInstanceNames.add(name); 325 } 326 } 327 assertEquals("#63295: masks work", 328 new HashSet (Arrays.asList(new String [] { 329 "org-netbeans-modules-options-OptionsWindowAction.shadow", 330 })), optionInstanceNames); 332 assertNotNull("system FS has xml/catalog", fs.findResource("Services/Hidden/org-netbeans-modules-xml-catalog-impl-XCatalogProvider.instance")); 333 assertNull("but one entry hidden by apisupport/project", fs.findResource("Services/Hidden/org-netbeans-modules-xml-catalog-impl-SystemCatalogProvider.instance")); 334 } 335 336 } 337 | Popular Tags |