use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class ErrorMessageExceptionTypeRouterTests method fallbackToNextMostSpecificCause.
@Test
public void fallbackToNextMostSpecificCause() {
Message<?> failedMessage = new GenericMessage<String>("foo");
IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
RuntimeException middleCause = new RuntimeException(rootCause);
MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause);
ErrorMessage message = new ErrorMessage(error);
ErrorMessageExceptionTypeRouter router = new ErrorMessageExceptionTypeRouter();
router.setBeanFactory(beanFactory);
router.setApplicationContext(TestUtils.createTestApplicationContext());
router.setChannelMapping(RuntimeException.class.getName(), "runtimeExceptionChannel");
router.setChannelMapping(MessageHandlingException.class.getName(), "runtimeExceptionChannel");
router.setDefaultOutputChannel(defaultChannel);
router.handleMessage(message);
assertNotNull(runtimeExceptionChannel.receive(1000));
assertNull(illegalArgumentChannel.receive(0));
assertNull(defaultChannel.receive(0));
assertNull(messageHandlingExceptionChannel.receive(0));
}
use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class ErrorMessageExceptionTypeRouterTests method fallbackToDefaultChannel.
@Test
public void fallbackToDefaultChannel() {
Message<?> failedMessage = new GenericMessage<String>("foo");
IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
RuntimeException middleCause = new RuntimeException(rootCause);
MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause);
ErrorMessage message = new ErrorMessage(error);
ErrorMessageExceptionTypeRouter router = new ErrorMessageExceptionTypeRouter();
router.setApplicationContext(TestUtils.createTestApplicationContext());
router.setDefaultOutputChannel(defaultChannel);
router.handleMessage(message);
assertNotNull(defaultChannel.receive(1000));
assertNull(runtimeExceptionChannel.receive(0));
assertNull(illegalArgumentChannel.receive(0));
assertNull(messageHandlingExceptionChannel.receive(0));
}
use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class ErrorMessageExceptionTypeRouterTests method testHierarchicalMapping.
@Test
public void testHierarchicalMapping() {
IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
MessageHandlingException error = new MessageRejectedException(new GenericMessage<Object>("foo"), "failed", rootCause);
ErrorMessage message = new ErrorMessage(error);
ErrorMessageExceptionTypeRouter router = new ErrorMessageExceptionTypeRouter();
router.setBeanFactory(beanFactory);
router.setApplicationContext(TestUtils.createTestApplicationContext());
router.setChannelMapping(MessageHandlingException.class.getName(), "messageHandlingExceptionChannel");
router.setDefaultOutputChannel(defaultChannel);
router.handleMessage(message);
assertNotNull(messageHandlingExceptionChannel.receive(1000));
assertNull(defaultChannel.receive(0));
}
use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class AdvisedMessageHandlerTests method propagateOnSuccessExpressionFailures.
@Test
public void propagateOnSuccessExpressionFailures() {
final AtomicBoolean doFail = new AtomicBoolean();
AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
if (doFail.get()) {
throw new RuntimeException("qux");
}
return "baz";
}
};
QueueChannel replies = new QueueChannel();
handler.setOutputChannel(replies);
Message<String> message = new GenericMessage<String>("Hello, world!");
PollableChannel successChannel = new QueueChannel();
PollableChannel failureChannel = new QueueChannel();
ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
advice.setBeanFactory(mock(BeanFactory.class));
advice.setSuccessChannel(successChannel);
advice.setFailureChannel(failureChannel);
advice.setOnSuccessExpressionString("1/0");
advice.setOnFailureExpressionString("1/0");
List<Advice> adviceChain = new ArrayList<Advice>();
adviceChain.add(advice);
handler.setAdviceChain(adviceChain);
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
// failing advice with success
handler.handleMessage(message);
Message<?> reply = replies.receive(1000);
assertNotNull(reply);
assertEquals("baz", reply.getPayload());
Message<?> success = successChannel.receive(1000);
assertNotNull(success);
assertEquals("Hello, world!", ((AdviceMessage<?>) success).getInputMessage().getPayload());
assertEquals(ArithmeticException.class, success.getPayload().getClass());
assertEquals("/ by zero", ((Exception) success.getPayload()).getMessage());
// propagate failing advice with success
advice.setPropagateEvaluationFailures(true);
try {
handler.handleMessage(message);
fail("Expected Exception");
} catch (MessageHandlingException e) {
assertEquals("/ by zero", e.getCause().getMessage());
}
reply = replies.receive(1);
assertNull(reply);
success = successChannel.receive(1000);
assertNotNull(success);
assertEquals("Hello, world!", ((AdviceMessage<?>) success).getInputMessage().getPayload());
assertEquals(ArithmeticException.class, success.getPayload().getClass());
assertEquals("/ by zero", ((Exception) success.getPayload()).getMessage());
}
use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.
the class RemoteFileOutboundGatewayTests method testGetExists.
@SuppressWarnings("unchecked")
@Test
public void testGetExists() throws Exception {
SessionFactory sessionFactory = mock(SessionFactory.class);
TestRemoteFileOutboundGateway gw = new TestRemoteFileOutboundGateway(sessionFactory, "get", "payload");
gw.setLocalDirectory(new File(this.tmpDir));
gw.afterPropertiesSet();
File outFile = new File(this.tmpDir + "/f1");
FileOutputStream fos = new FileOutputStream(outFile);
fos.write("foo".getBytes());
fos.close();
when(sessionFactory.getSession()).thenReturn(new TestSession() {
@Override
public TestLsEntry[] list(String path) throws IOException {
return new TestLsEntry[] { new TestLsEntry("f1", 1234, false, false, 12345, "-rw-r--r--") };
}
@Override
public void read(String source, OutputStream outputStream) throws IOException {
outputStream.write("testfile".getBytes());
}
});
// default (null)
MessageBuilder<File> out;
try {
out = (MessageBuilder<File>) gw.handleRequestMessage(new GenericMessage<>("f1"));
fail("Exception expected");
} catch (MessageHandlingException e) {
assertThat(e.getMessage(), containsString("already exists"));
}
gw.setFileExistsMode(FileExistsMode.FAIL);
try {
out = (MessageBuilder<File>) gw.handleRequestMessage(new GenericMessage<>("f1"));
fail("Exception expected");
} catch (MessageHandlingException e) {
assertThat(e.getMessage(), containsString("already exists"));
}
gw.setFileExistsMode(FileExistsMode.IGNORE);
out = (MessageBuilder<File>) gw.handleRequestMessage(new GenericMessage<>("f1"));
assertEquals(outFile, out.getPayload());
assertContents("foo", outFile);
gw.setFileExistsMode(FileExistsMode.APPEND);
out = (MessageBuilder<File>) gw.handleRequestMessage(new GenericMessage<>("f1"));
assertEquals(outFile, out.getPayload());
assertContents("footestfile", outFile);
gw.setFileExistsMode(FileExistsMode.REPLACE);
out = (MessageBuilder<File>) gw.handleRequestMessage(new GenericMessage<>("f1"));
assertEquals(outFile, out.getPayload());
assertContents("testfile", outFile);
outFile.delete();
}
Aggregations