changeset 882:cb81daef9f58

Disable GC button for dead vms No point showing a gc button (in the 'Memory' tab) for dead vms. Reviewed-by: rkennke Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-December/004409.html
author Omair Majid <omajid@redhat.com>
date Mon, 31 Dec 2012 11:12:50 -0500
parents 4902d8be1833
children 86a3f700aeac
files vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/MemoryStatsService.java vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/MemoryStatsView.java vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/internal/Activator.java vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/internal/MemoryStatsController.java vm-memory/client-core/src/test/java/com/redhat/thermostat/vm/memory/client/core/internal/ActivatorTest.java vm-memory/client-core/src/test/java/com/redhat/thermostat/vm/memory/client/core/internal/MemoryStatsControllerTest.java vm-memory/client-swing/src/main/java/com/redhat/thermostat/vm/memory/client/swing/internal/MemoryStatsViewImpl.java vm-memory/client-swing/src/test/java/com/redhat/thermostat/vm/memory/client/swing/internal/MemoryStatsViewImplTest.java
diffstat 8 files changed, 161 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/MemoryStatsService.java	Fri Dec 21 10:01:37 2012 -0500
+++ b/vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/MemoryStatsService.java	Mon Dec 31 11:12:50 2012 -0500
@@ -42,6 +42,7 @@
 import com.redhat.thermostat.client.core.controllers.InformationServiceController;
 import com.redhat.thermostat.common.ApplicationService;
 import com.redhat.thermostat.common.dao.AgentInfoDAO;
+import com.redhat.thermostat.common.dao.VmInfoDAO;
 import com.redhat.thermostat.common.dao.VmMemoryStatDAO;
 import com.redhat.thermostat.common.dao.VmRef;
 import com.redhat.thermostat.common.utils.OSGIUtils;
@@ -54,12 +55,14 @@
     private Filter<VmRef> filter = new NameMatchingRefFilter<>();
 
     private ApplicationService appSvc;
+    private VmInfoDAO vmInfoDao;
     private VmMemoryStatDAO vmMemoryStatDao;
     private AgentInfoDAO agentDAO;
     private GCRequest gcRequest;
     
