KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > settings > convertors > XMLPropertiesConvertorTest


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 2002 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.settings.convertors;
21
22 import java.io.*;
23
24 import org.netbeans.junit.NbTestCase;
25 import org.netbeans.junit.NbTestSuite;
26
27 import junit.textui.TestRunner;
28
29 import org.netbeans.modules.settings.convertors.FooSetting;
30 import org.netbeans.spi.settings.Convertor;
31 import org.netbeans.spi.settings.Saver;
32 import org.openide.cookies.InstanceCookie;
33 import org.openide.cookies.SaveCookie;
34 import org.openide.filesystems.FileLock;
35
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileSystem;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.filesystems.Repository;
40 import org.openide.filesystems.XMLFileSystem;
41 import org.openide.loaders.*;
42 import org.openide.modules.ModuleInfo;
43 import org.openide.util.Lookup;
44
45 /** JUnit tests
46  *
47  * @author Jan Pokorsky
48  */

49 public final class XMLPropertiesConvertorTest extends NbTestCase {
50     private FileSystem fs;
51     private FileSystem sfs;
52     private FileObject root;
53     
54     /** Creates a new instance of XMLPropertiesConvertorTest */
55     public XMLPropertiesConvertorTest(String JavaDoc name) {
56         super(name);
57     }
58     
59     protected void setUp() throws java.lang.Exception JavaDoc {
60         super.setUp();
61         Lookup.getDefault().lookup(ModuleInfo.class);
62         java.net.URL JavaDoc layer = this.getClass().getResource("data/layer.xml");
63         fs = new XMLFileSystem(layer);
64         sfs = Repository.getDefault().getDefaultFileSystem();
65         root = sfs.getRoot();
66         assertNotNull("SFS root not found", root);
67         
68         FileObject serdata = sfs.findResource("xml/lookups/NetBeans/DTD_Session_settings_1_0.instance");
69         assertNotNull("missing registration for serialdata format", serdata);
70         Object JavaDoc attr = serdata.getAttribute("instanceCreate");
71         assertNotNull("core's registration for serialdata format", attr);
72         assertEquals(SerialDataConvertor.Provider.class, attr.getClass());
73     }
74     
75     public void testReadWrite() throws Exception JavaDoc {
76         FileObject dtdFO = Repository.getDefault().getDefaultFileSystem().
77             findResource("/xml/lookups/NetBeans_org_netbeans_modules_settings_xtest/DTD_XML_FooSetting_1_0.instance");
78         assertNotNull("Provider not found", dtdFO);
79         Convertor c = XMLPropertiesConvertor.create(dtdFO);
80         FooSetting foo = new FooSetting();
81         foo.setProperty1("xxx");
82         CharArrayWriter caw = new CharArrayWriter(1024);
83         c.write(caw, foo);
84         caw.flush();
85         caw.close();
86         CharArrayReader car = new CharArrayReader(caw.toCharArray());
87         Object JavaDoc obj = c.read(car);
88         assertEquals(foo, obj);
89     }
90     
91     public void testRegisterUnregisterSaver() throws Exception JavaDoc {
92         FileObject dtdFO = fs.findResource("/xml/lookups/NetBeans_org_netbeans_modules_settings/DTD_XML_FooSetting_1_0.instance");
93         assertNotNull("Provider not found", dtdFO);
94         Convertor c = XMLPropertiesConvertor.create(dtdFO);
95         FooSetting foo = new FooSetting();
96         SaverImpl s = new SaverImpl();
97         c.registerSaver(foo, s);
98         foo.setProperty1("xxx");
99         assertEquals("Saver was not notified about the change.", SaverImpl.SAVE, s.state);
100         c.unregisterSaver(foo, s);
101         s.state = SaverImpl.NOT_CHANGED;
102         foo.setProperty1("yyy");
103         assertEquals("Saver was notified about the change.", SaverImpl.NOT_CHANGED, s.state);
104         
105         Object JavaDoc obj = new Object JavaDoc();
106         c.registerSaver(obj, s);
107         c.unregisterSaver(obj, s);
108     }
109     
110     public void testSaverNotification() throws Exception JavaDoc {
111         // test xmlproperties.ignoreChanges=aaa, property1
112
FileObject dtdFO = fs.findResource("/xml/lookups/NetBeans_org_netbeans_modules_settings/DTD_XML_FooSetting1_1_0.instance");
113         assertNotNull("Provider not found", dtdFO);
114         Convertor c = XMLPropertiesConvertor.create(dtdFO);
115         FooSetting foo = new FooSetting();
116         SaverImpl s = new SaverImpl();
117         c.registerSaver(foo, s);
118         foo.setProperty1("xxx");
119         assertEquals("Saver was notified about the ignored change.", SaverImpl.NOT_CHANGED, s.state);
120         
121         // test xmlproperties.ignoreChanges=all
122
dtdFO = fs.findResource("/xml/lookups/NetBeans_org_netbeans_modules_settings/DTD_XML_FooSetting2_1_0.instance");
123         assertNotNull("Provider not found", dtdFO);
124         c = XMLPropertiesConvertor.create(dtdFO);
125         foo = new FooSetting();
126         s = new SaverImpl();
127         c.registerSaver(foo, s);
128         foo.setProperty1("xxx");
129         assertEquals("Saver was notified about the ignored change.", SaverImpl.NOT_CHANGED, s.state);
130         
131         // test xmlproperties.preventStoring=true
132
dtdFO = fs.findResource("/xml/lookups/NetBeans_org_netbeans_modules_settings/DTD_XML_FooSetting3_1_0.instance");
133         assertNotNull("Provider not found", dtdFO);
134         c = XMLPropertiesConvertor.create(dtdFO);
135         foo = new FooSetting();
136         s = new SaverImpl();
137         c.registerSaver(foo, s);
138         foo.setProperty1("xxx");
139         assertEquals("Saver was not marked as dirty.", SaverImpl.DIRTY, s.state);
140     }
141     
142     //////////////////////////////////////////////////////////
143
// Tests on SFS
144
//////////////////////////////////////////////////////////
145

146     
147     /** Checks whether the instance is the same.
148      */

149     public void testSame() throws Exception JavaDoc {
150         FileObject tsFO = root.createFolder("testSame");
151         assertNotNull("folder 'testSame' is not created!", tsFO);
152         DataFolder folder = (DataFolder) DataObject.find(tsFO).getCookie(DataFolder.class);
153         assertNotNull("missing data folder" + folder);
154         
155         FooSetting ser = new FooSetting("A");
156         
157         InstanceDataObject i = InstanceDataObject.create (folder, null, ser, null);
158         
159         InstanceCookie.Of ic = (InstanceCookie.Of) i.getCookie(InstanceCookie.Of.class);
160         assertNotNull (i + " does not contain instance cookie", ic);
161         
162         assertTrue("instanceOf failed", ic.instanceOf(ser.getClass()));
163         assertEquals("instanceClass", ser.getClass(), ic.instanceClass());
164         
165         Object JavaDoc n = ic.instanceCreate ();
166         assertEquals("Value is different from stored one", System.identityHashCode(ser), System.identityHashCode(n));
167         
168         ser.setProperty1("B");
169         ic = (InstanceCookie.Of) i.getCookie(InstanceCookie.Of.class);
170         assertEquals("Value is different from stored one", ser, ic.instanceCreate());
171         
172         for (int j = 0; j <100; j++) {
173             ser.setProperty1(String.valueOf(j));
174         }
175         ic = (InstanceCookie.Of) i.getCookie(InstanceCookie.Of.class);
176         assertEquals("Value is different from stored one", ser, ic.instanceCreate());
177         
178         i.delete();
179     }
180     
181     /** Test whether instances survive garbage collection.
182      */

183     public void testSameWithGC () throws Exception JavaDoc {
184         FileObject tsFO = root.createFolder("testSameWithGC");
185         assertNotNull("folder 'testSameWithGC' is not created!", tsFO);
186         DataFolder folder = (DataFolder) DataObject.find(tsFO).getCookie(DataFolder.class);
187         assertNotNull("missing data folder" + folder);
188         
189         FooSetting ser = new FooSetting("testSameWithGC");
190         
191         FileObject prim = InstanceDataObject.create (folder, "MyName", ser, null).getPrimaryFile ();
192         String JavaDoc name = prim.getName ();
193         String JavaDoc ext = prim.getExt ();
194         prim = null;
195
196         System.gc ();
197         System.gc ();
198         System.gc ();
199         System.gc ();
200         System.gc ();
201         System.gc ();
202         System.gc ();
203         System.gc ();
204         System.gc ();
205         
206         FileObject fo = folder.getPrimaryFile ().getFileObject (name, ext);
207         assertNotNull ("MyName.settings not found", fo);
208         
209         DataObject obj = DataObject.find (fo);
210         
211         InstanceCookie.Of ic = (InstanceCookie.Of) obj.getCookie(InstanceCookie.Of.class);
212         assertNotNull (obj + " does not contain instance cookie", ic);
213         
214         assertTrue("instanceOf failed", ic.instanceOf(ser.getClass()));
215         assertEquals("instanceClass", ser.getClass(), ic.instanceClass());
216         
217         Object JavaDoc value = ic.instanceCreate ();
218         assertEquals("Value is different from stored one", System.identityHashCode(ser), System.identityHashCode(value));
219     }
220     
221     public void testLookupSetting() throws Exception JavaDoc {
222         Object JavaDoc obj = Lookup.getDefault().lookup(FooSetting.class);
223         assertNotNull("setting not found via the lookup api", obj);
224         assertEquals(FooSetting.class, obj.getClass());
225         
226         FooSetting foo = (FooSetting) obj;
227         assertEquals("localhost", foo.getProperty1());
228     }
229
230     /* default instance in serial data format -> xml properties format
231      */

232     public void testUpgradeSetting() throws Exception JavaDoc {
233         String JavaDoc res = "Settings/org-netbeans-modules-settings-convertors-FooSettingSerialData.settings";
234         FileObject fo = sfs.findResource(res);
235         assertNotNull(res, fo);
236         long last = fo.lastModified().getTime();
237         
238         DataObject dobj = DataObject.find (fo);
239         InstanceCookie.Of ic = (InstanceCookie.Of) dobj.getCookie(InstanceCookie.Of.class);
240         assertNotNull (dobj + " does not contain instance cookie", ic);
241         assertTrue("instanceOf failed", ic.instanceOf(FooSetting.class));
242         assertEquals("instanceClass failed", FooSetting.class, ic.instanceClass());
243         
244         FooSetting foo = (FooSetting) ic.instanceCreate();
245         assertEquals("too early upgrade", last, fo.lastModified().getTime());
246         
247         foo.setProperty1("A");
248         Thread.sleep(3000);
249         assertTrue("upgrade failed", last != fo.lastModified().getTime());
250     }
251     
252     /* object of deprecated class persisted in serial data format -> new object
253      * persisted in xml properties format
254      */

255     public void testUpgradeSetting2() throws Exception JavaDoc {
256         String JavaDoc res = "Settings/testUpgradeSetting2/ObsoleteClass.settings";
257         FileObject fo = sfs.findResource(res);
258         assertNotNull(res, fo);
259         long last = fo.lastModified().getTime();
260         
261         DataObject dobj = DataObject.find(fo);
262         InstanceCookie.Of ic = (InstanceCookie.Of) dobj.getCookie(InstanceCookie.Of.class);
263         assertNotNull (dobj + " does not contain instance cookie", ic);
264         assertTrue("instanceOf failed", ic.instanceOf(FooSetting.class));
265         assertEquals("instanceClass failed", FooSetting.class, ic.instanceClass());
266         
267         FooSetting foo = (FooSetting) ic.instanceCreate();
268         assertEquals("too early upgrade", last, fo.lastModified().getTime());
269         
270         foo.setProperty1("A");
271         Thread.sleep(3000);
272         assertTrue("upgrade failed", last != fo.lastModified().getTime());
273     }
274     
275     public void testUpgradeSettingWithUnknownClass() throws Exception JavaDoc {
276         String JavaDoc res = "Settings/org-netbeans-modules-settings-convertors-FooSettingSerialDataUnknownClass.settings";
277         FileObject fo = sfs.findResource(res);
278         assertNotNull(res, fo);
279         long last = fo.lastModified().getTime();
280         
281         DataObject dobj = DataObject.find (fo);
282         InstanceCookie.Of ic = (InstanceCookie.Of) dobj.getCookie(InstanceCookie.Of.class);
283         assertNotNull (dobj + " does not contain instance cookie", ic);
284         assertTrue("instanceOf failed", ic.instanceOf(FooSetting.class));
285         assertEquals("instanceClass failed", FooSetting.class, ic.instanceClass());
286         
287         FooSetting foo = (FooSetting) ic.instanceCreate();
288         assertEquals("too early upgrade", last, fo.lastModified().getTime());
289         
290         foo.setProperty1("A");
291         Thread.sleep(3000);
292         
293         DataInputStream dis = new DataInputStream(new BufferedInputStream(fo.getInputStream(), 1024));
294         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(dis.readLine());
295         sb.append(dis.readLine());
296         sb.append(dis.readLine());
297         dis.close();
298         String JavaDoc line = sb.toString();
299         
300         assertTrue("upgrade failed: " + line, line.indexOf("properties") > 0);
301 // assertTrue("upgrade failed", last != fo.lastModified().getTime());
302

303     }
304     
305     public void testDeleteSettings() throws Exception JavaDoc {
306         FileObject root = sfs.getRoot();
307         DataFolder folder = DataFolder.findFolder(root);
308         
309         String JavaDoc filename = "testDeleteSettings";
310         FooSetting obj = new FooSetting();
311         InstanceDataObject ido = InstanceDataObject.create(folder, filename, obj, null, false);
312         assertNotNull("InstanceDataObject.create cannot return null!", ido);
313         
314         obj.setProperty1("testDeleteSettings");
315         Thread.sleep(500);
316         ido.delete();
317         assertNull(filename + ".settings was not deleted!", root.getFileObject(filename));
318         assertEquals("Listener not deregistered", 0, obj.getListenerCount());
319         Thread.sleep(3000);
320         assertNull(filename + ".settings was not deleted!", root.getFileObject(filename));
321     }
322     
323     public void testModuleDisabling() throws Exception JavaDoc {
324         FileObject dtd = sfs.findResource("xml/lookups/NetBeans_org_netbeans_modules_settings_testModuleDisabling/DTD_XML_FooSetting_1_0.instance");
325         assertNotNull(dtd);
326         FileObject xml = sfs.findResource("Settings/org-netbeans-modules-settings-convertors-testModuleDisabling.settings");
327         assertNotNull(xml);
328         DataObject dobj = DataObject.find(xml);
329         InstanceCookie cookie = (InstanceCookie) dobj.getCookie(InstanceCookie.class);
330         assertNotNull(cookie);
331         cookie = null;
332         
333         FileObject folder = sfs.findResource("xml/lookups/NetBeans_org_netbeans_modules_settings_testModuleDisabling");
334         assertNotNull(folder);
335         // this simulate the disabling of a module; the layer containing the dtd
336
// registration is removed
337
folder.delete();
338         cookie = (InstanceCookie) dobj.getCookie(InstanceCookie.class);
339         assertNull("" + cookie, cookie);
340     }
341    
342     public void testCorruptedSettingsFile() throws Exception JavaDoc {
343         final FileObject corrupted = sfs.findResource("/Settings/org-netbeans-modules-settings-convertors-FooSettingXMLPropCorruptedTest.settings");
344         assertNotNull(corrupted);
345         
346         DataObject ido = DataObject.find(corrupted);
347         InstanceCookie ic = (InstanceCookie) ido.getCookie(InstanceCookie.class);
348         assertNotNull("Missing InstanceCookie", ic);
349         
350         Object JavaDoc obj = null;
351         try {
352             obj = ic.instanceCreate();
353         } catch (IOException ex) {
354         }
355         assertNull("corrupted .settings file cannot provide an object", obj);
356         
357         final FileObject valid = sfs.findResource("/Services/org-netbeans-modules-settings-convertors-FooSetting.settings");
358         assertNotNull(valid);
359         
360         // simulate revert to default of a corrupted setting object
361
corrupted.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
362             public void run() throws IOException {
363                 FileLock l = null;
364                 OutputStream os = null;
365                 try {
366                     l = corrupted.lock();
367                     os = corrupted.getOutputStream(l);
368                     FileUtil.copy(valid.getInputStream(), os);
369                     os.flush();
370                 } finally {
371                     if (os != null) try { os.close(); } catch (IOException ex) {}
372                     if (l != null) l.releaseLock();
373                 }
374             }
375         });
376         
377         ic = (InstanceCookie) ido.getCookie(InstanceCookie.class);
378         assertNotNull("Missing InstanceCookie", ic);
379         assertNotNull("the persisted object cannot be read", ic.instanceCreate());
380     }
381     
382     final class SaverImpl implements Saver {
383         static final int NOT_CHANGED = 0;
384         static final int DIRTY = 1;
385         static final int SAVE = 2;
386         int state = NOT_CHANGED;
387         public void markDirty() {
388             state = DIRTY;
389         }
390         public void requestSave() throws java.io.IOException JavaDoc {
391             state = SAVE;
392         }
393     }
394     
395     
396 }
397
Popular Tags