KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > XMLDataObjectTest


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.loaders;
21
22 import org.openide.filesystems.*;
23 import java.io.*;
24 import org.openide.cookies.*;
25 import org.openide.nodes.Node;
26 import org.openide.util.RequestProcessor;
27
28 /**
29  *
30  * @author Jaroslav Tulach
31  */

32 public class XMLDataObjectTest extends org.netbeans.junit.NbTestCase {
33     private FileObject data;
34
35     /** Creates new MultiFileLoaderHid */
36     public XMLDataObjectTest (String JavaDoc name) {
37         super (name);
38     }
39
40     protected void setUp () throws Exception JavaDoc {
41         super.setUp ();
42         System.setProperty ("org.openide.util.Lookup", "org.openide.loaders.XMLDataObjectTest$Lkp");
43         String JavaDoc fsstruct [] = new String JavaDoc [] {
44         };
45         TestUtilHid.destroyLocalFileSystem (getName());
46         FileSystem fs = TestUtilHid.createLocalFileSystem (getWorkDir(), fsstruct);
47         data = FileUtil.createData (
48             fs.getRoot (),
49             "kuk/test/my.xml"
50         );
51         FileLock lock = data.lock ();
52         OutputStream os = data.getOutputStream (lock);
53         PrintStream p = new PrintStream (os);
54         
55         p.println ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
56         p.println ("<root>");
57         p.println ("</root>");
58         
59         p.close ();
60         lock.releaseLock ();
61     }
62     
63     protected void tearDown () throws Exception JavaDoc {
64         super.tearDown ();
65         TestUtilHid.destroyLocalFileSystem (getName());
66     }
67     
68     public void testGetStatusBehaviour () throws Exception JavaDoc {
69         DataObject obj = DataObject.find (data);
70         
71         assertEquals ("Is xml", XMLDataObject.class, obj.getClass ());
72         
73         XMLDataObject xml = (XMLDataObject)obj;
74         
75         assertEquals ("not parsed yet", xml.STATUS_NOT, xml.getStatus ());
76         
77         org.w3c.dom.Document JavaDoc doc = xml.getDocument ();
78         assertEquals ("still not parsed as we have lazy document", xml.STATUS_NOT, xml.getStatus ());
79         
80         String JavaDoc id = doc.getDoctype ().getPublicId ();
81         assertEquals ("still not parsed as we have special support for publilc id", xml.STATUS_NOT, xml.getStatus ());
82         
83         org.w3c.dom.Element JavaDoc e = doc.getDocumentElement ();
84         assertNotNull ("Document parsed", doc);
85         
86         assertEquals ("status is ok", xml.STATUS_OK, xml.getStatus ());
87     }
88
89     public void testCookieIsUpdatedWhenContentChanges () throws Exception JavaDoc {
90         
91         
92         FileLock lck;
93         DataObject obj;
94         lck = data.lock();
95         
96         PCL pcl = new PCL ();
97         
98         // this next line causes the test to fail on 2004/03/03
99
obj = DataObject.find (data);
100         obj.addPropertyChangeListener (pcl);
101         
102         assertNull ("No instance cookie", obj.getCookie (org.openide.cookies.InstanceCookie.class));
103         assertEquals (0, pcl.cnt);
104         
105         try {
106             OutputStream ostm = data.getOutputStream(lck);
107             PrintWriter pw = new PrintWriter(new OutputStreamWriter(ostm, "UTF8")); //NOI18N
108
pw.println("<?xml version='1.0'?>"); //NOI18N
109
pw.println("<!DOCTYPE driver PUBLIC '-//NetBeans//DTD JDBC Driver 1.0//EN' 'http://www.netbeans.org/dtds/jdbc-driver-1_0.dtd'>"); //NOI18N
110
pw.println("<driver>"); //NOI18N
111
pw.println(" <name value='somename'/>"); //NOI18N
112
pw.println(" <class value='java.lang.String'/>"); //NOI18N
113
pw.println(" <urls>"); //NOI18N
114
pw.println(" </urls>"); //NOI18N
115
pw.println("</driver>"); //NOI18N
116
pw.flush();
117             pw.close();
118             ostm.close();
119         } finally {
120             lck.releaseLock();
121         }
122         assertEquals ("One change fired when the file was written", 1, pcl.cnt);
123         assertNotNull ("There is an cookie", obj.getCookie (org.openide.cookies.InstanceCookie.class));
124     }
125     
126     public void testToolbarsAreBrokenAsTheLookupIsClearedTooOftenIssue41360 () throws Exception JavaDoc {
127         FileLock lck;
128         DataObject obj;
129         lck = data.lock();
130         String JavaDoc id = "-//NetBeans//DTD Fake Toolbar 1.0//EN";
131         
132         XMLDataObject.Info info = new XMLDataObject.Info ();
133         info.addProcessorClass (ToolbarProcessor.class);
134         try {
135             XMLDataObject.registerInfo (id, info);
136             
137             
138             OutputStream ostm = data.getOutputStream(lck);
139             PrintWriter pw = new PrintWriter(new OutputStreamWriter(ostm, "UTF8")); //NOI18N
140
pw.println("<?xml version='1.0'?>"); //NOI18N
141
pw.println("<!DOCTYPE toolbar PUBLIC '" + id + "' 'http://www.netbeans.org/dtds/jdbc-driver-1_0.dtd'>"); //NOI18N
142
pw.println("<toolbar>"); //NOI18N
143
pw.println(" <name value='somename'/>"); //NOI18N
144
pw.println(" <class value='java.lang.String'/>"); //NOI18N
145
pw.println(" <urls>"); //NOI18N
146
pw.println(" </urls>"); //NOI18N
147
pw.println("</toolbar>"); //NOI18N
148
pw.flush();
149             pw.close();
150             ostm.close();
151             
152             obj = DataObject.find (data);
153             PCL pcl = new PCL ();
154             obj.addPropertyChangeListener (pcl);
155             
156             InstanceCookie cookie = (InstanceCookie)obj.getCookie (InstanceCookie.class);
157             assertNotNull (cookie);
158             assertEquals ("No changes yet", 0, pcl.cnt);
159             
160             ostm = data.getOutputStream(lck);
161             pw = new java.io.PrintWriter JavaDoc(new OutputStreamWriter(ostm, "UTF8")); //NOI18N
162
pw.println("<?xml version='1.0'?>"); //NOI18N
163
pw.println("<!DOCTYPE toolbar PUBLIC '" + id + "' 'http://www.netbeans.org/dtds/jdbc-driver-1_0.dtd'>"); //NOI18N
164
pw.println("<toolbar>"); //NOI18N
165
pw.println(" <name value='somename'/>"); //NOI18N
166
pw.println(" <class value='java.lang.String'/>"); //NOI18N
167
pw.println(" <urls>"); //NOI18N
168
pw.println(" </urls>"); //NOI18N
169
pw.println("</toolbar>"); //NOI18N
170
pw.flush();
171             pw.close();
172             ostm.close();
173             
174             InstanceCookie newCookie = (InstanceCookie)obj.getCookie (InstanceCookie.class);
175             assertNotNull (newCookie);
176             assertEquals ("One change in document", 1, pcl.docChange);
177             assertEquals ("The cookie is still the same", cookie, newCookie);
178             assertEquals ("No cookie change", 0, pcl.cnt);
179             
180         } finally {
181             XMLDataObject.registerInfo (id, null);
182             lck.releaseLock ();
183         }
184     }
185     
186     
187     public static final class Lkp extends org.openide.util.lookup.AbstractLookup
188     implements org.openide.loaders.Environment.Provider {
189         public Lkp () {
190             this (new org.openide.util.lookup.InstanceContent ());
191         }
192         
193         private Lkp (org.openide.util.lookup.InstanceContent ic) {
194             super (ic);
195             ic.add (this); // Environment.Provider
196
}
197         
198         public org.openide.util.Lookup getEnvironment (org.openide.loaders.DataObject obj) {
199             if (obj instanceof XMLDataObject) {
200                 try {
201                     XMLDataObject xml = (XMLDataObject)obj;
202                     final String JavaDoc id = xml.getDocument ().getDoctype ().getPublicId ();
203                     if (id != null) {
204                         return org.openide.util.lookup.Lookups.singleton (new org.openide.cookies.InstanceCookie () {
205                             public Object JavaDoc instanceCreate () {
206                                 return id;
207                             }
208                             public Class JavaDoc instanceClass () {
209                                 return String JavaDoc.class;
210                             }
211                             public String JavaDoc instanceName () {
212                                 return instanceClass ().getName ();
213                             }
214                         });
215                     }
216                 } catch (Exception JavaDoc ex) {
217                     fail (ex.getMessage ());
218                 }
219             }
220             return null;
221         }
222         
223     } // end of Lkp
224

225     
226     /** Processor.
227      */

228     public static class ToolbarProcessor
229     implements XMLDataObject.Processor, InstanceCookie.Of {
230         
231         public void attachTo (org.openide.loaders.XMLDataObject xmlDO) {
232         }
233         
234         public Class JavaDoc instanceClass () throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
235             return getClass ();
236         }
237         
238         public Object JavaDoc instanceCreate () throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
239             return this;
240         }
241         
242         public String JavaDoc instanceName () {
243             return getClass ().getName ();
244         }
245         
246         public boolean instanceOf (Class JavaDoc type) {
247             return type.isAssignableFrom (getClass());
248         }
249         
250     } // end of ToolbarProcessor
251

