changeset 272:d380c6cdf82a

Try to use client prefs to get the default value for --dbUrl This one of the fixes needed to resolve RP970. Reviewed-by: vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-May/001157.html
author Omair Majid <omajid@redhat.com>
date Wed, 02 May 2012 12:35:43 -0400
parents c090011cb55e
children 98a4eaab0fa6
files client/src/main/java/com/redhat/thermostat/client/Main.java client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java client/src/main/java/com/redhat/thermostat/client/config/ClientPreferences.java client/src/main/java/com/redhat/thermostat/client/config/ConnectionConfiguration.java client/src/main/java/com/redhat/thermostat/client/ui/ClientConfigurationController.java client/src/test/java/com/redhat/thermostat/client/config/ClientPreferencesTest.java client/src/test/java/com/redhat/thermostat/client/ui/ClientConfigurationControllerTest.java common/src/main/java/com/redhat/thermostat/cli/CommonCommandOptions.java common/src/main/java/com/redhat/thermostat/cli/Launcher.java common/src/main/java/com/redhat/thermostat/common/config/ClientPreferences.java common/src/test/java/com/redhat/thermostat/cli/CommonCommandOptionsTest.java common/src/test/java/com/redhat/thermostat/cli/HelpCommandTest.java common/src/test/java/com/redhat/thermostat/cli/LauncherTest.java common/src/test/java/com/redhat/thermostat/common/config/ClientPreferencesTest.java
diffstat 14 files changed, 170 insertions(+), 154 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/main/java/com/redhat/thermostat/client/Main.java	Wed May 02 16:54:08 2012 +0200
+++ b/client/src/main/java/com/redhat/thermostat/client/Main.java	Wed May 02 12:35:43 2012 -0400
@@ -47,7 +47,6 @@
 import javax.swing.JPopupMenu;
 import javax.swing.SwingUtilities;
 
-import com.redhat.thermostat.client.config.ClientPreferences;
 import com.redhat.thermostat.client.config.ConnectionConfiguration;
 import com.redhat.thermostat.client.locale.LocaleResources;
 import com.redhat.thermostat.client.ui.ConnectionSelectionDialog;
@@ -56,6 +55,7 @@
 import com.redhat.thermostat.common.ThreadPoolTimerFactory;
 import com.redhat.thermostat.common.TimerFactory;
 import com.redhat.thermostat.common.appctx.ApplicationContext;
+import com.redhat.thermostat.common.config.ClientPreferences;
 import com.redhat.thermostat.common.config.StartupConfiguration;
 import com.redhat.thermostat.common.dao.DAOFactory;
 import com.redhat.thermostat.common.dao.MongoDAOFactory;
--- a/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java	Wed May 02 16:54:08 2012 +0200
+++ b/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java	Wed May 02 12:35:43 2012 -0400
@@ -39,7 +39,6 @@
 import java.util.Collection;
 import java.util.concurrent.TimeUnit;
 
-import com.redhat.thermostat.client.config.ClientPreferences;
 import com.redhat.thermostat.client.ui.AboutDialog;
 import com.redhat.thermostat.client.ui.AgentConfigurationController;
 import com.redhat.thermostat.client.ui.AgentConfigurationModel;
@@ -54,6 +53,7 @@
 import com.redhat.thermostat.common.Timer;
 import com.redhat.thermostat.common.Timer.SchedulingType;
 import com.redhat.thermostat.common.appctx.ApplicationContext;
+import com.redhat.thermostat.common.config.ClientPreferences;
 import com.redhat.thermostat.common.dao.DAOFactory;
 import com.redhat.thermostat.common.dao.HostInfoDAO;
 import com.redhat.thermostat.common.dao.HostRef;
