Ticket #16946: v1-0001-PlatformHookWindowsTest-fix-openUrl-test-using-jm.patch
File v1-0001-PlatformHookWindowsTest-fix-openUrl-test-using-jm.patch, 3.7 KB (added by , 7 years ago) |
---|
-
test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java
From 117702751d824cb7ee62b25d3e82074ebbb073dd Mon Sep 17 00:00:00 2001 From: Robert Scott <code@humanleg.org.uk> Date: Sat, 3 Nov 2018 15:36:46 +0000 Subject: [PATCH v1 1/2] PlatformHookWindowsTest: fix openUrl test using jmockit, expand cases covered --- .../josm/tools/PlatformHookWindowsTest.java | 53 +++++++++++++++++----- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java b/test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java index 7255cb500..726e0a3b3 100644
a b import static org.junit.Assert.assertTrue; 9 9 import static org.junit.Assume.assumeFalse; 10 10 import static org.junit.Assert.fail; 11 11 12 import java.awt.Desktop; 12 13 import java.io.File; 13 14 import java.io.IOException; 14 15 import java.security.KeyStore; … … import java.util.Collection; 19 20 import org.junit.BeforeClass; 20 21 import org.junit.Test; 21 22 import org.openstreetmap.josm.JOSMFixture; 23 import org.openstreetmap.josm.TestUtils; 22 24 import org.openstreetmap.josm.io.remotecontrol.RemoteControlHttpsServer; 23 25 import org.openstreetmap.josm.io.remotecontrol.RemoteControlTest; 24 26 import org.openstreetmap.josm.spi.preferences.Config; 25 27 28 import mockit.Expectations; 29 import mockit.Injectable; 30 26 31 /** 27 32 * Unit tests of {@link PlatformHookWindows} class. 28 33 */ … … public class PlatformHookWindowsTest { 116 121 } 117 122 118 123 /** 119 * Test method for {@code PlatformHookWindows#openUrl} 124 * Test method for {@code PlatformHookWindows#openUrl} when Desktop works as expected 120 125 * @throws IOException if an error occurs 121 126 */ 122 127 @Test 123 public void testOpenUrl() throws IOException { 124 if (PlatformManager.isPlatformWindows()) { 125 hook.openUrl(Config.getUrls().getJOSMWebsite()); 126 } else { 127 try { 128 hook.openUrl(Config.getUrls().getJOSMWebsite()); 129 fail("Expected IOException"); 130 } catch (IOException e) { 131 Logging.info(e.getMessage()); 132 } 133 } 128 public void testOpenUrlSuccess(@Injectable final Desktop mockDesktop) throws IOException { 129 TestUtils.assumeWorkingJMockit(); 130 new Expectations(Desktop.class) {{ 131 // real implementation would raise HeadlessException 132 Desktop.getDesktop(); result = mockDesktop; times = 1; 133 }}; 134 new Expectations() {{ 135 mockDesktop.browse(withNotNull()); times = 1; 136 }}; 137 138 hook.openUrl(Config.getUrls().getJOSMWebsite()); 139 } 140 141 /** 142 * Test method for {@code PlatformHookWindows#openUrl} when Desktop fails 143 * @throws IOException if an error occurs 144 */ 145 @Test 146 public void testOpenUrlFallback(@Injectable final Desktop mockDesktop) throws IOException { 147 TestUtils.assumeWorkingJMockit(); 148 new Expectations(Desktop.class) {{ 149 // real implementation would raise HeadlessException 150 Desktop.getDesktop(); result = mockDesktop; times = 1; 151 }}; 152 new Expectations() {{ 153 mockDesktop.browse(withNotNull()); result = new IOException(); times = 1; 154 }}; 155 final Runtime anyRuntime = Runtime.getRuntime(); 156 new Expectations(Runtime.class) {{ 157 anyRuntime.exec(new String[] {"rundll32", "url.dll,FileProtocolHandler", Config.getUrls().getJOSMWebsite()}); result = null; times = 1; 158 // prevent a non-matching invocation being executed 159 anyRuntime.exec((String[]) withNotNull()); result = null; times = 0; 160 }}; 161 162 hook.openUrl(Config.getUrls().getJOSMWebsite()); 134 163 } 135 164 136 165 /**