KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DataLoaderTests > DataObjectTest > SomeTests


1 /*
2 /*
3  * The contents of this file are subject to the terms of the Common Development
4  * and Distribution License (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
8  * or http://www.netbeans.org/cddl.txt.
9  *
10  * When distributing Covered Code, include this CDDL Header Notice in each file
11  * and include the License file at http://www.netbeans.org/cddl.txt.
12  * If applicable, add the following below the CDDL Header, with the fields
13  * enclosed by brackets [] replaced by your own identifying information:
14  * "Portions Copyrighted [year] [name of copyright owner]"
15  *
16  * The Original Software is NetBeans. The Initial Developer of the Original
17  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
18  * Microsystems, Inc. All Rights Reserved.
19  */

20
21 package DataLoaderTests.DataObjectTest;
22
23 import java.awt.datatransfer.Transferable JavaDoc;
24
25 import junit.framework.*;
26 import java.io.*;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
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 /**
41  *
42  * @author pzajac
43  */

44 public class SomeTests extends NbTestCase {
45     // src/DataLoader/DataObjectTest/data dataFolder
46
private DataObject resources;
47     private FileObject resourcesFo;
48     
49     /** Creates a new instance of SomeTests */
50     public SomeTests(String JavaDoc name) {
51         super(name);
52     }
53     
54         
55     /** Testing connection Listener
56      */

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 JavaDoc, ClassCastException JavaDoc {
61             System.out.println("event fired");
62             lastEvent = ev;
63         }
64         
65         public ConnectionCookie.Event getLastEvent () {
66             return lastEvent;
67         }
68         /** erase last event */
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     /** Testing ConnectionCookie.Type implementation
88      */

89     
90     private static class ConnectionTypeA implements ConnectionCookie.Type {
91         
92         public Class JavaDoc 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     /**
109      * @param args the command line arguments
110      */

111     public static void main(String JavaDoc[] args) {
112         // TODO code application logic here
113
}
114
115     
116     
117     
118     protected void setUp() {
119             //initializing ide
120

121
122             String JavaDoc 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 JavaDoc {
132         //ConnectionCookie.Type
133
// get any MultiFileObject
134
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 JavaDoc 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          // test unregister
158
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