### Question:
LazyInstanceSelector extends InstanceSelector<T> { @Override T select( IndexSlot slot ) { return instances.computeIfAbsent( slot, s -> { assertOpen(); return factory.apply( s ); } ); } LazyInstanceSelector( Function<IndexSlot,T> factory );   LazyInstanceSelector( EnumMap<IndexSlot,T> map, Function<IndexSlot,T> factory );   }### Answer:
@Test public void shouldPerformActionOnAll() { Function<IndexSlot,String> factory = slotToStringFunction(); LazyInstanceSelector<String> selector = new LazyInstanceSelector<>( factory ); selector.select( STRING ); Consumer<String> consumer = mock( Consumer.class ); selector.forAll( consumer ); for ( IndexSlot slot : IndexSlot.values() ) { verify( consumer, times( 1 ) ).accept( String.valueOf( slot ) ); } verifyNoMoreInteractions( consumer ); }
@Test public void shouldCloseAllInstantiated() { Function<IndexSlot,String> factory = slotToStringFunction(); LazyInstanceSelector<String> selector = new LazyInstanceSelector<>( factory ); selector.select( NUMBER ); selector.select( STRING ); Consumer<String> consumer = mock( Consumer.class ); selector.close( consumer ); verify( consumer, times( 1 ) ).accept( String.valueOf( NUMBER ) ); verify( consumer, times( 1 ) ).accept( String.valueOf( STRING ) ); verifyNoMoreInteractions( consumer ); }
@Test public void shouldPreventInstantiationAfterClose() { Function<IndexSlot,String> factory = slotToStringFunction(); LazyInstanceSelector<String> selector = new LazyInstanceSelector<>( factory ); selector.select( NUMBER ); selector.select( STRING ); selector.close( mock( Consumer.class ) ); try { selector.select( TEMPORAL ); fail( "Should have failed" ); } catch ( IllegalStateException e ) { } }