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