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.modules.project.ui.actions;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.lang.ref.WeakReference;
047: import java.util.ArrayList;
048: import java.util.List;
049: import javax.swing.Action;
050: import org.netbeans.junit.NbTestCase;
051: import org.openide.filesystems.FileObject;
052: import org.openide.filesystems.FileUtil;
053: import org.openide.loaders.DataObject;
054: import org.openide.util.Lookup;
055:
056: public class LookupSensitiveActionTest extends NbTestCase {
057:
058: public LookupSensitiveActionTest(String name) {
059: super (name);
060: }
061:
062: private FileObject dir, f1, f2, f3, f4;
063: private DataObject d1, d2, d3, d4;
064:
065: protected void setUp() throws Exception {
066: super .setUp();
067: clearWorkDir();
068: dir = FileUtil.toFileObject(getWorkDir());
069: f1 = dir.createData("f1.java");
070: f2 = dir.createData("f2.java");
071: f3 = dir.createData("f3.properties");
072: f4 = dir.createData("f4.xml");
073: d1 = DataObject.find(f1);
074: d2 = DataObject.find(f2);
075: d3 = DataObject.find(f3);
076: d4 = DataObject.find(f4);
077: }
078:
079: protected void tearDown() throws Exception {
080: clearWorkDir();
081: super .tearDown();
082: }
083:
084: public boolean runInEQ() {
085: return true;
086: }
087:
088: public void testListening() throws Exception {
089:
090: // Lookup sensitive action has to refresh if and only if
091: // it has at least one property change listener
092:
093: TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
094: TestLSA tlsa = new TestLSA(lookup);
095: assertTrue("TestLSA action is enabled.", tlsa.isEnabled());
096: tlsa.refreshCounter = 0;
097:
098: lookup.change(d1);
099: assertEquals("No refresh should be called ", 0,
100: tlsa.refreshCounter);
101: lookup.change(d2);
102: lookup.change(d1);
103: assertEquals("No refresh should be called ", 0,
104: tlsa.refreshCounter);
105:
106: TestPropertyChangeListener tpcl = new TestPropertyChangeListener();
107: tlsa.addPropertyChangeListener(tpcl);
108: lookup.change(d2);
109: assertEquals("Refresh should be called once", 1,
110: tlsa.refreshCounter);
111: assertEquals("One event should be fired", 1, tpcl.getEvents()
112: .size());
113:
114: tlsa.clear();
115: tpcl.clear();
116: lookup.change(d3);
117: assertEquals("Refresh should be called once", 1,
118: tlsa.refreshCounter);
119: assertEquals("One event should be fired", 1, tpcl.getEvents()
120: .size());
121:
122: }
123:
124: public void testCorrectValuesWithoutListener() throws Exception {
125:
126: TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
127: TestLSA tlsa = new TestLSA(lookup);
128:
129: lookup.change(d1);
130: assertEquals("Action should return correct name ",
131: d1.getName(), tlsa.getValue(Action.NAME));
132:
133: assertEquals("Refresh should be called once", 1,
134: tlsa.refreshCounter);
135:
136: assertEquals("Action should return correct name ",
137: d1.getName(), tlsa.getValue(Action.NAME));
138: assertEquals("Refresh should still be called only once", 1,
139: tlsa.refreshCounter);
140:
141: }
142:
143: public void testActionGC() throws Exception {
144:
145: TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
146: TestLSA tlsa = new TestLSA(lookup);
147:
148: WeakReference<?> reference = new WeakReference<Object>(tlsa);
149: tlsa = null;
150:
151: assertGC("Action should be GCed", reference);
152:
153: }
154:
155: private static class TestLSA extends LookupSensitiveAction {
156:
157: private int performCounter;
158: private int refreshCounter;
159:
160: public TestLSA(Lookup lookup) {
161: super (null, lookup, new Class[] { DataObject.class });
162: }
163:
164: protected void actionPerformed(Lookup context) {
165: performCounter++;
166: }
167:
168: protected void refresh(Lookup context) {
169: refreshCounter++;
170:
171: DataObject dobj = context.lookup(DataObject.class);
172:
173: if (dobj != null) {
174: putValue(Action.NAME, dobj.getName());
175: }
176:
177: }
178:
179: public void clear() {
180: performCounter = refreshCounter = 0;
181: }
182:
183: }
184:
185: private static class TestPropertyChangeListener implements
186: PropertyChangeListener {
187:
188: List<PropertyChangeEvent> events = new ArrayList<PropertyChangeEvent>();
189:
190: public void propertyChange(PropertyChangeEvent e) {
191: events.add(e);
192: }
193:
194: void clear() {
195: events.clear();
196: }
197:
198: List<PropertyChangeEvent> getEvents() {
199: return events;
200: }
201:
202: }
203:
204: }
|