KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > actions > LookupSensitiveActionTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.project.ui.actions;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import org.netbeans.junit.NbTestCase;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.loaders.DataObject;
32 import org.openide.util.Lookup;
33
34 public class LookupSensitiveActionTest extends NbTestCase {
35
36     public LookupSensitiveActionTest(String JavaDoc name) {
37         super( name );
38     }
39             
40     private FileObject dir, f1, f2, f3, f4;
41     private DataObject d1, d2, d3, d4;
42         
43     protected void setUp() throws Exception JavaDoc {
44         super.setUp();
45         clearWorkDir();
46         dir = FileUtil.toFileObject(getWorkDir());
47         f1 = dir.createData("f1.java");
48         f2 = dir.createData("f2.java");
49         f3 = dir.createData("f3.properties");
50         f4 = dir.createData("f4.xml");
51         d1 = DataObject.find(f1);
52         d2 = DataObject.find(f2);
53         d3 = DataObject.find(f3);
54         d4 = DataObject.find(f4);
55     }
56     
57     protected void tearDown() throws Exception JavaDoc {
58         clearWorkDir();
59         super.tearDown();
60     }
61     
62     public boolean runInEQ () {
63         return true;
64     }
65     
66     public void testListening() throws Exception JavaDoc {
67     
68         // Lookup sensitive action has to refresh if and only if
69
// it has at least one property change listener
70

71         
72         TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
73         TestLSA tlsa = new TestLSA( lookup );
74     assertTrue ("TestLSA action is enabled.", tlsa.isEnabled ());
75     tlsa.refreshCounter = 0;
76     
77         lookup.change(d1);
78         assertEquals( "No refresh should be called ", 0, tlsa.refreshCounter );
79         lookup.change(d2);
80         lookup.change(d1);
81         assertEquals( "No refresh should be called ", 0, tlsa.refreshCounter );
82         
83                 
84         TestPropertyChangeListener tpcl = new TestPropertyChangeListener();
85         tlsa.addPropertyChangeListener( tpcl );
86         lookup.change(d2);
87         assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter );
88         assertEquals( "One event should be fired", 1, tpcl.getEvents().size() );
89         
90         
91         tlsa.clear();
92         tpcl.clear();
93         lookup.change(d3);
94         assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter );
95         assertEquals( "One event should be fired", 1, tpcl.getEvents().size() );
96         
97     }
98     
99     public void testCorrectValuesWithoutListener() throws Exception JavaDoc {
100         
101         TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
102         TestLSA tlsa = new TestLSA( lookup );
103         
104         lookup.change(d1);
105         assertEquals( "Action should return correct name ", d1.getName(), tlsa.getValue( Action.NAME ) );
106         
107         assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter );
108         
109         assertEquals( "Action should return correct name ", d1.getName(), tlsa.getValue( Action.NAME ) );
110         assertEquals( "Refresh should still be called only once", 1, tlsa.refreshCounter );
111         
112     }
113     
114     public void testActionGC() throws Exception JavaDoc {
115         
116         TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
117         TestLSA tlsa = new TestLSA( lookup );
118         
119         WeakReference JavaDoc<?> reference = new WeakReference JavaDoc<Object JavaDoc>(tlsa);
120         tlsa = null;
121         
122         assertGC( "Action should be GCed", reference );
123         
124     }
125     
126     
127     private static class TestLSA extends LookupSensitiveAction {
128         
129         private int performCounter;
130         private int refreshCounter;
131                
132         public TestLSA( Lookup lookup ) {
133             super( null, lookup, new Class JavaDoc[] { DataObject.class } );
134         }
135         
136         protected void actionPerformed( Lookup context ) {
137             performCounter++;
138         }
139            
140         protected void refresh( Lookup context ) {
141             refreshCounter++;
142             
143             DataObject dobj = context.lookup(DataObject.class);
144             
145             if (dobj != null) {
146         putValue( Action.NAME, dobj.getName() );
147         }
148             
149         }
150         
151         public void clear() {
152             performCounter = refreshCounter = 0;
153         }
154         
155         
156     }
157     
158     
159     private static class TestPropertyChangeListener implements PropertyChangeListener JavaDoc {
160         
161         List JavaDoc<PropertyChangeEvent JavaDoc> events = new ArrayList JavaDoc<PropertyChangeEvent JavaDoc>();
162         
163         public void propertyChange( PropertyChangeEvent JavaDoc e ) {
164             events.add( e );
165         }
166         
167         void clear() {
168             events.clear();
169         }
170         
171         List JavaDoc<PropertyChangeEvent JavaDoc> getEvents() {
172             return events;
173         }
174                 
175     }
176         
177     
178 }
179
Popular Tags