1 19 20 package org.netbeans.modules.db.sql.editor.ui.actions; 21 22 import java.awt.Component ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import javax.swing.Action ; 26 import javax.swing.JButton ; 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 39 public class SQLExecutionBaseActionTest extends NbTestCase { 40 41 private InstanceContent instanceContent; 42 private Lookup context; 43 private SQLExecutionBaseActionImpl baseAction; 44 private Action action; 45 private SQLExecutionImpl sqlExecution; 46 47 public SQLExecutionBaseActionTest(String 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 tp = ((Presenter.Toolbar)action).getToolbarPresenter(); 95 assertTrue("The toolbar presenter should be a JButton", tp instanceof JButton ); 96 97 JButton button = (JButton )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 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 propChangeSupport = new PropertyChangeSupport (this); 133 private boolean executing = false; 134 135 public void removePropertyChangeListener(PropertyChangeListener listener) { 136 propChangeSupport.removePropertyChangeListener(listener); 137 } 138 139 public void addPropertyChangeListener(PropertyChangeListener 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 |