source: josm/trunk/test/unit/org/openstreetmap/josm/actions/ExitActionTest.java@ 17383

Last change on this file since 17383 was 17277, checked in by Don-vip, 3 years ago

see #16567 - fix more obvious test errors

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.junit.jupiter.api.Assertions.assertTrue;
5
6import java.awt.GraphicsEnvironment;
7
8import org.junit.jupiter.api.Test;
9import org.junit.jupiter.api.extension.RegisterExtension;
10import org.openstreetmap.josm.TestUtils;
11import org.openstreetmap.josm.data.cache.JCSCacheManager;
12import org.openstreetmap.josm.gui.MainApplication;
13import org.openstreetmap.josm.gui.io.SaveLayersDialog;
14import org.openstreetmap.josm.gui.progress.swing.ProgressMonitorExecutor;
15import org.openstreetmap.josm.testutils.JOSMTestRules;
16import org.openstreetmap.josm.tools.ImageProvider;
17
18import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
19
20import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21import mockit.Invocation;
22import mockit.Mock;
23import mockit.MockUp;
24
25/**
26 * Unit tests for class {@link ExitAction}.
27 */
28final class ExitActionTest {
29
30 /**
31 * Setup test.
32 */
33 @RegisterExtension
34 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
35 public JOSMTestRules test = new JOSMTestRules().main();
36
37 /**
38 * Unit test of {@link ExitAction#actionPerformed}
39 */
40 @Test
41 @ExpectSystemExitWithStatus(0)
42 void testActionPerformed() {
43 TestUtils.assumeWorkingJMockit();
44
45 boolean[] workerShutdownCalled = {false};
46 boolean[] workerShutdownNowCalled = {false};
47 boolean[] imageProviderShutdownCalledNowFalse = {false};
48 boolean[] imageProviderShutdownCalledNowTrue = {false};
49 boolean[] jcsCacheManagerShutdownCalled = {false};
50 boolean[] saveLayersDialogCloseDialogCalled = {false};
51
52 // critically we don't proceed into the actual implementation in any of these mock methods -
53 // that would be quite annoying for tests following this one which were expecting to use any
54 // of these
55 new MockUp<ProgressMonitorExecutor>() {
56 @Mock
57 private void shutdown(Invocation invocation) {
58 if (invocation.getInvokedInstance() == MainApplication.worker) {
59 workerShutdownCalled[0] = true;
60 }
61 }
62
63 @Mock
64 private void shutdownNow(Invocation invocation) {
65 if (invocation.getInvokedInstance() == MainApplication.worker) {
66 // regular shutdown should have been called first
67 assertTrue(workerShutdownCalled[0]);
68 workerShutdownNowCalled[0] = true;
69 }
70 }
71 };
72 new MockUp<ImageProvider>() {
73 @Mock
74 private void shutdown(Invocation invocation, boolean now) {
75 if (now) {
76 // should have already been called with now = false
77 assertTrue(imageProviderShutdownCalledNowFalse[0]);
78 imageProviderShutdownCalledNowTrue[0] = true;
79 } else {
80 imageProviderShutdownCalledNowFalse[0] = true;
81 }
82 }
83 };
84 new MockUp<JCSCacheManager>() {
85 @Mock
86 private void shutdown(Invocation invocation) {
87 jcsCacheManagerShutdownCalled[0] = true;
88 }
89 };
90 if (!GraphicsEnvironment.isHeadless()) {
91 new MockUp<SaveLayersDialog>() {
92 @Mock
93 private void closeDialog(Invocation invocation) {
94 saveLayersDialogCloseDialogCalled[0] = true;
95 }
96 };
97 }
98
99 // No layer
100
101 try {
102 new ExitAction().actionPerformed(null);
103 } finally {
104 // ExpectedSystemExit presumably works using an exception, so executing anything after the
105 // previous line requires it to be put in a finally block
106 assertTrue(workerShutdownCalled[0]);
107 assertTrue(workerShutdownNowCalled[0]);
108 assertTrue(imageProviderShutdownCalledNowFalse[0]);
109 assertTrue(imageProviderShutdownCalledNowTrue[0]);
110 assertTrue(jcsCacheManagerShutdownCalled[0]);
111 if (!GraphicsEnvironment.isHeadless()) {
112 assertTrue(saveLayersDialogCloseDialogCalled[0]);
113 }
114 }
115 }
116}
Note: See TracBrowser for help on using the repository browser.