KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > XMLFileSystemTestHid


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.openide.filesystems;
21
22 import java.io.FileWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.File JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 public class XMLFileSystemTestHid extends TestBaseHid {
30
31     private String JavaDoc[] resources = new String JavaDoc[] {"a/b/c"};
32     XMLFileSystem xfs = null;
33
34     public XMLFileSystemTestHid(String JavaDoc testName) {
35         super(testName);
36     }
37
38     protected String JavaDoc[] getResources (String JavaDoc testName) {
39         return resources;
40     }
41
42
43     public void testReset () throws Exception JavaDoc {
44         FileObject a = xfs.findResource("a");
45         assertNotNull(a);
46         
47
48         FileChangeAdapter fcl = new FileChangeAdapter();
49         a.addFileChangeListener(fcl);
50         
51         resources = new String JavaDoc[] {"a/b/c","a/b1/c"};
52         xfs.setXmlUrl(createXMLLayer().toURL());
53         
54         FileObject b1 = xfs.findResource("a/b1");
55         assertNotNull(b1);
56         assertTrue(b1.isFolder());
57     }
58     
59     protected void setUp() throws Exception JavaDoc {
60         super.setUp();
61         
62         File JavaDoc f = createXMLLayer();
63         xfs = new XMLFileSystem ();
64         xfs.setXmlUrl(f.toURL());
65         this.testedFS = xfs;
66     }
67
68     private File JavaDoc createXMLLayer() throws IOException JavaDoc {
69         String JavaDoc testName = getName();
70         File JavaDoc f = TestUtilHid.createXMLLayer(testName, getResources(testName));
71         return f;
72     }
73
74     public void testChangesAreFiredOnSetXMLUrlsIssue59160() throws Exception JavaDoc {
75         File JavaDoc f = writeFile ("layer1.xml",
76 "<filesystem>\n" +
77     "<folder name='TestModule'>\n" +
78         "<file name='sample.txt' >Ahoj</file>\n" +
79     "</folder>\n" +
80 "</filesystem>\n"
81         );
82         
83         File JavaDoc f2 = writeFile ("layer2.xml",
84 "<filesystem>\n" +
85     "<folder name='TestModule'>\n" +
86         "<file name='sample.txt' >Hello!</file>\n" +
87     "</folder>\n" +
88 "</filesystem>\n"
89         );
90
91         
92         
93         
94         xfs = new XMLFileSystem (f.toURL());
95         
96         FileObject fo = xfs.findResource ("TestModule/sample.txt");
97         assertEquals ("Four bytes there", 4, fo.getSize ());
98         registerDefaultListener (fo);
99         
100         xfs.setXmlUrl (f2.toURL ());
101         
102         assertEquals ("Six bytes there", 6, fo.getSize ());
103         
104         fileChangedAssert ("Change in the content", 1);
105     }
106
107     public void testChangesAreFiredOnSetXMLUrlsWithURLsIssue59160() throws Exception JavaDoc {
108         File JavaDoc u1 = writeFile("u1.txt", "Ahoj");
109         File JavaDoc u2 = writeFile("u2.txt", "Hello!");
110         
111         File JavaDoc f = writeFile ("layer1.xml",
112 "<filesystem>\n" +
113     "<folder name='TestModule'>\n" +
114         "<file name='sample.txt' url='u1.txt' />\n" +
115     "</folder>\n" +
116 "</filesystem>\n"
117         );
118         
119         File JavaDoc f2 = writeFile ("layer2.xml",
120 "<filesystem>\n" +
121     "<folder name='TestModule'>\n" +
122         "<file name='sample.txt' url='u2.txt' />\n" +
123     "</folder>\n" +
124 "</filesystem>\n"
125         );
126
127         
128         
129         
130         xfs = new XMLFileSystem (f.toURL());
131         
132         FileObject fo = xfs.findResource ("TestModule/sample.txt");
133         assertEquals ("Four bytes there", 4, fo.getSize ());
134         registerDefaultListener (fo);
135         
136         xfs.setXmlUrl (f2.toURL ());
137         
138         assertEquals ("Six bytes there", 6, fo.getSize ());
139         
140         fileChangedAssert ("Change in the content", 1);
141     }
142     
143     public void testChangesAreFiredOnSetXMLUrlsWithAttributesIssue21204() throws Exception JavaDoc {
144         File JavaDoc f = writeFile ("layer1.xml",
145 "<filesystem>\n" +
146     "<folder name='TestModule'>\n" +
147         "<file name='sample.txt' >\n" +
148         " <attr name='value' stringvalue='old' />\n" +
149         "</file>\n" +
150     "</folder>\n" +
151 "</filesystem>\n"
152         );
153         
154         File JavaDoc f2 = writeFile ("layer2.xml",
155 "<filesystem>\n" +
156     "<folder name='TestModule'>\n" +
157         "<file name='sample.txt' >\n" +
158         " <attr name='value' stringvalue='new' />\n" +
159         "</file>\n" +
160     "</folder>\n" +
161 "</filesystem>\n"
162         );
163
164         
165         
166         
167         xfs = new XMLFileSystem (f.toURL());
168         
169         FileObject fo = xfs.findResource ("TestModule/sample.txt");
170         assertEquals("Old value is in the attribute", "old", fo.getAttribute("value"));
171         registerDefaultListener (fo);
172         
173         xfs.setXmlUrl (f2.toURL ());
174
175         assertEquals("New value is in the attribute", "new", fo.getAttribute("value"));
176         fileAttributeChangedAssert("Change in the content", 1);
177     }
178     
179     public void testChangesAreFiredOnSetXMLUrlsEvenWhenRemoved() throws Exception JavaDoc {
180         File JavaDoc u1 = writeFile("u1.txt", "Ahoj");
181         
182         File JavaDoc f = writeFile("layer1.xml",
183                 "<filesystem>\n" +
184                 "<folder name='TestModule'>\n" +
185                 "<file name='sample.txt' url='u1.txt' />\n" +
186                 "</folder>\n" +
187                 "</filesystem>\n"
188                 );
189         
190         File JavaDoc f2 = writeFile("layer2.xml",
191                 "<filesystem>\n" +
192                 "</filesystem>\n"
193                 );
194         
195         
196         
197         
198         xfs = new XMLFileSystem(f.toURL());
199         
200         FileObject fo = xfs.findResource("TestModule/sample.txt");
201         assertEquals("Four bytes there", 4, fo.getSize());
202         registerDefaultListener(fo);
203         
204         xfs.setXmlUrl(f2.toURL());
205         
206         assertFalse("Valid no more", fo.isValid());
207         assertEquals("Empty now", 0, fo.getSize());
208         
209         fileDeletedAssert("Change in the content", 1);
210     }
211     
212     public void testIssue62570() throws Exception JavaDoc {
213         File JavaDoc f = writeFile("layer3.xml",
214                 "<filesystem>\n" +
215                 "<folder name='TestModule'>\n" +
216                 "<file name='sample.txt' >Ahoj</file>\n" +
217                 "<file name='sample2.txt' url='sample2.txt'/>\n" +
218                 "</folder>\n" +
219                 "</filesystem>\n"
220                 );
221               
222         File JavaDoc f2 = new File JavaDoc(f.getParentFile(), "sample2.txt");
223         if (!f2.exists()) {
224             Thread.sleep(3000);
225             assertTrue(f2.createNewFile());
226         }
227         xfs = new XMLFileSystem(f.toURL());
228         FileObject fo = xfs.findResource ("TestModule/sample.txt");
229         assertNotNull(fo);
230         assertEquals(fo.lastModified().getTime(), f.lastModified());
231         
232         FileObject fo2 = xfs.findResource ("TestModule/sample2.txt");
233         assertNotNull(fo2);
234         assertEquals(fo2.lastModified().getTime(), f2.lastModified());
235         assertFalse(fo2.lastModified().equals(fo.lastModified()));
236         
237     }
238
239     
240     
241     private File JavaDoc writeFile(String JavaDoc name, String JavaDoc content) throws IOException JavaDoc {
242         File JavaDoc f = new File JavaDoc (getWorkDir (), name);
243         java.io.FileWriter JavaDoc w = new java.io.FileWriter JavaDoc (f);
244         w.write(content);
245         w.close();
246         return f;
247     }
248     
249     
250     public void testChangeOfAnAttributeInLayerIsFiredIfThereIsRealChange() throws Exception JavaDoc {
251         XMLFileSystem fs = new XMLFileSystem();
252         
253         File JavaDoc f1 = changeOfAnAttributeInLayerIsFiredgenerateLayer("Folder", "java.awt.List");
254         File JavaDoc f2 = changeOfAnAttributeInLayerIsFiredgenerateLayer("Folder", "java.awt.Button");
255         File JavaDoc f3 = changeOfAnAttributeInLayerIsFiredgenerateLayer("NoChange", "nochange");
256
257         fs.setXmlUrls (new URL JavaDoc[] { f1.toURL(), f3.toURL() } );
258         
259         FileObject file = fs.findResource("Folder/empty.xml");
260         assertNotNull("File found in layer", file);
261         
262         FSListener l = new FSListener();
263         file.addFileChangeListener(l);
264         
265         FileObject nochange = fs.findResource("NoChange/empty.xml");
266         assertNotNull("File found in layer", nochange);
267         FSListener no = new FSListener();
268         nochange.addFileChangeListener(no);
269         
270         assertAttr("The first value is list", file, "value", "java.awt.List");
271         assertAttr("Imutable value is nochange", nochange, "value", "nochange");
272         
273         fs.setXmlUrls (new URL JavaDoc[] { f2.toURL(), f3.toURL() } );
274         String JavaDoc v2 = (String JavaDoc) file.getAttribute("value");
275         assertEquals("The second value is button", "java.awt.Button", v2);
276         
277         assertEquals("One change: " + l.events, 1, l.events.size());
278         
279         if (!(l.events.get(0) instanceof FileAttributeEvent)) {
280             fail("Wrong event: " + l.events);
281         }
282         
283         assertAttr("Imutable value is still nochange", nochange, "value", "nochange");
284         assertEquals("No change in this attribute: " + no.events, 0, no.events.size());
285     }
286     
287     private static void assertAttr(String JavaDoc msg, FileObject fo, String JavaDoc attr, String JavaDoc value) throws IOException JavaDoc {
288         Object JavaDoc v = fo.getAttribute(attr);
289         assertEquals(msg + "[" + fo + "]", value, v);
290     }
291
292     int cnt;
293     private File JavaDoc changeOfAnAttributeInLayerIsFiredgenerateLayer(String JavaDoc folderName, String JavaDoc string) throws IOException JavaDoc {
294         File JavaDoc f = new File JavaDoc(getWorkDir(), "layer" + (cnt++) + ".xml");
295         FileWriter JavaDoc w = new FileWriter JavaDoc(f);
296         w.write(
297             "<filesystem>" +
298             "<folder name='" + folderName + "'>" +
299             " <file name='empty.xml' >" +
300             " <attr name='value' stringvalue='" + string + "' />" +
301             " </file>" +
302             "</folder>" +
303             "</filesystem>"
304         );
305         w.close();
306         return f;
307     }
308     
309     private static class FSListener extends FileChangeAdapter {
310         public List JavaDoc<FileEvent> events = new ArrayList JavaDoc<FileEvent>();
311         public List JavaDoc<FileEvent> change = new ArrayList JavaDoc<FileEvent>();
312         
313         
314         public void fileRenamed(FileRenameEvent fe) {
315             events.add(fe);
316         }
317
318         public void fileAttributeChanged(FileAttributeEvent fe) {
319             events.add(fe);
320         }
321
322         public void fileFolderCreated(FileEvent fe) {
323             events.add(fe);
324         }
325
326         public void fileDeleted(FileEvent fe) {
327             events.add(fe);
328         }
329
330         public void fileDataCreated(FileEvent fe) {
331             events.add(fe);
332         }
333
334         public void fileChanged(FileEvent fe) {
335             change.add(fe);
336         }
337         
338     }
339     
340 }
341
Popular Tags