diff --git a/spring-integration-core/src/test/java/org/springframework/integration/message/ExpressionEvaluatingMessageHandlerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/message/ExpressionEvaluatingMessageHandlerTests.java index 37cebae97c..c6cb3bcbf1 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/message/ExpressionEvaluatingMessageHandlerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/message/ExpressionEvaluatingMessageHandlerTests.java @@ -1 +1,95 @@ -/* * Copyright 2002-2013 the original author or authors. * * Licensed 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 org.springframework.integration.message; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import java.util.HashMap; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.integration.Message; import org.springframework.integration.MessagingException; import org.springframework.integration.handler.ExpressionEvaluatingMessageHandler; /** * @author Artem Bilan * @author Gary Russell * @since 2.1 */ public class ExpressionEvaluatingMessageHandlerTests { private ExpressionParser parser; @Before public void setup() { parser = new SpelExpressionParser(); } @Test public void validExpression() { Expression expression = parser.parseExpression("T(System).out.println(payload)"); ExpressionEvaluatingMessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage("test")); } @Test public void validExpressionWithNoArgs() { Expression expression = parser.parseExpression("T(System).out.println()"); ExpressionEvaluatingMessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(new GenericMessage("test")); } @Test public void validExpressionWithSomeArgs() { Expression expression = parser.parseExpression("T(System).out.write(payload.bytes, 0, headers.offset)"); ExpressionEvaluatingMessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); HashMap headers = new HashMap(); headers.put("offset", 4); handler.handleMessage(new GenericMessage("testtest", headers)); } @Test(expected = MessagingException.class) public void expressionWithReturnValue() { Message message = new GenericMessage(.1f); try { Expression expression = parser.parseExpression("T(System).out.printf('$%4.2f', payload)"); ExpressionEvaluatingMessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); handler.setBeanFactory(mock(BeanFactory.class)); handler.afterPropertiesSet(); handler.handleMessage(message); } catch (MessagingException e) { assertEquals(e.getFailedMessage(), message); throw e; } } } \ No newline at end of file +/* + * Copyright 2002-2013 the original author or authors. + * + * Licensed 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 org.springframework.integration.message; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +import java.util.HashMap; + +import org.junit.Before; +import org.junit.Test; + +import org.springframework.beans.factory.BeanFactory; +import org.springframework.expression.Expression; +import org.springframework.expression.ExpressionParser; +import org.springframework.expression.spel.standard.SpelExpressionParser; +import org.springframework.integration.Message; +import org.springframework.integration.MessagingException; +import org.springframework.integration.handler.ExpressionEvaluatingMessageHandler; + +/** + * @author Artem Bilan + * @author Gary Russell + * @since 2.1 + */ +public class ExpressionEvaluatingMessageHandlerTests { + + private ExpressionParser parser; + + @Before + public void setup() { + parser = new SpelExpressionParser(); + } + + + @Test + public void validExpression() { + Expression expression = parser.parseExpression("T(System).out.println(payload)"); + ExpressionEvaluatingMessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); + handler.setBeanFactory(mock(BeanFactory.class)); + handler.afterPropertiesSet(); + handler.handleMessage(new GenericMessage("test")); + } + + @Test + public void validExpressionWithNoArgs() { + Expression expression = parser.parseExpression("T(System).out.println()"); + ExpressionEvaluatingMessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); + handler.setBeanFactory(mock(BeanFactory.class)); + handler.afterPropertiesSet(); + handler.handleMessage(new GenericMessage("test")); + } + + @Test + public void validExpressionWithSomeArgs() { + Expression expression = parser.parseExpression("T(System).out.write(payload.bytes, 0, headers.offset)"); + ExpressionEvaluatingMessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); + handler.setBeanFactory(mock(BeanFactory.class)); + handler.afterPropertiesSet(); + HashMap headers = new HashMap(); + headers.put("offset", 4); + handler.handleMessage(new GenericMessage("testtest", headers)); + } + + @Test(expected = MessagingException.class) + public void expressionWithReturnValue() { + Message message = new GenericMessage(.1f); + try { + Expression expression = parser.parseExpression("T(System).out.printf('$%4.2f', payload)"); + ExpressionEvaluatingMessageHandler handler = new ExpressionEvaluatingMessageHandler(expression); + handler.setBeanFactory(mock(BeanFactory.class)); + handler.afterPropertiesSet(); + handler.handleMessage(message); + } + catch (MessagingException e) { + assertEquals(e.getFailedMessage(), message); + throw e; + } + } + +}