1 20 21 package DataLoaderTests.DataObjectTest; 22 23 import java.awt.datatransfer.Transferable ; 24 25 import junit.framework.*; 26 import java.io.*; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import org.netbeans.junit.*; 30 import org.openide.cookies.ConnectionCookie; 31 import org.openide.loaders.*; 32 import org.openide.filesystems.*; 33 import org.openide.nodes.AbstractNode; 34 import org.openide.nodes.Children; 35 import org.openide.nodes.Node; 36 import org.openide.util.Lookup; 37 import org.openide.util.lookup.AbstractLookup; 38 import org.openide.util.lookup.InstanceContent; 39 40 44 public class SomeTests extends NbTestCase { 45 private DataObject resources; 47 private FileObject resourcesFo; 48 49 50 public SomeTests(String name) { 51 super(name); 52 } 53 54 55 57 private static class ConListener implements ConnectionCookie.Listener { 58 private ConnectionCookie.Event lastEvent; 59 60 public void notify(org.openide.cookies.ConnectionCookie.Event ev) throws IllegalArgumentException , ClassCastException { 61 System.out.println("event fired"); 62 lastEvent = ev; 63 } 64 65 public ConnectionCookie.Event getLastEvent () { 66 return lastEvent; 67 } 68 69 public void clear() { 70 lastEvent = null; 71 } 72 73 } 74 private static class TestNode extends AbstractNode { 75 TestNode (Lookup lookup) { 76 super(Children.LEAF,lookup); 77 } 78 79 public static TestNode createInstance() { 80 InstanceContent ic = new InstanceContent(); 81 AbstractLookup lookup = new AbstractLookup(ic); 82 ic.add (new ConListener()); 83 return new TestNode(lookup); 84 } 85 86 } 87 89 90 private static class ConnectionTypeA implements ConnectionCookie.Type { 91 92 public Class getEventClass() { 93 return ConnectionCookie.Event.class; 94 } 95 96 public boolean isPersistent() { 97 return false; 98 } 99 100 public boolean overlaps(org.openide.cookies.ConnectionCookie.Type type) { 101 System.out.println("overlaps: " + type.getClass()); 102 boolean retType = type instanceof ConnectionTypeA; 103 System.out.println("overlaps: " + retType); 104 return retType; 105 } 106 } 107 108 111 public static void main(String [] args) { 112 } 114 115 116 117 118 protected void setUp() { 119 121 122 String xtestData = System.getProperty("xtest.data"); 123 File dataDir = new File (xtestData,"DataObjectTest"); 124 assertTrue(dataDir.exists()); 125 resourcesFo = FileUtil.toFileObject(dataDir); 126 assertNotNull(resourcesFo); 127 resources = DataFolder.findFolder(resourcesFo); 128 129 } 130 131 public void testDataConnection() throws Exception { 132 System.out.println(resourcesFo); 135 log(resourcesFo.getPath()); 136 MultiDataObject dobj = (MultiDataObject) DataObject.find(resourcesFo.getFileObject("SwingFormObject.java" )); 137 assertNotNull(dobj); 138 MultiDataObject.Entry entry = dobj.getPrimaryEntry(); 139 140 ConnectionTypeA connectionType = new ConnectionTypeA(); 141 142 ConnectionSupport conSupport = new ConnectionSupport(entry, new ConnectionCookie.Type[]{connectionType}); 143 TestNode node = TestNode.createInstance(); 144 conSupport.register(connectionType, node); 145 ConnectionCookie.Event event = new ConnectionCookie.Event(node, connectionType); 146 147 Collection registeredTypes = conSupport.getRegisteredTypes() ; 148 System.out.println("registered types:" + registeredTypes.size()); 149 assertTrue("number reagistered types must be 1", registeredTypes.size() == 1); 150 151 conSupport.fireEvent(event); 152 ConListener listener = (ConListener)node.getCookie(ConnectionCookie.Listener.class); 153 assertNotNull(listener); 154 assertNotNull(listener.getLastEvent()); 155 assertTrue ("fired and listened evend is not fired", listener.getLastEvent() == event); 156 157 conSupport.unregister(connectionType, node); 159 listener.clear(); 160 conSupport.fireEvent(event); 161 } 162 163 public static Test suite() { 164 NbTestSuite suite = new NbTestSuite(SomeTests.class); 165 return suite; 166 } 167 168 public void setFilter(Filter filter) { 169 super.setFilter(filter); 170 } 171 } 172 | Popular Tags |