--- a/client/src/main/java/com/redhat/thermostat/client/config/ClientPreferences.java	Wed May 02 16:54:08 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,67 +0,0 @@
-/*
- * 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.config;
-
-import java.util.prefs.BackingStoreException;
-import java.util.prefs.Preferences;
-
-public class ClientPreferences {
-
-    private static final String CONNECTION_URL = "connection-url";
-
-    private final Preferences prefs;
-
-    public ClientPreferences() {
-        this(Preferences.userRoot().node("thermostat"));
-    }
-
-    ClientPreferences(Preferences prefs) {
-        this.prefs = prefs;
-    }
-
-    public String getConnectionUrl() {
-        return prefs.get(CONNECTION_URL, "mongodb://127.0.0.1:27518");
-    }
-
-    public void setConnectionUrl(String url) {
-        prefs.put(CONNECTION_URL, url);
-    }
-
-    public void flush() throws BackingStoreException {
-        prefs.flush();
-    }
-}
--- a/client/src/main/java/com/redhat/thermostat/client/config/ConnectionConfiguration.java	Wed May 02 16:54:08 2012 +0200
+++ b/client/src/main/java/com/redhat/thermostat/client/config/ConnectionConfiguration.java	Wed May 02 12:35:43 2012 -0400
@@ -36,6 +36,7 @@
 
 package com.redhat.thermostat.client.config;
 
+import com.redhat.thermostat.common.config.ClientPreferences;
 import com.redhat.thermostat.common.config.StartupConfiguration;
 
 public class ConnectionConfiguration implements StartupConfiguration {
--- a/client/src/main/java/com/redhat/thermostat/client/ui/ClientConfigurationController.java	Wed May 02 16:54:08 2012 +0200
+++ b/client/src/main/java/com/redhat/thermostat/client/ui/ClientConfigurationController.java	Wed May 02 12:35:43 2012 -0400
@@ -40,10 +40,10 @@
 import java.util.logging.Logger;
 import java.util.prefs.BackingStoreException;
 
-import com.redhat.thermostat.client.config.ClientPreferences;
 import com.redhat.thermostat.client.ui.ClientConfigurationView.Action;
 import com.redhat.thermostat.common.ActionEvent;
 import com.redhat.thermostat.common.ActionListener;
+import com.redhat.thermostat.common.config.ClientPreferences;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 
 public class ClientConfigurationController implements ActionListener<Action> {
--- a/client/src/test/java/com/redhat/thermostat/client/config/ClientPreferencesTest.java	Wed May 02 16:54:08 2012 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-/*
- * 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.config;
-
-import static org.junit.Assert.assertEquals;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-import java.util.prefs.Preferences;
-
-import org.junit.Test;
-
-public class ClientPreferencesTest {
-
-    @Test
-    public void testGetConnectionUrl() {
-
-        Preferences prefs = mock(Preferences.class);
-        when(prefs.get(eq("connection-url"), any(String.class))).thenReturn("mock-value");
-
-        ClientPreferences clientPrefs = new ClientPreferences(prefs);
-        String value = clientPrefs.getConnectionUrl();
-
-        assertEquals("mock-value", value);
-        verify(prefs).get(eq("connection-url"), any(String.class));
-    }
-
-    @Test
-    public void testSetConnectionUrl() {
-
-        Preferences prefs = mock(Preferences.class);
-
-        ClientPreferences clientPrefs = new ClientPreferences(prefs);
-        clientPrefs.setConnectionUrl("test");
-
-        verify(prefs).put(eq("connection-url"), eq("test"));
-    }
-
-
-}
--- a/client/src/test/java/com/redhat/thermostat/client/ui/ClientConfigurationControllerTest.java	Wed May 02 16:54:08 2012 +0200
+++ b/client/src/test/java/com/redhat/thermostat/client/ui/ClientConfigurationControllerTest.java	Wed May 02 12:35:43 2012 -0400
@@ -45,8 +45,8 @@
 
 import org.junit.Test;
 
-import com.redhat.thermostat.client.config.ClientPreferences;
 import com.redhat.thermostat.common.ActionEvent;
+import com.redhat.thermostat.common.config.ClientPreferences;
 
 public class ClientConfigurationControllerTest {
 
--- a/common/src/main/java/com/redhat/thermostat/cli/CommonCommandOptions.java	Wed May 02 16:54:08 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/cli/CommonCommandOptions.java	Wed May 02 12:35:43 2012 -0400
@@ -58,7 +58,7 @@
 
     private void addDbUrlOptionForStorageCommand(Command cmd, Collection<ArgumentSpec> acceptedArguments) {
         if (cmd.isStorageRequired()) {
-            acceptedArguments.add(new SimpleArgumentSpec(DB_URL_ARG, DB_URL_DESC, true, true));
+            acceptedArguments.add(new SimpleArgumentSpec(DB_URL_ARG, DB_URL_DESC, false, true));
         }
     }
 
--- a/common/src/main/java/com/redhat/thermostat/cli/Launcher.java	Wed May 02 16:54:08 2012 +0200
+++ b/common/src/main/java/com/redhat/thermostat/cli/Launcher.java	Wed May 02 12:35:43 2012 -0400
@@ -41,12 +41,15 @@
 import java.util.ServiceLoader;
 import java.util.logging.Level;
 
+import com.redhat.thermostat.common.config.ClientPreferences;
 import com.redhat.thermostat.common.config.InvalidConfigurationException;
 import com.redhat.thermostat.common.storage.ConnectionException;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 
 public class Launcher {
 
+    private ClientPreferences prefs = new ClientPreferences();
+
     private String[] args;
 
     public void run(String[] args) {
@@ -60,6 +63,10 @@
         }
     }
 
+    void setPreferences(ClientPreferences prefs) {
+        this.prefs = prefs;
+    }
+
     private void initLogging() {
         try {
             LoggingUtils.loadGlobalLoggingConfig();
@@ -151,6 +158,9 @@
         CommandContext ctx = cmdCtxFactory.createContext(args);
         if (cmd.isStorageRequired()) {
             String dbUrl = ctx.getArguments().getArgument(CommonCommandOptions.DB_URL_ARG);
+            if (dbUrl == null) {
+                dbUrl = prefs.getConnectionUrl();
+            }
             try {
                 ctx.getAppContextSetup().setupAppContext(dbUrl);
             } catch (ConnectionException ex) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/src/main/java/com/redhat/thermostat/common/config/ClientPreferences.java	Wed May 02 12:35:43 2012 -0400
@@ -0,0 +1,67 @@
+/*
+ * 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.config;
+
+import java.util.prefs.BackingStoreException;
+import java.util.prefs.Preferences;
+
+public class ClientPreferences {
+
+    private static final String CONNECTION_URL = "connection-url";
+
+    private final Preferences prefs;
+
+    public ClientPreferences() {
+        this(Preferences.userRoot().node("thermostat"));
+    }
+
+    ClientPreferences(Preferences prefs) {
+        this.prefs = prefs;
+    }
+
+    public String getConnectionUrl() {
+        return prefs.get(CONNECTION_URL, "mongodb://127.0.0.1:27518");
+    }
+
+    public void setConnectionUrl(String url) {
+        prefs.put(CONNECTION_URL, url);
+    }
+
+    public void flush() throws BackingStoreException {
+        prefs.flush();
+    }
+}
--- a/common/src/test/java/com/redhat/thermostat/cli/CommonCommandOptionsTest.java	Wed May 02 16:54:08 2012 +0200
+++ b/common/src/test/java/com/redhat/thermostat/cli/CommonCommandOptionsTest.java	Wed May 02 12:35:43 2012 -0400
@@ -52,7 +52,7 @@
         CommonCommandOptions commonOpts = new CommonCommandOptions();
         Collection<ArgumentSpec> cmdOpts = commonOpts.getAcceptedOptionsFor(cmd);
 
-        assertTrue(cmdOpts.contains(new SimpleArgumentSpec("dbUrl", "the URL of the storage to connect to", true, true)));
+        assertTrue(cmdOpts.contains(new SimpleArgumentSpec("dbUrl", "the URL of the storage to connect to", false, true)));
     }
 
     @Test
--- a/common/src/test/java/com/redhat/thermostat/cli/HelpCommandTest.java	Wed May 02 16:54:08 2012 +0200
+++ b/common/src/test/java/com/redhat/thermostat/cli/HelpCommandTest.java	Wed May 02 12:35:43 2012 -0400
@@ -133,7 +133,7 @@
         cmd.run(ctxFactory.createContext(args));
 
         String actual = ctxFactory.getOutput();
-        assertEquals("usage: test1 -dbUrl <arg> [-logLevel <arg>]\ntest usage command 1\n  --dbUrl <arg>       the URL of the storage to connect to\n  --logLevel <arg>    log level\n", actual);
+        assertEquals("usage: test1 [-dbUrl <arg>] [-logLevel <arg>]\ntest usage command 1\n  --dbUrl <arg>       the URL of the storage to connect to\n  --logLevel <arg>    log level\n", actual);
     }
 
     @Test
--- a/common/src/test/java/com/redhat/thermostat/cli/LauncherTest.java	Wed May 02 16:54:08 2012 +0200
+++ b/common/src/test/java/com/redhat/thermostat/cli/LauncherTest.java	Wed May 02 12:35:43 2012 -0400
@@ -39,6 +39,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 import java.util.Arrays;
 import java.util.logging.Level;
@@ -48,6 +49,7 @@
 import org.junit.Before;
 import org.junit.Test;
 
+import com.redhat.thermostat.common.config.ClientPreferences;
 import com.redhat.thermostat.test.TestCommandContextFactory;
 
 public class LauncherTest {
@@ -217,9 +219,12 @@
         verify(appContextSetup).setupAppContext("mongo://fluff:12345");
     }
 
-    @Test
-    public void verifyStorageCommandRequiresDbUrl() {
-        new Launcher().run(new String[] { "test3" });
-        assertEquals("Missing required option: --dbUrl\n", ctxFactory.getError());
+    public void verifyPrefsAreUsed() {
+        ClientPreferences prefs = mock(ClientPreferences.class);
+        when(prefs.getConnectionUrl()).thenReturn("mongo://fluff:12345");
+        Launcher l = new Launcher();
+        l.setPreferences(prefs);
+        l.run(new String[] { "test3" });
+        verify(appContextSetup).setupAppContext("mongo://fluff:12345");
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/src/test/java/com/redhat/thermostat/common/config/ClientPreferencesTest.java	Wed May 02 12:35:43 2012 -0400
@@ -0,0 +1,76 @@
+/*
+ * 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.config;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import java.util.prefs.Preferences;
+
+import org.junit.Test;
+
+public class ClientPreferencesTest {
+
+    @Test
+    public void testGetConnectionUrl() {
+
+        Preferences prefs = mock(Preferences.class);
+        when(prefs.get(eq("connection-url"), any(String.class))).thenReturn("mock-value");
+
+        ClientPreferences clientPrefs = new ClientPreferences(prefs);
+        String value = clientPrefs.getConnectionUrl();
+
+        assertEquals("mock-value", value);
+        verify(prefs).get(eq("connection-url"), any(String.class));
+    }
+
+    @Test
+    public void testSetConnectionUrl() {
+
+        Preferences prefs = mock(Preferences.class);
+
+        ClientPreferences clientPrefs = new ClientPreferences(prefs);
+        clientPrefs.setConnectionUrl("test");
+
+        verify(prefs).put(eq("connection-url"), eq("test"));
+    }
+
+
+}