Java tutorial
/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with this * work for additional information regarding copyright ownership. The ASF * licenses this file to You under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package nl.rav.comparision.integration.unitofwork.java; import static org.junit.Assert.*; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import org.aopalliance.aop.Advice; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; import org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice; import org.springframework.integration.message.AdviceMessage; import org.springframework.messaging.Message; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.support.GenericMessage; import static org.mockito.Mockito.mock; import org.springframework.core.task.SimpleAsyncTaskExecutor; import org.springframework.integration.channel.ExecutorChannel; import org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException; import org.springframework.messaging.MessagingException; /** * @version */ public class UnitOfWorkSpringTest { private AbstractReplyProducingMessageHandler handler; private QueueChannel replies; PollableChannel successChannel; PollableChannel failureChannel; private AtomicBoolean doFail; private Message<?> completed; private Message<?> completed2; private Message<?> failed; private Message<?> failed2; private CountDownLatch doneLatch = new CountDownLatch(1); private CountDownLatch doneLatch2 = new CountDownLatch(1); @Before public void initializeTest() { doFail = new AtomicBoolean(); createRouteBuilder(); } @Test public void testSuccess() throws Exception { Message<?> reply = sendMessage(); assertNotNull(reply); assertEquals("baz", reply.getPayload()); assertNotNull(completed); assertEquals("Hello, world!", ((AdviceMessage) completed).getInputMessage().getPayload()); assertEquals("foo", completed.getPayload()); assertNull(failed); } @Test public void testFail() throws Exception { doFail.set(true); Message<?> reply = sendMessage(); assertNull(reply); assertNotNull(completed); assertEquals("Hello, world!", ((AdviceMessage) completed).getInputMessage().getPayload()); assertEquals("foo", completed.getPayload()); assertNotNull(failed); assertEquals("baz", ((MessagingException) failed.getPayload()).getFailedMessage().getPayload()); assertEquals("bar:qux", ((MessageHandlingExpressionEvaluatingAdviceException) failed.getPayload()).getEvaluationResult()); } // @Test // public void successFailureAdvice() { // Message<?> reply; // reply = sendMessage(); // assertNotNull(reply); // assertEquals("baz", reply.getPayload()); // // // // // advice with success // sendMessage(); // reply = replies.receive(1000); // assertNotNull(reply); // assertEquals("baz", reply.getPayload()); // // assertNotNull(success); // assertEquals("Hello, world!", ((AdviceMessage) success).getInputMessage().getPayload()); // assertEquals("foo", success.getPayload()); // // // advice with failure, not trapped // doFail.set(true); // try { // sendMessage(); // fail("Expected exception"); // } catch (Exception e) { // assertEquals("qux", e.getCause().getMessage()); // } // // Message<?> failure = failureChannel.receive(1000); // assertNotNull(failure); // assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload()); // assertEquals("bar:qux", ((MessageHandlingExpressionEvaluatingAdviceException) failure.getPayload()).getEvaluationResult()); // // // advice with failure, trapped // advice.setTrapException(true); // sendMessage(); // failure = failureChannel.receive(1000); // assertNotNull(failure); // assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload()); // assertEquals("bar:qux", ((MessageHandlingExpressionEvaluatingAdviceException) failure.getPayload()).getEvaluationResult()); // assertNull(replies.receive(1)); // // // advice with failure, eval is result // advice.setReturnFailureExpressionResult(true); // sendMessage(); // failure = failureChannel.receive(1000); // assertNotNull(failure); // assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload()); // assertEquals("bar:qux", ((MessageHandlingExpressionEvaluatingAdviceException) failure.getPayload()).getEvaluationResult()); // // reply = replies.receive(1000); // assertNotNull(reply); // assertEquals("bar:qux", reply.getPayload()); // // } private Message<?> sendMessage() { Message<String> message = new GenericMessage<String>("Hello, world!"); handler.handleMessage(message); Message<?> reply = replies.receive(1000); completed = successChannel.receive(1000); failed = failureChannel.receive(1000); return reply; } private void createRouteBuilder() { handler = new AbstractReplyProducingMessageHandler() { @Override protected Object handleRequestMessage(Message<?> requestMessage) { return "baz"; } }; AbstractReplyProducingMessageHandler handler2 = new AbstractReplyProducingMessageHandler() { @Override protected Object handleRequestMessage(Message<?> requestMessage) { if (doFail.get()) { throw new RuntimeException("qux"); } return requestMessage.getPayload(); } }; SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(); taskExecutor.setThreadNamePrefix("test-"); ExecutorChannel intermediate = new ExecutorChannel(taskExecutor); handler.setOutputChannel(intermediate); intermediate.subscribe(handler2); replies = new QueueChannel(); handler2.setOutputChannel(replies); successChannel = new QueueChannel(); failureChannel = new QueueChannel(); ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice(); advice.setBeanFactory(mock(BeanFactory.class)); advice.setSuccessChannel(successChannel); advice.setFailureChannel(failureChannel); advice.setOnSuccessExpression("'foo'"); advice.setOnFailureExpression("'bar:' + #exception.message"); List<Advice> adviceChain = new ArrayList<Advice>(); adviceChain.add(advice); handler.setAdviceChain(adviceChain); handler.afterPropertiesSet(); handler2.setAdviceChain(adviceChain); handler2.afterPropertiesSet(); } }