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

Last change on this file was 18985, checked in by taylor.smock, 2 months ago

Fix #23355: Sanity check JVM arguments on startup

See #17858: JOSM will no longer continue running if the user is on an unsupported
Java version (for this commit, older than Java 11; message indicates Java 17).
This does update the link for Azul from Java 17 to Java 21 as well.

In order to (hopefully) reduce confusion, the webstart and Java update nags will
also be reset in the event that JOSM will exit due to old Java versions. This is
mostly so that users will get the messages to update to OpenWebstart or the
appropriate Java link for their platform and architecture.

Additionally, this will (hopefully) reduce the number of tickets we have to close
due to missing JVM arguments by informing users of the missing arguments at startup.

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