changeset 196:585a09251bf6

Remove notifiers from base Application class reviewed-by: rkennke review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/000675.html
author Mario Torre <neugens.limasoftware@gmail.com>
date Wed, 04 Apr 2012 14:48:12 +0200
parents af1dff836e60
children a0b56e387957 832c61ca1f96
files agent/src/main/java/com/redhat/thermostat/agent/AgentApplication.java common/src/main/java/com/redhat/thermostat/tools/BasicApplication.java common/src/test/java/com/redhat/thermostat/tools/BasicApplicationTest.java tools/src/main/java/com/redhat/thermostat/tools/ThermostatService.java tools/src/main/java/com/redhat/thermostat/tools/db/DBService.java tools/src/test/java/com/redhat/thermostat/tools/db/DBServiceTest.java
diffstat 6 files changed, 116 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/agent/src/main/java/com/redhat/thermostat/agent/AgentApplication.java	Tue Apr 03 15:42:38 2012 -0400
+++ b/agent/src/main/java/com/redhat/thermostat/agent/AgentApplication.java	Wed Apr 04 14:48:12 2012 +0200
@@ -142,4 +142,9 @@
         service.parseArguments(Arrays.asList(args));
         service.run();
     }
+
+    @Override
+    public void printHelp() {
+        // TODO: add help
+    }
 }
--- a/common/src/main/java/com/redhat/thermostat/tools/BasicApplication.java	Tue Apr 03 15:42:38 2012 -0400
+++ b/common/src/main/java/com/redhat/thermostat/tools/BasicApplication.java	Wed Apr 04 14:48:12 2012 +0200
@@ -48,20 +48,9 @@
     public BasicApplication() {
         this.notifier = new ActionNotifier<>(this);
     }
-  
-    protected void notifyFail() {
-        notifier.fireAction(ApplicationState.FAIL);
-    }
-    
-    protected void notifySuccess() {
-        notifier.fireAction(ApplicationState.SUCCESS);
-    }
-    
+
     @Override
     public ActionNotifier<ApplicationState> getNotifier() {
         return notifier;
     }
