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.TestDICookie;
045: import org.netbeans.api.debugger.test.TestDebuggerManagerListener;
046: import org.netbeans.api.debugger.test.TestLazyDebuggerManagerListener;
047:
048: import java.util.*;
049: import java.beans.PropertyChangeEvent;
050:
051: /**
052: * Launches and finishes a debugger session. Tests services registration and lookup and event firing.
053: *
054: * @author Maros Sandor
055: */
056: public class LaunchDebuggerTest extends DebuggerApiTestBase {
057:
058: public LaunchDebuggerTest(String s) {
059: super (s);
060: }
061:
062: public void testLookup() throws Exception {
063:
064: DebuggerManager dm = DebuggerManager.getDebuggerManager();
065: TestDebuggerManagerListener dml = new TestDebuggerManagerListener();
066: dm.addDebuggerListener(dml);
067:
068: TestLazyDebuggerManagerListener ldml = null;
069: for (LazyDebuggerManagerListener _ldml : dm.lookup(null,
070: LazyDebuggerManagerListener.class)) {
071: if (_ldml instanceof TestLazyDebuggerManagerListener) {
072: ldml = (TestLazyDebuggerManagerListener) _ldml;
073: break;
074: }
075: }
076: assertNotNull("Lazy debugger manager listener not loaded", ldml);
077:
078: Map args = new HashMap();
079: TestDICookie tdi = TestDICookie.create(args);
080:
081: Object[] services = new Object[] { tdi, this };
082: DebuggerInfo di = DebuggerInfo
083: .create(TestDICookie.ID, services);
084:
085: DebuggerEngine engines[] = dm.startDebugging(di);
086: assertEquals("Wrong number of debugger engines started",
087: engines.length, 1);
088:
089: testStartEvents(dml, engines);
090: testStartEvents(ldml, engines);
091:
092: DebuggerEngine debugger = engines[0];
093: DebuggerInfo dic = debugger.lookupFirst(null,
094: DebuggerInfo.class);
095: assertSame("Wrong debugger info in engine lookup", dic, di);
096: assertTrue("Engine did not start", tdi
097: .hasInfo(ActionsManager.ACTION_START));
098:
099: dm.getCurrentSession().kill();
100: assertTrue("Engine did not finish", tdi
101: .hasInfo(ActionsManager.ACTION_KILL));
102:
103: testKillEvents(dml);
104: testKillEvents(ldml);
105:
106: dm.removeDebuggerListener(dml);
107: }
108:
109: private void testKillEvents(TestDebuggerManagerListener dml) {
110: List events;
111: TestDebuggerManagerListener.Event event;
112: events = dml.getEvents();
113: assertEquals("Wrong number of events generated", events.size(),
114: 5);
115: for (Iterator i = events.iterator(); i.hasNext();) {
116: event = (TestDebuggerManagerListener.Event) i.next();
117: if (event.getName().equals("sessionRemoved")) {
118: i.remove();
119: } else if (event.getName().equals("propertyChange")) {
120: PropertyChangeEvent pce = (PropertyChangeEvent) event
121: .getParam();
122: if (pce.getPropertyName().equals("sessions")) {
123: i.remove();
124: } else if (pce.getPropertyName().equals(
125: "debuggerEngines")) {
126: i.remove();
127: } else if (pce.getPropertyName()
128: .equals("currentEngine")) {
129: assertNull("Bad current engine", pce.getNewValue());
130: i.remove();
131: } else if (pce.getPropertyName().equals(
132: "currentSession")) {
133: assertNull("Bad current session", pce.getNewValue());
134: i.remove();
135: }
136: }
137: }
138: assertEquals("Wrong events generated", events.size(), 0);
139: }
140:
141: private void testStartEvents(TestDebuggerManagerListener dml,
142: DebuggerEngine[] engines) {
143: List events;
144: TestDebuggerManagerListener.Event event;
145: events = dml.getEvents();
146: assertEquals("Wrong number of events generated", events.size(),
147: 5);
148: for (Iterator i = events.iterator(); i.hasNext();) {
149: event = (TestDebuggerManagerListener.Event) i.next();
150: if (event.getName().equals("sessionAdded")) {
151: i.remove();
152: } else if (event.getName().equals("propertyChange")) {
153: PropertyChangeEvent pce = (PropertyChangeEvent) event
154: .getParam();
155: if (pce.getPropertyName().equals("sessions")) {
156: i.remove();
157: } else if (pce.getPropertyName().equals(
158: "debuggerEngines")) {
159: i.remove();
160: } else if (pce.getPropertyName()
161: .equals("currentEngine")) {
162: assertSame("Bad PCE new current engine", pce
163: .getNewValue(), engines[0]);
164: i.remove();
165: } else if (pce.getPropertyName().equals(
166: "currentSession")) {
167: i.remove();
168: }
169: }
170: }
171: assertEquals("Wrong events generated", events.size(), 0);
172: }
173: }
|