@Test public void createAndTermFirstMapperOkSecondMapperFails() throws TermLookupServiceException { InteractTypeMapperFactory factory1 = Mockito.mock(InteractTypeMapperFactory.class); InteractTypeMapper mapper = Mockito.mock(InteractTypeMapper.class); when(factory1.create()).thenReturn(mapper); InteractTypeMapperFactory factory2 = Mockito.mock(InteractTypeMapperFactory.class); when(factory2.create()).thenThrow(new TermLookupServiceException("kaboom!")); InteractTypeMapper interactTypeMapper = new InteractTypeMapperFactoryWithFallback(factory1, factory2).create(); assertThat(interactTypeMapper, is(mapper)); }
@Override public InteractTypeMapper create() throws TermLookupServiceException { InteractTypeMapper mapper = null; TermLookupServiceException lastException = null; for (InteractTypeMapperFactory factory : factoryAlternatives) { try { mapper = factory.create(); if (mapper != null) { break; } } catch (TermLookupServiceException ex) { lastException = ex; } } if (mapper == null) { throw lastException == null ? new TermLookupServiceException("failed to create interaction type mapper") : lastException; } return mapper; }
InteractTypeMapperFactoryWithFallback implements InteractTypeMapperFactory { @Override public InteractTypeMapper create() throws TermLookupServiceException { InteractTypeMapper mapper = null; TermLookupServiceException lastException = null; for (InteractTypeMapperFactory factory : factoryAlternatives) { try { mapper = factory.create(); if (mapper != null) { break; } } catch (TermLookupServiceException ex) { lastException = ex; } } if (mapper == null) { throw lastException == null ? new TermLookupServiceException("failed to create interaction type mapper") : lastException; } return mapper; } }
InteractTypeMapperFactoryWithFallback implements InteractTypeMapperFactory { @Override public InteractTypeMapper create() throws TermLookupServiceException { InteractTypeMapper mapper = null; TermLookupServiceException lastException = null; for (InteractTypeMapperFactory factory : factoryAlternatives) { try { mapper = factory.create(); if (mapper != null) { break; } } catch (TermLookupServiceException ex) { lastException = ex; } } if (mapper == null) { throw lastException == null ? new TermLookupServiceException("failed to create interaction type mapper") : lastException; } return mapper; } InteractTypeMapperFactoryWithFallback(InteractTypeMapperFactory... factory); }
InteractTypeMapperFactoryWithFallback implements InteractTypeMapperFactory { @Override public InteractTypeMapper create() throws TermLookupServiceException { InteractTypeMapper mapper = null; TermLookupServiceException lastException = null; for (InteractTypeMapperFactory factory : factoryAlternatives) { try { mapper = factory.create(); if (mapper != null) { break; } } catch (TermLookupServiceException ex) { lastException = ex; } } if (mapper == null) { throw lastException == null ? new TermLookupServiceException("failed to create interaction type mapper") : lastException; } return mapper; } InteractTypeMapperFactoryWithFallback(InteractTypeMapperFactory... factory); @Override InteractTypeMapper create(); }
InteractTypeMapperFactoryWithFallback implements InteractTypeMapperFactory { @Override public InteractTypeMapper create() throws TermLookupServiceException { InteractTypeMapper mapper = null; TermLookupServiceException lastException = null; for (InteractTypeMapperFactory factory : factoryAlternatives) { try { mapper = factory.create(); if (mapper != null) { break; } } catch (TermLookupServiceException ex) { lastException = ex; } } if (mapper == null) { throw lastException == null ? new TermLookupServiceException("failed to create interaction type mapper") : lastException; } return mapper; } InteractTypeMapperFactoryWithFallback(InteractTypeMapperFactory... factory); @Override InteractTypeMapper create();  }