-    
-    @Override
-    public void printHelp() {}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/src/test/java/com/redhat/thermostat/tools/BasicApplicationTest.java	Wed Apr 04 14:48:12 2012 +0200
@@ -0,0 +1,83 @@
+/*
+ * 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.tools;
+
+import static org.junit.Assert.*;
+
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.redhat.thermostat.common.config.InvalidConfigurationException;
+import com.redhat.thermostat.common.config.StartupConfiguration;
+
+public class BasicApplicationTest {
+
+    private BasicApplication application;
+
+    @Before
+    public void setUp() {
+        application = new BasicApplication() {
+            @Override
+            public void parseArguments(List<String> args)
+                    throws InvalidConfigurationException { }
+
+            @Override
+            public void run() {}
+
+            @Override
+            public StartupConfiguration getConfiguration() {
+                return null;
+            }
+
+            @Override
+            public void printHelp() {}
+        };
+    }
+
+    @After
+    public void tearDown() {
+        application = null;
+    }
+
+    @Test
+    public void testNotfier() {
+        assertNotNull(application.getNotifier());
+    }
+}
--- a/tools/src/main/java/com/redhat/thermostat/tools/ThermostatService.java	Tue Apr 03 15:42:38 2012 -0400
+++ b/tools/src/main/java/com/redhat/thermostat/tools/ThermostatService.java	Wed Apr 04 14:48:12 2012 +0200
@@ -112,7 +112,9 @@
     public void actionPerformed(ActionEvent<ApplicationState> actionEvent) {
         if (actionEvent.getSource().equals(database)) {
             switch (actionEvent.getActionId()) {
-            case SUCCESS:
+            // we are only interested in starting the agent if
+            // we started the service
+            case START:
                 String dbUrl = database.getConfiguration().getDBConnectionString();
                 List<String> args = new ArrayList<>();
                 args.add("--dbUrl");
--- a/tools/src/main/java/com/redhat/thermostat/tools/db/DBService.java	Tue Apr 03 15:42:38 2012 -0400
+++ b/tools/src/main/java/com/redhat/thermostat/tools/db/DBService.java	Wed Apr 04 14:48:12 2012 +0200
@@ -46,6 +46,7 @@
 import com.redhat.thermostat.common.config.ConfigUtils;
 import com.redhat.thermostat.common.config.InvalidConfigurationException;
 import com.redhat.thermostat.tools.ApplicationException;
+import com.redhat.thermostat.tools.ApplicationState;
 import com.redhat.thermostat.tools.BasicApplication;
 
 public class DBService extends BasicApplication {
@@ -69,7 +70,6 @@
         
         parser = new DBOptionParser(configuration, args);
         parser.parse();
-        
     }
     
     private void readAndSetProperties(File propertyFile) throws InvalidConfigurationException {
@@ -115,19 +115,20 @@
     
     private void startService() throws IOException, InterruptedException, InvalidConfigurationException, ApplicationException {
         runner.startService();
-        notifySuccess();
+        getNotifier().fireAction(ApplicationState.START);
     }
     
     
     private void stopService() throws IOException, InterruptedException, InvalidConfigurationException, ApplicationException {
         check();
         runner.stopService();
-        notifySuccess();
+        getNotifier().fireAction(ApplicationState.STOP);
     }
     
     @Override
     public void run() {
         
+        // dry run means we don't do anything at all
         if (parser.isDryRun()) return;
         
         runner = createRunner();
@@ -140,10 +141,13 @@
             case STOP:
                 stopService();
                 break;
+             default:
+                break;
             }
+            getNotifier().fireAction(ApplicationState.SUCCESS);
+            
         } catch (Exception e) {
-            // TODO: exception should be at least logged
-            notifyFail();
+            getNotifier().fireAction(ApplicationState.FAIL);
         }
     }
     
--- a/tools/src/test/java/com/redhat/thermostat/tools/db/DBServiceTest.java	Tue Apr 03 15:42:38 2012 -0400
+++ b/tools/src/test/java/com/redhat/thermostat/tools/db/DBServiceTest.java	Wed Apr 04 14:48:12 2012 +0200
@@ -157,20 +157,29 @@
     {
         DBService service = prepareService(true);
         
-        final CountDownLatch latch = new CountDownLatch(0);
-        final boolean[] result = new boolean[1];
+        final CountDownLatch latch = new CountDownLatch(2);
+        
+        final boolean[] result = new boolean[2];
         service.getNotifier().addActionListener(new ActionListener<ApplicationState>() {
             @Override
             public void actionPerformed(ActionEvent<ApplicationState> actionEvent) {
                 switch (actionEvent.getActionId()) {
                 case FAIL:
                     result[0] = false;
+                    latch.countDown();
+                    latch.countDown();
                     break;
+                    
                 case SUCCESS:
                     result[0] = true;
+                    latch.countDown();
                     break;
+
+                case START:
+                    result[1] = true;
+                    latch.countDown();
+                    break;                    
                 }
-                latch.countDown();
             }
         });
         
@@ -178,16 +187,16 @@
         latch.await();
         
         Assert.assertTrue(result[0]);
+        Assert.assertTrue(result[1]);
     }
     
-    
     @Test
     public void testListenersFail() throws InvalidConfigurationException,
             InterruptedException, IOException, ApplicationException
     {
         DBService service = prepareService(false);
         
-        final CountDownLatch latch = new CountDownLatch(0);
+        final CountDownLatch latch = new CountDownLatch(1);
         final boolean[] result = new boolean[1];
         service.getNotifier().addActionListener(new ActionListener<ApplicationState>() {
             @Override
@@ -196,6 +205,7 @@
                 case FAIL:
                     result[0] = true;
                     break;
+                    
                 case SUCCESS:
                     result[0] = false;
                     break;