KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.List JavaDoc;
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 /** Some tests for the ConnectionSupport
41  *
42  * @author Jaroslav Tulach
43  */

44 public class ConnectionSupportTest extends TestCase {
45     static {
46         System.setProperty ("org.openide.util.Lookup", "org.openide.loaders.ConnectionSupportTest$Lkp"); // NOI18N
47
}
48     
49     public ConnectionSupportTest (String JavaDoc testName) {
50         super (testName);
51     }
52     
53     public void testFireEvent () throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc 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 JavaDoc 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 JavaDoc {
121         public Node getNode () {
122             return MN.myNode;
123         }
124     }
125     
126     private static final class T implements ConnectionSupport.Type {
127         public Class JavaDoc getEventClass () {
128             return javax.swing.event.ChangeListener JavaDoc.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 JavaDoc loaders;
154         
155         public Pool () {
156         }
157
158         public Enumeration JavaDoc loaders () {
159             if (loaders == null) {
160                 return Enumerations.empty ();
161             }
162             return Collections.enumeration (loaders);
163         }
164     }
165     
166 }
167
Popular Tags