-    public MemoryStatsService(ApplicationService appSvc, VmMemoryStatDAO vmMemoryStatDao, AgentInfoDAO agentDAO, GCRequest gcRequest) {
+    public MemoryStatsService(ApplicationService appSvc, VmInfoDAO vmInfoDao, VmMemoryStatDAO vmMemoryStatDao, AgentInfoDAO agentDAO, GCRequest gcRequest) {
         this.appSvc = appSvc;
+        this.vmInfoDao = vmInfoDao;
         this.vmMemoryStatDao = vmMemoryStatDao;
         this.gcRequest = gcRequest;
         this.agentDAO = agentDAO;
@@ -68,7 +71,7 @@
     @Override
     public InformationServiceController<VmRef> getInformationServiceController(VmRef ref) {
         MemoryStatsViewProvider viewProvider = OSGIUtils.getInstance().getService(MemoryStatsViewProvider.class);
-        return new MemoryStatsController(appSvc, vmMemoryStatDao, ref, viewProvider, agentDAO, gcRequest);
+        return new MemoryStatsController(appSvc, vmInfoDao, vmMemoryStatDao, ref, viewProvider, agentDAO, gcRequest);
     }
 
     @Override
--- a/vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/MemoryStatsView.java	Fri Dec 21 10:01:37 2012 -0500
+++ b/vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/MemoryStatsView.java	Mon Dec 31 11:12:50 2012 -0500
@@ -46,6 +46,7 @@
     public abstract void addRegion(Payload region);
     public abstract void updateRegion(Payload region);
     
+    public abstract void setEnableGCAction(boolean enable);
     public abstract void addGCActionListener(ActionListener<GCCommand> listener);
     
     public abstract void requestRepaint();
--- a/vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/internal/Activator.java	Fri Dec 21 10:01:37 2012 -0500
+++ b/vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/internal/Activator.java	Mon Dec 31 11:12:50 2012 -0500
@@ -50,6 +50,7 @@
 import com.redhat.thermostat.common.MultipleServiceTracker;
 import com.redhat.thermostat.common.MultipleServiceTracker.Action;
 import com.redhat.thermostat.common.dao.AgentInfoDAO;
+import com.redhat.thermostat.common.dao.VmInfoDAO;
 import com.redhat.thermostat.common.dao.VmMemoryStatDAO;
 import com.redhat.thermostat.common.dao.VmRef;
 import com.redhat.thermostat.gc.remote.common.GCRequest;
@@ -64,6 +65,7 @@
     public void start(final BundleContext context) throws Exception {
         Class<?>[] deps = new Class<?>[] {
             ApplicationService.class,
+            VmInfoDAO.class,
             VmMemoryStatDAO.class,
             GCRequest.class,
             AgentInfoDAO.class,
@@ -79,12 +81,13 @@
 
             @Override
             public void dependenciesAvailable(Map<String, Object> services) {
+                VmInfoDAO vmInfoDao = (VmInfoDAO) services.get(VmInfoDAO.class.getName());
                 VmMemoryStatDAO memoryStatDao = (VmMemoryStatDAO) services.get(VmMemoryStatDAO.class.getName());
                 AgentInfoDAO agentDAO = (AgentInfoDAO) services.get(AgentInfoDAO.class.getName());
                 GCRequest gcRequest = (GCRequest) services.get(GCRequest.class.getName());
                 ApplicationService appSvc = (ApplicationService) services.get(ApplicationService.class.getName());
 
-                MemoryStatsService impl = new MemoryStatsService(appSvc, memoryStatDao, agentDAO, gcRequest);
+                MemoryStatsService impl = new MemoryStatsService(appSvc, vmInfoDao, memoryStatDao, agentDAO, gcRequest);
                 Dictionary<String, String> properties = new Hashtable<>();
                 properties.put(Constants.GENERIC_SERVICE_CLASSNAME, VmRef.class.getName());
                 memoryStatRegistration = context.registerService(InformationService.class.getName(), impl , properties);
--- a/vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/internal/MemoryStatsController.java	Fri Dec 21 10:01:37 2012 -0500
+++ b/vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/internal/MemoryStatsController.java	Mon Dec 31 11:12:50 2012 -0500
@@ -55,6 +55,7 @@
 import com.redhat.thermostat.common.command.Response;
 import com.redhat.thermostat.common.command.Response.ResponseType;
 import com.redhat.thermostat.common.dao.AgentInfoDAO;
+import com.redhat.thermostat.common.dao.VmInfoDAO;
 import com.redhat.thermostat.common.dao.VmMemoryStatDAO;
 import com.redhat.thermostat.common.dao.VmRef;
 import com.redhat.thermostat.common.locale.Translate;
@@ -152,7 +153,8 @@
         }
     }
     
-    public MemoryStatsController(ApplicationService appSvc, final VmMemoryStatDAO vmMemoryStatDao,
+    public MemoryStatsController(ApplicationService appSvc, final VmInfoDAO vmInfoDao,
+                                 final VmMemoryStatDAO vmMemoryStatDao,
                                  final VmRef ref, MemoryStatsViewProvider viewProvider,
                                  final AgentInfoDAO agentDAO, final GCRequest gcRequest) {
         
@@ -206,6 +208,11 @@
                 gcRequest.sendGCRequestToAgent(ref, agentDAO, listener);
             }
         });
+
+        if (!vmInfoDao.getVmInfo(ref).isAlive()) {
+            view.setEnableGCAction(false);
+        }
+
     }
     
     // for testing
--- a/vm-memory/client-core/src/test/java/com/redhat/thermostat/vm/memory/client/core/internal/ActivatorTest.java	Fri Dec 21 10:01:37 2012 -0500
+++ b/vm-memory/client-core/src/test/java/com/redhat/thermostat/vm/memory/client/core/internal/ActivatorTest.java	Mon Dec 31 11:12:50 2012 -0500
@@ -46,6 +46,7 @@
 import com.redhat.thermostat.client.core.InformationService;
 import com.redhat.thermostat.common.ApplicationService;
 import com.redhat.thermostat.common.dao.AgentInfoDAO;
+import com.redhat.thermostat.common.dao.VmInfoDAO;
 import com.redhat.thermostat.common.dao.VmMemoryStatDAO;
 import com.redhat.thermostat.gc.remote.common.GCRequest;
 import com.redhat.thermostat.test.StubBundleContext;
@@ -72,11 +73,13 @@
     @Test
     public void verifyActivatorRegistersServices() throws Exception {
         StubBundleContext context = new StubBundleContext();
+        VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class);
         VmMemoryStatDAO vmMemoryStatDAO = mock(VmMemoryStatDAO.class);
         ApplicationService appSvc = mock(ApplicationService.class);
         GCRequest gcRequest = mock(GCRequest.class);
         AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class);
 
