changeset 769:003c2660ff00

Fix client preferences handling. Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-November/004087.html
author Roman Kennke <rkennke@redhat.com>
date Tue, 13 Nov 2012 11:16:40 +0100
parents f16b52396b14
children 2870f5754308
files client/core/src/main/java/com/redhat/thermostat/client/ui/ClientConfigReconnector.java client/core/src/main/java/com/redhat/thermostat/client/ui/ClientConfigurationController.java client/core/src/test/java/com/redhat/thermostat/client/ui/ClientConfigurationControllerTest.java client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/Main.java web/client/src/main/java/com/redhat/thermostat/web/client/WebStorage.java
diffstat 5 files changed, 134 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/ClientConfigReconnector.java	Tue Nov 13 11:16:40 2012 +0100
@@ -0,0 +1,46 @@
+/*
+ * 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.client.ui;
+
+import com.redhat.thermostat.common.config.ClientPreferences;
+
+public interface ClientConfigReconnector {
+
+    void reconnect(ClientPreferences prefs);
+    void abort();
+}
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/ClientConfigurationController.java	Mon Nov 12 16:40:56 2012 +0100
+++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/ClientConfigurationController.java	Tue Nov 13 11:16:40 2012 +0100
@@ -53,11 +53,16 @@
 
     private final ClientConfigurationView view;
     private final ClientPreferences model;
+    private final ClientConfigReconnector reconnector;
 
     public ClientConfigurationController(ClientPreferences model, ClientConfigurationView view) {
+        this(model, view, null);
+    }
+
+    public ClientConfigurationController(ClientPreferences model, ClientConfigurationView view, ClientConfigReconnector reconnector) {
         this.model = model;
         this.view = view;
-
+        this.reconnector = reconnector;
         view.addListener(this);
     }
 
@@ -96,9 +101,16 @@
         switch (actionEvent.getActionId()) {
             case CLOSE_ACCEPT:
                 updateModelFromView();
-                /* fall through */
+                view.hideDialog();
+                if (reconnector != null) {
+                    reconnector.reconnect(model);
+                }
+                break;
             case CLOSE_CANCEL:
                 view.hideDialog();
+                if (reconnector != null) {
+                    reconnector.abort();
+                }
                 break;
         }
 
--- a/client/core/src/test/java/com/redhat/thermostat/client/ui/ClientConfigurationControllerTest.java	Mon Nov 12 16:40:56 2012 +0100
+++ b/client/core/src/test/java/com/redhat/thermostat/client/ui/ClientConfigurationControllerTest.java	Tue Nov 13 11:16:40 2012 +0100
@@ -43,6 +43,7 @@
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import org.junit.Before;
 import org.junit.Test;
 
 import com.redhat.thermostat.client.core.views.ClientConfigurationView;
