Author: davsclaus
Date: Sun Apr 24 17:00:45 2011
New Revision: 1096346
URL: http://svn.apache.org/viewvc?rev=1096346&view=rev
Log:
CAMEL-3531: Fixed scala content based router not evaluating as predicate.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathContentBasedRouterTest.java
(with props)
camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/XPathContentBasedRouterTest.scala
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectHelperTest.java
camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SChoiceDefinition.scala
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=1096346&r1=1096345&r2=1096346&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
Sun Apr 24 17:00:45 2011
@@ -38,6 +38,7 @@ import java.util.Map;
import java.util.Properties;
import java.util.Scanner;
+import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -1118,6 +1119,14 @@ public final class ObjectHelper {
} else if ("false".equalsIgnoreCase((String)value)) {
return false;
}
+ } else if (value instanceof NodeList) {
+ // is it an empty dom
+ NodeList list = (NodeList) value;
+ return list.getLength() > 0;
+ } else if (value instanceof Collection) {
+ // is it an empty collection
+ Collection col = (Collection) value;
+ return col.size() > 0;
}
return value != null;
}
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathContentBasedRouterTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathContentBasedRouterTest.java?rev=1096346&view=auto
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathContentBasedRouterTest.java
(added)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathContentBasedRouterTest.java
Sun Apr 24 17:00:45 2011
@@ -0,0 +1,53 @@
+/**
+ * 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.xml;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class XPathContentBasedRouterTest extends ContextTestSupport {
+
+ public void testXPathContentBasedRouter() throws Exception {
+ getMockEndpoint("mock:english").expectedBodiesReceived("<hello/>");
+ getMockEndpoint("mock:dutch").expectedBodiesReceived("<hallo/>");
+ getMockEndpoint("mock:german").expectedBodiesReceived("<hallo/>");
+ getMockEndpoint("mock:french").expectedBodiesReceived("<hellos/>");
+
+ template.sendBody("direct:a", "<hello/>");
+ template.sendBody("direct:a", "<hallo/>");
+ template.sendBody("direct:a", "<hellos/>");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:a")
+ .choice()
+ .when(xpath("//hello")).to("mock:english")
+ .when(xpath("//hallo")).to("mock:dutch", "mock:german")
+ .otherwise().to("mock:french");
+ }
+ };
+ }
+}
Propchange:
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathContentBasedRouterTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathContentBasedRouterTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectHelperTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectHelperTest.java?rev=1096346&r1=1096345&r2=1096346&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectHelperTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/converter/ObjectHelperTest.java
Sun Apr 24 17:00:45 2011
@@ -16,7 +16,9 @@
*/
package org.apache.camel.converter;
+import java.util.ArrayList;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import java.util.Properties;
@@ -24,7 +26,6 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import junit.framework.TestCase;
-
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.util.ObjectHelper;
@@ -118,7 +119,7 @@ public class ObjectHelperTest extends Te
it.next();
assertFalse(it.hasNext());
}
-
+
public void testGetCamelContextPropertiesWithPrefix() {
CamelContext context = new DefaultCamelContext();
Map<String, String> properties = context.getProperties();
@@ -132,4 +133,22 @@ public class ObjectHelperTest extends Te
assertEquals("It should contain the test2", "test2",
result.get("test2"));
}
+ public void testEvaluateAsPredicate() throws Exception {
+ assertEquals(false, ObjectHelper.evaluateValuePredicate(null));
+ assertEquals(true, ObjectHelper.evaluateValuePredicate(123));
+
+ assertEquals(true, ObjectHelper.evaluateValuePredicate("true"));
+ assertEquals(true, ObjectHelper.evaluateValuePredicate("TRUE"));
+ assertEquals(false, ObjectHelper.evaluateValuePredicate("false"));
+ assertEquals(false, ObjectHelper.evaluateValuePredicate("FALSE"));
+ assertEquals(true, ObjectHelper.evaluateValuePredicate("foobar"));
+ assertEquals(true, ObjectHelper.evaluateValuePredicate(""));
+ assertEquals(true, ObjectHelper.evaluateValuePredicate(" "));
+
+ List<String> list = new ArrayList<String>();
+ assertEquals(false, ObjectHelper.evaluateValuePredicate(list));
+ list.add("foo");
+ assertEquals(true, ObjectHelper.evaluateValuePredicate(list));
+ }
+
}
Modified:
camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SChoiceDefinition.scala
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SChoiceDefinition.scala?rev=1096346&r1=1096345&r2=1096346&view=diff
==============================================================================
---
camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SChoiceDefinition.scala
(original)
+++
camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SChoiceDefinition.scala
Sun Apr 24 17:00:45 2011
@@ -17,6 +17,7 @@
package org.apache.camel
package scala.dsl
+import org.apache.camel.builder.PredicateBuilder
import org.apache.camel.model.ChoiceDefinition
import org.apache.camel.scala.dsl.builder.RouteBuilder
@@ -28,7 +29,9 @@ case class SChoiceDefinition(override va
}
override def when(filter: Exchange => Any) = {
- target.when(filter)
+ // should be evaluated as a predicate
+ val predicate = PredicateBuilder.toPredicate(filter)
+ target.when(predicate)
this
}
Added:
camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/XPathContentBasedRouterTest.scala
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/XPathContentBasedRouterTest.scala?rev=1096346&view=auto
==============================================================================
---
camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/XPathContentBasedRouterTest.scala
(added)
+++
camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/XPathContentBasedRouterTest.scala
Sun Apr 24 17:00:45 2011
@@ -0,0 +1,51 @@
+/**
+ * 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.scala.dsl
+
+import builder.RouteBuilder
+
+/**
+ * Content Based Router test with XPath expressions
+ */
+class XPathContentBasedRouterTest extends ScalaTestSupport {
+
+ def testXPathContentBasedRouter = {
+ "mock:english" expect {_.expectedBodiesReceived("<hello/>")}
+ "mock:dutch" expect {_.expectedBodiesReceived("<hallo/>")}
+ "mock:german" expect {_.expectedBodiesReceived("<hallo/>")}
+ "mock:french" expect {_.expectedBodiesReceived("<hellos/>")}
+
+ test {
+ "direct:a" ! ("<hello/>", "<hallo/>", "<hellos/>")
+ }
+
+ assertMockEndpointsSatisfied()
+ }
+
+ val builder = new RouteBuilder {
+ //START SNIPPET: cbr
+ "direct:a" ==> {
+ choice {
+ when (xpath("//hello")) { to ("mock:english") }
+ when (xpath("//hallo")) { to("mock:dutch", "mock:german") }
+ otherwise to ("mock:french")
+ }
+ }
+ //END SNIPPET: cbr
+ }
+
+}
\ No newline at end of file