activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r530987 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ main/java/org/apache/camel/component/mock/ test/java/org/apache/camel/processor/
Date Sat, 21 Apr 2007 06:38:10 GMT
Author: jstrachan
Date: Fri Apr 20 23:38:09 2007
New Revision: 530987

URL: http://svn.apache.org/viewvc?view=rev&rev=530987
Log:
added the ability to add assertions to individual messages using the DSL

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java?view=diff&rev=530987&r1=530986&r2=530987
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
Fri Apr 20 23:38:09 2007
@@ -44,57 +44,57 @@
     @Fluent
     public Predicate<E> isNotEqualTo(@FluentArg("value") Object value) {
         Expression<E> right = ExpressionBuilder.constantExpression(value);
-        return PredicateBuilder.isNotEqualTo(expression, right);
+        return onNewPredicate(PredicateBuilder.isNotEqualTo(expression, right));
     }
 
     @Fluent
     public Predicate<E> isEqualTo(@FluentArg("value") Object value) {
         Expression<E> right = ExpressionBuilder.constantExpression(value);
-        return PredicateBuilder.isEqualTo(expression, right);
+        return onNewPredicate(PredicateBuilder.isEqualTo(expression, right));
     }
 
     @Fluent
     public Predicate<E> isLessThan(@FluentArg("value") Object value) {
         Expression<E> right = ExpressionBuilder.constantExpression(value);
-        return PredicateBuilder.isLessThan(expression, right);
+        return onNewPredicate(PredicateBuilder.isLessThan(expression, right));
     }
 
     @Fluent
     public Predicate<E> isLessThanOrEqualTo(@FluentArg("value") Object value) {
         Expression<E> right = ExpressionBuilder.constantExpression(value);
-        return PredicateBuilder.isLessThanOrEqualTo(expression, right);
+        return onNewPredicate(PredicateBuilder.isLessThanOrEqualTo(expression, right));
     }
 
     @Fluent
     public Predicate<E> isGreaterThan(@FluentArg("value") Object value) {
         Expression<E> right = ExpressionBuilder.constantExpression(value);
-        return PredicateBuilder.isGreaterThan(expression, right);
+        return onNewPredicate(PredicateBuilder.isGreaterThan(expression, right));
     }
 
     @Fluent
     public Predicate<E> isGreaterThanOrEqualTo(@FluentArg("value") Object value) {
         Expression<E> right = ExpressionBuilder.constantExpression(value);
-        return PredicateBuilder.isGreaterThanOrEqualTo(expression, right);
+        return onNewPredicate(PredicateBuilder.isGreaterThanOrEqualTo(expression, right));
     }
 
     @Fluent
     public Predicate<E> isInstanceOf(@FluentArg("class") Class type) {
-        return PredicateBuilder.isInstanceOf(expression, type);
+        return onNewPredicate(PredicateBuilder.isInstanceOf(expression, type));
     }
 
     @Fluent
     public Predicate<E> matchesRegex(@FluentArg("regex") String regex) {
-        return PredicateBuilder.regex(expression, regex);
+        return onNewPredicate(PredicateBuilder.regex(expression, regex));
     }
 
     @Fluent
     public Predicate<E> isNull() {
-        return PredicateBuilder.isNull(expression);
+        return onNewPredicate(PredicateBuilder.isNull(expression));
     }
 
     @Fluent
     public Predicate<E> isNotNull() {
-        return PredicateBuilder.isNotNull(expression);
+        return onNewPredicate(PredicateBuilder.isNotNull(expression));
     }
 
     @Fluent
@@ -130,4 +130,11 @@
         return convertTo(String.class);
     }
 
+    /**
+     * A stategy method to allow derived classes to deal with the newly created predicate
+     * in different ways
+     */
+    protected Predicate<E> onNewPredicate(Predicate<E> predicate) {
+        return predicate;
+    }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java?view=diff&rev=530987&r1=530986&r2=530987
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java
Fri Apr 20 23:38:09 2007
@@ -19,12 +19,16 @@
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
 import static org.apache.camel.builder.ExpressionBuilder.bodyExpression;
 import static org.apache.camel.builder.ExpressionBuilder.headerExpression;
 import org.apache.camel.builder.Fluent;
 import org.apache.camel.builder.FluentArg;
 import org.apache.camel.builder.ValueBuilder;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * A builder of assertions on message exchanges
  *