@@ -51,15 +52,32 @@
 
 public class ClientConfigurationControllerTest {
 
-    @Test
-    public void verifyShowDialog() {
-        ClientPreferences model = mock(ClientPreferences.class);
+    private ClientPreferences model;
+    private ClientConfigurationView view;
+
+    @Before
+    public void setUp() {
+        model = mock(ClientPreferences.class);
         when(model.getConnectionUrl()).thenReturn("mock-connection-url");
         when(model.getPassword()).thenReturn("mock-password");
         when(model.getUserName()).thenReturn("mock-username");
         when(model.getSaveEntitlements()).thenReturn(false);
+
+        view = mock(ClientConfigurationView.class);
+        when(view.getConnectionUrl()).thenReturn("mock-connection-url");
+        when(view.getPassword()).thenReturn("mock-password");
+        when(view.getUserName()).thenReturn("mock-username");
+        when(view.getSaveEntitlements()).thenReturn(true);
+    }
+
+    public void tearDown() {
+        view = null;
+        model = null;
+    }
+
+    @Test
+    public void verifyShowDialog() {
         
-        ClientConfigurationView view = mock(ClientConfigurationView.class);
         ClientConfigurationController controller = new ClientConfigurationController(model, view);
 
         controller.showDialog();
@@ -74,12 +92,25 @@
 
     @Test
     public void verifyCloseCancel() {
-        ClientPreferences model = mock(ClientPreferences.class);
-        ClientConfigurationView view = mock(ClientConfigurationView.class);
         ClientConfigurationController controller = new ClientConfigurationController(model, view);
 
         controller.actionPerformed(new ActionEvent<>(view, ClientConfigurationView.Action.CLOSE_CANCEL));
 
+        verifyCloseCancelCommon();
+    }
+
+    @Test
+    public void verifyCloseCancelWithReconnector() {
+        ClientConfigReconnector reconnector = mock(ClientConfigReconnector.class);
+        ClientConfigurationController controller = new ClientConfigurationController(model, view, reconnector);
+
+        controller.actionPerformed(new ActionEvent<>(view, ClientConfigurationView.Action.CLOSE_CANCEL));
+
+        verifyCloseCancelCommon();
+        verify(reconnector).abort();
+    }
+
+    private void verifyCloseCancelCommon() {
         verify(model, times(0)).setConnectionUrl(any(String.class));
         verify(model, times(0)).setCredentials(any(String.class), any(String.class));
         
@@ -90,17 +121,27 @@
 
     @Test
     public void verifyCloseAccept() {
-        ClientPreferences model = mock(ClientPreferences.class);
-        ClientConfigurationView view = mock(ClientConfigurationView.class);
-        when(view.getConnectionUrl()).thenReturn("mock-connection-url");
-        when(view.getPassword()).thenReturn("mock-password");
-        when(view.getUserName()).thenReturn("mock-username");
-        when(view.getSaveEntitlements()).thenReturn(true);
         
         ClientConfigurationController controller = new ClientConfigurationController(model, view);
 
         controller.actionPerformed(new ActionEvent<>(view, ClientConfigurationView.Action.CLOSE_ACCEPT));
 
+        verifyCloseAcceptCommon();
+    }
+
+    @Test
+    public void verifyCloseAcceptWithReconnector() {
+        
+        ClientConfigReconnector reconnector = mock(ClientConfigReconnector.class);
+        ClientConfigurationController controller = new ClientConfigurationController(model, view, reconnector);
+
+        controller.actionPerformed(new ActionEvent<>(view, ClientConfigurationView.Action.CLOSE_ACCEPT));
+
+        verifyCloseAcceptCommon();
+        verify(reconnector).reconnect(model);
+    }
+
+    private void verifyCloseAcceptCommon() {
         verify(model).setConnectionUrl(eq("mock-connection-url"));
         verify(model).setCredentials(eq("mock-username"), eq("mock-password"));
         verify(model).setSaveEntitlements(eq(true));
@@ -110,4 +151,5 @@
         verify(view).hideDialog();
     }
 
+    
 }
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/Main.java	Mon Nov 12 16:40:56 2012 +0100
+++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/Main.java	Tue Nov 13 11:16:40 2012 +0100
@@ -54,11 +54,10 @@
 import com.redhat.thermostat.client.osgi.service.ApplicationService;
 import com.redhat.thermostat.client.swing.internal.config.ConnectionConfiguration;
 import com.redhat.thermostat.client.swing.views.ClientConfigurationSwing;
+import com.redhat.thermostat.client.ui.ClientConfigReconnector;
 import com.redhat.thermostat.client.ui.ClientConfigurationController;
 import com.redhat.thermostat.client.ui.MainWindowController;
 import com.redhat.thermostat.client.ui.UiFacadeFactory;
-import com.redhat.thermostat.common.ActionEvent;
-import com.redhat.thermostat.common.ActionListener;
 import com.redhat.thermostat.common.Constants;
 import com.redhat.thermostat.common.ThreadPoolTimerFactory;
 import com.redhat.thermostat.common.TimerFactory;
@@ -233,26 +232,23 @@
         service.execute(new Connector(connection));
     }
     
-    private class ConfigDialogListener implements ActionListener<ClientConfigurationView.Action> {
+    private class MainClientConfigReconnector implements ClientConfigReconnector {
         private Connection connection;
         private ExecutorService service;
-        public ConfigDialogListener(Connection connection, ExecutorService service) {
+        public MainClientConfigReconnector(Connection connection, ExecutorService service) {
             this.connection = connection;
             this.service = service;
         }
         
         @Override
-        public void actionPerformed(ActionEvent<ClientConfigurationView.Action> actionEvent) {
-            switch (actionEvent.getActionId()) {
-            case CLOSE_CANCEL:
-                uiFacadeFactory.shutdown(Constants.EXIT_UNABLE_TO_CONNECT_TO_DATABASE);
-                break;
-            
-            case CLOSE_ACCEPT:
-            default:
-                connect(connection, service);
-                break;
-            }
+        public void reconnect(ClientPreferences prefs) {
+            connection.setUrl(prefs.getConnectionUrl());
+            connect(connection, service);
+        }
+
+        @Override
+        public void abort() {
+            uiFacadeFactory.shutdown(Constants.EXIT_UNABLE_TO_CONNECT_TO_DATABASE);
         }
     }
     
@@ -261,9 +257,8 @@
         ClientPreferences prefs = new ClientPreferences(OSGIUtils.getInstance().getService(Keyring.class));
         ClientConfigurationView configDialog = new ClientConfigurationSwing();
         ClientConfigurationController controller =
-                new ClientConfigurationController(prefs, configDialog);
+                new ClientConfigurationController(prefs, configDialog, new MainClientConfigReconnector(connection, service));
         
-        configDialog.addListener(new ConfigDialogListener(connection, service));
         controller.showDialog();
     }
     
@@ -284,7 +279,6 @@
         @Override
         public void changed(ConnectionStatus newStatus) {
             if (newStatus == ConnectionStatus.CONNECTED) {
-                
                 // register the storage, so other services can request it
                 daoFactory.registerDAOsAndStorageAsOSGiServices();
                 uiFacadeFactory.setHostInfoDao(daoFactory.getHostInfoDAO());
--- a/web/client/src/main/java/com/redhat/thermostat/web/client/WebStorage.java	Mon Nov 12 16:40:56 2012 +0100
+++ b/web/client/src/main/java/com/redhat/thermostat/web/client/WebStorage.java	Tue Nov 13 11:16:40 2012 +0100
@@ -99,10 +99,17 @@
                 ping();
                 connected = true;
                 fireChanged(ConnectionStatus.CONNECTED);
-            } catch (IOException ex) {
+            } catch (Exception ex) {
                 fireChanged(ConnectionStatus.FAILED_TO_CONNECT);
             }
         }
+
+        @Override
+        public void setUrl(String url) {
+            super.setUrl(url);
+            endpoint = url;
+        }
+
         @Override
         public String getUrl() {
             return endpoint;