changeset 183:5d2a538b1020

Remove easymock dependency. Reviewed-by: mtorre Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/000613.html
author Roman Kennke <rkennke@redhat.com>
date Mon, 02 Apr 2012 14:46:46 +0200
parents 70bc509ad120
children ad08bad26237
files common/pom.xml common/src/test/java/com/redhat/thermostat/common/dao/ConnectionTest.java common/src/test/java/com/redhat/thermostat/common/dao/MongoConnectionTest.java pom.xml
diffstat 4 files changed, 185 insertions(+), 111 deletions(-) [+]
line wrap: on
line diff
--- a/common/pom.xml	Mon Apr 02 14:46:36 2012 +0200
+++ b/common/pom.xml	Mon Apr 02 14:46:46 2012 +0200
@@ -83,18 +83,6 @@
       <scope>test</scope>
     </dependency>
 
-      <!-- TODO: We should get rid of the following 2 dependencies! -->
-    <dependency>
-      <groupId>org.powermock</groupId>
-      <artifactId>powermock-api-easymock</artifactId>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.easymock</groupId>
-      <artifactId>easymock</artifactId>
-      <scope>test</scope>
-    </dependency>
-
     <dependency>
       <groupId>org.mongodb</groupId>
       <artifactId>mongo-java-driver</artifactId>
--- a/common/src/test/java/com/redhat/thermostat/common/dao/ConnectionTest.java	Mon Apr 02 14:46:36 2012 +0200
+++ b/common/src/test/java/com/redhat/thermostat/common/dao/ConnectionTest.java	Mon Apr 02 14:46:46 2012 +0200
@@ -36,103 +36,77 @@
 
 package com.redhat.thermostat.common.dao;
 
-import java.util.concurrent.CountDownLatch;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 
-import junit.framework.Assert;
-
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
 
-import com.redhat.thermostat.common.config.StartupConfiguration;
 import com.redhat.thermostat.common.dao.Connection.ConnectionListener;
 import com.redhat.thermostat.common.dao.Connection.ConnectionStatus;
