### Question:
RuntimeDelegate { public static void setInstance(final RuntimeDelegate delegate) { SecurityManager security = getSecurityManager(); if (security != null) { security.checkPermission(SUPPRESS_ACCESS_CHECKS_PERMISSION); } synchronized(RuntimeDelegate.CACHED_DELEGATE) { RuntimeDelegate.CACHED_DELEGATE.set(delegate); } } protected  RuntimeDelegate(); static RuntimeDelegate getInstance(); static void setInstance(final RuntimeDelegate delegate); abstract boolean isOffline(); abstract WriteSession createSession(); abstract ResourceSnapshotResolver createResourceResolver(URI canonicalBase, ReadSession session); abstract void registerShutdownListener(ShutdownListener listener); static final String APPLICATION_ENGINE_SPI_FINDER; static final String APPLICATION_ENGINE_SPI_CFG; static final String APPLICATION_ENGINE_SPI_PROPERTY; }### Answer:
@Test public void testSetInstance$null() throws Exception { RuntimeDelegate.setInstance(null); verifyIsDefault(sut()); }
@Test public void testSetInstance$securityManager$happyPath() throws Exception { class CustomSecurityManager extends SecurityManager { private final List<Permission> permissions=Lists.newArrayList(); @Override public void checkPermission(Permission permission) { this.permissions.add(permission); } }; final CustomSecurityManager manager=new CustomSecurityManager(); new MockUp<RuntimeDelegate>() { @Mock public SecurityManager getSecurityManager() { return manager; } }; RuntimeDelegate.setInstance(null); assertThat(hasReflectionPermission(manager.permissions),equalTo(true)); }
@Test public void testSetInstance$securityManager$exceptionPath() throws Exception { class CustomSecurityManager extends SecurityManager { @Override public void checkPermission(Permission permission) { if(permission instanceof ReflectPermission) { ReflectPermission rp=(ReflectPermission)permission; if(rp.getName().equals("suppressAccessChecks")) { throw new SecurityException("FAILURE"); } } } }; final SecurityManager manager=new CustomSecurityManager(); new MockUp<RuntimeDelegate>() { @Mock public SecurityManager getSecurityManager() { return manager; } }; try { RuntimeDelegate.setInstance(new CustomRuntimeDelegate()); fail("Should not be able to set instance if reflection is not allowed"); } catch (SecurityException e) { assertThat(e.getMessage(),equalTo("FAILURE")); } }