public void testOpenInterceptors() throws Exception { RepeatTemplate template = new RepeatTemplate(); final List<Object> calls = new ArrayList<>(); template.setListeners(new RepeatListener[] { new RepeatListenerSupport() { @Override public void open(RepeatContext context) { calls.add("1"); } }, new RepeatListenerSupport() { @Override public void open(RepeatContext context) { calls.add("2"); context.setCompleteOnly(); } } }); template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; return RepeatStatus.CONTINUABLE; } }); assertEquals(0, count); assertEquals("[1, 2]", calls.toString()); }
public void testAfterInterceptors() throws Exception { RepeatTemplate template = new RepeatTemplate(); final List<Object> calls = new ArrayList<>(); template.setListeners(new RepeatListener[] { new RepeatListenerSupport() { @Override public void after(RepeatContext context, RepeatStatus result) { calls.add("1"); } }, new RepeatListenerSupport() { @Override public void after(RepeatContext context, RepeatStatus result) { calls.add("2"); } } }); template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; return RepeatStatus.continueIf(count <= 1); } }); // 2 calls to the callback, and the second one had no processing... assertEquals(2, count); // ... so the interceptor after() is not called: assertEquals("[2, 1]", calls.toString()); }
public void testCloseInterceptors() throws Exception { RepeatTemplate template = new RepeatTemplate(); final List<Object> calls = new ArrayList<>(); template.setListeners(new RepeatListener[] { new RepeatListenerSupport() { @Override public void close(RepeatContext context) { calls.add("1"); } }, new RepeatListenerSupport() { @Override public void close(RepeatContext context) { calls.add("2"); } } }); template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; return RepeatStatus.continueIf(count < 2); } }); // Test that more than one call comes in to the callback... assertEquals(2, count); // ... but the interceptor is only called once. assertEquals("[2, 1]", calls.toString()); }
public void testBeforeInterceptors() throws Exception { RepeatTemplate template = new RepeatTemplate(); final List<Object> calls = new ArrayList<>(); template.setListeners(new RepeatListener[] { new RepeatListenerSupport() { @Override public void before(RepeatContext context) { calls.add("1"); } }, new RepeatListenerSupport() { @Override public void before(RepeatContext context) { calls.add("2"); } } }); template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; return RepeatStatus.continueIf(count <= 1); } }); // 2 calls: the second time there is no processing // (despite the fact that the callback returned null and batch was // complete). Is this OK? assertEquals(2, count); // ... but the interceptor before() was called: assertEquals("[1, 2, 1, 2]", calls.toString()); }
public void testOnErrorInterceptors() throws Exception { RepeatTemplate template = new RepeatTemplate(); final List<Object> calls = new ArrayList<>(); template.setListeners(new RepeatListener[] { new RepeatListenerSupport() { @Override public void onError(RepeatContext context, Throwable t) { calls.add("1"); } }, new RepeatListenerSupport() { @Override public void onError(RepeatContext context, Throwable t) { calls.add("2"); } } }); try { template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { throw new IllegalStateException("Bogus"); } }); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { // expected } assertEquals(0, count); assertEquals("[2, 1]", calls.toString()); }
public void testOnErrorInterceptorsPrecedence() throws Exception { RepeatTemplate template = new RepeatTemplate(); final List<Object> calls = new ArrayList<>(); template.setListeners(new RepeatListener[] { new RepeatListenerSupport() { @Override public void after(RepeatContext context, RepeatStatus result) { calls.add("1"); } }, new RepeatListenerSupport() { @Override public void onError(RepeatContext context, Throwable t) { calls.add("2"); } } }); try { template.iterate(new RepeatCallback() { @Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { throw new IllegalStateException("Bogus"); } }); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { // expected } assertEquals(0, count); // The after is not executed, if there is an error... assertEquals("[2]", calls.toString()); }
template.setListeners(new RepeatListener[] { listener });