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.layers;
043:
044: import java.io.File;
045: import java.util.Arrays;
046: import java.util.Collections;
047: import java.util.HashMap;
048: import java.util.HashSet;
049: import java.util.Locale;
050: import java.util.Map;
051: import java.util.Set;
052: import java.util.jar.Manifest;
053: import org.netbeans.api.project.ProjectManager;
054: import org.netbeans.modules.apisupport.project.CreatedModifiedFiles;
055: import org.netbeans.modules.apisupport.project.NbModuleProject;
056: import org.netbeans.modules.apisupport.project.NbModuleProjectGenerator;
057: import org.netbeans.modules.apisupport.project.TestBase;
058: import org.netbeans.modules.apisupport.project.suite.SuiteProjectGenerator;
059: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
060: import org.openide.filesystems.FileObject;
061: import org.openide.filesystems.FileSystem;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.loaders.DataObject;
064:
065: /**
066: * Test writing changes to layers.
067: * @author Jesse Glick
068: */
069: public class LayerUtilsTest extends LayerTestBase {
070:
071: public LayerUtilsTest(String name) {
072: super (name);
073: }
074:
075: protected @Override
076: void setUp() throws Exception {
077: super .setUp();
078: TestBase.initializeBuildProperties(getWorkDir(), getDataDir());
079: }
080:
081: public void testLayerHandle() throws Exception {
082: NbModuleProject project = TestBase.generateStandaloneModule(
083: getWorkDir(), "module");
084: LayerUtils.LayerHandle handle = LayerUtils
085: .layerForProject(project);
086: FileObject expectedLayerXML = project.getProjectDirectory()
087: .getFileObject(
088: "src/org/example/module/resources/layer.xml");
089: assertNotNull(expectedLayerXML);
090: FileObject layerXML = handle.getLayerFile();
091: assertNotNull("layer.xml already exists", layerXML);
092: assertEquals("right layer file", expectedLayerXML, layerXML);
093: FileSystem fs = handle.layer(true);
094: assertEquals("initially empty", 0,
095: fs.getRoot().getChildren().length);
096: long initialSize = layerXML.getSize();
097: fs.getRoot().createData("foo");
098: assertEquals("not saved yet", initialSize, layerXML.getSize());
099: fs = handle.layer(true);
100: assertNotNull("still have in-memory mods", fs
101: .findResource("foo"));
102: fs.getRoot().createData("bar");
103: handle.save();
104: assertTrue("now it is saved", layerXML.getSize() > initialSize);
105: String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
106: + "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n"
107: + "<filesystem>\n" + " <file name=\"bar\"/>\n"
108: + " <file name=\"foo\"/>\n" + "</filesystem>\n";
109: assertEquals("right contents too", xml, TestBase
110: .slurp(layerXML));
111: // XXX test that nbres: file contents work
112: }
113:
114: public void testLayerAutoSave() throws Exception {
115: NbModuleProject project = TestBase.generateStandaloneModule(
116: getWorkDir(), "module");
117: LayerUtils.LayerHandle handle = LayerUtils
118: .layerForProject(project);
119: FileSystem fs = handle.layer(true);
120: handle.setAutosave(true);
121: FileObject foo = fs.getRoot().createData("foo");
122: FileObject layerXML = handle.getLayerFile();
123: String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
124: + "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n"
125: + "<filesystem>\n" + " <file name=\"foo\"/>\n"
126: + "</filesystem>\n";
127: assertEquals("saved automatically", xml, TestBase
128: .slurp(layerXML));
129: foo.setAttribute("a", Boolean.TRUE);
130: xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
131: + "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n"
132: + "<filesystem>\n" + " <file name=\"foo\">\n"
133: + " <attr name=\"a\" boolvalue=\"true\"/>\n"
134: + " </file>\n" + "</filesystem>\n";
135: assertEquals(
136: "saved automatically from an attribute change too",
137: xml, TestBase.slurp(layerXML));
138: }
139:
140: // XXX testInitiallyInvalidLayer
141: // XXX testInitiallyMissingLayer
142: // XXX testGcLayerHandle
143:
144: public void testSystemFilesystemStandaloneProject()
145: throws Exception {
146: NbModuleProject project = TestBase.generateStandaloneModule(
147: getWorkDir(), "module");
148: LayerUtils.LayerHandle handle = LayerUtils
149: .layerForProject(project);
150: FileObject layerXML = handle.getLayerFile();
151: String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
152: + "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n"
153: + "<filesystem>\n" + " <file name=\"foo\"/>\n"
154: + "</filesystem>\n";
155: TestBase.dump(layerXML, xml);
156: long start = System.currentTimeMillis();
157: FileSystem fs = LayerUtils
158: .getEffectiveSystemFilesystem(project);
159: System.err
160: .println("LayerUtils.getEffectiveSystemFilesystem ran in "
161: + (System.currentTimeMillis() - start) + "msec");
162: assertFalse("can write to it", fs.isReadOnly());
163: assertNotNull("have stuff from the platform", fs
164: .findResource("Menu/File"));
165: assertNotNull("have stuff from my own layer", fs
166: .findResource("foo"));
167: fs.getRoot().createData("quux");
168: handle.save();
169: xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
170: + "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n"
171: + "<filesystem>\n" + " <file name=\"foo\"/>\n"
172: + " <file name=\"quux\"/>\n" + "</filesystem>\n";
173: assertEquals("new layer stored", xml, TestBase.slurp(layerXML));
174: }
175:
176: public void testSystemFilesystemSuiteComponentProject()
177: throws Exception {
178: File suiteDir = new File(getWorkDir(), "testSuite");
179: SuiteProjectGenerator.createSuiteProject(suiteDir,
180: NbPlatform.PLATFORM_ID_DEFAULT);
181: File module1Dir = new File(suiteDir, "testModule1");
182: NbModuleProjectGenerator.createSuiteComponentModule(module1Dir,
183: "test.module1", "module1",
184: "test/module1/resources/Bundle.properties",
185: "test/module1/resources/layer.xml", suiteDir);
186: NbModuleProject module1 = (NbModuleProject) ProjectManager
187: .getDefault().findProject(
188: FileUtil.toFileObject(module1Dir));
189: LayerUtils.LayerHandle handle = LayerUtils
190: .layerForProject(module1);
191: FileUtil.createData(handle.layer(true).getRoot(),
192: "random/stuff");
193: handle.save();
194: File module2Dir = new File(suiteDir, "testModule2");
195: NbModuleProjectGenerator.createSuiteComponentModule(module2Dir,
196: "test.module2", "module2",
197: "test/module2/resources/Bundle.properties",
198: "test/module2/resources/layer.xml", suiteDir);
199: NbModuleProject module2 = (NbModuleProject) ProjectManager
200: .getDefault().findProject(
201: FileUtil.toFileObject(module2Dir));
202: handle = LayerUtils.layerForProject(module2);
203: FileObject layerXML = handle.getLayerFile();
204: String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
205: + "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n"
206: + "<filesystem>\n" + " <file name=\"existing\"/>\n"
207: + "</filesystem>\n";
208: TestBase.dump(layerXML, xml);
209: FileSystem fs = LayerUtils
210: .getEffectiveSystemFilesystem(module2);
211: assertFalse("can write to it", fs.isReadOnly());
212: assertNotNull("have stuff from the platform", fs
213: .findResource("Menu/File"));
214: assertNotNull("have stuff from my own layer", fs
215: .findResource("existing"));
216: assertNotNull(
217: "have stuff from other modules in the same suite", fs
218: .findResource("random/stuff"));
219: fs.getRoot().createData("new");
220: handle.save();
221: xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
222: + "<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.1//EN\" \"http://www.netbeans.org/dtds/filesystem-1_1.dtd\">\n"
223: + "<filesystem>\n" + " <file name=\"existing\"/>\n"
224: + " <file name=\"new\"/>\n" + "</filesystem>\n";
225: assertEquals("new layer stored", xml, TestBase.slurp(layerXML));
226: }
227:
228: public void testSystemFilesystemLocalizedNames() throws Exception {
229: File suiteDir = new File(getWorkDir(), "testSuite");
230: SuiteProjectGenerator.createSuiteProject(suiteDir,
231: NbPlatform.PLATFORM_ID_DEFAULT);
232: File module1Dir = new File(suiteDir, "testModule1");
233: NbModuleProjectGenerator.createSuiteComponentModule(module1Dir,
234: "test.module1", "module1",
235: "test/module1/resources/Bundle.properties",
236: "test/module1/resources/layer.xml", suiteDir);
237: NbModuleProject module1 = (NbModuleProject) ProjectManager
238: .getDefault().findProject(
239: FileUtil.toFileObject(module1Dir));
240: CreatedModifiedFiles cmf = new CreatedModifiedFiles(module1);
241: cmf.add(cmf.createLayerEntry("foo", null, null, "Foo", null));
242: cmf.run();
243: File module2Dir = new File(suiteDir, "testModule2");
244: NbModuleProjectGenerator.createSuiteComponentModule(module2Dir,
245: "test.module2", "module2",
246: "test/module2/resources/Bundle.properties",
247: "test/module2/resources/layer.xml", suiteDir);
248: NbModuleProject module2 = (NbModuleProject) ProjectManager
249: .getDefault().findProject(
250: FileUtil.toFileObject(module2Dir));
251: cmf = new CreatedModifiedFiles(module2);
252: cmf.add(cmf.createLayerEntry("bar", null, null, "Bar", null));
253: cmf.add(cmf.createLayerEntry("test-module2-MyAction.instance",
254: null, null, null, null));
255: cmf.add(cmf.createLayerEntry(
256: "test-module2-some-action.instance", null, null, null,
257: Collections.<String, Object> singletonMap(
258: "instanceClass", "test.module2.SomeAction")));
259: cmf.add(cmf.createLayerEntry(
260: "test-module2-another-action.instance", null, null,
261: null, Collections.<String, Object> singletonMap(
262: "instanceCreate",
263: "newvalue:test.module2.AnotherAction")));
264: cmf
265: .add(cmf
266: .createLayerEntry(
267: "test-module2-factory-action.instance",
268: null,
269: null,
270: null,
271: Collections
272: .<String, Object> singletonMap(
273: "instanceCreate",
274: "methodvalue:test.module2.FactoryAction.create")));
275: cmf.add(cmf.createLayerEntry("sep-42.instance", null, null,
276: null, Collections.<String, Object> singletonMap(
277: "instanceClass", "javax.swing.JSeparator")));
278: cmf
279: .add(cmf
280: .createLayerEntry(
281: "link-to-standard.shadow",
282: null,
283: null,
284: null,
285: Collections
286: .<String, Object> singletonMap(
287: "originalFile",
288: "Actions/System/org-openide-actions-OpenAction.instance")));
289: cmf.add(cmf.createLayerEntry("link-to-custom.shadow", null,
290: null, null, Collections.<String, Object> singletonMap(
291: "originalFile",
292: "test-module2-MyAction.instance")));
293: File dummyDir = new File(getWorkDir(), "dummy");
294: dummyDir.mkdir();
295: cmf.add(cmf.createLayerEntry("link-to-url.shadow", null, null,
296: null, Collections.<String, Object> singletonMap(
297: "originalFile", dummyDir.toURI().toURL())));
298: cmf.run();
299: FileSystem fs = LayerUtils
300: .getEffectiveSystemFilesystem(module2);
301: assertDisplayName(fs, "right display name for platform file",
302: "Menu/RunProject", "Run");
303: assertDisplayName(fs, "label for file in suite", "foo", "Foo");
304: assertDisplayName(fs, "label for file in this project", "bar",
305: "Bar");
306: assertDisplayName(fs,
307: "right display name for well-known action",
308: "Menu/File/org-openide-actions-SaveAction.instance",
309: "Save");
310: assertDisplayName(fs, "label for simple instance",
311: "test-module2-MyAction.instance",
312: "<instance of MyAction>");
313: assertDisplayName(fs, "label for instanceClass",
314: "test-module2-some-action.instance",
315: "<instance of SomeAction>");
316: assertDisplayName(fs, "label for newvalue instanceCreate",
317: "test-module2-another-action.instance",
318: "<instance of AnotherAction>");
319: assertDisplayName(fs, "label for methodvalue instanceCreate",
320: "test-module2-factory-action.instance",
321: "<instance from FactoryAction.create>");
322: assertDisplayName(fs, "label for menu separator",
323: "sep-42.instance", "<separator>");
324: assertDisplayName(fs, "link to standard menu item",
325: "link-to-standard.shadow", "Open");
326: assertDisplayName(fs, "link to custom menu item",
327: "link-to-custom.shadow", "<instance of MyAction>");
328: DataObject.find(fs.findResource("link-to-url.shadow"))
329: .getNodeDelegate().getDisplayName(); // #65665
330: /* XXX too hard to unit test in practice, since we will get a CNFE trying to load a class from editor here:
331: //System.err.println("items in Menu/Edit: " + java.util.Arrays.asList(fs.findResource("Menu/Edit").getChildren()));
332: assertDisplayName(fs, "right display name for non-action with only menu presenter", "Menu/Edit/org-netbeans-modules-editor-MainMenuAction$FindSelectionAction.instance", "Find Selection");
333: */
334: }
335:
336: public void testSystemFilesystemLocalizedNamesI18N()
337: throws Exception {
338: Locale orig = Locale.getDefault();
339: try {
340: Locale.setDefault(Locale.JAPAN);
341: File platformDir = new File(getWorkDir(), "testPlatform");
342: Manifest mf = new Manifest();
343: mf.getMainAttributes().putValue("OpenIDE-Module",
344: "platform.module");
345: mf.getMainAttributes().putValue("OpenIDE-Module-Layer",
346: "platform/module/layer.xml");
347: Map<String, String> contents = new HashMap<String, String>();
348: contents.put("platform/module/Bundle.properties",
349: "folder/file=English");
350: contents
351: .put(
352: "platform/module/layer.xml",
353: "<filesystem><folder name=\"folder\"><file name=\"file\"><attr name=\"SystemFileSystem.localizingBundle\" stringvalue=\"platform.module.Bundle\"/></file></folder></filesystem>");
354: TestBase.createJar(new File(platformDir,
355: "cluster/modules/platform-module.jar".replace('/',
356: File.separatorChar)), contents, mf);
357: mf = new Manifest();
358: contents = new HashMap<String, String>();
359: contents.put("platform/module/Bundle_ja.properties",
360: "folder/file=Japanese");
361: TestBase.createJar(new File(platformDir,
362: "cluster/modules/locale/platform-module_ja.jar"
363: .replace('/', File.separatorChar)),
364: contents, mf);
365: // To satisfy NbPlatform.isValid:
366: TestBase.createJar(new File(new File(new File(platformDir,
367: "platform"), "core"), "core.jar"),
368: Collections.EMPTY_MAP, new Manifest());
369: NbPlatform.addPlatform("testplatform", platformDir,
370: "Test Platform");
371: File suiteDir = new File(getWorkDir(), "testSuite");
372: SuiteProjectGenerator.createSuiteProject(suiteDir,
373: "testplatform");
374: File moduleDir = new File(suiteDir, "testModule");
375: NbModuleProjectGenerator.createSuiteComponentModule(
376: moduleDir, "test.module", "module",
377: "test/module/resources/Bundle.properties",
378: "test/module/resources/layer.xml", suiteDir);
379: NbModuleProject module = (NbModuleProject) ProjectManager
380: .getDefault().findProject(
381: FileUtil.toFileObject(moduleDir));
382: FileSystem fs = LayerUtils
383: .getEffectiveSystemFilesystem(module);
384: assertDisplayName(fs,
385: "#64779: localized platform filename",
386: "folder/file", "Japanese");
387: } finally {
388: Locale.setDefault(orig);
389: }
390: }
391:
392: public void testSystemFilesystemNetBeansOrgProject()
393: throws Exception {
394: FileObject nbroot = FileUtil.toFileObject(new File(System
395: .getProperty("test.nbroot")));
396: NbModuleProject p = (NbModuleProject) ProjectManager
397: .getDefault()
398: .findProject(nbroot.getFileObject("image"));
399: FileSystem fs = LayerUtils.getEffectiveSystemFilesystem(p);
400: assertDisplayName(fs,
401: "right display name for netbeans.org standard file",
402: "Menu/RunProject", "Run");
403: assertNull(
404: "not loading files from extra modules",
405: fs
406: .findResource("Templates/Documents/docbook-article.xml"));
407: FileObject docbook = nbroot.getFileObject("contrib/docbook");
408: if (docbook == null) {
409: System.err
410: .println("Skipping part of testSystemFilesystemNetBeansOrgProject since contrib is not checked out");
411: return;
412: }
413: p = (NbModuleProject) ProjectManager.getDefault().findProject(
414: docbook);
415: fs = LayerUtils.getEffectiveSystemFilesystem(p);
416: assertDisplayName(fs,
417: "right display name for file from extra module",
418: "Templates/Documents/docbook-article.xml",
419: "DocBook Article");
420: }
421:
422: // XXX testClusterAndModuleExclusions
423: // XXX testSystemFilesystemSuiteProject
424:
425: private static void assertDisplayName(FileSystem fs,
426: String message, String path, String label) throws Exception {
427: FileObject file = fs.findResource(path);
428: assertNotNull("found " + path, file);
429: assertEquals(message, label, DataObject.find(file)
430: .getNodeDelegate().getDisplayName());
431: }
432:
433: public void testMasks() throws Exception {
434: NbModuleProject project = TestBase.generateStandaloneModule(
435: getWorkDir(), "module");
436: FileSystem fs = LayerUtils
437: .getEffectiveSystemFilesystem(project);
438: Set<String> optionInstanceNames = new HashSet<String>();
439: FileObject toolsMenu = fs.findResource("Menu/Tools");
440: assertNotNull(toolsMenu);
441: for (FileObject kid : toolsMenu.getChildren()) {
442: String name = kid.getNameExt();
443: if (name.indexOf("Options") != -1) {
444: optionInstanceNames.add(name);
445: }
446: }
447: assertEquals(
448: "#63295: masks work",
449: new HashSet<String>(
450: Arrays
451: .asList("org-netbeans-modules-options-OptionsWindowAction.shadow"
452: // org-netbeans-core-actions-OptionsAction.instance should be masked
453: )), optionInstanceNames);
454: assertNotNull(
455: "system FS has xml/catalog",
456: fs
457: .findResource("Services/Hidden/CatalogProvider/org-netbeans-modules-xml-catalog-impl-XCatalogProvider.instance"));
458: assertNull(
459: "but one entry hidden by apisupport/project",
460: fs
461: .findResource("Services/Hidden/org-netbeans-modules-xml-catalog-impl-SystemCatalogProvider.instance"));
462: }
463:
464: }
|