+        context.registerService(VmInfoDAO.class, vmInfoDAO, null);
         context.registerService(VmMemoryStatDAO.class, vmMemoryStatDAO, null);
         context.registerService(ApplicationService.class, appSvc, null);
         context.registerService(GCRequest.class, gcRequest, null);
@@ -91,7 +94,7 @@
         activator.stop(context);
 
         assertEquals(0, context.getServiceListeners().size());
-        assertEquals(4, context.getAllServices().size());
+        assertEquals(5, context.getAllServices().size());
     }
 
 }
--- a/vm-memory/client-core/src/test/java/com/redhat/thermostat/vm/memory/client/core/internal/MemoryStatsControllerTest.java	Fri Dec 21 10:01:37 2012 -0500
+++ b/vm-memory/client-core/src/test/java/com/redhat/thermostat/vm/memory/client/core/internal/MemoryStatsControllerTest.java	Mon Dec 31 11:12:50 2012 -0500
@@ -66,10 +66,12 @@
 import com.redhat.thermostat.common.TimerFactory;
 import com.redhat.thermostat.common.command.RequestResponseListener;
 import com.redhat.thermostat.common.dao.AgentInfoDAO;
+import com.redhat.thermostat.common.dao.VmInfoDAO;
 import com.redhat.thermostat.common.dao.VmMemoryStatDAO;
 import com.redhat.thermostat.common.dao.VmRef;
 import com.redhat.thermostat.gc.remote.common.GCRequest;
 import com.redhat.thermostat.gc.remote.common.command.GCCommand;
+import com.redhat.thermostat.storage.model.VmInfo;
 import com.redhat.thermostat.storage.model.VmMemoryStat;
 import com.redhat.thermostat.storage.model.VmMemoryStat.Generation;
 import com.redhat.thermostat.storage.model.VmMemoryStat.Space;
@@ -81,6 +83,7 @@
 
     private Generation[] generations = new Generation[2];
     
+    private VmInfoDAO infoDao;
     private VmMemoryStatDAO memoryStatDao;
     private MemoryStatsView view;
     private Timer timer;
