1 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 32 public class XMLDataObjectTest extends org.netbeans.junit.NbTestCase { 33 private FileObject data; 34 35 36 public XMLDataObjectTest (String name) { 37 super (name); 38 } 39 40 protected void setUp () throws Exception { 41 super.setUp (); 42 System.setProperty ("org.openide.util.Lookup", "org.openide.loaders.XMLDataObjectTest$Lkp"); 43 String fsstruct [] = new String [] { 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 { 64 super.tearDown (); 65 TestUtilHid.destroyLocalFileSystem (getName()); 66 } 67 68 public void testGetStatusBehaviour () throws Exception { 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 doc = xml.getDocument (); 78 assertEquals ("still not parsed as we have lazy document", xml.STATUS_NOT, xml.getStatus ()); 79 80 String 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 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 { 90 91 92 FileLock lck; 93 DataObject obj; 94 lck = data.lock(); 95 96 PCL pcl = new PCL (); 97 98 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")); pw.println("<?xml version='1.0'?>"); pw.println("<!DOCTYPE driver PUBLIC '-//NetBeans//DTD JDBC Driver 1.0//EN' 'http://www.netbeans.org/dtds/jdbc-driver-1_0.dtd'>"); pw.println("<driver>"); pw.println(" <name value='somename'/>"); pw.println(" <class value='java.lang.String'/>"); pw.println(" <urls>"); pw.println(" </urls>"); pw.println("</driver>"); 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 { 127 FileLock lck; 128 DataObject obj; 129 lck = data.lock(); 130 String 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")); pw.println("<?xml version='1.0'?>"); pw.println("<!DOCTYPE toolbar PUBLIC '" + id + "' 'http://www.netbeans.org/dtds/jdbc-driver-1_0.dtd'>"); pw.println("<toolbar>"); pw.println(" <name value='somename'/>"); pw.println(" <class value='java.lang.String'/>"); pw.println(" <urls>"); pw.println(" </urls>"); pw.println("</toolbar>"); 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 (new OutputStreamWriter(ostm, "UTF8")); pw.println("<?xml version='1.0'?>"); pw.println("<!DOCTYPE toolbar PUBLIC '" + id + "' 'http://www.netbeans.org/dtds/jdbc-driver-1_0.dtd'>"); pw.println("<toolbar>"); pw.println(" <name value='somename'/>"); pw.println(" <class value='java.lang.String'/>"); pw.println(" <urls>"); pw.println(" </urls>"); pw.println("</toolbar>"); 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); } 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 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 instanceCreate () { 206 return id; 207 } 208 public Class instanceClass () { 209 return String .class; 210 } 211 public String instanceName () { 212 return instanceClass ().getName (); 213 } 214 }); 215 } 216 } catch (Exception ex) { 217 fail (ex.getMessage ()); 218 } 219 } 220 return null; 221 } 222 223 } 225 226 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 instanceClass () throws java.io.IOException , ClassNotFoundException { 235 return getClass (); 236 } 237 238 public Object instanceCreate () throws java.io.IOException , ClassNotFoundException { 239 return this; 240 } 241 242 public String instanceName () { 243 return getClass ().getName (); 244 } 245 246 public boolean instanceOf (Class type) { 247 return type.isAssignableFrom (getClass()); 248 } 249 250 } 252 public void testGetCookieCannotBeReentrantFromMoreThreads () throws Exception { 253 FileLock lck; 254 DataObject obj; 255 lck = data.lock(); 256 String 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")); pw.println("<?xml version='1.0'?>"); pw.println("<!DOCTYPE toolbar PUBLIC '" + id + "' 'http://www.netbeans.org/dtds/jdbc-driver-1_0.dtd'>"); pw.println("<toolbar>"); pw.println(" <name value='somename'/>"); pw.println(" <class value='java.lang.String'/>"); pw.println(" <urls>"); pw.println(" </urls>"); pw.println("</toolbar>"); pw.flush(); 275 pw.close(); 276 ostm.close(); 277 278 obj = DataObject.find (data); 279 280 Object 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 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 299 public static class XProcessor 300 implements XMLDataObject.Processor, InstanceCookie.Of, Runnable { 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 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 instanceClass () throws java.io.IOException , ClassNotFoundException { 323 return getClass (); 324 } 325 326 public Object instanceCreate () throws java.io.IOException , ClassNotFoundException { 327 return this; 328 } 329 330 public String instanceName () { 331 return getClass ().getName (); 332 } 333 334 public boolean instanceOf (Class type) { 335 return type.isAssignableFrom (getClass()); 336 } 337 338 } 340 private static class PCL implements java.beans.PropertyChangeListener { 341 int cnt; 342 int docChange; 343 344 public void propertyChange (java.beans.PropertyChangeEvent 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 } 354 } 355 | Popular Tags |