KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > editor > ui > actions > SQLExecutionBaseActionTest


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.db.sql.editor.ui.actions;
21
22 import java.awt.Component JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import javax.swing.JButton JavaDoc;
27 import org.netbeans.api.db.explorer.DatabaseConnection;
28 import org.netbeans.junit.NbTestCase;
29 import org.netbeans.modules.db.api.sql.execute.SQLExecution;
30 import org.openide.util.Lookup;
31 import org.openide.util.actions.Presenter;
32 import org.openide.util.lookup.AbstractLookup;
33 import org.openide.util.lookup.InstanceContent;
34
35 /**
36  *
37  * @author Andrei Badea
38  */

39 public class SQLExecutionBaseActionTest extends NbTestCase {
40
41     private InstanceContent instanceContent;
42     private Lookup context;
43     private SQLExecutionBaseActionImpl baseAction;
44     private Action JavaDoc action;
45     private SQLExecutionImpl sqlExecution;
46
47     public SQLExecutionBaseActionTest(String JavaDoc testName) {
48         super(testName);
49     }
50
51     public void setUp() {
52         instanceContent = new InstanceContent();
53         context = new AbstractLookup(instanceContent);
54         baseAction = new SQLExecutionBaseActionImpl();
55         action = baseAction.createContextAwareInstance(context);
56         sqlExecution = new SQLExecutionImpl();
57     }
58
59     public void tearDown() {
60         sqlExecution = null;
61         action = null;
62         baseAction = null;
63         context = null;
64         instanceContent = null;
65     }
66
67     protected boolean runInEQ() {
68         return true;
69     }
70
71     public void testEnabled() {
72         assertFalse("Should be disabled when no SQLExecution in the context", action.isEnabled());
73         assertEquals("none", action.getValue(Action.NAME));
74
75         instanceContent.add(sqlExecution);
76         assertTrue("Should be enabled when SQLExecution in the context", action.isEnabled());
77         assertEquals("idle", action.getValue(Action.NAME));
78
79         sqlExecution.setExecuting(true);
80         assertFalse("Should be disabled while executing", action.isEnabled());
81         assertEquals("executing", action.getValue(Action.NAME));
82
83         sqlExecution.setExecuting(false);
84         assertTrue("Should be disabled when execution finished", action.isEnabled());
85         assertEquals("idle", action.getValue(Action.NAME));
86
87         instanceContent.remove(sqlExecution);
88         assertFalse("Should be disabled when no SQLExecution removed from the context", action.isEnabled());
89         assertEquals("none", action.getValue(Action.NAME));
90     }
91
92     public void testActionPerformed() {
93         instanceContent.add(sqlExecution);
94         Component JavaDoc tp = ((Presenter.Toolbar)action).getToolbarPresenter();
95         assertTrue("The toolbar presenter should be a JButton", tp instanceof JButton JavaDoc);
96
97         JButton JavaDoc button = (JButton JavaDoc)tp;
98         button.doClick();
99         assertTrue("Should perform the action when SQLExecution in the context", baseAction.actionPeformedCount == 1);
100
101         instanceContent.remove(sqlExecution);
102         button.doClick();
103         assertTrue("Should not perform the action when no SQLExecution in the context", baseAction.actionPeformedCount == 1);
104
105         instanceContent.add(sqlExecution);
106         button.doClick();
107         assertTrue("Should perform the action when SQLExecution in the context", baseAction.actionPeformedCount == 2);
108     }
109
110     private static final class SQLExecutionBaseActionImpl extends SQLExecutionBaseAction {
111
112         private int actionPeformedCount;
113
114         public String JavaDoc getDisplayName(SQLExecution sqlExecution) {
115             if (sqlExecution == null) {
116                 return "none";
117             } else if (sqlExecution.isExecuting()) {
118                 return "executing";
119             } else {
120                 return "idle";
121             }
122         }
123
124         protected void actionPerformed(SQLExecution sqlExecution) {
125             assertNotNull(sqlExecution);
126             actionPeformedCount++;
127         }
128     }
129
130     private static final class SQLExecutionImpl implements SQLExecution {
131
132         private PropertyChangeSupport JavaDoc propChangeSupport = new PropertyChangeSupport JavaDoc(this);
133         private boolean executing = false;
134
135         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
136             propChangeSupport.removePropertyChangeListener(listener);
137         }
138
139         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
140             propChangeSupport.addPropertyChangeListener(listener);
141         }
142
143         public boolean isExecuting() {
144             return executing;
145         }
146         
147         public void setExecuting(boolean executing) {
148             this.executing = executing;
149             propChangeSupport.firePropertyChange(SQLExecution.PROP_EXECUTING, null, null);
150         }
151         
152         public boolean isSelection() {
153             return false;
154         }
155
156         public void execute() {
157         }
158
159         public void executeSelection() {
160         }
161
162         public void setDatabaseConnection(DatabaseConnection dbconn) {
163         }
164
165         public DatabaseConnection getDatabaseConnection() {
166             return null;
167         }
168     }
169 }
170
Popular Tags