KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > layers > LayerUtilsTest


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.layers;
21
22 import java.io.File JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.jar.Manifest JavaDoc;
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 /**
44  * Test writing changes to layers.
45  * @author Jesse Glick
46  */

47 public class LayerUtilsTest extends LayerTestBase {
48     
49     public LayerUtilsTest(String JavaDoc name) {
50         super(name);
51     }
52     
53     protected void setUp() throws Exception JavaDoc {
54         super.setUp();
55         TestBase.initializeBuildProperties(getWorkDir(), getDataDir());
56     }
57     
58     public void testLayerHandle() throws Exception JavaDoc {
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 JavaDoc 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         // XXX test that nbres: file contents work
85
}
86     
87     public void testLayerAutoSave() throws Exception JavaDoc {
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 JavaDoc 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     // XXX testInitiallyInvalidLayer
114
// XXX testInitiallyMissingLayer
115
// XXX testGcLayerHandle
116

117     public void testSystemFilesystemStandaloneProject() throws Exception JavaDoc {
118         NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module");
119         LayerUtils.LayerHandle handle = LayerUtils.layerForProject(project);
120         FileObject layerXML = handle.getLayerFile();
121         String JavaDoc 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 JavaDoc {
147         File JavaDoc suiteDir = new File JavaDoc(getWorkDir(), "testSuite");
148         SuiteProjectGenerator.createSuiteProject(suiteDir, NbPlatform.PLATFORM_ID_DEFAULT);
149         File JavaDoc module1Dir = new File JavaDoc(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 JavaDoc module2Dir = new File JavaDoc(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 JavaDoc 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 JavaDoc {
197         File JavaDoc suiteDir = new File JavaDoc(getWorkDir(), "testSuite");
198         SuiteProjectGenerator.createSuiteProject(suiteDir, NbPlatform.PLATFORM_ID_DEFAULT);
199         File JavaDoc module1Dir = new File JavaDoc(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 JavaDoc module2Dir = new File JavaDoc(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 JavaDoc,Object JavaDoc>singletonMap("instanceClass", "test.module2.SomeAction")));
224         cmf.add(cmf.createLayerEntry("test-module2-another-action.instance", null, null, null, Collections.<String JavaDoc,Object JavaDoc>singletonMap("instanceCreate", "newvalue:test.module2.AnotherAction")));
225         cmf.add(cmf.createLayerEntry("test-module2-factory-action.instance", null, null, null, Collections.<String JavaDoc,Object JavaDoc>singletonMap("instanceCreate", "methodvalue:test.module2.FactoryAction.create")));
226         cmf.add(cmf.createLayerEntry("sep-42.instance", null, null, null, Collections.<String JavaDoc,Object JavaDoc>singletonMap("instanceClass", "javax.swing.JSeparator")));
227         cmf.add(cmf.createLayerEntry("link-to-standard.shadow", null, null, null, Collections.<String JavaDoc,Object JavaDoc>singletonMap("originalFile", "Actions/System/org-openide-actions-OpenAction.instance")));
228         cmf.add(cmf.createLayerEntry("link-to-custom.shadow", null, null, null, Collections.<String JavaDoc,Object JavaDoc>singletonMap("originalFile", "test-module2-MyAction.instance")));
229         File JavaDoc dummyDir = new File JavaDoc(getWorkDir(), "dummy");
230         dummyDir.mkdir();
231         cmf.add(cmf.createLayerEntry("link-to-url.shadow", null, null, null, Collections.<String JavaDoc,Object JavaDoc>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(); // #65665
246
/* XXX too hard to unit test in practice, since we will get a CNFE trying to load a class from editor here:
247         //System.err.println("items in Menu/Edit: " + java.util.Arrays.asList(fs.findResource("Menu/Edit").getChildren()));
248         assertDisplayName(fs, "right display name for non-action with only menu presenter", "Menu/Edit/org-netbeans-modules-editor-MainMenuAction$FindSelectionAction.instance", "Find Selection");
249          */

250     }
251     
252     public void testSystemFilesystemLocalizedNamesI18N() throws Exception JavaDoc {
253         Locale JavaDoc orig = Locale.getDefault();
254         try {
255             Locale.setDefault(Locale.JAPAN);
256             File JavaDoc platformDir = new File JavaDoc(getWorkDir(), "testPlatform");
257             Manifest JavaDoc mf = new Manifest JavaDoc();
258             mf.getMainAttributes().putValue("OpenIDE-Module", "platform.module");
259             mf.getMainAttributes().putValue("OpenIDE-Module-Layer", "platform/module/layer.xml");
260             Map JavaDoc/*<String,String>*/ contents = new HashMap JavaDoc();
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 JavaDoc(platformDir, "cluster/modules/platform-module.jar".replace('/', File.separatorChar)), contents, mf);
264             mf = new Manifest JavaDoc();
265             contents = new HashMap JavaDoc();
266             contents.put("platform/module/Bundle_ja.properties", "folder/file=Japanese");
267             TestBase.createJar(new File JavaDoc(platformDir, "cluster/modules/locale/platform-module_ja.jar".replace('/', File.separatorChar)), contents, mf);
268             // To satisfy NbPlatform.isValid:
269
TestBase.createJar(new File JavaDoc(new File JavaDoc(new File JavaDoc(platformDir, "platform"), "core"), "core.jar"), Collections.EMPTY_MAP, new Manifest JavaDoc());
270             NbPlatform.addPlatform("testplatform", platformDir, "Test Platform");
271             File JavaDoc suiteDir = new File JavaDoc(getWorkDir(), "testSuite");
272             SuiteProjectGenerator.createSuiteProject(suiteDir, "testplatform");
273             File JavaDoc moduleDir = new File JavaDoc(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 JavaDoc {
290         FileObject nbcvsroot = FileUtil.toFileObject(new File JavaDoc(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     // XXX testClusterAndModuleExclusions
306
// XXX testSystemFilesystemSuiteProject
307

308     private static void assertDisplayName(FileSystem fs, String JavaDoc message, String JavaDoc path, String JavaDoc label) throws Exception JavaDoc {
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 JavaDoc {
315         NbModuleProject project = TestBase.generateStandaloneModule(getWorkDir(), "module");
316         FileSystem fs = LayerUtils.getEffectiveSystemFilesystem(project);
317         Set JavaDoc/*<String>*/ optionInstanceNames = new HashSet JavaDoc();
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 JavaDoc name = kids[i].getNameExt();
323             if (name.indexOf("Options") != -1) {
324                 optionInstanceNames.add(name);
325             }
326         }
327         assertEquals("#63295: masks work",
328                 new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] {
329             "org-netbeans-modules-options-OptionsWindowAction.shadow",
330             // org-netbeans-core-actions-OptionsAction.instance should be masked
331
})), 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