Author: davsclaus
Date: Sun Feb 15 13:28:54 2009
New Revision: 744668
URL: http://svn.apache.org/viewvc?rev=744668&view=rev
Log:
CAMEL-1343: Added in operator for predicate builders. Added wiki example for
compound predicates.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ChoiceCompoundPredicateTest.java
- copied, changed from r744631,
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ChoiceTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.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=744668&r1=744667&r2=744668&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
Sun Feb 15 13:28:54 2009
@@ -16,7 +16,7 @@
*/
package org.apache.camel.builder;
-
+import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -24,7 +24,6 @@
import org.apache.camel.Expression;
import org.apache.camel.Predicate;
import org.apache.camel.util.ObjectHelper;
-
import static org.apache.camel.util.ObjectHelper.compare;
import static org.apache.camel.util.ObjectHelper.notNull;
@@ -112,6 +111,30 @@
};
}
+ /**
+ * A helper method to return true if any of the predicates
+ * matches.
+ */
+ public static Predicate in(final Predicate... predicates) {
+ notNull(predicates, "predicates");
+
+ return new PredicateSupport() {
+ public boolean matches(Exchange exchange) {
+ for (Predicate in : predicates) {
+ if (in.matches(exchange)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return "in (" + Arrays.asList(predicates) + ")";
+ }
+ };
+ }
+
public static Predicate isEqualTo(final Expression left, final Expression
right) {
return new BinaryPredicateSupport(left, right) {
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java?rev=744668&r1=744667&r2=744668&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
Sun Feb 15 13:28:54 2009
@@ -16,6 +16,9 @@
*/
package org.apache.camel.builder;
+import java.util.ArrayList;
+import java.util.List;
+
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Predicate;
@@ -99,6 +102,20 @@
return onNewPredicate(PredicateBuilder.not(predicate));
}
+ public Predicate in(Object... values) {
+ List<Predicate> predicates = new ArrayList<Predicate>();
+ for (Object value : values) {
+ Expression right = asExpression(value);
+ Predicate predicate =
onNewPredicate(PredicateBuilder.isEqualTo(expression, right));
+ predicates.add(predicate);
+ }
+ return in(predicates.toArray(new Predicate[predicates.size()]));
+ }
+
+ public Predicate in(Predicate... predicates) {
+ return onNewPredicate(PredicateBuilder.in(predicates));
+ }
+
/**
* Create a predicate that the left hand expression contains the value of
* the right hand expression
Modified:
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java?rev=744668&r1=744667&r2=744668&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java
Sun Feb 15 13:28:54 2009
@@ -22,8 +22,8 @@
import org.apache.camel.TestSupport;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultExchange;
-
import static org.apache.camel.builder.Builder.constant;
+import static org.apache.camel.builder.PredicateBuilder.in;
import static org.apache.camel.builder.PredicateBuilder.not;
/**
@@ -48,6 +48,40 @@
assertDoesNotMatch(not(header("size").isLessThan(constant(100))));
}
+ public void testCompoundOrPredicates() throws Exception {
+ Predicate p1 = header("name").isEqualTo(constant("Hiram"));
+ Predicate p2 = header("size").isGreaterThanOrEqualTo(constant(10));
+ Predicate or = PredicateBuilder.or(p1, p2);
+
+ assertMatches(or);
+ }
+
+ public void testCompoundAndPredicates() throws Exception {
+ Predicate p1 = header("name").isEqualTo(constant("James"));
+ Predicate p2 = header("size").isGreaterThanOrEqualTo(constant(10));
+ Predicate and = PredicateBuilder.and(p1, p2);
+
+ assertMatches(and);
+ }
+
+ public void testCompoundAndOrPredicates() throws Exception {
+ Predicate p1 = header("name").isEqualTo(constant("Hiram"));
+ Predicate p2 = header("size").isGreaterThan(constant(100));
+ Predicate p3 = header("location").contains("London");
+ Predicate and = PredicateBuilder.and(p1, p2);
+ Predicate andor = PredicateBuilder.or(and, p3);
+
+ assertMatches(andor);
+ }
+
+ public void testPredicateIn() throws Exception {
+ assertMatches(in(header("name").isEqualTo("Hiram"),
header("name").isEqualTo("James")));
+ }
+
+ public void testValueIn() throws Exception {
+ assertMatches(header("name").in("Hiram", "Jonathan", "James",
"Claus"));
+ }
+
@Override
protected void setUp() throws Exception {
super.setUp();
Copied:
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ChoiceCompoundPredicateTest.java
(from r744631,
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ChoiceTest.java)
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ChoiceCompoundPredicateTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ChoiceCompoundPredicateTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ChoiceTest.java&r1=744631&r2=744668&rev=744668&view=diff
==============================================================================
---
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ChoiceTest.java
(original)
+++
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ChoiceCompoundPredicateTest.java
Sun Feb 15 13:28:54 2009
@@ -16,78 +16,107 @@
*/
package org.apache.camel.processor;
+import java.util.HashMap;
+import java.util.Map;
+
import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Predicate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
-import static org.apache.camel.component.mock.MockEndpoint.expectsMessageCount;
+import static org.apache.camel.builder.PredicateBuilder.and;
+import static org.apache.camel.builder.PredicateBuilder.or;
/**
* @version $Revision$
*/
-public class ChoiceTest extends ContextTestSupport {
- protected MockEndpoint x;
- protected MockEndpoint y;
- protected MockEndpoint z;
- protected MockEndpoint end;
-
- public void testSendToFirstWhen() throws Exception {
- String body = "<one/>";
- x.expectedBodiesReceived(body);
- end.expectedBodiesReceived(body);
- // The SpringChoiceTest.java can't setup the header by Spring
configure file
- // x.expectedHeaderReceived("name", "a");
- expectsMessageCount(0, y, z);
+public class ChoiceCompoundPredicateTest extends ContextTestSupport {
+
+ public void testGuest() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:guest");
+ mock.expectedMessageCount(1);
- sendMessage("bar", body);
+ template.sendBody("direct:start", "Hello World");
assertMockEndpointsSatisfied();
}
- public void testSendToSecondWhen() throws Exception {
- String body = "<two/>";
- y.expectedBodiesReceived(body);
- end.expectedBodiesReceived(body);
- expectsMessageCount(0, x, z);
+ public void testUser() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:user");
+ mock.expectedMessageCount(1);
- sendMessage("cheese", body);
+ template.sendBodyAndHeader("direct:start", "Hello World", "username",
"goofy");
assertMockEndpointsSatisfied();
}
- public void testSendToOtherwiseClause() throws Exception {
- String body = "<three/>";
- z.expectedBodiesReceived(body);
- end.expectedBodiesReceived(body);
- expectsMessageCount(0, x, y);
-
- sendMessage("somethingUndefined", body);
+ public void testAdmin() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:admin");
+ mock.expectedMessageCount(1);
+
+ Map<String, Object> headers = new HashMap<String, Object>();
+ headers.put("username", "donald");
+ headers.put("admin", "true");
+ template.sendBodyAndHeaders("direct:start", "Hello World", headers);
assertMockEndpointsSatisfied();
}
- protected void sendMessage(final Object headerValue, final Object body)
throws Exception {
- template.sendBodyAndHeader("direct:start", body, "foo", headerValue);
+ public void testGod() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:god");
+ mock.expectedMessageCount(1);
+
+ Map<String, Object> headers = new HashMap<String, Object>();
+ headers.put("username", "pluto");
+ headers.put("admin", "true");
+ headers.put("type", "god");
+ template.sendBodyAndHeaders("direct:start", "Hello World", headers);
+
+ assertMockEndpointsSatisfied();
}
- @Override
- protected void setUp() throws Exception {
- super.setUp();
-
- x = getMockEndpoint("mock:x");
- y = getMockEndpoint("mock:y");
- z = getMockEndpoint("mock:z");
- end = getMockEndpoint("mock:end");
+ public void testRiderGod() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:god");
+ mock.expectedMessageCount(1);
+
+ Map<String, Object> headers = new HashMap<String, Object>();
+ headers.put("username", "Camel");
+ headers.put("admin", "true");
+ template.sendBodyAndHeaders("direct:start", "Hello Camel Rider",
headers);
+
+ assertMockEndpointsSatisfied();
}
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
public void configure() {
+ // START SNIPPET: e1
+ // We define 3 predicates based on some user roles
+ // we have static imported and/or from
org.apache.camel.builder.PredicateBuilder
+
+ // First we have a regular user that is just identified having
a username header
+ Predicate user = header("username").isNotNull();
+
+ // The admin user must be a user AND have a admin header as
true
+ Predicate admin = and(user, header("admin").isEqualTo("true"));
+
+ // And God must be an admin and (either have type god or a
special message containing Camel Rider)
+ Predicate god = and(admin, or(body().contains("Camel Rider"),
header("type").isEqualTo("god")));
+
+ // As you can see with the predicates above we can stack them
to build compound predicates
+
+ // In our route below we can create a nice content based
rotuer based on the predicates we
+ // have defined. Then the route is easy to read and understand.
+ // We encourge you to define complex predicates outside the
fluent router bulder as
+ // it will just get a bit complex for humans to read
from("direct:start").choice()
- .when().xpath("$foo = 'bar'").to("mock:x")
- .when().xpath("$foo = 'cheese'").to("mock:y")
- .otherwise().to("mock:z").end().to("mock:end");
+ .when(god).to("mock:god")
+ .when(admin).to("mock:admin")
+ .when(user).to("mock:user")
+ .otherwise().to("mock:guest")
+ .end();
+ // END SNIPPET: e1
}
};
}
-}
+}
\ No newline at end of file