changeset 184:ad08bad26237

Use new Timer in client Reviewed-by: vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-March/000590.html
author Roman Kennke <rkennke@redhat.com>
date Mon, 02 Apr 2012 14:48:02 +0200
parents 5d2a538b1020
children 4f2137a2f3d8
files client/src/main/java/com/redhat/thermostat/client/SummaryPanelFacadeImpl.java client/src/test/java/com/redhat/thermostat/client/SummaryPanelFacadeImplTest.java
diffstat 2 files changed, 116 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/main/java/com/redhat/thermostat/client/SummaryPanelFacadeImpl.java	Mon Apr 02 14:46:46 2012 +0200
+++ b/client/src/main/java/com/redhat/thermostat/client/SummaryPanelFacadeImpl.java	Mon Apr 02 14:48:02 2012 +0200
@@ -38,12 +38,13 @@
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Timer;
-import java.util.TimerTask;
 import java.util.concurrent.TimeUnit;
 
 import com.mongodb.DB;
 import com.mongodb.DBCollection;
+import com.redhat.thermostat.client.appctx.ApplicationContext;
+import com.redhat.thermostat.common.Timer;
+import com.redhat.thermostat.common.Timer.SchedulingType;
 
 public class SummaryPanelFacadeImpl implements SummaryPanelFacade {
 
@@ -53,7 +54,7 @@
     private final ChangeableText connectedAgentText;
     private final ChangeableText connectedVmText;
 
-    private final Timer backgroundUpdateTimer = new Timer();
+    private final Timer backgroundUpdateTimer;
 
     public SummaryPanelFacadeImpl(DB db) {
         this.agentConfigCollection = db.getCollection("agent-config");
@@ -61,22 +62,30 @@
 
         this.connectedVmText = new ChangeableText("");
         this.connectedAgentText = new ChangeableText("");
-    }
 
-    @Override
-    public void start() {
-        backgroundUpdateTimer.scheduleAtFixedRate(new TimerTask() {
+        backgroundUpdateTimer = ApplicationContext.getInstance().getTimerFactory().createTimer();
+        backgroundUpdateTimer.setAction(new Runnable() {
+            
             @Override
             public void run() {
                 connectedVmText.setText(String.valueOf(vmInfoCollection.getCount()));
                 connectedAgentText.setText(String.valueOf(agentConfigCollection.getCount()));
             }
-        }, 0, TimeUnit.SECONDS.toMillis(10));
+        });
+        backgroundUpdateTimer.setInitialDelay(0);
+        backgroundUpdateTimer.setDelay(10);
+        backgroundUpdateTimer.setTimeUnit(TimeUnit.SECONDS);
+        backgroundUpdateTimer.setSchedulingType(SchedulingType.FIXED_RATE);
+    }
+
+    @Override
+    public void start() {
+        backgroundUpdateTimer.start();
     }
 
     @Override
     public void stop() {
-        backgroundUpdateTimer.cancel();
+        backgroundUpdateTimer.stop();
     }
 
     @Override
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/src/test/java/com/redhat/thermostat/client/SummaryPanelFacadeImplTest.java	Mon Apr 02 14:48:02 2012 +0200
@@ -0,0 +1,98 @@
+/*
+ * 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;
+
+import static org.mockito.Matchers.isNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.concurrent.TimeUnit;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.mongodb.DB;
+import com.redhat.thermostat.client.appctx.ApplicationContext;
+import com.redhat.thermostat.client.appctx.ApplicationContextUtil;
+import com.redhat.thermostat.common.Timer;
+import com.redhat.thermostat.common.Timer.SchedulingType;
+import com.redhat.thermostat.common.TimerFactory;
+
+public class SummaryPanelFacadeImplTest {
+
+    private Timer timer;
+
+    @Before
+    public void setUp() {
+        ApplicationContextUtil.resetApplicationContext();
+        timer = mock(Timer.class);
+        TimerFactory timerFactory = mock(TimerFactory.class);
+        when(timerFactory.createTimer()).thenReturn(timer);
+        ApplicationContext.getInstance().setTimerFactory(timerFactory);
+    }
+
+    @After
+    public void tearDown() {
+        timer = null;
+        ApplicationContextUtil.resetApplicationContext();
+    }
+
+    @Test
+    public void testTimer() {
+
+        DB db = mock(DB.class);
+
+        SummaryPanelFacadeImpl summaryPanelCtrl = new SummaryPanelFacadeImpl(db);
+        summaryPanelCtrl.start();
+
+        verify(timer).setAction(isNotNull(Runnable.class));
+        verify(timer).setDelay(10);
+        verify(timer).setTimeUnit(TimeUnit.SECONDS);
+        verify(timer).setInitialDelay(0);
+        verify(timer).setSchedulingType(SchedulingType.FIXED_RATE);
+        verify(timer).start();
+
+        summaryPanelCtrl.stop();
+
+        verify(timer).stop();
+
+        // TODO: Also test for the actual action, as soon as the MVC refactoring is done.
+    }
+
+}