001: /*
002: /*
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
004: *
005: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common
009: * Development and Distribution License("CDDL") (collectively, the
010: * "License"). You may not use this file except in compliance with the
011: * License. You can obtain a copy of the License at
012: * http://www.netbeans.org/cddl-gplv2.html
013: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
014: * specific language governing permissions and limitations under the
015: * License. When distributing the software, include this License Header
016: * Notice in each file and include the License file at
017: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
018: * particular file as subject to the "Classpath" exception as provided
019: * by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the
021: * License Header, with the fields enclosed by brackets [] replaced by
022: * your own identifying information:
023: * "Portions Copyrighted [year] [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * The Original Software is NetBeans. The Initial Developer of the Original
028: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
029: * Microsystems, Inc. All Rights Reserved.
030: *
031: * If you wish your version of this file to be governed by only the CDDL
032: * or only the GPL Version 2, indicate your decision by adding
033: * "[Contributor] elects to include this software in this distribution
034: * under the [CDDL or GPL Version 2] license." If you do not indicate a
035: * single choice of license, a recipient has the option to distribute
036: * your version of this file under either the CDDL, the GPL Version 2 or
037: * to extend the choice of license to its licensees as provided above.
038: * However, if you add GPL Version 2 code and therefore, elected the GPL
039: * Version 2 license, then the option applies only if the new code is
040: * made subject to such option by the copyright holder.
041: */
042:
043: package DataLoaderTests.DataObjectTest;
044:
045: import java.awt.datatransfer.Transferable;
046:
047: import junit.framework.*;
048: import java.io.*;
049: import java.util.Collection;
050: import java.util.Iterator;
051: import org.netbeans.junit.*;
052: import org.openide.cookies.ConnectionCookie;
053: import org.openide.loaders.*;
054: import org.openide.filesystems.*;
055: import org.openide.nodes.AbstractNode;
056: import org.openide.nodes.Children;
057: import org.openide.nodes.Node;
058: import org.openide.util.Lookup;
059: import org.openide.util.lookup.AbstractLookup;
060: import org.openide.util.lookup.InstanceContent;
061:
062: /**
063: *
064: * @author pzajac
065: */
066: public class SomeTests extends NbTestCase {
067: // src/DataLoader/DataObjectTest/data dataFolder
068: private DataObject resources;
069: private FileObject resourcesFo;
070:
071: /** Creates a new instance of SomeTests */
072: public SomeTests(String name) {
073: super (name);
074: }
075:
076: /** Testing connection Listener
077: */
078: private static class ConListener implements
079: ConnectionCookie.Listener {
080: private ConnectionCookie.Event lastEvent;
081:
082: public void notify(org.openide.cookies.ConnectionCookie.Event ev)
083: throws IllegalArgumentException, ClassCastException {
084: System.out.println("event fired");
085: lastEvent = ev;
086: }
087:
088: public ConnectionCookie.Event getLastEvent() {
089: return lastEvent;
090: }
091:
092: /** erase last event */
093: public void clear() {
094: lastEvent = null;
095: }
096:
097: }
098:
099: private static class TestNode extends AbstractNode {
100: TestNode(Lookup lookup) {
101: super (Children.LEAF, lookup);
102: }
103:
104: public static TestNode createInstance() {
105: InstanceContent ic = new InstanceContent();
106: AbstractLookup lookup = new AbstractLookup(ic);
107: ic.add(new ConListener());
108: return new TestNode(lookup);
109: }
110:
111: }
112:
113: /** Testing ConnectionCookie.Type implementation
114: */
115:
116: private static class ConnectionTypeA implements
117: ConnectionCookie.Type {
118:
119: public Class getEventClass() {
120: return ConnectionCookie.Event.class;
121: }
122:
123: public boolean isPersistent() {
124: return false;
125: }
126:
127: public boolean overlaps(
128: org.openide.cookies.ConnectionCookie.Type type) {
129: System.out.println("overlaps: " + type.getClass());
130: boolean retType = type instanceof ConnectionTypeA;
131: System.out.println("overlaps: " + retType);
132: return retType;
133: }
134: }
135:
136: /**
137: * @param args the command line arguments
138: */
139: public static void main(String[] args) {
140: // TODO code application logic here
141: }
142:
143: protected void setUp() {
144: //initializing ide
145:
146: String xtestData = System.getProperty("xtest.data");
147: File dataDir = new File(xtestData, "DataObjectTest");
148: assertTrue(dataDir.exists());
149: resourcesFo = FileUtil.toFileObject(dataDir);
150: assertNotNull(resourcesFo);
151: resources = DataFolder.findFolder(resourcesFo);
152:
153: }
154:
155: public void testDataConnection() throws Exception {
156: //ConnectionCookie.Type
157: // get any MultiFileObject
158: System.out.println(resourcesFo);
159: log(resourcesFo.getPath());
160: MultiDataObject dobj = (MultiDataObject) DataObject
161: .find(resourcesFo.getFileObject("SwingFormObject.java"));
162: assertNotNull(dobj);
163: MultiDataObject.Entry entry = dobj.getPrimaryEntry();
164:
165: ConnectionTypeA connectionType = new ConnectionTypeA();
166:
167: ConnectionSupport conSupport = new ConnectionSupport(entry,
168: new ConnectionCookie.Type[] { connectionType });
169: TestNode node = TestNode.createInstance();
170: conSupport.register(connectionType, node);
171: ConnectionCookie.Event event = new ConnectionCookie.Event(node,
172: connectionType);
173:
174: Collection registeredTypes = conSupport.getRegisteredTypes();
175: System.out
176: .println("registered types:" + registeredTypes.size());
177: assertTrue("number reagistered types must be 1",
178: registeredTypes.size() == 1);
179:
180: conSupport.fireEvent(event);
181: ConListener listener = (ConListener) node
182: .getCookie(ConnectionCookie.Listener.class);
183: assertNotNull(listener);
184: assertNotNull(listener.getLastEvent());
185: assertTrue("fired and listened evend is not fired", listener
186: .getLastEvent() == event);
187:
188: // test unregister
189: conSupport.unregister(connectionType, node);
190: listener.clear();
191: conSupport.fireEvent(event);
192: }
193:
194: public static Test suite() {
195: NbTestSuite suite = new NbTestSuite(SomeTests.class);
196: return suite;
197: }
198:
199: public void setFilter(Filter filter) {
200: super.setFilter(filter);
201: }
202: }
|