Revision: 6418
http://squirrel-sql.svn.sourceforge.net/squirrel-sql/?rev=6418&view=rev
Author: manningr
Date: 2011-09-27 01:12:19 +0000 (Tue, 27 Sep 2011)
Log Message:
-----------
Added sample tests for plugin project.
Modified Paths:
--------------
trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/main/java/ExampleSqlExecutionListener.java
Added Paths:
-----------
trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/
trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/java/
trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/java/ExampleExceptionFormatterTest.java
trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/java/ExamplePluginTest.java
trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/java/ExampleSqlExecutionListenerTest.java
Modified: trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/main/java/ExampleSqlExecutionListener.java
===================================================================
--- trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/main/java/ExampleSqlExecutionListener.java 2011-09-26 02:09:24 UTC (rev 6417)
+++ trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/main/java/ExampleSqlExecutionListener.java 2011-09-27 01:12:19 UTC (rev 6418)
@@ -1,6 +1,5 @@
package ${package};
-import net.sourceforge.squirrel_sql.client.session.event.ISQLExecutionListener;
import net.sourceforge.squirrel_sql.client.session.event.SQLExecutionAdapter;
import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
Added: trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/java/ExampleExceptionFormatterTest.java
===================================================================
--- trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/java/ExampleExceptionFormatterTest.java (rev 0)
+++ trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/java/ExampleExceptionFormatterTest.java 2011-09-27 01:12:19 UTC (rev 6418)
@@ -0,0 +1,34 @@
+package net.sourceforge.squirrel_sql.plugins.example;
+
+import net.sourceforge.squirrel_sql.fw.util.AbstractExceptionFormatterTest;
+import net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter;
+
+/*
+ * Copyright (C) 2010 Rob Manning
+ * man...@us...
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+public class ExampleExceptionFormatterTest extends AbstractExceptionFormatterTest
+{
+
+ @Override
+ protected ExceptionFormatter getExceptionFormatterToTest()
+ {
+ return new ExampleExceptionFormatter();
+ }
+
+}
Added: trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/java/ExamplePluginTest.java
===================================================================
--- trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/java/ExamplePluginTest.java (rev 0)
+++ trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/java/ExamplePluginTest.java 2011-09-27 01:12:19 UTC (rev 6418)
@@ -0,0 +1,98 @@
+package net.sourceforge.squirrel_sql.plugins.example;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.when;
+import net.sourceforge.squirrel_sql.client.plugin.AbstractSessionPluginTest;
+import net.sourceforge.squirrel_sql.client.plugin.IPlugin;
+
+import org.junit.Test;
+
+/*
+ * Copyright (C) 2010 Rob Manning
+ * man...@us...
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/**
+ * This test extends AbstractSessionPluginTest which provides testing for many of the methods in the
+ * ISessionPlugin interface that are implemented by such a plugin. Run this test through a code coverage tool
+ * such as Emma and you will find that many of the simpler methods in the interface are covered.
+ */
+public class ExamplePluginTest extends AbstractSessionPluginTest
+{
+ @Override
+ protected IPlugin getPluginToTest() throws Exception
+ {
+ return new ExamplePlugin();
+ }
+
+ /**
+ * getDatabaseProductName returns "DB2" because that is the product name for sessions that the example
+ * plugin is interested in. This value will be returned by the SQLDatabaseMetaData for the method called
+ * getDatabaseProductName() that is associated with Session that is used when testing methods that on the
+ * ISessionPlugin interface that require a Session.
+ *
+ * @see net.sourceforge.squirrel_sql.client.plugin.AbstractSessionPluginTest#getDatabaseProductName()
+ */
+ @Override
+ protected String getDatabaseProductName()
+ {
+ return "DB2";
+ }
+
+ /**
+ * No version is given here, because it is not required to distinguish dialects, since there is only one
+ * DB2 dialect at the current time.
+ *
+ * @see net.sourceforge.squirrel_sql.client.plugin.AbstractSessionPluginTest#getDatabaseProductVersion()
+ */
+ @Override
+ protected String getDatabaseProductVersion()
+ {
+ return null;
+ }
+
+ @Test
+ public void testSessionStarted() throws Exception
+ {
+ classUnderTest.initialize();
+ ((ExamplePlugin) classUnderTest).sessionStarted(mockSession);
+ }
+
+ @Test
+ public void testSessionStartedNullApplication() throws Exception
+ {
+ try
+ {
+ when(mockSession.getApplication()).thenReturn(null);
+ classUnderTest.initialize();
+ ((ExamplePlugin) classUnderTest).sessionStarted(mockSession);
+ fail("Exception to get an exception for null IApplication returned from Session.getApplication");
+ }
+ catch (Exception e)
+ {
+ // This is expected.
+ }
+ }
+
+ @Test
+ public void testGetGlobalPreferencePanels()
+ {
+ assertNotNull(classUnderTest.getGlobalPreferencePanels());
+ }
+
+}
Added: trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/java/ExampleSqlExecutionListenerTest.java
===================================================================
--- trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/java/ExampleSqlExecutionListenerTest.java (rev 0)
+++ trunk/sql12/plugins/squirrelsql-plugin-archetype/src/main/resources/archetype-resources/src/test/java/ExampleSqlExecutionListenerTest.java 2011-09-27 01:12:19 UTC (rev 6418)
@@ -0,0 +1,78 @@
+package net.sourceforge.squirrel_sql.plugins.example;
+
+import static org.junit.Assert.*;
+
+import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+
+/*
+ * Copyright (C) 2010 Rob Manning
+ * man...@us...
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+/**
+ * A simple test that uses Mockito to create a mock (IMessageHandler) that is required to construct the
+ * class that is being tested (ExampleSqlExecutionListener).
+ */
+@RunWith(org.mockito.runners.MockitoJUnitRunner.class)
+public class ExampleSqlExecutionListenerTest
+{
+
+ /**
+ * Class that is being tested. This should always be called classUnderTest to distinguish it as the
+ * thing being tested.
+ */
+ private ExampleSqlExecutionListener classUnderTest = null;
+
+ /**
+ * The @Mock annotation in conjunction with the MockitoJUnitRunner is pure magic. This creates the mock
+ * automatically and injects it into this test class. To the naked eye, mockMessageHandler appears to be
+ * uninitialized here. However, because it's annotated with @Mock, it actually is set to a Mockito
+ * mock implementation of IMessageHandler.
+ */
+ @Mock
+ private IMessageHandler mockMessageHandler;
+
+ @Before
+ public void setUp() {
+ classUnderTest = new ExampleSqlExecutionListener(mockMessageHandler);
+ }
+
+ @Test
+ public void testStatementExecuted()
+ {
+ final String sqlToTest = "select * from testtable";
+ classUnderTest.statementExecuted(sqlToTest);
+
+ // We just want to verify that the message handler's showMessage method was called.
+ Mockito.verify(mockMessageHandler).showMessage("statementExecuted: "+sqlToTest);
+ }
+
+ @Test
+ public void testStatementExecuting()
+ {
+ final String sqlToTest = "select * from testtable";
+ String result = classUnderTest.statementExecuting(sqlToTest);
+ assertEquals(sqlToTest, result);
+ }
+
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|