1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
5 |
|
---|
6 | import java.awt.GraphicsEnvironment;
|
---|
7 |
|
---|
8 | import org.junit.jupiter.api.Test;
|
---|
9 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
10 | import org.openstreetmap.josm.TestUtils;
|
---|
11 | import org.openstreetmap.josm.data.cache.JCSCacheManager;
|
---|
12 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
13 | import org.openstreetmap.josm.gui.io.SaveLayersDialog;
|
---|
14 | import org.openstreetmap.josm.gui.progress.swing.ProgressMonitorExecutor;
|
---|
15 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
16 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
17 |
|
---|
18 | import com.ginsberg.junit.exit.ExpectSystemExitWithStatus;
|
---|
19 |
|
---|
20 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
21 | import mockit.Invocation;
|
---|
22 | import mockit.Mock;
|
---|
23 | import mockit.MockUp;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Unit tests for class {@link ExitAction}.
|
---|
27 | */
|
---|
28 | final 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 | }
|
---|