1 19 20 package org.openide.loaders; 21 22 import java.io.IOException ; 23 import java.io.Serializable ; 24 import java.util.Collections ; 25 import java.util.Enumeration ; 26 import java.util.List ; 27 import junit.framework.TestCase; 28 import org.openide.cookies.ConnectionCookie; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileUtil; 31 import org.openide.filesystems.Repository; 32 import org.openide.nodes.AbstractNode; 33 import org.openide.nodes.Children; 34 import org.openide.nodes.Node; 35 import org.openide.util.Enumerations; 36 import org.openide.util.RequestProcessor; 37 import org.openide.util.lookup.AbstractLookup; 38 import org.openide.util.lookup.InstanceContent; 39 40 44 public class ConnectionSupportTest extends TestCase { 45 static { 46 System.setProperty ("org.openide.util.Lookup", "org.openide.loaders.ConnectionSupportTest$Lkp"); } 48 49 public ConnectionSupportTest (String testName) { 50 super (testName); 51 } 52 53 public void testFireEvent () throws Exception { 54 FileObject root = Repository.getDefault ().getDefaultFileSystem ().getRoot (); 55 FileObject fo = FileUtil.createData (root, "SomeData.txt"); 56 57 DataObject obj = DataObject.find (fo); 58 if (! (obj instanceof MultiDataObject)) { 59 fail ("It should be multi data object: " + obj); 60 } 61 62 final T t = new T (); 63 final MultiDataObject.Entry e = ((MultiDataObject)obj).getPrimaryEntry (); 64 final ConnectionSupport sup = new ConnectionSupport ( 65 e, new T[] { t } 66 ); 67 68 sup.register (t, MN.myNode); 69 70 class BreakIt implements ConnectionSupport.Listener, Runnable { 71 public boolean called; 72 public boolean finished; 73 74 public void notify (ConnectionCookie.Event ev) { 75 called = true; 76 RequestProcessor.getDefault ().post (this).waitFinished (); 77 finished = true; 78 } 79 80 public void run () { 81 try { 82 sup.unregister (t, MN.myNode); 83 } catch (IOException ex) { 84 ex.printStackTrace(); 85 } 86 } 87 } 88 89 BreakIt b = new BreakIt (); 90 MN.myNode.b = b; 91 92 sup.fireEvent (new ConnectionSupport.Event (e.getDataObject ().getNodeDelegate (), t)); 93 94 assertTrue ("Notify called", b.called); 95 assertTrue ("Plus when calling notify none holds a lock that would prevent" + 96 "other thread from reentering the ConnectionSupport", b.finished); 97 } 98 99 private static final class MN extends AbstractNode { 100 public static MN myNode = new MN (); 101 102 public ConnectionCookie.Listener b; 103 private MN () { 104 super (Children.LEAF); 105 } 106 107 public Node.Cookie getCookie (Class c) { 108 if (c == ConnectionCookie.Listener.class) { 109 return b; 110 } 111 return null; 112 } 113 114 public Node.Handle getHandle () { 115 return new H (); 116 } 117 118 } 119 120 private static final class H implements Node.Handle, Serializable { 121 public Node getNode () { 122 return MN.myNode; 123 } 124 } 125 126 private static final class T implements ConnectionSupport.Type { 127 public Class getEventClass () { 128 return javax.swing.event.ChangeListener .class; 129 } 130 131 public boolean isPersistent () { 132 return true; 133 } 134 135 public boolean overlaps(ConnectionCookie.Type type) { 136 return getClass () == type.getClass (); 137 } 138 139 } 140 141 public static final class Lkp extends AbstractLookup { 142 public Lkp () { 143 this (new InstanceContent ()); 144 } 145 146 private Lkp (InstanceContent ic) { 147 super (ic); 148 ic.add (new Pool ()); 149 } 150 } 151 152 private static final class Pool extends DataLoaderPool { 153 static List loaders; 154 155 public Pool () { 156 } 157 158 public Enumeration loaders () { 159 if (loaders == null) { 160 return Enumerations.empty (); 161 } 162 return Collections.enumeration (loaders); 163 } 164 } 165 166 } 167 | Popular Tags |