001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.api.debugger;
043:
044: import org.netbeans.api.debugger.test.TestLookupServiceFirst;
045: import org.netbeans.api.debugger.test.TestLookupServiceInterface;
046: import org.netbeans.api.debugger.test.TestLookupServiceSecond;
047:
048: import java.net.Socket;
049: import java.util.*;
050: import java.io.Serializable;
051:
052: /**
053: * Tests lookup functionality.
054: *
055: * @author Maros Sandor
056: */
057: public class LookupTest extends DebuggerApiTestBase {
058:
059: public LookupTest(String s) {
060: super (s);
061: }
062:
063: public static class LookupContext {
064: }
065:
066: public void testCompoundLookup() throws Exception {
067:
068: Object stringInstance = "stringInstace";
069: HashMap hashMapInstance = new HashMap();
070: Socket socketInstance = new Socket();
071: TreeMap treeMapInstance = new TreeMap();
072: StringBuffer sbInstance = new StringBuffer();
073: Object[] instances = new Object[] { stringInstance,
074: hashMapInstance, socketInstance, treeMapInstance,
075: sbInstance };
076:
077: Object stringInstance2 = "stringInstace";
078: HashMap hashMapInstance2 = new HashMap();
079: Socket socketInstance2 = new Socket();
080: TreeMap treeMapInstance2 = new TreeMap();
081: StringBuffer sbInstance2 = new StringBuffer();
082: Object[] instances2 = new Object[] { stringInstance2,
083: hashMapInstance2, socketInstance2, treeMapInstance2,
084: sbInstance2 };
085:
086: Lookup l1 = new Lookup.Instance(instances);
087: Lookup l2 = new Lookup.Instance(instances2);
088:
089: Lookup.Compound l = new Lookup.Compound(l1, l2);
090:
091: List services = l.lookup(null, String.class);
092: assertEquals("Wrong number of objects in lookup", 2, services
093: .size());
094: assertContains("Wrong looked up object", stringInstance,
095: services);
096: assertContains("Wrong looked up object", stringInstance2,
097: services);
098:
099: services = l.lookup(null, Class.class);
100: assertEquals("Wrong number of objects in lookup", 0, services
101: .size());
102:
103: services = l.lookup(null, Serializable.class);
104: assertEquals("Wrong number of objects in lookup", 8, services
105: .size());
106: assertContains("Wrong looked up object", stringInstance,
107: services);
108: assertContains("Wrong looked up object", stringInstance2,
109: services);
110: assertContains("Wrong looked up object", sbInstance, services);
111: assertContains("Wrong looked up object", sbInstance2, services);
112: assertContains("Wrong looked up object", hashMapInstance,
113: services);
114: assertContains("Wrong looked up object", hashMapInstance2,
115: services);
116: assertContains("Wrong looked up object", treeMapInstance,
117: services);
118: assertContains("Wrong looked up object", treeMapInstance2,
119: services);
120: }
121:
122: public void testMetainfLookup() throws Exception {
123:
124: Lookup.MetaInf l = new Lookup.MetaInf("unittest");
125: List list = l.lookup(null, TestLookupServiceFirst.class);
126: assertEquals("Wrong looked up object", 1, list.size());
127: assertInstanceOf("Wrong looked up object", list.get(0),
128: TestLookupServiceFirst.class);
129:
130: Object o = l.lookupFirst(null, TestLookupServiceFirst.class);
131: assertInstanceOf("Wrong looked up object", o,
132: TestLookupServiceFirst.class);
133:
134: o = l.lookupFirst(null, TestLookupServiceInterface.class);
135: assertInstanceOf("Wrong looked up object", o,
136: TestLookupServiceSecond.class);
137:
138: o = l.lookupFirst(null, TestLookupServiceSecond.class);
139: assertNull("Wrong looked up object", o);
140: }
141:
142: public void testEmptyMetainfLookup() throws Exception {
143:
144: Lookup.MetaInf l = new Lookup.MetaInf("baddir");
145: List list = l.lookup(null, TestLookupServiceFirst.class);
146: assertEquals("Wrong looked up object", 0, list.size());
147:
148: Object o = l.lookupFirst(null, TestLookupServiceFirst.class);
149: assertNull("Wrong looked up object", o);
150: }
151:
152: public void testEmptyInstanceLookup() throws Exception {
153:
154: Object[] instances = new Object[0];
155: Lookup l = new Lookup.Instance(instances);
156:
157: List services = l.lookup(null, Object.class);
158: assertEquals("Wrong number of objects in lookup", 0, services
159: .size());
160:
161: Object o = l.lookupFirst(null, Class.class);
162: assertNull("Wrong looked up object", o);
163:
164: o = l.lookupFirst(null, Object.class);
165: assertNull("Wrong looked up object", o);
166: }
167:
168: public void testInstanceLookup() throws Exception {
169:
170: Object stringInstance = "stringInstace";
171: HashMap hashMapInstance = new HashMap();
172: Socket socketInstance = new Socket();
173: TreeMap treeMapInstance = new TreeMap();
174: StringBuffer sbInstance = new StringBuffer();
175:
176: Object[] instances = new Object[] { stringInstance,
177: hashMapInstance, socketInstance, treeMapInstance,
178: sbInstance };
179: Lookup l = new Lookup.Instance(instances);
180:
181: List services = l.lookup(null, Object.class);
182: assertEquals("Wrong number of objects in lookup", 5, services
183: .size());
184: assertContains("Object not present in lookup", stringInstance,
185: services);
186: assertContains("Object not present in lookup", hashMapInstance,
187: services);
188: assertContains("Object not present in lookup", socketInstance,
189: services);
190: assertContains("Object not present in lookup", treeMapInstance,
191: services);
192: assertContains("Object not present in lookup", sbInstance,
193: services);
194:
195: services = l.lookup(null, CharSequence.class);
196: assertEquals("Wrong number of objects in lookup", 2, services
197: .size());
198: assertContains("Object not present in lookup", stringInstance,
199: services);
200: assertContains("Object not present in lookup", sbInstance,
201: services);
202:
203: services = l.lookup(null, Serializable.class);
204: assertEquals("Wrong number of objects in lookup", 4, services
205: .size());
206: assertContains("Object not present in lookup", stringInstance,
207: services);
208: assertContains("Object not present in lookup", hashMapInstance,
209: services);
210: assertContains("Object not present in lookup", treeMapInstance,
211: services);
212: assertContains("Object not present in lookup", sbInstance,
213: services);
214:
215: services = l.lookup(null, Class.class);
216: assertEquals("Wrong number of objects in lookup", 0, services
217: .size());
218:
219: Object o = l.lookupFirst(null, Class.class);
220: assertNull("Wrong looked up object", o);
221:
222: o = l.lookupFirst(null, Socket.class);
223: assertSame("Wrong looked up object", socketInstance, o);
224: }
225:
226: private void assertContains(String msg, Object obj,
227: Collection collection) {
228: for (Iterator i = collection.iterator(); i.hasNext();) {
229: if (i.next() == obj)
230: return;
231: }
232: throw new AssertionError(msg + ": " + obj);
233: }
234: }
|