@Test(expected = IllegalStateException.class) public void testWrongReturnTypeUsingMethodObject() throws SecurityException, NoSuchMethodException { class TestReleaseStrategy { @SuppressWarnings("unused") public int wrongReturnType(List<Message<?>> message) { return 0; } } MethodInvokingReleaseStrategy wrongReturnType = new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), TestReleaseStrategy.class.getMethod( "wrongReturnType", List.class)); wrongReturnType.setBeanFactory(mock(BeanFactory.class)); wrongReturnType.canRelease(mock(MessageGroup.class)); }
@Override public void afterPropertiesSet() throws Exception { if (this.target instanceof ReleaseStrategy && !StringUtils.hasText(this.methodName)) { this.strategy = (ReleaseStrategy) this.target; return; } if (this.target != null) { if (StringUtils.hasText(this.methodName)) { this.strategy = new MethodInvokingReleaseStrategy(this.target, this.methodName); } else { Method method = MessagingAnnotationUtils.findAnnotatedMethod(this.target, org.springframework.integration.annotation.ReleaseStrategy.class); if (method != null) { this.strategy = new MethodInvokingReleaseStrategy(this.target, method); } else { if (logger.isWarnEnabled()) { logger.warn("No ReleaseStrategy annotated method found on " + this.target.getClass().getSimpleName() + "; falling back to SimpleSequenceSizeReleaseStrategy, target: " + this.target + ", methodName: " + this.methodName); } } } } else { logger.warn("No target supplied; falling back to SimpleSequenceSizeReleaseStrategy"); } }
@Test(expected = IllegalStateException.class) public void testTooManyParametersUsingMethodName() { class TestReleaseStrategy { @SuppressWarnings("unused") public boolean tooManyParameters(List<?> c1, List<?> c2) { return false; } } new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), "tooManyParameters"); }
@Test public void testListSubclassParameterUsingMethodObject() throws SecurityException, NoSuchMethodException { class TestReleaseStrategy { @SuppressWarnings("unused") public boolean listSubclassParameter(LinkedList<?> l1) { return true; } } MethodInvokingReleaseStrategy adapter = new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), TestReleaseStrategy.class.getMethod("listSubclassParameter", LinkedList.class)); adapter.setBeanFactory(mock(BeanFactory.class)); MessageGroup messages = createListOfMessages(3); assertTrue(adapter.canRelease(messages)); }
@Test public void testNotEnoughParametersUsingMethodObject() throws SecurityException, NoSuchMethodException { class TestReleaseStrategy { @SuppressWarnings("unused") public boolean notEnoughParameters() { return false; } } new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), TestReleaseStrategy.class.getMethod( "notEnoughParameters")); }
@Test(expected = IllegalStateException.class) public void testInvalidParameterTypeUsingMethodName() { class TestReleaseStrategy { @SuppressWarnings("unused") public boolean invalidParameterType(Date invalid) { return true; } } MethodInvokingReleaseStrategy adapter = new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), "invalidParameterType"); adapter.setBeanFactory(mock(BeanFactory.class)); MessageGroup messages = createListOfMessages(3); assertTrue(adapter.canRelease(messages)); }
@Test(expected = IllegalArgumentException.class) public void testTooManyParametersUsingMethodObject() throws SecurityException, NoSuchMethodException { class TestReleaseStrategy { @SuppressWarnings("unused") public boolean tooManyParameters(List<?> c1, List<?> c2) { return false; } } new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), TestReleaseStrategy.class.getMethod( "tooManyParameters", List.class, List.class)); }
@Test public void testListSubclassParameterUsingMethodName() { class TestReleaseStrategy { @SuppressWarnings("unused") public boolean listSubclassParameter(LinkedList<?> l1) { return true; } } MethodInvokingReleaseStrategy adapter = new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), "listSubclassParameter"); adapter.setBeanFactory(mock(BeanFactory.class)); MessageGroup messages = createListOfMessages(3); assertTrue(adapter.canRelease(messages)); }
@Test public void testNotEnoughParametersUsingMethodName() { class TestReleaseStrategy { @SuppressWarnings("unused") public boolean notEnoughParameters() { return false; } } new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), "notEnoughParameters"); }
@Test(expected = ConversionFailedException.class) public void testWrongReturnType() throws SecurityException, NoSuchMethodError { class TestReleaseStrategy { @SuppressWarnings("unused") public String wrongReturnType(List<Message<?>> messages) { return "foo"; } } MethodInvokingReleaseStrategy adapter = new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), "wrongReturnType"); adapter.setBeanFactory(mock(BeanFactory.class)); MessageGroup messages = createListOfMessages(3); assertTrue(adapter.canRelease(messages)); }
@Test(expected = IllegalStateException.class) public void testAdapterWithWrongMethodName() { class TestReleaseStrategy { } new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), "methodThatDoesNotExist"); }
@Test public void testAdapterWithTypeParametrizedMessageBasedMethod() { class TestReleaseStrategy { @SuppressWarnings("unused") public boolean checkCompletenessOnListOfMessagesParametrizedWithString(List<Message<String>> messages) { assertTrue(messages.size() > 0); return messages.size() > new IntegrationMessageHeaderAccessor(messages.iterator().next()) .getSequenceSize(); } } MethodInvokingReleaseStrategy adapter = new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), "checkCompletenessOnListOfMessagesParametrizedWithString"); adapter.setBeanFactory(mock(BeanFactory.class)); MessageGroup messages = createListOfMessages(3); assertTrue(adapter.canRelease(messages)); }
Method releaseStrategyMethod = MessagingAnnotationUtils.findAnnotatedMethod(bean, ReleaseStrategy.class); if (releaseStrategyMethod != null) { releaseStrategy = new MethodInvokingReleaseStrategy(bean, releaseStrategyMethod);
@Test public void testAdapterWithWildcardParametrizedMessageBasedMethod() { class TestReleaseStrategy { @SuppressWarnings("unused") public boolean checkCompletenessOnListOfMessagesParametrizedWithWildcard(List<Message<?>> messages) { assertTrue(messages.size() > 0); return messages.size() > new IntegrationMessageHeaderAccessor(messages.iterator().next()).getSequenceSize(); } } MethodInvokingReleaseStrategy adapter = new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), "checkCompletenessOnListOfMessagesParametrizedWithWildcard"); adapter.setBeanFactory(mock(BeanFactory.class)); MessageGroup messages = createListOfMessages(3); assertTrue(adapter.canRelease(messages)); }
@Override public void afterPropertiesSet() throws Exception { if (this.target instanceof ReleaseStrategy && !StringUtils.hasText(this.methodName)) { this.strategy = (ReleaseStrategy) this.target; return; } if (this.target != null) { if (StringUtils.hasText(this.methodName)) { this.strategy = new MethodInvokingReleaseStrategy(this.target, this.methodName); } else { Method method = MessagingAnnotationUtils.findAnnotatedMethod(this.target, org.springframework.integration.annotation.ReleaseStrategy.class); if (method != null) { this.strategy = new MethodInvokingReleaseStrategy(this.target, method); } else { if (logger.isWarnEnabled()) { logger.warn("No ReleaseStrategy annotated method found on " + this.target.getClass().getSimpleName() + "; falling back to SimpleSequenceSizeReleaseStrategy, target: " + this.target + ", methodName: " + this.methodName); } } } } else { logger.warn("No target supplied; falling back to SimpleSequenceSizeReleaseStrategy"); } }
@Test public void testAdapterWithNonParameterizedMessageListBasedMethod() { class TestReleaseStrategy { @SuppressWarnings("unused") public boolean checkCompletenessOnNonParameterizedListOfMessages(List<Message<?>> messages) { assertTrue(messages.size() > 0); return messages.size() > new IntegrationMessageHeaderAccessor(messages.iterator().next()).getSequenceSize(); } } MethodInvokingReleaseStrategy adapter = new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), "checkCompletenessOnNonParameterizedListOfMessages"); adapter.setBeanFactory(mock(BeanFactory.class)); MessageGroup messages = createListOfMessages(3); assertTrue(adapter.canRelease(messages)); }
Method releaseStrategyMethod = MessagingAnnotationUtils.findAnnotatedMethod(bean, ReleaseStrategy.class); if (releaseStrategyMethod != null) { releaseStrategy = new MethodInvokingReleaseStrategy(bean, releaseStrategyMethod);
@Test public void testFalseConvertedProperly() { MethodInvokingReleaseStrategy adapter = new MethodInvokingReleaseStrategy(new AlwaysFalseReleaseStrategy(), "checkCompleteness"); adapter.setBeanFactory(mock(BeanFactory.class)); assertFalse(adapter.canRelease(createListOfMessages(0))); }
@Test public void testAdapterWithPojoBasedMethod() { class TestReleaseStrategy { @SuppressWarnings("unused") // Example for the case when completeness is checked on the structure of // the data public boolean checkCompletenessOnListOfStrings(List<String> messages) { StringBuilder buffer = new StringBuilder(); for (String content : messages) { buffer.append(content); } return buffer.length() >= 9; } } MethodInvokingReleaseStrategy adapter = new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), "checkCompletenessOnListOfStrings"); adapter.setBeanFactory(mock(BeanFactory.class)); MessageGroup messages = createListOfMessages(3); assertTrue(adapter.canRelease(messages)); }
@Test public void testAdapterWithPojoBasedMethodReturningObject() { class TestReleaseStrategy { @SuppressWarnings("unused") // Example for the case when completeness is checked on the structure of // the data public boolean checkCompletenessOnListOfStrings(List<String> messages) { StringBuilder buffer = new StringBuilder(); for (String content : messages) { buffer.append(content); } return buffer.length() >= 9; } } MethodInvokingReleaseStrategy adapter = new MethodInvokingReleaseStrategy(new TestReleaseStrategy(), "checkCompletenessOnListOfStrings"); adapter.setBeanFactory(mock(BeanFactory.class)); MessageGroup messages = createListOfMessages(3); assertTrue(adapter.canRelease(messages)); }