# HG changeset patch # User Jon VanAlten # Date 1392369827 25200 # Node ID 366d8950cef3575e1f27a4f616f36e89a233c1e6 # Parent ef11ae1f62705574c7249ba86dd81bcb39e0e692 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 diff -r ef11ae1f6270 -r 366d8950cef3 client/core/src/main/java/com/redhat/thermostat/client/ui/ClientConfigurationController.java --- 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 { @@ -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); } } diff -r ef11ae1f6270 -r 366d8950cef3 client/core/src/test/java/com/redhat/thermostat/client/ui/ClientConfigurationControllerTest.java --- 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(); + } + } + } diff -r ef11ae1f6270 -r 366d8950cef3 keyring/src/main/java/com/redhat/thermostat/utils/keyring/Keyring.java --- 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); diff -r ef11ae1f6270 -r 366d8950cef3 keyring/src/main/java/com/redhat/thermostat/utils/keyring/KeyringException.java --- /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 + * . + * + * 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); + } + +} diff -r ef11ae1f6270 -r 366d8950cef3 keyring/src/main/java/com/redhat/thermostat/utils/keyring/impl/KeyringImpl.java --- 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."); } }