|
13 | 13 | import static org.mockito.Mockito.*; |
14 | 14 | import java.net.URL; |
15 | 15 | import java.util.concurrent.ConcurrentHashMap; |
| 16 | +import java.lang.reflect.Field; |
16 | 17 |
|
17 | 18 | public class CacheTest { |
18 | 19 | private static RemoteWebDriver mockedDriver; |
@@ -58,4 +59,70 @@ public void testCommandExecutorUrl() { |
58 | 59 | String commandExecutorUrl = driverMetadata.getCommandExecutorUrl(); |
59 | 60 | assertEquals(Cache.CACHE_MAP.get(key), commandExecutorUrl); |
60 | 61 | } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testCacheInstantiable() { |
| 65 | + // Exercises the implicit default constructor of Cache (its only line). |
| 66 | + assertNotNull(new Cache()); |
| 67 | + } |
| 68 | + |
| 69 | + // ------------------------------------------------------------------ |
| 70 | + // getCommandExecutorUrl: TracedCommandExecutor unwrap branch. |
| 71 | + // |
| 72 | + // When the executor's class name contains "TracedCommandExecutor", |
| 73 | + // DriverMetadata reflectively reads its private `delegate` field and |
| 74 | + // unwraps to the underlying HttpCommandExecutor. These fixtures let us |
| 75 | + // drive both the successful unwrap and the reflective-failure fallback |
| 76 | + // without a live Selenium tracing executor. |
| 77 | + // ------------------------------------------------------------------ |
| 78 | + |
| 79 | + /** Mirrors Selenium's internal wrapper: a delegate field holding the real executor. */ |
| 80 | + static class TracedCommandExecutor implements CommandExecutor { |
| 81 | + @SuppressWarnings("unused") |
| 82 | + private final CommandExecutor delegate; |
| 83 | + TracedCommandExecutor(CommandExecutor delegate) { this.delegate = delegate; } |
| 84 | + @Override |
| 85 | + public org.openqa.selenium.remote.Response execute(org.openqa.selenium.remote.Command command) { |
| 86 | + throw new UnsupportedOperationException(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** Same name suffix but without a `delegate` field, to drive the catch fallback. */ |
| 91 | + static class BrokenTracedCommandExecutor implements CommandExecutor { |
| 92 | + @Override |
| 93 | + public org.openqa.selenium.remote.Response execute(org.openqa.selenium.remote.Command command) { |
| 94 | + throw new UnsupportedOperationException(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testCommandExecutorUrlUnwrapsTracedExecutor() throws Exception { |
| 100 | + Cache.CACHE_MAP.clear(); |
| 101 | + RemoteWebDriver driver = mock(RemoteWebDriver.class); |
| 102 | + when(driver.getSessionId()).thenReturn(new SessionId("traced-1")); |
| 103 | + |
| 104 | + HttpCommandExecutor inner = mock(HttpCommandExecutor.class); |
| 105 | + when(inner.getAddressOfRemoteServer()).thenReturn(new URL("https://hub.example.com/wd/hub")); |
| 106 | + TracedCommandExecutor traced = new TracedCommandExecutor(inner); |
| 107 | + when(driver.getCommandExecutor()).thenReturn(traced); |
| 108 | + |
| 109 | + DriverMetadata driverMetadata = new DriverMetadata(driver); |
| 110 | + String url = driverMetadata.getCommandExecutorUrl(); |
| 111 | + assertEquals("https://hub.example.com/wd/hub", url); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testCommandExecutorUrlReturnsErrorWhenDelegateFieldMissing() { |
| 116 | + Cache.CACHE_MAP.clear(); |
| 117 | + RemoteWebDriver driver = mock(RemoteWebDriver.class); |
| 118 | + when(driver.getSessionId()).thenReturn(new SessionId("traced-2")); |
| 119 | + when(driver.getCommandExecutor()).thenReturn(new BrokenTracedCommandExecutor()); |
| 120 | + |
| 121 | + DriverMetadata driverMetadata = new DriverMetadata(driver); |
| 122 | + // No `delegate` field -> reflective lookup throws and the catch returns |
| 123 | + // the exception's string form rather than a URL. |
| 124 | + String result = driverMetadata.getCommandExecutorUrl(); |
| 125 | + assertNotNull(result); |
| 126 | + assertTrue(result.contains("NoSuchFieldException") || result.contains("delegate")); |
| 127 | + } |
61 | 128 | } |
0 commit comments