Revision: 6106
http://squirrel-sql.svn.sourceforge.net/squirrel-sql/?rev=6106&view=rev
Author: manningr
Date: 2011-01-02 22:22:24 +0000 (Sun, 02 Jan 2011)
Log Message:
-----------
Added common mocks for testing and new tests for plugins that implement ISessionPlugin interface.
Modified Paths:
--------------
trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/plugin/AbstractPluginTest.java
trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/plugin/AbstractSessionPluginTest.java
Modified: trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/plugin/AbstractPluginTest.java
===================================================================
--- trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/plugin/AbstractPluginTest.java 2011-01-02 22:21:17 UTC (rev 6105)
+++ trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/plugin/AbstractPluginTest.java 2011-01-02 22:22:24 UTC (rev 6106)
@@ -19,64 +19,183 @@
package net.sourceforge.squirrel_sql.client.plugin;
import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.when;
+import javax.swing.Action;
+import javax.swing.JMenu;
+
+import net.sourceforge.squirrel_sql.client.IApplication;
+import net.sourceforge.squirrel_sql.client.action.ActionCollection;
+import net.sourceforge.squirrel_sql.client.gui.builders.UIFactory;
+import net.sourceforge.squirrel_sql.client.preferences.IGlobalPreferencesPanel;
+import net.sourceforge.squirrel_sql.client.preferences.INewSessionPropertiesPanel;
+import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
+import net.sourceforge.squirrel_sql.client.session.SessionManager;
+import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
+import net.sourceforge.squirrel_sql.fw.util.IResources;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
/**
- * This class provides common tests for plugins. Each plugin test should simply extend this class to
- * pickup the common tests.
- *
+ * This class provides common tests for plugins. Each plugin test should simply extend this class to pickup
+ * the common tests.
*/
@RunWith(org.mockito.runners.MockitoJUnitRunner.class)
public abstract class AbstractPluginTest
{
protected IPlugin classUnderTest = null;
+
+ @Mock
+ protected IApplication mockApplication;
+
+ @Mock
+ protected IMessageHandler mockMessageHandler;
+
+ @Mock
+ protected SquirrelPreferences mockSquirrelPreferences;
+
+ @Mock
+ protected ActionCollection mockActionCollection;
+ @Mock
+ protected SessionManager mockSessionManager;
+ @Mock
+ protected IResources mockIResources;
+
+ @Mock
+ protected IPluginResourcesFactory mockIPluginResourcesFactory;
+
+ @Mock
+ protected JMenu mockJMenu;
+
+ @Mock
+ private Action mockAction;
+
+
+ /**
+ * Sub-class tests must implement this to return an instance of the Plugin being tested.
+ *
+ * @return an instance of the Plugin being tested.
+ */
+ protected abstract IPlugin getPluginToTest() throws Exception;
+
+ @Before
+ @SuppressWarnings("unchecked")
+ public void setUp() throws Exception
+ {
+ when(mockApplication.getMessageHandler()).thenReturn(mockMessageHandler);
+ when(mockApplication.getSquirrelPreferences()).thenReturn(mockSquirrelPreferences);
+ when(mockApplication.getActionCollection()).thenReturn(mockActionCollection);
+ when(mockApplication.getSessionManager()).thenReturn(mockSessionManager);
+ when(mockIPluginResourcesFactory.createResource(anyString(), any(IPlugin.class))).thenReturn(
+ mockIResources);
+ when(mockIResources.createMenu(Mockito.anyString())).thenReturn(mockJMenu);
+ when(mockActionCollection.get((Class<? extends Action>)Mockito.any())).thenReturn(mockAction);
+
+ try {
+ UIFactory.initialize(mockSquirrelPreferences, mockApplication);
+ } catch (Exception e) {}
+
+ classUnderTest = getPluginToTest();
+ classUnderTest.load(mockApplication);
+ classUnderTest.initialize();
+ }
+
+ @After
+ public void tearDown() throws Exception
+ {
+ classUnderTest.unload();
+ classUnderTest = null;
+ }
+
@Test
- public void testGetInternalName() {
+ public void testGetInternalName()
+ {
assertNotNull(classUnderTest.getInternalName());
}
-
+
@Test
- public void testGetDescriptiveName() {
+ public void testGetDescriptiveName()
+ {
assertNotNull(classUnderTest.getDescriptiveName());
}
- @Test
- public void testGetVersion() {
+ @Test
+ public void testGetVersion()
+ {
assertNotNull(classUnderTest.getVersion());
}
- @Test
- public void testGetAuthor() {
+ @Test
+ public void testGetAuthor()
+ {
assertNotNull(classUnderTest.getAuthor());
}
- @Test
- public void testGetChangeLogFilename() {
+ @Test
+ public void testGetChangeLogFilename()
+ {
assertNotNull(classUnderTest.getChangeLogFileName());
}
- @Test
- public void testGetHelpFilename() {
+ @Test
+ public void testGetHelpFilename()
+ {
assertNotNull(classUnderTest.getHelpFileName());
}
-
+
@Test
- public void testGetLicenseFilename() {
+ public void testGetLicenseFilename()
+ {
assertNotNull(classUnderTest.getLicenceFileName());
}
-
+
@Test
- public void testGetWebsite() {
+ public void testGetWebsite()
+ {
assertNotNull(classUnderTest.getWebSite());
}
@Test
- public void testGetContributors() {
+ public void testGetContributors()
+ {
assertNotNull(classUnderTest.getContributors());
}
+ @Test(expected = IllegalArgumentException.class)
+ public void testLoad() throws Exception
+ {
+ classUnderTest.load(null);
+ }
+
+ @Test
+ public void testGetGlobalPreferencePanels()
+ {
+ // Result should be null or a non-empty array.
+ IGlobalPreferencesPanel[] result = classUnderTest.getGlobalPreferencePanels();
+ if (result != null)
+ {
+ Assert.assertTrue(result.length > 0);
+ }
+ }
+
+ @Test
+ public void testGetNewSessionPropertiesPanel()
+ {
+ // Result should be null or a non-empty array.
+ INewSessionPropertiesPanel[] result = classUnderTest.getNewSessionPropertiesPanels();
+ if (result != null)
+ {
+ Assert.assertTrue(result.length > 0);
+ }
+ }
}
Modified: trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/plugin/AbstractSessionPluginTest.java
===================================================================
--- trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/plugin/AbstractSessionPluginTest.java 2011-01-02 22:21:17 UTC (rev 6105)
+++ trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/plugin/AbstractSessionPluginTest.java 2011-01-02 22:22:24 UTC (rev 6106)
@@ -19,15 +19,16 @@
package net.sourceforge.squirrel_sql.client.plugin;
import static org.mockito.Mockito.when;
-import net.sourceforge.squirrel_sql.client.IApplication;
+
+import java.sql.Connection;
+
import net.sourceforge.squirrel_sql.client.gui.session.SessionInternalFrame;
import net.sourceforge.squirrel_sql.client.gui.session.SessionPanel;
-import net.sourceforge.squirrel_sql.client.preferences.SquirrelPreferences;
import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
import net.sourceforge.squirrel_sql.client.session.ISession;
+import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
-import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
import org.junit.Assert;
import org.junit.Before;
@@ -45,39 +46,41 @@
protected ISQLDatabaseMetaData mockSQLDatabaseMetaData;
@Mock
- protected IApplication mockApplication;
+ protected SessionInternalFrame mockSessionInternalFrame;
@Mock
- protected IMessageHandler mockMessageHandler;
-
- @Mock
- protected SessionInternalFrame mockSessionInternalFrame;
-
- @Mock
- protected SquirrelPreferences mockSquirrelPreferences;
-
- @Mock
protected IObjectTreeAPI mockObjectTreeAPI;
@Mock
protected SessionPanel mockSessionPanel;
-
+
@Mock
protected ISQLPanelAPI mockPanelAPI;
+ @Mock
+ protected ISQLConnection mockIsqlConnection;
+
+ @Mock
+ protected Connection mockConnection;
@Before
- public void setUp() throws Exception {
- when(mockApplication.getMessageHandler()).thenReturn(mockMessageHandler);
- when(mockApplication.getSquirrelPreferences()).thenReturn(mockSquirrelPreferences);
- when(mockSession.getApplication()).thenReturn(mockApplication);
- when(mockSession.getMetaData()).thenReturn(mockSQLDatabaseMetaData);
- when(mockSession.getSessionInternalFrame()).thenReturn(mockSessionInternalFrame);
- when(mockSession.getSessionSheet()).thenReturn(mockSessionPanel);
- when(mockSQLDatabaseMetaData.getDatabaseProductName()).thenReturn(getDatabaseProductName());
- when(mockSQLDatabaseMetaData.getDatabaseProductVersion()).thenReturn(getDatabaseProductVersion());
- when(mockSessionInternalFrame.getObjectTreeAPI()).thenReturn(mockObjectTreeAPI);
- when(mockSessionPanel.getSQLPaneAPI()).thenReturn(mockPanelAPI);
+ public void setUp() throws Exception
+ {
+ // Initializes the classUnderTest according to the sub-class test implementation. It is important
+ // for tests that override setUp or declare @Before to call super.setUp to pickup initialization
+ // code in base test classes.
+ super.setUp();
+
+ when(mockSession.getApplication()).thenReturn(mockApplication);
+ when(mockSession.getMetaData()).thenReturn(mockSQLDatabaseMetaData);
+ when(mockSession.getSessionInternalFrame()).thenReturn(mockSessionInternalFrame);
+ when(mockSession.getSessionSheet()).thenReturn(mockSessionPanel);
+ when(mockSession.getSQLConnection()).thenReturn(mockIsqlConnection);
+ when(mockIsqlConnection.getConnection()).thenReturn(mockConnection);
+ when(mockSQLDatabaseMetaData.getDatabaseProductName()).thenReturn(getDatabaseProductName());
+ when(mockSQLDatabaseMetaData.getDatabaseProductVersion()).thenReturn(getDatabaseProductVersion());
+ when(mockSessionInternalFrame.getObjectTreeAPI()).thenReturn(mockObjectTreeAPI);
+ when(mockSessionPanel.getSQLPaneAPI()).thenReturn(mockPanelAPI);
}
@Test
@@ -90,16 +93,28 @@
}
}
+ @Test
+ public void testAllowSessionStartedInBackground()
+ {
+ ((ISessionPlugin) classUnderTest).allowsSessionStartedInBackground();
+ }
+
/**
* Subclass tests should provide a database product name for a session that corresponds to the plugin being
- * tested by overriding this method
+ * tested by overriding this method. If the plugin merely listens for sessions, but doesn't care what
+ * type of session they are, then this implementation will suffice.
*/
- protected abstract String getDatabaseProductName();
+ protected String getDatabaseProductName() {
+ return null;
+ }
/**
* Subclass tests should provide a database product version for a session that corresponds to the plugin
- * being tested by overriding this method
+ * being tested by overriding this method. If the plugin merely listens for sessions, but doesn't care what
+ * type of session they are, then this implementation will suffice.
*/
- protected abstract String getDatabaseProductVersion();
+ protected String getDatabaseProductVersion() {
+ return null;
+ }
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|