source: josm/trunk/test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java@ 17275

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

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertNotNull;
7import static org.junit.jupiter.api.Assertions.assertNull;
8import static org.junit.jupiter.api.Assertions.assertTrue;
9import static org.junit.jupiter.api.Assertions.fail;
10
11import java.awt.Desktop;
12import java.io.File;
13import java.io.IOException;
14import java.security.KeyStoreException;
15import java.util.Collection;
16
17import org.junit.jupiter.api.Test;
18import org.junit.jupiter.api.BeforeAll;
19import org.junit.jupiter.api.extension.RegisterExtension;
20import org.openstreetmap.josm.TestUtils;
21import org.openstreetmap.josm.spi.preferences.Config;
22import org.openstreetmap.josm.testutils.JOSMTestRules;
23
24import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25
26import mockit.Expectations;
27import mockit.Mocked;
28
29/**
30 * Unit tests of {@link PlatformHookWindows} class.
31 */
32class PlatformHookWindowsTest {
33
34 /**
35 * Setup tests
36 */
37 @RegisterExtension
38 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
39 public JOSMTestRules test = new JOSMTestRules().preferences().https();
40
41 static PlatformHookWindows hook;
42
43 /**
44 * Setup test.
45 */
46 @BeforeAll
47 public static void setUp() {
48 hook = new PlatformHookWindows();
49 }
50
51 /**
52 * Test method for {@code PlatformHookWindows#startupHook}
53 */
54 @Test
55 void testStartupHook() {
56 hook.startupHook((a, b, c, d) -> System.out.println("callback"));
57 }
58
59 /**
60 * Test method for {@code PlatformHookWindows#getRootKeystore}
61 * @throws Exception if an error occurs
62 */
63 @Test
64 void testGetRootKeystore() throws Exception {
65 if (PlatformManager.isPlatformWindows()) {
66 assertNotNull(PlatformHookWindows.getRootKeystore());
67 } else {
68 try {
69 PlatformHookWindows.getRootKeystore();
70 fail("Expected KeyStoreException");
71 } catch (KeyStoreException e) {
72 Logging.info(e.getMessage());
73 }
74 }
75 }
76
77 /**
78 * Test method for {@code PlatformHookWindows#afterPrefStartupHook}
79 */
80 @Test
81 void testAfterPrefStartupHook() {
82 hook.afterPrefStartupHook();
83 }
84
85 /**
86 * Test method for {@code PlatformHookWindows#openUrl} when Desktop works as expected
87 * @param mockDesktop desktop mock
88 * @throws IOException if an error occurs
89 */
90 @Test
91 void testOpenUrlSuccess(@Mocked final Desktop mockDesktop) throws IOException {
92 TestUtils.assumeWorkingJMockit();
93 new Expectations() {{
94 // real implementation would raise HeadlessException
95 Desktop.getDesktop(); result = mockDesktop; times = 1;
96 mockDesktop.browse(withNotNull()); times = 1;
97 }};
98
99 hook.openUrl(Config.getUrls().getJOSMWebsite());
100 }
101
102 /**
103 * Test method for {@code PlatformHookWindows#openUrl} when Desktop fails
104 * @param mockDesktop desktop mock
105 * @param anyRuntime runtime mock
106 * @throws IOException if an error occurs
107 */
108 @Test
109 void testOpenUrlFallback(@Mocked final Desktop mockDesktop, @Mocked Runtime anyRuntime) throws IOException {
110 TestUtils.assumeWorkingJMockit();
111 new Expectations() {{
112 // real implementation would raise HeadlessException
113 Desktop.getDesktop(); result = mockDesktop; times = 1;
114 mockDesktop.browse(withNotNull()); result = new IOException(); times = 1;
115
116 // mock rundll32 in Runtime
117 Runtime.getRuntime(); result = anyRuntime; times = 1;
118 anyRuntime.exec(new String[] {"rundll32", "url.dll,FileProtocolHandler", Config.getUrls().getJOSMWebsite()});
119 result = null;
120 times = 1;
121 // prevent a non-matching invocation being executed
122 anyRuntime.exec((String[]) withNotNull()); result = null; times = 0;
123 }};
124
125 hook.openUrl(Config.getUrls().getJOSMWebsite());
126 }
127
128 /**
129 * Test method for {@code PlatformHookWindows#getAdditionalFonts}
130 */
131 @Test
132 void testGetAdditionalFonts() {
133 assertFalse(hook.getAdditionalFonts().isEmpty());
134 }
135
136 /**
137 * Test method for {@code PlatformHookWindows#getDefaultCacheDirectory}
138 */
139 @Test
140 void testGetDefaultCacheDirectory() {
141 File cache = hook.getDefaultCacheDirectory();
142 assertNotNull(cache);
143 if (PlatformManager.isPlatformWindows()) {
144 assertTrue(cache.toString().contains(":"));
145 }
146 }
147
148 /**
149 * Test method for {@code PlatformHookWindows#getDefaultPrefDirectory}
150 */
151 @Test
152 void testGetDefaultPrefDirectory() {
153 File cache = hook.getDefaultPrefDirectory();
154 assertNotNull(cache);
155 if (PlatformManager.isPlatformWindows()) {
156 assertTrue(cache.toString().contains(":"));
157 }
158 }
159
160 /**
161 * Test method for {@code PlatformHookWindows#getDefaultStyle}
162 */
163 @Test
164 void testGetDefaultStyle() {
165 assertEquals("com.sun.java.swing.plaf.windows.WindowsLookAndFeel", hook.getDefaultStyle());
166 }
167
168 /**
169 * Test method for {@code PlatformHookWindows#getInstalledFonts}
170 */
171 @Test
172 void testGetInstalledFonts() {
173 Collection<String> fonts = hook.getInstalledFonts();
174 if (PlatformManager.isPlatformWindows()) {
175 assertFalse(fonts.isEmpty());
176 } else {
177 assertNull(fonts);
178 }
179 }
180
181 /**
182 * Test method for {@code PlatformHookWindows#getOSDescription}
183 */
184 @Test
185 void testGetOSDescription() {
186 String os = hook.getOSDescription();
187 if (PlatformManager.isPlatformWindows()) {
188 assertTrue(os.contains("Windows"));
189 } else {
190 assertFalse(os.contains("Windows"));
191 }
192 }
193
194 /**
195 * Test method for {@code PlatformHookWindows#initSystemShortcuts}
196 */
197 @Test
198 void testInitSystemShortcuts() {
199 hook.initSystemShortcuts();
200 }
201}
Note: See TracBrowser for help on using the repository browser.