-import com.redhat.thermostat.common.dao.Connection.ConnectionType;
 
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static org.powermock.api.easymock.PowerMock.createPartialMockAndInvokeDefaultConstructor;
-
-@RunWith(PowerMockRunner.class)
-@PrepareForTest(MongoConnection.class)
 public class ConnectionTest {
-    
-    /**
-     * Test if the appropriate events are notified upon connection
-     * success
-     * @throws Exception 
-     */
-    @Test
-    public void testLocalConnection() throws Exception {
-        
-        // FIXME: We should not use this level of mocking, but this will be
-        // fixed when the Storage and Connection classes refactoring
-        // will be complete.
-        Connection connection =
-                createPartialMockAndInvokeDefaultConstructor(MongoConnection.class,
-                                                             "createConnection",
-                                                             "testConnection");
-        final boolean [] testPassed = new boolean[1];
-        testPassed[0] = false;
-        final CountDownLatch latch = new CountDownLatch(1);
-        
-        connection.addListener(new ConnectionListener() {
+
+    private Connection connection;
+
+    private ConnectionListener listener1;
+    private ConnectionListener listener2;
+
+    @Before
+    public void setUp() {
+
+        connection = new Connection() {
+            
             @Override
-            public void changed(ConnectionStatus newStatus) {
-                if (newStatus == ConnectionStatus.CONNECTING) {
-                    // only release the latch when we are done
-                    return;
-                }
-                if (newStatus == ConnectionStatus.CONNECTED) {
-                    testPassed[0] = true;
-                }
-                latch.countDown();
+            public void disconnect() {
+                // TODO Auto-generated method stub
+                
+            }
+            
+            @Override
+            public void connect() {
+                // TODO Auto-generated method stub
+                
             }
-        });
-        connection.connect();
-        
-        latch.await();
-        Assert.assertTrue(testPassed[0]);
+        };
+        listener1 = mock(ConnectionListener.class);
+        listener2 = mock(ConnectionListener.class);
+        connection.addListener(listener1);
+        connection.addListener(listener2);
     }
-    
-    /**
-     * Test if the connection fails given an invalid db url
-     */
+
+    @After
+    public void tearDown() {
+        connection = null;
+        listener1 = null;
+        listener2 = null;
+    }
+
+    @Test
+    public void testListenersConnecting() throws Exception {
+        verifyListenersStatus(ConnectionStatus.CONNECTING);
+    }
+
     @Test
-    public void testInvalidLocalConnection() throws InterruptedException {
-        
-        StartupConfiguration conf = mock(StartupConfiguration.class);
-        when(conf.getDBConnectionString()).thenReturn("fluff");
-        ConnectionProvider connProv = new MongoConnectionProvider(conf);
-        
-        Connection connection = connProv.createConnection();
-        Assert.assertNotNull(connection);
-        
-        connection.setType(ConnectionType.LOCAL);
-        Assert.assertTrue(ConnectionType.LOCAL == connection.getType());
-        
-        final boolean [] testPassed = new boolean[1];
-        testPassed[0] = false;
-        final CountDownLatch latch = new CountDownLatch(1);
-        
-        connection.addListener(new ConnectionListener() {
-            @Override
-            public void changed(ConnectionStatus newStatus) {
-                if (newStatus == ConnectionStatus.CONNECTING) {
-                    // only release the latch when we are done
-                    return;
-                }
-                
-                if (newStatus == ConnectionStatus.FAILED_TO_CONNECT) {
-                    testPassed[0] = true;
-                }
-                latch.countDown();
-            }
-        });
-        connection.connect();
-        
-        latch.await();
-        Assert.assertTrue(testPassed[0]);
+    public void testListenersConnected() throws Exception {
+        verifyListenersStatus(ConnectionStatus.CONNECTED);
+    }
+
+    @Test
+    public void testListenersFailedToConnect() throws Exception {
+        verifyListenersStatus(ConnectionStatus.FAILED_TO_CONNECT);
     }
+
+    @Test
+    public void testListenersDisconnected() throws Exception {
+        verifyListenersStatus(ConnectionStatus.DISCONNECTED);
+    }
+
+    private void verifyListenersStatus(ConnectionStatus status) {
+        connection.fireChanged(status);
+        verify(listener1).changed(status);
+        verify(listener2).changed(status);
+    }
+
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/src/test/java/com/redhat/thermostat/common/dao/MongoConnectionTest.java	Mon Apr 02 14:46:46 2012 +0200
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.common.dao;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.net.UnknownHostException;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import com.mongodb.DB;
+import com.mongodb.DBCollection;
+import com.mongodb.Mongo;
+import com.mongodb.MongoException;
+import com.mongodb.MongoURI;
+import com.redhat.thermostat.common.config.StartupConfiguration;
+import com.redhat.thermostat.common.dao.Connection.ConnectionListener;
+import com.redhat.thermostat.common.dao.Connection.ConnectionStatus;
+import com.redhat.thermostat.common.storage.StorageConstants;
+
+@PrepareForTest(MongoConnection.class)
+@RunWith(PowerMockRunner.class)
+public class MongoConnectionTest {
+
+    private MongoConnection connection;
+    private ConnectionListener listener;
+
+    @Before
+    public void setUp() {
+        StartupConfiguration conf = mock(StartupConfiguration.class);
+        when(conf.getDBConnectionString()).thenReturn("mongodb://127.0.0.1:27518");
+        connection = new MongoConnection(conf);
+        listener = mock(ConnectionListener.class);
+        connection.addListener(listener);
+    }
+
+    @After
+    public void tearDown() {
+        connection = null;
+    }
+
+    @Test
+    public void testConnectSuccess() throws Exception {
+
+        DBCollection collection = mock(DBCollection.class);
+
+        DB db = mock(DB.class);
+        when(db.getCollection("agent-config")).thenReturn(collection);
+
+        Mongo mongo = mock(Mongo.class);
+        when(mongo.getDB(StorageConstants.THERMOSTAT_DB_NAME)).thenReturn(db);
+
+        PowerMockito.whenNew(Mongo.class).withParameterTypes(MongoURI.class).withArguments(any(MongoURI.class)).thenReturn(mongo);
+
+        
+        connection.connect();
+
+        
+        verify(listener).changed(ConnectionStatus.CONNECTED);
+    }
+
+    @Test
+    public void testConnectUnknownHostException() throws Exception {
+
+        PowerMockito.whenNew(Mongo.class).withParameterTypes(MongoURI.class).withArguments(any(MongoURI.class)).thenThrow(new UnknownHostException());
+
+        connection.connect();
+
+        verify(listener).changed(ConnectionStatus.FAILED_TO_CONNECT);
+    }
+
+    @Test
+    public void testConnectMongoException() throws Exception {
+
+        PowerMockito.whenNew(Mongo.class).withParameterTypes(MongoURI.class).withArguments(any(MongoURI.class)).thenThrow(new MongoException("fluff"));
+
+        connection.connect();
+
+        verify(listener).changed(ConnectionStatus.FAILED_TO_CONNECT);
+    }
+}
--- a/pom.xml	Mon Apr 02 14:46:36 2012 +0200
+++ b/pom.xml	Mon Apr 02 14:46:46 2012 +0200
@@ -147,18 +147,6 @@
         <version>${powermock.version}</version>
       </dependency>
 
-      <!-- TODO: We should get rid of the following 2 dependencies! -->
-      <dependency>
-    	<groupId>org.powermock</groupId>
-    	<artifactId>powermock-api-easymock</artifactId>
-    	<version>${powermock.version}</version>
-      </dependency>
-      <dependency>
-        <groupId>org.easymock</groupId>
-        <artifactId>easymock</artifactId>
-        <version>${easymock.version}</version>
-      </dependency>
-
       <dependency>
         <groupId>org.jfree</groupId>
         <artifactId>jfreechart</artifactId>