ExitAction extends SwingWorker<Void, Void> { public void start() { final Collection<ProfileForm> allProfiles = new ArrayList<>(context.allProfiles()); latch = new CountDownLatch(allProfiles.size()); final List<ProfileForm> dirtyProfiles = context.allDirtyProfiles(); if (!dirtyProfiles.isEmpty()) { dialog.open(); final int response = dialog.getResponse(); if (response == ProfileSelectionDialog.YES) { final List<ProfileForm> profilesToSave = dialog.getSelectedProfiles(); for (final ProfileForm profile : profilesToSave) { allProfiles.remove(profile); final JFileChooser fileChooser = context.getProfileFileChooser(); fileChooser.setDialogTitle(String.format("Save profile '%s'", profile.getName())); final FileChooserProxy fileChooserDialog = new FileChooserProxyImpl(profile, fileChooser); final java.nio.file.Path loadedFrom = profile.getProfile().getLoadedFrom(); fileChooser.setSelectedFile(loadedFrom != null ? loadedFrom.toFile() : new File(profile.getName())); final SaveProfileWorker saveJob = new SaveProfileWorker(profileManager, profile, fileChooserDialog); saveJob.addPropertyChangeListener(new SaveJobCompletionListener()); saveJob.start(false); } } else { cancel(true); } } if (!isCancelled()) { execute(); for (final ProfileForm profile : allProfiles) { try { profileManager.closeProfile(profile.getProfile().getUuid()); context.remove(profile.getProfile().getUuid()); } finally { latch.countDown(); } } } } ExitAction(DroidUIContext context, ProfileSelectionDialog dialog, ProfileManager profileManager); void start();  }
@Test public void testExitWhen2DirtyProfilesAreLoadedButWarningDialogReturnsCancel() throws Exception { List<ProfileForm> dirty = new ArrayList<ProfileForm>(); dirty.add(dirtyProfileForm1); dirty.add(dirtyProfileForm2); when(dialog.getResponse()).thenReturn(JOptionPaneProxy.CANCEL); when(context.allDirtyProfiles()).thenReturn(dirty); action.start(); try { action.get(); fail("Expected CancellationException"); } catch (CancellationException e) { } verify(profileManager, never()).save(anyString(), any(Path.class), any(ProgressObserver.class)); verify(profileManager, never()).closeProfile(anyString()); verify(context, never()).remove(anyString()); }
@Test public void testExitWhen2DirtyProfilesAreLoadedAndBothWantSaving() throws Exception { JFileChooser fileChooser = mock(JFileChooser.class); when(fileChooser.getSelectedFile()).thenReturn(new File("foo")); when(context.getProfileFileChooser()).thenReturn(fileChooser); List<ProfileForm> dirty = new ArrayList<ProfileForm>(); dirty.add(dirtyProfileForm1); dirty.add(dirtyProfileForm2); when(context.allDirtyProfiles()).thenReturn(dirty); when(dialog.getSelectedProfiles()).thenReturn(dirty); when(dialog.getResponse()).thenReturn(JOptionPaneProxy.YES); when(profileManager.save(eq("dirty1"), eq(Paths.get("foo.droid")), any(ProgressObserver.class))).thenReturn(dirtyProfile1); when(profileManager.save(eq("dirty2"), eq(Paths.get("foo.droid")), any(ProgressObserver.class))).thenReturn(dirtyProfile2); action.start(); action.get(); verify(profileManager).save(eq("dirty1"), eq(Paths.get("foo.droid")), any(ProgressObserver.class)); verify(profileManager).save(eq("dirty2"), eq(Paths.get("foo.droid")), any(ProgressObserver.class)); verify(profileManager).closeProfile("dirty1"); verify(profileManager).closeProfile("dirty2"); verify(context).remove("dirty1"); verify(context).remove("dirty2"); }
@Test public void testExitWhenTwoDirtyDirtyProfilesAreLoadedAndOneWantsSaving() throws Exception { JFileChooser fileChooser = mock(JFileChooser.class); when(fileChooser.getSelectedFile()).thenReturn(new File("foo")); when(context.getProfileFileChooser()).thenReturn(fileChooser); List<ProfileForm> dirty = new ArrayList<ProfileForm>(); dirty.add(dirtyProfileForm1); dirty.add(dirtyProfileForm2); List<ProfileForm> selected = new ArrayList<ProfileForm>(); selected.add(dirtyProfileForm2); when(profileManager.save(eq("dirty1"), eq(Paths.get("foo.droid")), any(ProgressObserver.class))).thenReturn(dirtyProfile1); when(profileManager.save(eq("dirty2"), eq(Paths.get("foo.droid")), any(ProgressObserver.class))).thenReturn(dirtyProfile2); when(context.allDirtyProfiles()).thenReturn(dirty); when(dialog.getSelectedProfiles()).thenReturn(selected); when(dialog.getResponse()).thenReturn(JOptionPaneProxy.YES); action.start(); action.get(); verify(profileManager, never()).save(eq("dirty1"), any(Path.class), any(ProgressObserver.class)); verify(profileManager).save(eq("dirty2"), eq(Paths.get("foo.droid")), any(ProgressObserver.class)); verify(profileManager).closeProfile("dirty1"); verify(profileManager).closeProfile("dirty2"); verify(context).remove("dirty1"); verify(context).remove("dirty2"); }