252     public void testGetCookieCannotBeReentrantFromMoreThreads () throws Exception JavaDoc {
253         FileLock lck;
254         DataObject obj;
255         lck = data.lock();
256         String JavaDoc id = "-//NetBeans//DTD X Prcs 1.0//EN";
257         
258         XMLDataObject.Info info = new XMLDataObject.Info ();
259         info.addProcessorClass (XProcessor.class);
260         try {
261             XMLDataObject.registerInfo (id, info);
262             
263             
264             OutputStream ostm = data.getOutputStream(lck);
265             PrintWriter pw = new PrintWriter(new OutputStreamWriter(ostm, "UTF8")); //NOI18N
266
pw.println("<?xml version='1.0'?>"); //NOI18N
267
pw.println("<!DOCTYPE toolbar PUBLIC '" + id + "' 'http://www.netbeans.org/dtds/jdbc-driver-1_0.dtd'>"); //NOI18N
268
pw.println("<toolbar>"); //NOI18N
269
pw.println(" <name value='somename'/>"); //NOI18N
270
pw.println(" <class value='java.lang.String'/>"); //NOI18N
271
pw.println(" <urls>"); //NOI18N
272
pw.println(" </urls>"); //NOI18N
273
pw.println("</toolbar>"); //NOI18N
274
pw.flush();
275             pw.close();
276             ostm.close();
277             
278             obj = DataObject.find (data);
279             
280             Object JavaDoc ic = obj.getCookie(InstanceCookie.class);
281             assertNotNull("There is a cookie", ic);
282             assertEquals("The right class", XProcessor.class, ic.getClass());
283             
284             XProcessor xp = (XProcessor)ic;
285             
286             // now it can finish
287
xp.task.waitFinished();
288             
289             assertNotNull("Cookie created", xp.cookie);
290             assertEquals("It is the same as me", xp.cookie, xp);
291         } finally {
292             XMLDataObject.registerInfo (id, null);
293             lck.releaseLock ();
294         }
295     }
296
297     /** Processor.
298      */