@@ -32,58 +36,90 @@
  */
 public abstract class AssertionClause<E extends Exchange> implements Runnable {
 
+    private List<Predicate<E>> predicates = new ArrayList<Predicate<E>>();
+
     // Builder methods
     //-------------------------------------------------------------------------
 
     /**
+     * Adds the given predicate to this assertion clause
+     */
+    public AssertionClause<E> predicate(Predicate<E> predicate) {
+        addPredicate(predicate);
+        return this;
+    }
+
+    /**
      * Returns a predicate and value builder for headers on an exchange
      */
     @Fluent
-    public ValueBuilder<E> header(@FluentArg("name") String name) {
+    public ValueBuilder<E> header(@FluentArg("name")String name) {
         Expression<E> expression = headerExpression(name);
-        return new ValueBuilder<E>(expression);
+        return new PredicateValueBuilder(expression);
     }
 
     /**
      * Returns a predicate and value builder for the inbound body on an exchange
      */
     @Fluent
-    public ValueBuilder<E> body() {
+    public PredicateValueBuilder body() {
         Expression<E> expression = bodyExpression();
-        return new ValueBuilder<E>(expression);
+        return new PredicateValueBuilder(expression);
     }
 
     /**
      * Returns a predicate and value builder for the inbound message body as a specific type
      */
     @Fluent
-    public <T> ValueBuilder<E> bodyAs(@FluentArg("class") Class<T> type)
{
+    public <T> PredicateValueBuilder bodyAs(@FluentArg("class")Class<T> type)
{
         Expression<E> expression = bodyExpression(type);
-        return new ValueBuilder<E>(expression);
+        return new PredicateValueBuilder(expression);
     }
 
     /**
      * Returns a predicate and value builder for the outbound body on an exchange
      */
     @Fluent
-    public ValueBuilder<E> outBody() {
+    public PredicateValueBuilder outBody() {
         Expression<E> expression = bodyExpression();
-        return new ValueBuilder<E>(expression);
+        return new PredicateValueBuilder(expression);
     }
 
     /**
      * Returns a predicate and value builder for the outbound message body as a specific
type
      */
     @Fluent
-    public <T> ValueBuilder<E> outBody(@FluentArg("class") Class<T> type)
{
+    public <T> PredicateValueBuilder outBody(@FluentArg("class")Class<T> type)
{
         Expression<E> expression = bodyExpression(type);
-        return new ValueBuilder<E>(expression);
+        return new PredicateValueBuilder(expression);
     }
 
     /**
      * Performs any assertions on the given exchange
      */
-    protected void applyAssertionOn(int index, Exchange exchange) {
-        // TODO perform the predicate on the given exchange 
+    protected void applyAssertionOn(MockEndpoint endpoint, int index, E exchange) {
+        for (Predicate<E> predicate : predicates) {
+            if (!predicate.matches(exchange)) {
+                endpoint.fail("Message " + index + " failed Predicate " + predicate + " with
" + exchange);
+            }
+        }
+    }
+
+    protected void addPredicate(Predicate<E> predicate) {
+        predicates.add(predicate);
+    }
+
+    protected class PredicateValueBuilder extends ValueBuilder<E> {
+
+        public PredicateValueBuilder(Expression<E> expression) {
+            super(expression);
+        }
+
+
+        protected Predicate<E> onNewPredicate(Predicate<E> predicate) {
+            //addPredicate(predicate);
+            predicates.add(predicate);
+            return predicate;
+        }
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java?view=diff&rev=530987&r1=530986&r2=530987
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
Fri Apr 20 23:38:09 2007
@@ -209,7 +209,7 @@
     public AssertionClause message(final int messageIndex) {
         AssertionClause clause = new AssertionClause() {
             public void run() {
-                applyAssertionOn(messageIndex, assertExchangeReceived(messageIndex));
+                applyAssertionOn(MockEndpoint.this, messageIndex, assertExchangeReceived(messageIndex));
             }
         };
         expects(clause);
@@ -219,7 +219,6 @@
     /**
      * Adds an assertion to all the received messages
      *
-     * @param messageIndex the number of the message
      * @return the assertion clause
      */
     public AssertionClause allMessages() {
@@ -228,7 +227,7 @@
                 List<Exchange> list = getExchangesReceived();
                 int index = 0;
                 for (Exchange exchange : list) {
-                    applyAssertionOn(index++, exchange);
+                    applyAssertionOn(MockEndpoint.this, index++, exchange);
                 }
             }
         };
@@ -242,7 +241,7 @@
     public Exchange assertExchangeReceived(int index) {
         int count = getReceivedCounter();
         assertTrue("Not enough messages received. Was: " + count, count > index);
-        return getExchangesReceived().get(count);
+        return getExchangesReceived().get(index);
     }
 
     // Properties

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelTest.java?view=diff&rev=530987&r1=530986&r2=530987
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelTest.java
(original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/DeadLetterChannelTest.java
Fri Apr 20 23:38:09 2007
@@ -36,6 +36,8 @@
 
     public void testFirstFewAttemptsFail() throws Exception {
         successEndpoint.expectedBodiesReceived(body);
+        //successEndpoint.message(0).header(DeadLetterChannel.REDELIVERED).isEqualTo(true);
+        
         deadEndpoint.expectedMessageCount(0);
 
         send("direct:start", body);



Mime
View raw message