changeset 1387:366d8950cef3

PR1675: Better handling of keyring failure Introduce keyring-specific runtime exception type, and catch this exception in ClientConfigurationController. Backport of 1639 from trunk to 1.0 branch reviewed-by: omajid review-thread: http://icedtea.classpath.org/pipermail/thermostat/2014-February/009174.html
author Jon VanAlten <jon.vanalten@redhat.com>
date Fri, 14 Feb 2014 02:23:47 -0700
parents ef11ae1f6270
children 76079eeed002
files client/core/src/main/java/com/redhat/thermostat/client/ui/ClientConfigurationController.java client/core/src/test/java/com/redhat/thermostat/client/ui/ClientConfigurationControllerTest.java keyring/src/main/java/com/redhat/thermostat/utils/keyring/Keyring.java keyring/src/main/java/com/redhat/thermostat/utils/keyring/KeyringException.java keyring/src/main/java/com/redhat/thermostat/utils/keyring/impl/KeyringImpl.java
diffstat 5 files changed, 75 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/client/core/src/main/java/com/redhat/thermostat/client/ui/ClientConfigurationController.java	Thu Feb 13 18:31:01 2014 +0100
+++ b/client/core/src/main/java/com/redhat/thermostat/client/ui/ClientConfigurationController.java	Fri Feb 14 02:23:47 2014 -0700
@@ -46,6 +46,7 @@
 import com.redhat.thermostat.common.ActionListener;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 import com.redhat.thermostat.storage.core.StorageCredentials;
+import com.redhat.thermostat.utils.keyring.KeyringException;
 
 public class ClientConfigurationController implements ActionListener<Action> {
 
@@ -83,10 +84,10 @@
         model.setSaveEntitlements(view.getSaveEntitlements());
         model.setConnectionUrl(view.getConnectionUrl());
         
-        model.setCredentials(view.getUserName(), view.getPassword());
         try {
+            model.setCredentials(view.getUserName(), view.getPassword());
             model.flush();
-        } catch (IOException e) {
+        } catch (IOException|KeyringException e) {
             logger.log(Level.WARNING, "error saving client preferences", e);
         }
     }
--- a/client/core/src/test/java/com/redhat/thermostat/client/ui/ClientConfigurationControllerTest.java	Thu Feb 13 18:31:01 2014 +0100
+++ b/client/core/src/test/java/com/redhat/thermostat/client/ui/ClientConfigurationControllerTest.java	Fri Feb 14 02:23:47 2014 -0700
@@ -36,9 +36,11 @@
 
 package com.redhat.thermostat.client.ui;
 
+import static org.junit.Assert.fail;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Matchers.isA;
+import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
@@ -48,8 +50,10 @@
 import org.junit.Test;
 
 import com.redhat.thermostat.client.core.views.ClientConfigurationView;
+import com.redhat.thermostat.client.core.views.ClientConfigurationView.Action;
 import com.redhat.thermostat.common.ActionEvent;
 import com.redhat.thermostat.storage.core.StorageCredentials;
+import com.redhat.thermostat.utils.keyring.KeyringException;
 
 public class ClientConfigurationControllerTest {
 
@@ -152,6 +156,21 @@
         verify(view).hideDialog();
     }
 
-    
+    @Test
+    public void verifyCatchesKeyringException() {
+        ClientPreferencesModel badModel = mock(ClientPreferencesModel.class);
+        doThrow(new KeyringException("")).when(badModel).setCredentials("mock-username", "mock-password".toCharArray());
+        ClientConfigurationController controller = new ClientConfigurationController(badModel, view);
+        ActionEvent event = mock(ActionEvent.class);
+        when(event.getActionId()).thenReturn(Action.CLOSE_ACCEPT);
+        try {
+            controller.actionPerformed(event);
+        } catch (KeyringException e) {
+            e.printStackTrace();
+            // Such an exception should be caught within the controller.
+            fail();
+        }
+    }
+
 }
 
--- a/keyring/src/main/java/com/redhat/thermostat/utils/keyring/Keyring.java	Thu Feb 13 18:31:01 2014 +0100
+++ b/keyring/src/main/java/com/redhat/thermostat/utils/keyring/Keyring.java	Fri Feb 14 02:23:47 2014 -0700
@@ -50,6 +50,7 @@
      * @param url The url for this saved password
      * @param username The username for this saved password
      * @param password The password to be saved
+     * @throws KeyringException to indicate password could not be saved due to an error
      */
     public void savePassword(String url, String username, char[] password);
 
@@ -58,6 +59,7 @@
      * @param url The url for the desired password
      * @param username The username for the desired password
      * @return The password mapped to the given url and username, if any.  Null otherwise
+     * @throws KeyringException if error is encountered when attempting to retrieve password
      */
     public char[] getPassword(String url, String username);
 
@@ -65,6 +67,7 @@
      * Clear the password associated with the given url and username, if any.
      * @param url The url for the password to be cleared
      * @param username The username for the password to be cleared
+     * @throws KeyringException if error is encountered when attempting to retrieve password
      */
     public void clearPassword(String url, String username);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/keyring/src/main/java/com/redhat/thermostat/utils/keyring/KeyringException.java	Fri Feb 14 02:23:47 2014 -0700
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2014 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.utils.keyring;
+
+public class KeyringException extends RuntimeException {
+
+    private static final long serialVersionUID = -7031713489739688863L;
+
+    public KeyringException(String string) {
+        super(string);
+    }
+
+}
--- a/keyring/src/main/java/com/redhat/thermostat/utils/keyring/impl/KeyringImpl.java	Thu Feb 13 18:31:01 2014 +0100
+++ b/keyring/src/main/java/com/redhat/thermostat/utils/keyring/impl/KeyringImpl.java	Fri Feb 14 02:23:47 2014 -0700
@@ -43,6 +43,7 @@
 
 import com.redhat.thermostat.shared.config.NativeLibraryResolver;
 import com.redhat.thermostat.utils.keyring.Keyring;
+import com.redhat.thermostat.utils.keyring.KeyringException;
 
 public class KeyringImpl implements Keyring {
 
@@ -71,7 +72,7 @@
             }
         }
         if (!success) {
-            throw new RuntimeException("Couldn't save password.");
+            throw new KeyringException("Couldn't save password.");
         }
     }