### Question:
RuntimeDelegate { public static RuntimeDelegate getInstance() { RuntimeDelegate result = RuntimeDelegate.CACHED_DELEGATE.get(); if (result != null) { return result; } synchronized(RuntimeDelegate.CACHED_DELEGATE) { result=RuntimeDelegate.CACHED_DELEGATE.get(); if(result==null) { result=findDelegate(); if(result==null) { result=new DefaultRuntimeDelegate(); } RuntimeDelegate.CACHED_DELEGATE.set(result); } return result; } } 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 testGetInstance$cacheWorks() throws Exception { RuntimeDelegate sut1 = sut(); RuntimeDelegate sut2 = sut(); assertThat(sut2,sameInstance(sut1)); }
@Test public void testGetInstance$spi$enabled() throws Exception { System.clearProperty(RuntimeDelegate.APPLICATION_ENGINE_SPI_FINDER); new MockUp<ServiceLoader<RuntimeDelegate>>() { @Mock public Iterator<RuntimeDelegate> iterator() { return new Iterator<RuntimeDelegate>() { int counter=0; @Override public boolean hasNext() { this.counter++; return this.counter<3; } @Override public RuntimeDelegate next() { if(counter<2) { throw new ServiceConfigurationError("FAILURE"); } else { return new CustomRuntimeDelegate(); } } @Override public void remove() { } }; } }; assertThat(sut(),instanceOf(CustomRuntimeDelegate.class)); }
@Test public void testGetInstance$spi$disabled() throws Exception { verifyIsDefault(sut()); }
@Test public void testGetInstance$extension$notReadable() throws Exception { final File file=new File("Not exists"); new MockUp<RuntimeDelegate>() { @Mock File getConfigurationFile() { return file; } }; verifyIsDefault(sut()); }
@Test public void testGetInstance$extension$empty() throws Exception { final File file=extensionConfigFile("empty.cfg"); new MockUp<RuntimeDelegate>() { @Mock File getConfigurationFile() { return file; } }; verifyIsDefault(sut()); }
@Test public void testGetInstance$extension$notFound() throws Exception { final File file=extensionConfigFile("not_found.cfg"); new MockUp<RuntimeDelegate>() { @Mock File getConfigurationFile() { return file; } }; verifyIsDefault(sut()); }
@Test public void testGetInstance$extension$notValid() throws Exception { final File file=extensionConfigFile("not_valid.cfg"); new MockUp<RuntimeDelegate>() { @Mock File getConfigurationFile() { return file; } }; verifyIsDefault(sut()); }
@Test public void testGetInstance$systemProperty$notFound() throws Exception { System.setProperty(RuntimeDelegate.APPLICATION_ENGINE_SPI_PROPERTY,"not found"); verifyIsDefault(sut()); }
@Test public void testGetInstance$systemProperty$notValid() throws Exception { System.setProperty(RuntimeDelegate.APPLICATION_ENGINE_SPI_PROPERTY,NotRuntimeDelegate.class.getName()); verifyIsDefault(sut()); }
@Test public void testGetInstance$systemProperty$valid() throws Exception { System.setProperty(RuntimeDelegate.APPLICATION_ENGINE_SPI_PROPERTY,CustomRuntimeDelegate.class.getName()); assertThat(sut(),instanceOf(CustomRuntimeDelegate.class)); }