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.TestDebuggerManagerListener;
045: import java.beans.PropertyChangeListener;
046: import java.beans.PropertyChangeEvent;
047: import java.util.List;
048:
049: /**
050: * Tests adding and removing of breakpoints and firing of breakpoint events.
051: *
052: * @author Maros Sandor
053: */
054: public class BreakpointsTest extends DebuggerApiTestBase {
055:
056: public BreakpointsTest(String s) {
057: super (s);
058: }
059:
060: public void testBreakpoints() throws Exception {
061: DebuggerManager dm = DebuggerManager.getDebuggerManager();
062: TestBreakpoint tb = new TestBreakpoint();
063: TestDebuggerManagerListener dml = new TestDebuggerManagerListener();
064: dm.addDebuggerListener(dml);
065:
066: initBreakpoints(dm, dml);
067: addBreakpoint(dm, tb, dml);
068: addBreakpoint(dm, tb, dml);
069: addBreakpoint(dm, tb, dml);
070: removeBreakpoint(dm, tb, dml);
071: removeBreakpoint(dm, tb, dml);
072: addBreakpoint(dm, tb, dml);
073: removeBreakpoint(dm, tb, dml);
074: addBreakpoint(dm, tb, dml);
075: removeBreakpoint(dm, tb, dml);
076: removeBreakpoint(dm, tb, dml);
077:
078: dm.removeDebuggerListener(dml);
079: }
080:
081: public void testBreakpointValidity() throws Exception {
082: DebuggerManager dm = DebuggerManager.getDebuggerManager();
083: TestBreakpoint tb = new TestBreakpoint();
084: TestDebuggerManagerListener dml = new TestDebuggerManagerListener();
085: dm.addDebuggerListener(dml);
086:
087: initBreakpoints(dm, dml);
088: addBreakpoint(dm, tb, dml);
089: assertEquals("Wrong initial validity",
090: Breakpoint.VALIDITY.UNKNOWN, tb.getValidity());
091: final PropertyChangeEvent[] propEventPtr = new PropertyChangeEvent[] { null };
092: tb.addPropertyChangeListener(Breakpoint.PROP_VALIDITY,
093: new PropertyChangeListener() {
094: public void propertyChange(PropertyChangeEvent evt) {
095: propEventPtr[0] = evt;
096: }
097: });
098: tb.doSetValidity(Breakpoint.VALIDITY.VALID, null);
099: assertNotNull("Got no prop change event!", propEventPtr[0]);
100: assertEquals("Bad event, unexpected new validity",
101: Breakpoint.VALIDITY.VALID, propEventPtr[0]
102: .getNewValue());
103: assertEquals("Unexpected validity", Breakpoint.VALIDITY.VALID,
104: tb.getValidity());
105: propEventPtr[0] = null;
106: tb.doSetValidity(Breakpoint.VALIDITY.INVALID,
107: "Some crazy breakpoint");
108: assertNotNull("Got no prop change event!", propEventPtr[0]);
109: assertEquals("Bad event, unexpected new validity",
110: Breakpoint.VALIDITY.INVALID, propEventPtr[0]
111: .getNewValue());
112: assertEquals("Unexpected validity",
113: Breakpoint.VALIDITY.INVALID, tb.getValidity());
114: assertEquals("Unexpected reason", "Some crazy breakpoint", tb
115: .getValidityMessage());
116: }
117:
118: private void initBreakpoints(DebuggerManager dm,
119: TestDebuggerManagerListener dml) {
120: dm.getBreakpoints(); // trigger the "breakpointsInit" property change
121: TestDebuggerManagerListener.Event event;
122: List events = dml.getEvents();
123: assertEquals("Wrong PCS", 0, events.size());
124: /*
125: event = (TestDebuggerManagerListener.Event) events.get(0);
126: assertEquals("Wrong PCS", "propertyChange", event.getName());
127: PropertyChangeEvent pce = (PropertyChangeEvent) event.getParam();
128: assertEquals("Wrong PCE name", "breakpointsInit", pce.getPropertyName());
129: */
130: }
131:
132: private void removeBreakpoint(DebuggerManager dm,
133: TestBreakpoint tb, TestDebuggerManagerListener dml) {
134: List events;
135: TestDebuggerManagerListener.Event event;
136: Breakpoint[] bpts;
137:
138: int bptSize = dm.getBreakpoints().length;
139: dm.removeBreakpoint(tb);
140: events = dml.getEvents();
141: assertEquals("Wrong PCS", 2, events.size());
142: assertTrue("Wrong PCS", events
143: .remove(new TestDebuggerManagerListener.Event(
144: "breakpointRemoved", tb)));
145: event = (TestDebuggerManagerListener.Event) events.get(0);
146: assertEquals("Wrong PCS", "propertyChange", event.getName());
147: PropertyChangeEvent pce = (PropertyChangeEvent) event
148: .getParam();
149: assertEquals("Wrong PCE name", "breakpoints", pce
150: .getPropertyName());
151: bpts = dm.getBreakpoints();
152: assertEquals("Wrong number of installed breakpoionts",
153: bptSize - 1, bpts.length);
154: }
155:
156: private void addBreakpoint(DebuggerManager dm, TestBreakpoint tb,
157: TestDebuggerManagerListener dml) {
158: List events;
159: TestDebuggerManagerListener.Event event;
160: Breakpoint[] bpts;
161:
162: int bptSize = dm.getBreakpoints().length;
163: dm.addBreakpoint(tb);
164: events = dml.getEvents();
165: assertEquals("Wrong PCS", 2, events.size());
166: assertTrue("Wrong PCS", events
167: .remove(new TestDebuggerManagerListener.Event(
168: "breakpointAdded", tb)));
169: event = (TestDebuggerManagerListener.Event) events.get(0);
170: assertEquals("Wrong PCS", "propertyChange", event.getName());
171: PropertyChangeEvent pce = (PropertyChangeEvent) event
172: .getParam();
173: assertEquals("Wrong PCE name", "breakpoints", pce
174: .getPropertyName());
175: bpts = dm.getBreakpoints();
176: assertEquals("Wrong number of installed breakpoints",
177: bptSize + 1, bpts.length);
178: }
179:
180: class TestBreakpoint extends Breakpoint {
181: private boolean isEnabled;
182:
183: public boolean isEnabled() {
184: return isEnabled;
185: }
186:
187: public void disable() {
188: isEnabled = false;
189: }
190:
191: public void enable() {
192: isEnabled = true;
193: }
194:
195: public void doSetValidity(Breakpoint.VALIDITY validity,
196: String reason) {
197: setValidity(validity, reason);
198: }
199:
200: }
201: }
|