changeset 1362:fefa99b232b5

Fix GUI hang if connection url is bad Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-November/008914.html
author Omair Majid <omajid@redhat.com>
date Mon, 25 Nov 2013 12:11:41 -0500
parents 9c31ba800d36
children cf184d4064b2
files storage/mongo/src/main/java/com/redhat/thermostat/storage/mongodb/internal/MongoConnection.java storage/mongo/src/test/java/com/redhat/thermostat/storage/mongodb/internal/MongoConnectionTest.java
diffstat 2 files changed, 33 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/storage/mongo/src/main/java/com/redhat/thermostat/storage/mongodb/internal/MongoConnection.java	Mon Nov 25 11:59:22 2013 -0500
+++ b/storage/mongo/src/main/java/com/redhat/thermostat/storage/mongodb/internal/MongoConnection.java	Mon Nov 25 12:11:41 2013 -0500
@@ -55,6 +55,7 @@
 import com.redhat.thermostat.common.utils.HostPortPair;
 import com.redhat.thermostat.common.utils.HostPortsParser;
 import com.redhat.thermostat.common.utils.LoggingUtils;
+import com.redhat.thermostat.shared.config.InvalidConfigurationException;
 import com.redhat.thermostat.shared.config.SSLConfiguration;
 import com.redhat.thermostat.storage.config.AuthenticationConfiguration;
 import com.redhat.thermostat.storage.config.StartupConfiguration;
@@ -85,7 +86,7 @@
             testConnection();
             connected = true;
 
-        } catch (IOException | MongoException | IllegalArgumentException e) {
+        } catch (IOException | MongoException | InvalidConfigurationException | IllegalArgumentException e) {
             logger.log(Level.WARNING, "Failed to connect to storage", e);
             fireChanged(ConnectionStatus.FAILED_TO_CONNECT);
             throw new ConnectionException(e.getMessage(), e);
@@ -124,7 +125,7 @@
     }
 
     // package visibility for testing purposes.
-    void createConnection() throws MongoException, UnknownHostException {
+    void createConnection() throws MongoException, InvalidConfigurationException, UnknownHostException {
         if (sslConf.enableForBackingStorage()) {
             logger.log(Level.FINE, "Using SSL socket for mongodb:// protocol");
             this.m = getSSLMongo();
@@ -156,7 +157,7 @@
         return new Mongo(getServerAddress(), opts);
     }
 
-    ServerAddress getServerAddress() throws UnknownHostException {
+    ServerAddress getServerAddress() throws InvalidConfigurationException, UnknownHostException {
         String url = conf.getDBConnectionString();
         // Strip mongodb prefix: "mongodb://".length() == 10
         String hostPort = url.substring(10);
--- a/storage/mongo/src/test/java/com/redhat/thermostat/storage/mongodb/internal/MongoConnectionTest.java	Mon Nov 25 11:59:22 2013 -0500
+++ b/storage/mongo/src/test/java/com/redhat/thermostat/storage/mongodb/internal/MongoConnectionTest.java	Mon Nov 25 12:11:41 2013 -0500
@@ -40,9 +40,12 @@
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Matchers.isA;
 import static org.mockito.Mockito.doCallRealMethod;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -74,6 +77,8 @@
 import com.mongodb.MongoURI;
 import com.mongodb.ServerAddress;
 import com.redhat.thermostat.common.ssl.SSLContextFactory;
+import com.redhat.thermostat.common.utils.HostPortsParser;
+import com.redhat.thermostat.shared.config.InvalidConfigurationException;
 import com.redhat.thermostat.shared.config.SSLConfiguration;
 import com.redhat.thermostat.storage.config.StartupConfiguration;
 import com.redhat.thermostat.storage.core.Connection.ConnectionListener;
@@ -171,6 +176,30 @@
         assertTrue(exceptionThrown);
     }
     
+    @PrepareForTest({ MongoConnection.class, HostPortsParser.class })
+    @Test
+    public void testConnectInvalidConfigurationexception() throws Exception {
+        HostPortsParser failingParser = mock(HostPortsParser.class);
+        doThrow(new InvalidConfigurationException("let's pretend the configuration is invalid"))
+                .when(failingParser).parse();
+
+        PowerMockito
+            .whenNew(HostPortsParser.class)
+            .withParameterTypes(String.class)
+            .withArguments(anyString())
+            .thenReturn(failingParser);
+
+        boolean exceptionThrown = false;
+        try {
+            conn.connect();
+        } catch (ConnectionException ex) {
+            exceptionThrown = true;
+        }
+
+        verify(listener).changed(ConnectionStatus.FAILED_TO_CONNECT);
+        assertTrue(exceptionThrown);
+    }
+
     @PrepareForTest({ MongoConnection.class,
         SSLContextFactory.class, SSLContext.class, SSLSocketFactory.class })
     @Test