DoWhile extends While { @Override public void doService(AdaptrisMessage msg) throws ServiceException { int loopCount = 0; try { do { getThen().getService().doService(msg); loopCount++; if (!continueLooping(loopCount, msg)) { break; } log.trace("Testing condition for 'DO-WHILE', with condition class {}", this.getCondition().getClass().getSimpleName()); } while (getCondition().evaluate(msg)); log.trace("Logical 'DO-WHILE' completed, exiting."); } catch (Exception e) { throw ExceptionHelper.wrapServiceException(e); } } DoWhile(); @Override void doService(AdaptrisMessage msg);  }
@Test public void testShouldRunServiceOnce() throws Exception { when(mockCondition.evaluate(message)) .thenReturn(false); doWhile.doService(message); verify(mockService, times(1)).doService(message); }
@Test public void testShouldRunServiceMaxDefault() throws Exception { when(mockCondition.evaluate(message)) .thenReturn(true); doWhile.doService(message); verify(mockService, times(10)).doService(message); }
@Test public void testShouldRunServiceConfiguredFive() throws Exception { when(mockCondition.evaluate(message)) .thenReturn(true); doWhile.withMaxLoops(5); doWhile.withOnMaxLoops((e) -> { return; }); doWhile.doService(message); verify(mockService, times(5)).doService(message); }
@Test public void testMaxLoops_ThenFail() throws Exception { when(mockCondition.evaluate(message)).thenReturn(true); doWhile.withMaxLoops(5); doWhile.withOnMaxLoops(new OnMaxThrowException()); try { doWhile.doService(message); } catch (ServiceException expected) { } verify(mockService, times(5)).doService(message); }
@Test public void testShouldRunServiceUnconfiguredFive() throws Exception { when(mockCondition.evaluate(message)) .thenReturn(true) .thenReturn(true) .thenReturn(true) .thenReturn(true) .thenReturn(false); doWhile.withMaxLoops(0); doWhile.doService(message); verify(mockService, times(5)).doService(message); }
@Test public void testInnerServiceExceptionPropagated() throws Exception { when(mockCondition.evaluate(message)) .thenReturn(true); doThrow(new ServiceException()) .when(mockService) .doService(message); try { doWhile.doService(message); fail("Expected a service exception"); } catch (ServiceException ex) { } }