299     public static class XProcessor
300     implements XMLDataObject.Processor, InstanceCookie.Of, Runnable JavaDoc {
301         private XMLDataObject obj;
302         private Node.Cookie cookie;
303         private RequestProcessor.Task task;
304         
305         public void attachTo (org.openide.loaders.XMLDataObject xmlDO) {
306             obj = xmlDO;
307             task = RequestProcessor.getDefault().post(this);
308             try {
309                 assertFalse("This is going to time out", task.waitFinished(500));
310             } catch (InterruptedException JavaDoc ex) {
311                 ex.printStackTrace();
312                 fail("No exceptions please");
313             }
314             assertNull("Cookie is still null", cookie);
315             
316         }
317         
318         public void run () {
319             cookie = obj.getCookie(InstanceCookie.class);
320         }
321         
322         public Class JavaDoc instanceClass () throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
323             return getClass ();
324         }
325         
326         public Object JavaDoc instanceCreate () throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
327             return this;
328         }
329         
330         public String JavaDoc instanceName () {
331             return getClass ().getName ();
332         }
333         
334         public boolean instanceOf (Class JavaDoc type) {
335             return type.isAssignableFrom (getClass());
336         }
337         
338     } // end of XProcessor
339

340     private static class PCL implements java.beans.PropertyChangeListener JavaDoc {
341         int cnt;
342         int docChange;
343
344         public void propertyChange (java.beans.PropertyChangeEvent JavaDoc ev) {
345             if (DataObject.PROP_COOKIE.equals (ev.getPropertyName ())) {
346                 cnt++;
347             }
348             if (XMLDataObject.PROP_DOCUMENT.equals (ev.getPropertyName ())) {
349                 docChange++;
350             }
351         }
352     } // end of PCL
353

354 }
355
Popular Tags