QuitAction extends ContextAwareAction { @Override public void actionPerformed(ActionEvent e) { Object[] options = { SHUTDOWN_BUNDLE.getString("OK"), SHUTDOWN_BUNDLE.getString("CANCEL") }; Map<String, Object> hints = new HashMap<String, Object>(); hints.put(WindowManagerImpl.MESSAGE_TYPE, OptionBox.WARNING_MESSAGE); hints.put(WindowManagerImpl.OPTION_TYPE, OptionBox.YES_NO_OPTION); hints.put(WindowManagerImpl.PARENT_COMPONENT, (MCTAbstractHousing) UserEnvironmentRegistry.getActiveHousing()); Object response = PlatformAccess.getPlatform().getWindowManager().showInputDialog( SHUTDOWN_BUNDLE.getString("TITLE"), SHUTDOWN_BUNDLE.getString("MESSAGE"), options, options[0], hints); if (response != null && response.equals(options[0])) { for (MCTAbstractHousing housing : housings) { housing.closeHousing(); } if (UserEnvironmentRegistry.getHousingCount() == 0) { OSGIRuntime osgiRuntime = OSGIRuntimeImpl.getOSGIRuntime(); try { osgiRuntime.stopOSGI(); } catch (Exception e1) { LoggerFactory.getLogger(QuitAction.class).warn(e1.getMessage(), e1); } } } } QuitAction(); @Override void actionPerformed(ActionEvent e); @Override boolean canHandle(ActionContext context); @Override boolean isEnabled();  }
@Test (dataProvider = "generateTestCases") public void testActionPerformed(final boolean confirmed, int housings) throws Exception { MCTAbstractHousing[] mockHousings = initializeRegistry(housings); ContextAwareAction quit = new QuitAction(); Mockito.reset(mockRuntime); WindowManager mockWindowManager = Mockito.mock(WindowManager.class); Mockito.when(mockPlatform.getWindowManager()).thenReturn(mockWindowManager); Mockito.when(mockWindowManager.showInputDialog(Mockito.anyString(), Mockito.anyString(), Mockito.<Object[]>any(), Mockito.any(), Mockito.<Map<String,Object>>any())).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] options = (Object[]) invocation.getArguments()[2]; return confirmed ? options[0] : options[1]; } }); Assert.assertEquals(quit.canHandle(mockActionContext), housings > 0); quit.actionPerformed(mockEvent); Mockito.verify(mockWindowManager, Mockito.times(1)).showInputDialog(Mockito.anyString(), Mockito.anyString(), Mockito.<Object[]>any(), Mockito.any(), Mockito.<Map<String,Object>>any()); for (MCTAbstractHousing mockHousing : mockHousings) { Mockito.verify(mockHousing, confirmed ? Mockito.times(1) : Mockito.never()).closeHousing(); } Assert.assertEquals(UserEnvironmentRegistry.getHousingCount(), confirmed ? 0 : housings); Mockito.verify(mockRuntime, confirmed ? Mockito.times(1) : Mockito.never()).stopOSGI(); }