Author: davsclaus Date: Tue May 19 05:59:37 2009 New Revision: 776194 URL: http://svn.apache.org/viewvc?rev=776194&view=rev Log: CAMEL-1419: PredicateBuilder binary predicates uses type coerce when testing. This allows you to compare String with Integer even for > operators and the likes. Fixed CS.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceRouteTest.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceTypeTest.java (with props) Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguageSupport.java camel/trunk/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExcludedMethodTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleOperatorTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java?rev=776194&r1=776193&r2=776194&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java Tue May 19 05:59:37 2009 @@ -138,7 +138,15 @@ return new BinaryPredicateSupport(left, right) { protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - return ObjectHelper.equal(leftValue, rightValue); + if (leftValue == null && rightValue == null) { + // they are equal + return true; + } else if (leftValue == null || rightValue == null) { + // only one of them is null so they are not equal + return false; + } + + return ObjectHelper.typeCoerceEquals(exchange.getContext().getTypeConverter(), leftValue, rightValue); } protected String getOperationText() { @@ -151,7 +159,15 @@ return new BinaryPredicateSupport(left, right) { protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - return !ObjectHelper.equal(leftValue, rightValue); + if (leftValue == null && rightValue == null) { + // they are equal + return false; + } else if (leftValue == null || rightValue == null) { + // only one of them is null so they are not equal + return true; + } + + return ObjectHelper.typeCoerceNotEquals(exchange.getContext().getTypeConverter(), leftValue, rightValue); } protected String getOperationText() { @@ -164,7 +180,15 @@ return new BinaryPredicateSupport(left, right) { protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - return compare(leftValue, rightValue) < 0; + if (leftValue == null && rightValue == null) { + // they are equal + return true; + } else if (leftValue == null || rightValue == null) { + // only one of them is null so they are not equal + return false; + } + + return ObjectHelper.typeCoerceCompare(exchange.getContext().getTypeConverter(), leftValue, rightValue) < 0; } protected String getOperationText() { @@ -177,7 +201,15 @@ return new BinaryPredicateSupport(left, right) { protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - return compare(leftValue, rightValue) <= 0; + if (leftValue == null && rightValue == null) { + // they are equal + return true; + } else if (leftValue == null || rightValue == null) { + // only one of them is null so they are not equal + return false; + } + + return ObjectHelper.typeCoerceCompare(exchange.getContext().getTypeConverter(), leftValue, rightValue) <= 0; } protected String getOperationText() { @@ -190,7 +222,15 @@ return new BinaryPredicateSupport(left, right) { protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - return compare(leftValue, rightValue) > 0; + if (leftValue == null && rightValue == null) { + // they are equal + return false; + } else if (leftValue == null || rightValue == null) { + // only one of them is null so they are not equal + return false; + } + + return ObjectHelper.typeCoerceCompare(exchange.getContext().getTypeConverter(), leftValue, rightValue) > 0; } protected String getOperationText() { @@ -203,7 +243,15 @@ return new BinaryPredicateSupport(left, right) { protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - return compare(leftValue, rightValue) >= 0; + if (leftValue == null && rightValue == null) { + // they are equal + return true; + } else if (leftValue == null || rightValue == null) { + // only one of them is null so they are not equal + return false; + } + + return ObjectHelper.typeCoerceCompare(exchange.getContext().getTypeConverter(), leftValue, rightValue) >= 0; } protected String getOperationText() { @@ -216,6 +264,14 @@ return new BinaryPredicateSupport(left, right) { protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { + if (leftValue == null && rightValue == null) { + // they are equal + return true; + } else if (leftValue == null || rightValue == null) { + // only one of them is null so they are not equal + return false; + } + return ObjectHelper.contains(leftValue, rightValue); } @@ -226,11 +282,43 @@ } public static Predicate isNull(final Expression expression) { - return isEqualTo(expression, ExpressionBuilder.constantExpression(null)); + return new BinaryPredicateSupport(expression, ExpressionBuilder.constantExpression(null)) { + + protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { + if (leftValue == null) { + // the left operator is null so its true + return true; + } + + return ObjectHelper.typeCoerceEquals(exchange.getContext().getTypeConverter(), leftValue, rightValue); + } + + protected String getOperationText() { + // leave the operation text as "is not" as Camel will insert right and left expression around it + // so it will be displayed as: XXX is null + return "is"; + } + }; } public static Predicate isNotNull(final Expression expression) { - return isNotEqualTo(expression, ExpressionBuilder.constantExpression(null)); + return new BinaryPredicateSupport(expression, ExpressionBuilder.constantExpression(null)) { + + protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { + if (leftValue != null) { + // the left operator is not null so its true + return true; + } + + return ObjectHelper.typeCoerceNotEquals(exchange.getContext().getTypeConverter(), leftValue, rightValue); + } + + protected String getOperationText() { + // leave the operation text as "is not" as Camel will insert right and left expression around it + // so it will be displayed as: XXX is not null + return "is not"; + } + }; } public static Predicate isInstanceOf(final Expression expression, final Class<?> type) { Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java?rev=776194&r1=776193&r2=776194&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java Tue May 19 05:59:37 2009 @@ -349,7 +349,7 @@ @SuppressWarnings("unchecked") private MethodInfo chooseMethodWithMatchingBody(Exchange exchange, Collection<MethodInfo> operationList) - throws AmbiguousMethodCallException { + throws AmbiguousMethodCallException { // lets see if we can find a method who's body param type matches the message body Message in = exchange.getIn(); Object body = in.getBody(); @@ -509,7 +509,7 @@ } private MethodInfo chooseMethodWithCustomAnnotations(Exchange exchange, Collection<MethodInfo> possibles) - throws AmbiguousMethodCallException { + throws AmbiguousMethodCallException { // if we have only one method with custom annotations lets choose that MethodInfo chosen = null; for (MethodInfo possible : possibles) { Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java?rev=776194&r1=776193&r2=776194&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/CamelPostProcessorHelper.java Tue May 19 05:59:37 2009 @@ -120,9 +120,7 @@ */ public Object getInjectionValue(Class<?> type, String endpointUri, String endpointRef, String injectionPointName) { if (type.isAssignableFrom(ProducerTemplate.class)) { - // endpoint is optional for this injection point - Endpoint endpoint = getEndpointInjection(endpointUri, endpointRef, injectionPointName, false); - return new DefaultProducerTemplate(getCamelContext(), endpoint); + return createInjectionProducerTemplate(endpointUri, endpointRef, injectionPointName); } else { Endpoint endpoint = getEndpointInjection(endpointUri, endpointRef, injectionPointName, true); if (endpoint != null) { @@ -148,8 +146,13 @@ } } - protected RuntimeException createProxyInstantiationRuntimeException(Class<?> type, Endpoint endpoint, Exception e) { - return new ProxyInstantiationException(type, endpoint, e); + /** + * Factory method to create a {...@link org.apache.camel.ProducerTemplate} to be injected into a POJO + */ + protected ProducerTemplate createInjectionProducerTemplate(String endpointUri, String endpointRef, String injectionPointName) { + // endpoint is optional for this injection point + Endpoint endpoint = getEndpointInjection(endpointUri, endpointRef, injectionPointName, false); + return new DefaultProducerTemplate(getCamelContext(), endpoint); } /** @@ -177,4 +180,8 @@ throw ObjectHelper.wrapRuntimeCamelException(e); } } + + protected RuntimeException createProxyInstantiationRuntimeException(Class<?> type, Endpoint endpoint, Exception e) { + return new ProxyInstantiationException(type, endpoint, e); + } } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguageSupport.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguageSupport.java?rev=776194&r1=776193&r2=776194&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguageSupport.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguageSupport.java Tue May 19 05:59:37 2009 @@ -78,11 +78,14 @@ final Expression right; final Expression rightConverted; + final Boolean isNull; // special null handling - if ("null".equals(text)) { + if ("null".equals(text) || "'null'".equals(text)) { + isNull = Boolean.TRUE; right = createConstantExpression(null); rightConverted = right; } else { + isNull = Boolean.FALSE; // text can either be a constant enclosed by ' ' or another expression using ${ } placeholders String constant = ObjectHelper.between(text, "'", "'"); if (constant == null) { @@ -100,7 +103,14 @@ return new Expression() { public <T> T evaluate(Exchange exchange, Class<T> type) { Predicate predicate = null; - if (operator == EQ) { + + if (operator == EQ && isNull) { + // special for EQ null + predicate = PredicateBuilder.isNull(left); + } else if (operator == NOT && isNull) { + // special for not EQ null + predicate = PredicateBuilder.isNotNull(left); + } else if (operator == EQ) { predicate = PredicateBuilder.isEqualTo(left, rightConverted); } else if (operator == GT) { predicate = PredicateBuilder.isGreaterThan(left, rightConverted); Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java?rev=776194&r1=776193&r2=776194&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java Tue May 19 05:59:37 2009 @@ -58,7 +58,7 @@ if (isStopped()) { LOG.warn("Ignoring exchange sent after processor is stopped: " + exchange); } else { - throw new IllegalStateException("No producer, this processor has not been started!"); + throw new IllegalStateException("No producer, this processor has not been started: " + this); } } else { exchange = configureExchange(exchange); Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=776194&r1=776193&r2=776194&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Tue May 19 05:59:37 2009 @@ -38,6 +38,7 @@ import org.apache.camel.CamelExecutionException; import org.apache.camel.Exchange; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.TypeConverter; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -56,6 +57,71 @@ } /** + * A helper method for comparing objects for equality in which it uses type coerce to coerce + * types between the left and right values. This allows you to equal test eg String and Integer as + * Camel will be able to coerce the types + */ + public static boolean typeCoerceEquals(TypeConverter converter, Object leftValue, Object rightValue) { + // try without type coerce + boolean answer = equal(leftValue, rightValue); + if (answer) { + return true; + } + + if (leftValue == null || rightValue == null) { + // no reason to continue as the first equal did not match and now one of the values is null + // so it wont help to type coerece to a null type + return false; + } + + // convert left to right + Object value = converter.convertTo(rightValue.getClass(), leftValue); + answer = equal(value, rightValue); + if (answer) { + return true; + } + + // convert right to left + value = converter.convertTo(leftValue.getClass(), rightValue); + answer = equal(leftValue, value); + return answer; + } + + /** + * A helper method for comparing objects for equality in which it uses type coerce to coerce + * types between the left and right values. This allows you to equal test eg String and Integer as + * Camel will be able to coerce the types + */ + public static boolean typeCoerceNotEquals(TypeConverter converter, Object leftValue, Object rightValue) { + return !typeCoerceEquals(converter, leftValue, rightValue); + } + + /** + * A helper method for comparing objects ordering in which it uses type coerce to coerce + * types between the left and right values. This allows you to equal test eg String and Integer as + * Camel will be able to coerce the types + */ + @SuppressWarnings("unchecked") + public static int typeCoerceCompare(TypeConverter converter, Object leftValue, Object rightValue) { + if (rightValue instanceof Comparable) { + Object value = converter.convertTo(rightValue.getClass(), leftValue); + if (value != null) { + return ((Comparable) rightValue).compareTo(value) * -1; + } + } + + if (leftValue instanceof Comparable) { + Object value = converter.convertTo(leftValue.getClass(), rightValue); + if (value != null) { + return ((Comparable) leftValue).compareTo(value); + } + } + + // use regular compare + return compare(leftValue, rightValue); + } + + /** * A helper method for comparing objects for equality while handling nulls */ public static boolean equal(Object a, Object b) { @@ -610,9 +676,9 @@ * @return <tt>true</tt> if it override, <tt>false</tt> otherwise */ public static boolean isOverridingMethod(Method source, Method target) { - if (source.getName().equals(target.getName()) && - source.getReturnType().equals(target.getReturnType()) && - source.getParameterTypes().length == target.getParameterTypes().length) { + if (source.getName().equals(target.getName()) + && source.getReturnType().equals(target.getReturnType()) + && source.getParameterTypes().length == target.getParameterTypes().length) { // test if parameter types is the same as well for (int i = 0; i < source.getParameterTypes().length; i++) { Added: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceRouteTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceRouteTest.java?rev=776194&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceRouteTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceRouteTest.java Tue May 19 05:59:37 2009 @@ -0,0 +1,183 @@ +/** + * 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 org.apache.camel.builder; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.WaitForTaskToComplete; + +/** + * @version $Revision$ + */ +public class PredicateBinaryCoerceRouteTest extends ContextTestSupport { + + public void testNoHeder() throws Exception { + getMockEndpoint("mock:123").expectedMessageCount(0); + getMockEndpoint("mock:456").expectedMessageCount(0); + getMockEndpoint("mock:other").expectedMessageCount(1); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + public void testHederAsNumber123() throws Exception { + getMockEndpoint("mock:123").expectedMessageCount(1); + getMockEndpoint("mock:456").expectedMessageCount(0); + getMockEndpoint("mock:other").expectedMessageCount(0); + + template.sendBodyAndHeader("direct:start", "Hello World", "foo", 123); + + assertMockEndpointsSatisfied(); + } + + public void testHederAsNumber456() throws Exception { + getMockEndpoint("mock:123").expectedMessageCount(0); + getMockEndpoint("mock:456").expectedMessageCount(1); + getMockEndpoint("mock:other").expectedMessageCount(0); + + template.sendBodyAndHeader("direct:start", "Hello World", "foo", 456); + + assertMockEndpointsSatisfied(); + } + + public void testHederAsNumber999() throws Exception { + getMockEndpoint("mock:123").expectedMessageCount(0); + getMockEndpoint("mock:456").expectedMessageCount(0); + getMockEndpoint("mock:other").expectedMessageCount(1); + + template.sendBodyAndHeader("direct:start", "Hello World", "foo", 999); + + assertMockEndpointsSatisfied(); + } + + public void testHederAsString123() throws Exception { + getMockEndpoint("mock:123").expectedMessageCount(1); + getMockEndpoint("mock:456").expectedMessageCount(0); + getMockEndpoint("mock:other").expectedMessageCount(0); + + template.sendBodyAndHeader("direct:start", "Hello World", "foo", "123"); + + assertMockEndpointsSatisfied(); + } + + public void testHederAsString456() throws Exception { + getMockEndpoint("mock:123").expectedMessageCount(0); + getMockEndpoint("mock:456").expectedMessageCount(1); + getMockEndpoint("mock:other").expectedMessageCount(0); + + template.sendBodyAndHeader("direct:start", "Hello World", "foo", "456"); + + assertMockEndpointsSatisfied(); + } + + public void testHederAsString999() throws Exception { + getMockEndpoint("mock:123").expectedMessageCount(0); + getMockEndpoint("mock:456").expectedMessageCount(0); + getMockEndpoint("mock:other").expectedMessageCount(1); + + template.sendBodyAndHeader("direct:start", "Hello World", "foo", "999"); + + assertMockEndpointsSatisfied(); + } + + public void testHederAsEnum() throws Exception { + getMockEndpoint("mock:enum").expectedMessageCount(1); + + template.sendBodyAndHeader("direct:start", "Hello World", "foo", WaitForTaskToComplete.Always); + + assertMockEndpointsSatisfied(); + } + + public void testHederAsEnumString() throws Exception { + getMockEndpoint("mock:enum").expectedMessageCount(1); + + template.sendBodyAndHeader("direct:start", "Hello World", "foo", "Always"); + + assertMockEndpointsSatisfied(); + } + + public void testOtherMax() throws Exception { + getMockEndpoint("mock:max").expectedMessageCount(1); + getMockEndpoint("mock:min").expectedMessageCount(0); + + template.sendBodyAndHeader("direct:foo", "Hello World", "foo", "250"); + + assertMockEndpointsSatisfied(); + } + + public void testOtherMin() throws Exception { + getMockEndpoint("mock:max").expectedMessageCount(0); + getMockEndpoint("mock:min").expectedMessageCount(1); + + template.sendBodyAndHeader("direct:foo", "Hello World", "foo", "200"); + + assertMockEndpointsSatisfied(); + } + + public void testOtherAlways() throws Exception { + getMockEndpoint("mock:max").expectedMessageCount(0); + getMockEndpoint("mock:min").expectedMessageCount(0); + getMockEndpoint("mock:enum").expectedMessageCount(1); + + template.sendBodyAndHeader("direct:foo", "Hello World", "enum", "Always"); + + assertMockEndpointsSatisfied(); + } + + public void testOtherNewer() throws Exception { + getMockEndpoint("mock:max").expectedMessageCount(0); + getMockEndpoint("mock:min").expectedMessageCount(0); + getMockEndpoint("mock:enum").expectedMessageCount(0); + getMockEndpoint("mock:other").expectedMessageCount(1); + + template.sendBodyAndHeader("direct:foo", "Hello World", "enum", "Newer"); + + assertMockEndpointsSatisfied(); + } + + public void testOtherIfReplyExpected() throws Exception { + getMockEndpoint("mock:max").expectedMessageCount(0); + getMockEndpoint("mock:min").expectedMessageCount(0); + getMockEndpoint("mock:enum").expectedMessageCount(1); + + template.sendBodyAndHeader("direct:foo", "Hello World", "enum", WaitForTaskToComplete.IfReplyExpected); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .choice() + .when(header("foo").isEqualTo("123")).to("mock:123") + .when(header("foo").isEqualTo(456)).to("mock:456") + .when(header("foo").isEqualTo(WaitForTaskToComplete.Always)).to("mock:enum") + .otherwise().to("mock:other"); + + from("direct:foo") + .choice() + .when(header("enum").isGreaterThanOrEqualTo(WaitForTaskToComplete.IfReplyExpected)).to("mock:enum") + .when(header("foo").isGreaterThan("200")).to("mock:max") + .when(header("foo").isLessThanOrEqualTo(200)).to("mock:min") + .otherwise().to("mock:other"); + } + }; + } +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceRouteTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceRouteTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceTypeTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceTypeTest.java?rev=776194&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceTypeTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceTypeTest.java Tue May 19 05:59:37 2009 @@ -0,0 +1,209 @@ +/** + * 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 org.apache.camel.builder; + +import org.apache.camel.Exchange; +import org.apache.camel.Expression; +import org.apache.camel.Message; +import org.apache.camel.Predicate; +import org.apache.camel.TestSupport; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.DefaultExchange; + +/** + * @version $Revision$ + */ +public class PredicateBinaryCoerceTypeTest extends TestSupport { + protected Exchange exchange = new DefaultExchange(new DefaultCamelContext()); + + public void testIsNull() throws Exception { + Expression a = ExpressionBuilder.constantExpression("123"); + assertDoesNotMatch(PredicateBuilder.isNull(a)); + + a = ExpressionBuilder.constantExpression(null); + assertMatches(PredicateBuilder.isNull(a)); + } + + public void testIsNotNull() throws Exception { + Expression a = ExpressionBuilder.constantExpression("123"); + assertMatches(PredicateBuilder.isNotNull(a)); + + a = ExpressionBuilder.constantExpression(null); + assertDoesNotMatch(PredicateBuilder.isNotNull(a)); + } + + public void testEqual() throws Exception { + Expression a = ExpressionBuilder.constantExpression("123"); + Expression b = ExpressionBuilder.constantExpression(Integer.valueOf("123")); + assertMatches(PredicateBuilder.isEqualTo(a, b)); + + // reverse the type and try again + a = ExpressionBuilder.constantExpression(Integer.valueOf("123")); + b = ExpressionBuilder.constantExpression("123"); + assertMatches(PredicateBuilder.isEqualTo(a, b)); + } + + public void testEqualWithNull() throws Exception { + Expression a = ExpressionBuilder.constantExpression("123"); + Expression b = ExpressionBuilder.constantExpression(null); + assertDoesNotMatch(PredicateBuilder.isEqualTo(a, b)); + + // reverse the type and try again + a = ExpressionBuilder.constantExpression(null); + b = ExpressionBuilder.constantExpression("123"); + assertDoesNotMatch(PredicateBuilder.isEqualTo(a, b)); + + // try two null values + a = ExpressionBuilder.constantExpression(null); + b = ExpressionBuilder.constantExpression(null); + assertMatches(PredicateBuilder.isEqualTo(a, b)); + } + + public void testNotEqual() throws Exception { + Expression a = ExpressionBuilder.constantExpression("123"); + Expression b = ExpressionBuilder.constantExpression(Integer.valueOf("123")); + + assertDoesNotMatch(PredicateBuilder.isNotEqualTo(a, b)); + + a = ExpressionBuilder.constantExpression("333"); + assertMatches(PredicateBuilder.isNotEqualTo(a, b)); + } + + public void testNotEqualWithNull() throws Exception { + Expression a = ExpressionBuilder.constantExpression("123"); + Expression b = ExpressionBuilder.constantExpression(null); + assertMatches(PredicateBuilder.isNotEqualTo(a, b)); + + // reverse the type and try again + a = ExpressionBuilder.constantExpression(null); + b = ExpressionBuilder.constantExpression("123"); + assertMatches(PredicateBuilder.isNotEqualTo(a, b)); + + // try two null values + a = ExpressionBuilder.constantExpression(null); + b = ExpressionBuilder.constantExpression(null); + assertDoesNotMatch(PredicateBuilder.isNotEqualTo(a, b)); + } + + public void testGreatherThan() throws Exception { + Expression a = ExpressionBuilder.constantExpression("200"); + Expression b = ExpressionBuilder.constantExpression(Integer.valueOf("100")); + + assertMatches(PredicateBuilder.isGreaterThan(a, b)); + assertDoesNotMatch(PredicateBuilder.isGreaterThan(b, a)); + + // reverse the types and try again + a = ExpressionBuilder.constantExpression(Integer.valueOf("100")); + b = ExpressionBuilder.constantExpression("200"); + + assertDoesNotMatch(PredicateBuilder.isGreaterThan(a, b)); + assertMatches(PredicateBuilder.isGreaterThan(b, a)); + } + + public void testGreatherThanOrEqual() throws Exception { + // greather than + Expression a = ExpressionBuilder.constantExpression("200"); + Expression b = ExpressionBuilder.constantExpression(Integer.valueOf("100")); + + assertMatches(PredicateBuilder.isGreaterThanOrEqualTo(a, b)); + assertDoesNotMatch(PredicateBuilder.isGreaterThanOrEqualTo(b, a)); + + // reverse the types and try again + a = ExpressionBuilder.constantExpression(Integer.valueOf("100")); + b = ExpressionBuilder.constantExpression("200"); + + assertDoesNotMatch(PredicateBuilder.isGreaterThanOrEqualTo(a, b)); + assertMatches(PredicateBuilder.isGreaterThanOrEqualTo(b, a)); + + // equal + a = ExpressionBuilder.constantExpression("100"); + b = ExpressionBuilder.constantExpression(Integer.valueOf("100")); + + assertMatches(PredicateBuilder.isGreaterThanOrEqualTo(a, b)); + assertMatches(PredicateBuilder.isGreaterThanOrEqualTo(b, a)); + + // reverse the types and try again + a = ExpressionBuilder.constantExpression(Integer.valueOf("100")); + b = ExpressionBuilder.constantExpression("100"); + + assertMatches(PredicateBuilder.isGreaterThanOrEqualTo(a, b)); + assertMatches(PredicateBuilder.isGreaterThanOrEqualTo(b, a)); + } + + public void testLessThan() throws Exception { + Expression a = ExpressionBuilder.constantExpression("100"); + Expression b = ExpressionBuilder.constantExpression(Integer.valueOf("200")); + + assertMatches(PredicateBuilder.isLessThan(a, b)); + assertDoesNotMatch(PredicateBuilder.isLessThan(b, a)); + + // reverse the types and try again + a = ExpressionBuilder.constantExpression(Integer.valueOf("100")); + b = ExpressionBuilder.constantExpression("200"); + + assertMatches(PredicateBuilder.isLessThan(a, b)); + assertDoesNotMatch(PredicateBuilder.isLessThan(b, a)); + } + + public void testLessThanOrEqual() throws Exception { + // less than + Expression a = ExpressionBuilder.constantExpression("100"); + Expression b = ExpressionBuilder.constantExpression(Integer.valueOf("200")); + + assertMatches(PredicateBuilder.isLessThanOrEqualTo(a, b)); + assertDoesNotMatch(PredicateBuilder.isLessThanOrEqualTo(b, a)); + + // reverse the types and try again + a = ExpressionBuilder.constantExpression(Integer.valueOf("200")); + b = ExpressionBuilder.constantExpression("100"); + + assertDoesNotMatch(PredicateBuilder.isLessThanOrEqualTo(a, b)); + assertMatches(PredicateBuilder.isLessThanOrEqualTo(b, a)); + + // equal + a = ExpressionBuilder.constantExpression("100"); + b = ExpressionBuilder.constantExpression(Integer.valueOf("100")); + + assertMatches(PredicateBuilder.isLessThanOrEqualTo(a, b)); + assertMatches(PredicateBuilder.isLessThanOrEqualTo(b, a)); + + // reverse the types and try again + a = ExpressionBuilder.constantExpression(Integer.valueOf("100")); + b = ExpressionBuilder.constantExpression("100"); + + assertMatches(PredicateBuilder.isGreaterThanOrEqualTo(a, b)); + assertMatches(PredicateBuilder.isGreaterThanOrEqualTo(b, a)); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + Message in = exchange.getIn(); + in.setBody("Hello there!"); + in.setHeader("name", "James"); + in.setHeader("location", "Islington,London,UK"); + in.setHeader("size", 10); + } + + protected void assertMatches(Predicate predicate) { + assertPredicateMatches(predicate, exchange); + } + + protected void assertDoesNotMatch(Predicate predicate) { + assertPredicateDoesNotMatch(predicate, exchange); + } +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceTypeTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBinaryCoerceTypeTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExcludedMethodTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExcludedMethodTest.java?rev=776194&r1=776193&r2=776194&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExcludedMethodTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanExcludedMethodTest.java Tue May 19 05:59:37 2009 @@ -53,6 +53,11 @@ } @Override + public int hashCode() { + return super.hashCode(); + } + + @Override public String toString() { return "dummy"; } @@ -67,6 +72,11 @@ } @Override + public int hashCode() { + return super.hashCode(); + } + + @Override public String toString() { return "dummy"; } Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleOperatorTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleOperatorTest.java?rev=776194&r1=776193&r2=776194&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleOperatorTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleOperatorTest.java Tue May 19 05:59:37 2009 @@ -129,11 +129,17 @@ public void testIsNull() throws Exception { assertExpression("${in.header.foo} == null", false); assertExpression("${in.header.none} == null", true); + + assertExpression("${in.header.foo} == 'null'", false); + assertExpression("${in.header.none} == 'null'", true); } public void testIsNotNull() throws Exception { assertExpression("${in.header.foo} != null", true); assertExpression("${in.header.none} != null", false); + + assertExpression("${in.header.foo} != 'null'", true); + assertExpression("${in.header.none} != 'null'", false); } public void testRightOperatorIsSimpleLanauge() throws Exception { Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceTest.java?rev=776194&r1=776193&r2=776194&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RandomLoadBalanceTest.java Tue May 19 05:59:37 2009 @@ -44,10 +44,10 @@ } public void testRandom() throws Exception { - // it should be safe to assume that they should at least each get > 10 messages - x.expectedMinimumMessageCount(10); - y.expectedMinimumMessageCount(10); - z.expectedMinimumMessageCount(10); + // it should be safe to assume that they should at least each get > 5 messages + x.expectedMinimumMessageCount(5); + y.expectedMinimumMessageCount(5); + z.expectedMinimumMessageCount(5); for (int i = 0; i < 100; i++) { template.sendBody("direct:start", "Hello World"); Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteTest.java?rev=776194&r1=776193&r2=776194&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteTest.java Tue May 19 05:59:37 2009 @@ -112,13 +112,10 @@ // as it turns into a async route later we get a Future as response assertIsInstanceOf(Exchange.class, out); Future future = out.getOut().getBody(Future.class); + assertFalse("Should not be done", future.isDone()); assertMockEndpointsSatisfied(); - // for slower computers we invoke the get with a timeout - future.get(1, TimeUnit.SECONDS); - assertTrue("Should be done", future.isDone()); - assertEquals("AB", route); }