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

Last change on this file since 14126 was 14126, checked in by Don-vip, 6 years ago

fix #16590 - fix unit tests (patch by ris)

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.junit.Assert.assertTrue;
5
6import org.junit.Rule;
7import org.junit.Test;
8import org.junit.contrib.java.lang.system.ExpectedSystemExit;
9import org.openstreetmap.josm.TestUtils;
10import org.openstreetmap.josm.data.cache.JCSCacheManager;
11import org.openstreetmap.josm.gui.MainApplication;
12import org.openstreetmap.josm.gui.progress.swing.ProgressMonitorExecutor;
13import org.openstreetmap.josm.testutils.JOSMTestRules;
14import org.openstreetmap.josm.tools.ImageProvider;
15
16import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17import mockit.Invocation;
18import mockit.Mock;
19import mockit.MockUp;
20
21/**
22 * Unit tests for class {@link ExitAction}.
23 */
24public 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().platform().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}
Note: See TracBrowser for help on using the repository browser.