changeset 129:17916c4207d3

Implement shutdown notification for MainWindow.
author Roman Kennke <rkennke@redhat.com>
date Wed, 21 Mar 2012 20:52:40 +0100
parents 9d043bbbdbcd
children 997c134b844e
files client/src/main/java/com/redhat/thermostat/client/MainWindowFacadeImpl.java client/src/main/java/com/redhat/thermostat/client/ui/MainWindow.java client/src/test/java/com/redhat/thermostat/client/MainWindowFacadeImplTest.java client/src/test/java/com/redhat/thermostat/client/ui/MainWindowTest.java
diffstat 4 files changed, 154 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/main/java/com/redhat/thermostat/client/MainWindowFacadeImpl.java	Wed Mar 21 20:52:33 2012 +0100
+++ b/client/src/main/java/com/redhat/thermostat/client/MainWindowFacadeImpl.java	Wed Mar 21 20:52:40 2012 +0100
@@ -144,9 +144,12 @@
 
             @Override
             public void propertyChange(PropertyChangeEvent evt) {
-                if (evt.getPropertyName().equals(MainWindow.HOST_VM_TREE_FILTER_PROPERTY)) {
+                String propertyName = evt.getPropertyName(); 
+                if (propertyName.equals(MainWindow.HOST_VM_TREE_FILTER_PROPERTY)) {
                     String filter = (String) evt.getNewValue();
                     setHostVmTreeFilter(filter);
+                } else if (propertyName.equals(MainWindow.SHUTDOWN_PROPERTY)) {
+                    stop();
                 }
             }
             
--- a/client/src/main/java/com/redhat/thermostat/client/ui/MainWindow.java	Wed Mar 21 20:52:33 2012 +0100
+++ b/client/src/main/java/com/redhat/thermostat/client/ui/MainWindow.java	Wed Mar 21 20:52:40 2012 +0100
@@ -219,6 +219,8 @@
     // TODO: When we break out a view interface, this constant needs to go there.
     public static final String HOST_VM_TREE_FILTER_PROPERTY = "hostVMTreeFilter";
 
+    public static final String SHUTDOWN_PROPERTY = "shutdown";
+
     private final UiFacadeFactory facadeFactory;
     private final MainWindowFacade facade;
 
@@ -246,7 +248,7 @@
         this.facadeFactory = facadeFactory;
         this.facade = facadeFactory.getMainWindow();
         facade.initView(this);
-        shutdownAction = new ShutdownClient(facade, this);
+        shutdownAction = new ShutdownClient();
 
         searchField = new JTextField();
         searchField.setName("hostVMTreeFilter");
@@ -410,15 +412,7 @@
         return result;
     }
 