@@ -100,6 +103,12 @@
     @SuppressWarnings({ "unchecked", "rawtypes" })
     @Before
     public void setUp() {
+        initialize(true);
+    }
+
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+    public void initialize(boolean vmIsAlive) {
+
         timer = mock(Timer.class);
         ArgumentCaptor<Runnable> actionCaptor = ArgumentCaptor.forClass(Runnable.class);
         doNothing().when(timer).setAction(actionCaptor.capture());
@@ -109,6 +118,11 @@
         ApplicationService appSvc = mock(ApplicationService.class);
         when(appSvc.getTimerFactory()).thenReturn(timerFactory);
         
+        VmInfo vmOverallInformation = mock(VmInfo.class);
+        when(vmOverallInformation.isAlive()).thenReturn(vmIsAlive);
+        infoDao = mock(VmInfoDAO.class);
+        when(infoDao.getVmInfo(any(VmRef.class))).thenReturn(vmOverallInformation);
+
         List<VmMemoryStat> vmInfo = new ArrayList<>();
         
         for (int i = 0; i < 2; i++) {
@@ -163,7 +177,7 @@
         agentDAO = mock(AgentInfoDAO.class);
         gcRequest = mock(GCRequest.class);
         
-        controller = new MemoryStatsController(appSvc, memoryStatDao, ref, viewProvider, agentDAO, gcRequest);
+        controller = new MemoryStatsController(appSvc, infoDao, memoryStatDao, ref, viewProvider, agentDAO, gcRequest);
         
         viewListener = viewArgumentCaptor.getValue();
         gcActionListener = gcArgumentCaptor.getValue();
@@ -182,6 +196,13 @@
     }
 
     @Test
+    public void testGCIsDisabledForDeadVms() {
+        initialize(false);
+
+        verify(view).setEnableGCAction(false);
+    }
+
+    @Test
     public void testGCInvoked() {
         gcActionListener.actionPerformed(new ActionEvent<>(view, GCCommand.REQUEST_GC));
         verify(gcRequest).sendGCRequestToAgent(eq(ref), eq(agentDAO), isA(RequestResponseListener.class));
--- a/vm-memory/client-swing/src/main/java/com/redhat/thermostat/vm/memory/client/swing/internal/MemoryStatsViewImpl.java	Fri Dec 21 10:01:37 2012 -0500
+++ b/vm-memory/client-swing/src/main/java/com/redhat/thermostat/vm/memory/client/swing/internal/MemoryStatsViewImpl.java	Mon Dec 31 11:12:50 2012 -0500
@@ -69,7 +69,8 @@
     
     private final Map<String, MemoryGraphPanel> regions;
     
-    private RequestGCAction toobarButtonAction;
+    private ToolbarGCButton toolbarButton;
+    private RequestGCAction toolbarButtonAction;
     
     private Dimension preferredSize;
     
@@ -98,8 +99,11 @@
         realPanel.setLayout(new BoxLayout(realPanel, BoxLayout.Y_AXIS));
         visiblePanel.setContent(realPanel);
         
-        toobarButtonAction = new RequestGCAction();
-        visiblePanel.addToolBarButton(new ToolbarGCButton(toobarButtonAction));
+        toolbarButtonAction = new RequestGCAction();
+        toolbarButton = new ToolbarGCButton(toolbarButtonAction);
+        toolbarButton.setName("gcButton");
+        visiblePanel.addToolBarButton(toolbarButton);
+
     }
     
     @Transient
@@ -119,8 +123,18 @@
     }
     
     @Override
+    public void setEnableGCAction(final boolean enable) {
+        SwingUtilities.invokeLater(new Runnable() {
+            @Override
+            public void run() {
+                toolbarButton.setEnabled(enable);
+            }
+        });
+    }
+
+    @Override
     public void addGCActionListener(ActionListener<GCCommand> listener) {
-        toobarButtonAction.addActionListener(listener);
+        toolbarButtonAction.addActionListener(listener);
     }
     
     @Override
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vm-memory/client-swing/src/test/java/com/redhat/thermostat/vm/memory/client/swing/internal/MemoryStatsViewImplTest.java	Mon Dec 31 11:12:50 2012 -0500
@@ -0,0 +1,99 @@
+/*
+ * 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.vm.memory.client.swing.internal;
+
+import javax.swing.JFrame;
+
+import net.java.openjdk.cacio.ctc.junit.CacioFESTRunner;
+
+import org.fest.swing.annotation.GUITest;
+import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
+import org.fest.swing.edt.GuiActionRunner;
+import org.fest.swing.edt.GuiTask;
+import org.fest.swing.fixture.FrameFixture;
+import org.fest.swing.fixture.JButtonFixture;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+@RunWith(CacioFESTRunner.class)
+public class MemoryStatsViewImplTest {
+
+    private JFrame frame;
+    private FrameFixture frameFixture;
+    private MemoryStatsViewImpl view;
+
+    @BeforeClass
+    public static void setUpOnce() {
+        FailOnThreadViolationRepaintManager.install();
+    }
+
+    @Before
+    public void setUp() {
+        GuiActionRunner.execute(new GuiTask() {
+            @Override
+            protected void executeInEDT() throws Throwable {
+                view = new MemoryStatsViewImpl();
+                frame = new JFrame();
+                frame.add(view.getUiComponent());
+
+            }
+        });
+        frameFixture = new FrameFixture(frame);
+    }
+
+    @After
+    public void tearDown() {
+        frameFixture.cleanUp();
+        frameFixture = null;
+    }
+
+    @Category(GUITest.class)
+    @GUITest
+    @Test
+    public void testDisableControl() {
+        frameFixture.show();
+
+        view.setEnableGCAction(false);
+
+        JButtonFixture togglefixture = frameFixture.button("gcButton");
+        togglefixture.requireDisabled();
+    }
+}