-    public static class ShutdownClient extends WindowAdapter implements ActionListener {
-
-        private JFrame toDispose;
-        private MainWindowFacade facade;
-
-        public ShutdownClient(MainWindowFacade facade, JFrame toDispose) {
-            this.facade = facade;
-            this.toDispose = toDispose;
-        }
+    public class ShutdownClient extends WindowAdapter implements ActionListener {
 
         @Override
         public void windowClosing(WindowEvent e) {
@@ -431,8 +425,8 @@
         }
 
         private void shutdown() {
-            toDispose.dispose();
-            facade.stop();
+            dispose();
+            fireViewPropertyChange(SHUTDOWN_PROPERTY, false, true);
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/src/test/java/com/redhat/thermostat/client/MainWindowFacadeImplTest.java	Wed Mar 21 20:52:40 2012 +0100
@@ -0,0 +1,109 @@
+/*
+ * 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.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import com.mongodb.DB;
+import com.redhat.thermostat.client.ui.MainWindow;
+
+public class MainWindowFacadeImplTest {
+
+    private PropertyChangeListener l;
+
+    private MainWindowFacadeImpl controller;
+
+    private MainWindow view;
+
+    @Before
+    public void setUp() {
+        
+        DB db = mock(DB.class);
+        controller = new MainWindowFacadeImpl(db);
+        controller = spy(controller);
+        view = mock(MainWindow.class);
+        doAnswer(new Answer<Void>() {
+
+            @Override
+            public Void answer(InvocationOnMock invocation) throws Throwable {
+                l = (PropertyChangeListener) invocation.getArguments()[0];
+                return null;
+            }
+            
+        }).when(view).addViewPropertyListener(any(PropertyChangeListener.class));
+        controller.initView(view);
+    }
+
+    @After
+    public void tearDown() {
+        view = null;
+        controller = null;
+        l = null;
+    }
+
+    @Test
+    public void verifyThatShutdownEventStopsController() {
+
+        l.propertyChange(new PropertyChangeEvent(view, MainWindow.SHUTDOWN_PROPERTY, false, true));
+
+        verify(controller).stop();
+
+    }
+
+    @Test
+    public void verifyThatHostsVmsFilterChangeUpdatesTree() {
+
+        l.propertyChange(new PropertyChangeEvent(view, MainWindow.HOST_VM_TREE_FILTER_PROPERTY, null, "test"));
+
+        verify(view).updateTree(eq("test"), any(HostsVMsLoader.class));
+
+    }
+}
--- a/client/src/test/java/com/redhat/thermostat/client/ui/MainWindowTest.java	Wed Mar 21 20:52:33 2012 +0100
+++ b/client/src/test/java/com/redhat/thermostat/client/ui/MainWindowTest.java	Wed Mar 21 20:52:40 2012 +0100
@@ -39,6 +39,7 @@
 import static org.mockito.Matchers.argThat;
 import static org.mockito.Mockito.inOrder;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 import java.beans.PropertyChangeEvent;
@@ -48,6 +49,8 @@
 import org.fest.swing.fixture.FrameFixture;
 import org.fest.swing.fixture.JTextComponentFixture;
 import org.hamcrest.Description;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.mockito.ArgumentMatcher;
@@ -85,9 +88,12 @@
         }
     }
 
-    @Category(GUITest.class)
-    @Test
-    public void testHostVMTreeFilterPropertySupport() {
+    private FrameFixture frameFixture;
+    private MainWindow window;
+    private PropertyChangeListener l;
+
+    @Before
+    public void setUp() {
         MainWindowFacade mainWindowFacade = mock(MainWindowFacade.class);
 
         SummaryPanelFacade summaryPanelFacade = mock(SummaryPanelFacade.class);
@@ -98,11 +104,24 @@
         when(uiFacadeFactory.getMainWindow()).thenReturn(mainWindowFacade);
         when(uiFacadeFactory.getSummaryPanel()).thenReturn(summaryPanelFacade);
 
-        MainWindow window = new MainWindow(uiFacadeFactory);
-        PropertyChangeListener l = mock(PropertyChangeListener.class);
+        window = new MainWindow(uiFacadeFactory);
+        l = mock(PropertyChangeListener.class);
         window.addViewPropertyListener(l);
 
-        FrameFixture frameFixture = new FrameFixture(window);
+        frameFixture = new FrameFixture(window);
+    }
+
+    @After
+    public void tearDown() {
+        frameFixture.cleanUp();
+        frameFixture = null;
+        window = null;
+        l = null;
+    }
+
+    @Category(GUITest.class)
+    @Test
+    public void testHostVMTreeFilterPropertySupport() {
         frameFixture.show();
         JTextComponentFixture hostVMTreeFilterField = frameFixture.textBox("hostVMTreeFilter");
         hostVMTreeFilterField.enterText("test");
@@ -114,4 +133,14 @@
         inOrder.verify(l).propertyChange(argThat(new PropertyChangeEventMatcher(new PropertyChangeEvent(window, MainWindow.HOST_VM_TREE_FILTER_PROPERTY, null, "test"))));
     }
 
+    @Category(GUITest.class)
+    @Test
+    public void verifyThatCloseFiresShutdownEvent() {
+
+        frameFixture.show();
+
+        frameFixture.close();
+        frameFixture.requireNotVisible();
+        verify(l).propertyChange(argThat(new PropertyChangeEventMatcher(new PropertyChangeEvent(window, MainWindow.SHUTDOWN_PROPERTY, false, true))));
+    }
 }