You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2007/10/15 18:13:06 UTC

svn commit: r584813 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/builder/ camel-core/src/main/java/org/apache/camel/component/mock/ camel-core/src/main/java/org/apache/camel/model/language/ components/camel-bam/src/main/java/or...

Author: jstrachan
Date: Mon Oct 15 09:13:05 2007
New Revision: 584813

URL: http://svn.apache.org/viewvc?rev=584813&view=rev
Log:
More changes for CAMEL-181 to allow nicer expressions to be used inside mock assertions such as endpoint.message(2).predicate().xpath("/foo/bar") etc. Also fixed failing BAM test.

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java   (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.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/model/language/ExpressionType.java
    activemq/camel/trunk/components/camel-bam/src/main/java/org/apache/camel/bam/processor/ActivityMonitorEngine.java
    activemq/camel/trunk/components/camel-bam/src/test/java/org/apache/camel/bam/MultipleProcessesTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java?rev=584813&r1=584812&r2=584813&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java Mon Oct 15 09:13:05 2007
@@ -17,22 +17,16 @@
  */
 package org.apache.camel.builder;
 
-import org.apache.camel.Expression;
 import org.apache.camel.model.ExpressionNode;
 import org.apache.camel.model.ProcessorType;
-import org.apache.camel.model.language.ExpressionType;
 
 /**
- * Represents an expression clause within the DSL
+ * Represents an expression clause within the DSL which when the expression is complete
+ * the clause continues to another part of the DSL
  *
  * @version $Revision: 1.1 $
  */
-public class ExpressionClause<T extends ProcessorType> extends ExpressionType {
-    private T result;
-    private String language;
-    private String expressionText;
-    private Expression expression;
-
+public class ExpressionClause<T extends ProcessorType> extends ExpressionClauseSupport<T> {
     public static <T extends ExpressionNode> ExpressionClause<T> createAndSetExpression(T result) {
         ExpressionClause<T> clause = new ExpressionClause<T>(result);
         result.setExpression(clause);
@@ -40,9 +34,10 @@
     }
 
     public ExpressionClause(T result) {
-        this.result = result;
+        super(result);
     }
 
+
     // Fluent API
     //-------------------------------------------------------------------------
 
@@ -165,18 +160,7 @@
      * @return the builder to continue processing the DSL
      */
     public T language(String language, String expression) {
-        setLanguage(language);
-        setExpression(expression);
-        return result;
-    }
-
-    // Properties
-    //-------------------------------------------------------------------------
-    public String getLanguage() {
-        return language;
+        return super.language(language, expression);
     }
 
-    public void setLanguage(String language) {
-        this.language = language;
-    }
 }

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java?rev=584813&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java Mon Oct 15 09:13:05 2007
@@ -0,0 +1,179 @@
+/**
+ *
+ * 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.model.language.ExpressionType;
+
+
+/**
+ * TODO workaround for a compiler bug. Remove ASAP!
+ * We seem to get compile time errors when creating routes of the form
+ *
+ * <code>from("foo").filter().xpath("//foo").to("bar")</code>
+ * as the type of the return value of xpath(String) is of type {@link Object} rather than
+ * the expected FilterType if we remove the "extends ProcessorType" from the T declaration
+ * so we've split the ExpressionClause class into two; one with the extends and one without
+ * with some cut and paste between them which is not ideal
+ *
+ * @version $Revision: 1.1 $
+ */
+public class ExpressionClauseSupport<T> extends ExpressionType {
+    private T result;
+    private String language;
+
+    public ExpressionClauseSupport(T result) {
+        this.result = result;
+    }
+
+    // Fluent API
+    //-------------------------------------------------------------------------
+
+    /**
+     * Evaluates the  <a href="http://activemq.apache.org/camel/el.html">EL Language from JSP and JSF</a>
+     * using the <a href="http://activemq.apache.org/camel/juel.html">JUEL library</a>
+     *
+     * @param text the expression to be evaluated
+     * @return the builder to continue processing the DSL
+     */
+    public T el(String text) {
+        return language("el", text);
+    }
+
+    /**
+     * Evaluates a <a href="http://activemq.apache.org/camel/groovy.html">Groovy expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @return the builder to continue processing the DSL
+     */
+    public T groovy(String text) {
+        return language("groovy", text);
+    }
+
+    /**
+     * Evaluates a <a href="http://activemq.apache.org/camel/java-script.html">JavaScript expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @return the builder to continue processing the DSL
+     */
+    public T javaScript(String text) {
+        return language("js", text);
+    }
+
+    /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/ognl.html">OGNL expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @return the builder to continue processing the DSL
+     */
+    public T ognl(String text) {
+        return language("ognl", text);
+    }
+
+    /**
+     * Evaluates a <a href="http://activemq.apache.org/camel/php.html">PHP expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @return the builder to continue processing the DSL
+     */
+    public T php(String text) {
+        return language("php", text);
+    }
+
+    /**
+     * Evaluates a <a href="http://activemq.apache.org/camel/python.html">Python expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @return the builder to continue processing the DSL
+     */
+    public T python(String text) {
+        return language("python", text);
+    }
+
+    /**
+     * Evaluates a <a href="http://activemq.apache.org/camel/ruby.html">Ruby expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @return the builder to continue processing the DSL
+     */
+    public T ruby(String text) {
+        return language("ruby", text);
+    }
+
+    /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/sql.html">SQL expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @return the builder to continue processing the DSL
+     */
+    public T sql(String text) {
+        return language("sql", text);
+    }
+
+    /**
+     * Evaluates a <a href="http://activemq.apache.org/camel/simple.html">Simple expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @return the builder to continue processing the DSL
+     */
+    public T simple(String text) {
+        return language("simple", text);
+    }
+
+    /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/xpath.html">XPath expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @return the builder to continue processing the DSL
+     */
+    public T xpath(String text) {
+        return language("xpath", text);
+    }
+
+    /**
+     * Evaluates an <a href="http://activemq.apache.org/camel/xquery.html">XQuery expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @return the builder to continue processing the DSL
+     */
+    public T xquery(String text) {
+        return language("xquery", text);
+    }
+
+    /**
+     * Evaluates a given language name with the expression text
+     *
+     * @param language   the name of the language
+     * @param expression the expression in the given language
+     * @return the builder to continue processing the DSL
+     */
+    public T language(String language, String expression) {
+        setLanguage(language);
+        setExpression(expression);
+        return result;
+    }
+
+    // Properties
+    //-------------------------------------------------------------------------
+    public String getLanguage() {
+        return language;
+    }
+
+    public void setLanguage(String language) {
+        this.language = language;
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
------------------------------------------------------------------------------
    svn:eol-style = native

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?rev=584813&r1=584812&r2=584813&view=diff
==============================================================================
--- 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 Mon Oct 15 09:13:05 2007
@@ -22,19 +22,20 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
-import org.apache.camel.builder.ValueBuilder;
-
 import static org.apache.camel.builder.ExpressionBuilder.bodyExpression;
 import static org.apache.camel.builder.ExpressionBuilder.headerExpression;
+import org.apache.camel.builder.ExpressionClause;
+import org.apache.camel.builder.ExpressionClauseSupport;
+import org.apache.camel.builder.ValueBuilder;
 
 /**
  * A builder of assertions on message exchanges
  * 
  * @version $Revision: 1.1 $
  */
-public abstract class AssertionClause<E extends Exchange> implements Runnable {
+public abstract class AssertionClause implements Runnable {
 
-    private List<Predicate<E>> predicates = new ArrayList<Predicate<E>>();
+    private List<Predicate<Exchange>> predicates = new ArrayList<Predicate<Exchange>>();
 
     // Builder methods
     // -------------------------------------------------------------------------
@@ -42,17 +43,23 @@
     /**
      * Adds the given predicate to this assertion clause
      */
-    public AssertionClause<E> predicate(Predicate<E> predicate) {
+    public AssertionClause predicate(Predicate<Exchange> predicate) {
         addPredicate(predicate);
         return this;
     }
 
+    public ExpressionClauseSupport<AssertionClause> predicate() {
+        ExpressionClauseSupport<AssertionClause> clause = new ExpressionClauseSupport<AssertionClause>(this);
+        addPredicate(clause);
+        return clause;
+    }
+
     /**
      * Returns a predicate and value builder for headers on an exchange
      */
 
-    public ValueBuilder<E> header(String name) {
-        Expression<E> expression = headerExpression(name);
+    public ValueBuilder<Exchange> header(String name) {
+        Expression<Exchange> expression = headerExpression(name);
         return new PredicateValueBuilder(expression);
     }
 
@@ -61,7 +68,7 @@
      */
 
     public PredicateValueBuilder body() {
-        Expression<E> expression = bodyExpression();
+        Expression<Exchange> expression = bodyExpression();
         return new PredicateValueBuilder(expression);
     }
 
@@ -71,7 +78,7 @@
      */
 
     public <T> PredicateValueBuilder bodyAs(Class<T> type) {
-        Expression<E> expression = bodyExpression(type);
+        Expression<Exchange> expression = bodyExpression(type);
         return new PredicateValueBuilder(expression);
     }
 
@@ -81,7 +88,7 @@
      */
 
     public PredicateValueBuilder outBody() {
-        Expression<E> expression = bodyExpression();
+        Expression<Exchange> expression = bodyExpression();
         return new PredicateValueBuilder(expression);
     }
 
@@ -91,30 +98,32 @@
      */
 
     public <T> PredicateValueBuilder outBody(Class<T> type) {
-        Expression<E> expression = bodyExpression(type);
+        Expression<Exchange> expression = bodyExpression(type);
         return new PredicateValueBuilder(expression);
     }
 
     /**
      * Performs any assertions on the given exchange
      */
-    protected void applyAssertionOn(MockEndpoint endpoint, int index, E exchange) {
-        for (Predicate<E> predicate : predicates) {
+    protected void applyAssertionOn(MockEndpoint endpoint, int index, Exchange exchange) {
+        for (Predicate<Exchange> predicate : predicates) {
             predicate.assertMatches(endpoint.getEndpointUri() + " ", exchange);
         }
     }
 
-    protected void addPredicate(Predicate<E> predicate) {
+    protected void addPredicate(Predicate<Exchange> predicate) {
         predicates.add(predicate);
     }
 
-    public class PredicateValueBuilder extends ValueBuilder<E> {
+    
+
+    public class PredicateValueBuilder extends ValueBuilder<Exchange> {
 
-        public PredicateValueBuilder(Expression<E> expression) {
+        public PredicateValueBuilder(Expression<Exchange> expression) {
             super(expression);
         }
 
-        protected Predicate<E> onNewPredicate(Predicate<E> predicate) {
+        protected Predicate<Exchange> onNewPredicate(Predicate<Exchange> predicate) {
             addPredicate(predicate);
             return predicate;
         }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionType.java?rev=584813&r1=584812&r2=584813&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionType.java Mon Oct 15 09:13:05 2007
@@ -32,11 +32,12 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
+import org.apache.camel.impl.ExpressionSupport;
 import org.apache.camel.impl.RouteContext;
 import org.apache.camel.spi.Language;
 import org.apache.camel.util.CollectionStringBuffer;
-import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.IntrospectionSupport;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * A useful base class for an expression
@@ -45,7 +46,7 @@
  */
 @XmlType(name = "expressionType")
 @XmlAccessorType(XmlAccessType.FIELD)
-public class ExpressionType implements Expression<Exchange> {
+public class ExpressionType implements Expression<Exchange>, Predicate<Exchange> {
     @XmlAttribute
     @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
     @XmlID
@@ -92,6 +93,21 @@
         }
         ObjectHelper.notNull(expressionValue, "expressionValue");
         return expressionValue.evaluate(exchange);
+    }
+
+    public void assertMatches(String text, Exchange exchange) throws AssertionError {
+        if (!matches(exchange)) {
+            throw new AssertionError(text + getExpression() + " for exchange: " + exchange);
+        }
+    }
+
+    public boolean matches(Exchange exchange) {
+        if (predicate == null) {
+            RouteContext routeContext = new RouteContext(exchange.getContext());
+            predicate = createPredicate(routeContext);
+        }
+        ObjectHelper.notNull(predicate, "predicate");
+        return predicate.matches(exchange);
     }
 
     public String getLanguage() {

Modified: activemq/camel/trunk/components/camel-bam/src/main/java/org/apache/camel/bam/processor/ActivityMonitorEngine.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-bam/src/main/java/org/apache/camel/bam/processor/ActivityMonitorEngine.java?rev=584813&r1=584812&r2=584813&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-bam/src/main/java/org/apache/camel/bam/processor/ActivityMonitorEngine.java (original)
+++ activemq/camel/trunk/components/camel-bam/src/main/java/org/apache/camel/bam/processor/ActivityMonitorEngine.java Mon Oct 15 09:13:05 2007
@@ -76,7 +76,8 @@
 
                 transactionTemplate.execute(new TransactionCallbackWithoutResult() {
                     protected void doInTransactionWithoutResult(TransactionStatus status) {
-                        List<ActivityState> list = template.find("select x from " + ActivityState.class.getName() + " x where x.escalationLevel = ?1 and x.timeOverdue < ?2", escalateLevel, timeNow);
+                        //List<ActivityState> list = template.find("select x from " + ActivityState.class.getName() + " x where x.escalationLevel = ?1 and x.timeOverdue < ?2", escalateLevel, timeNow);
+                        List<ActivityState> list = template.find("select x from " + ActivityState.class.getName() + " x where x.timeOverdue < ?1", timeNow);
                         for (ActivityState activityState : list) {
                             fireExpiredEvent(activityState);
                         }
@@ -119,7 +120,8 @@
                 } catch (Exception e) {
                     LOG.error("Failed to process expiration of: " + activityState + ". Reason: " + e, e);
                 }
-                activityState.setEscalationLevel(escalateLevel + 1);
+                activityState.setTimeOverdue(null);
+                //activityState.setEscalationLevel(escalateLevel + 1);
                 return null;
             }
         });

Modified: activemq/camel/trunk/components/camel-bam/src/test/java/org/apache/camel/bam/MultipleProcessesTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-bam/src/test/java/org/apache/camel/bam/MultipleProcessesTest.java?rev=584813&r1=584812&r2=584813&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-bam/src/test/java/org/apache/camel/bam/MultipleProcessesTest.java (original)
+++ activemq/camel/trunk/components/camel-bam/src/test/java/org/apache/camel/bam/MultipleProcessesTest.java Mon Oct 15 09:13:05 2007
@@ -17,32 +17,43 @@
  */
 package org.apache.camel.bam;
 
-import static org.apache.camel.language.juel.JuelExpression.el;
+import org.springframework.transaction.TransactionStatus;
+import org.springframework.transaction.support.TransactionCallbackWithoutResult;
+import org.springframework.transaction.support.TransactionTemplate;
 
 /**
  * @version $Revision: 1.1 $
  */
 public class MultipleProcessesTest extends BamRouteTest {
-
     @Override
     public void testBam() throws Exception {
         overdueEndpoint.expectedMessageCount(1);
-        overdueEndpoint.message(0).predicate(el("${in.body.correlationKey == '124'}"));
+        //overdueEndpoint.message(0).predicate().el("${in.body.correlationKey == '124'}");
 
         sendAMessages();
         sendBMessages();
 
-        //overdueEndpoint.assertIsSatisfied();
+        overdueEndpoint.assertIsSatisfied();
     }
 
     protected void sendAMessages() {
-        template.sendBody("direct:a", "<hello id='123'>A</hello>");
-        template.sendBody("direct:a", "<hello id='124'>B</hello>");
-        template.sendBody("direct:a", "<hello id='125'>C</hello>");
+        TransactionTemplate transaction = getMandatoryBean(TransactionTemplate.class, "transactionTemplate");
+        transaction.execute(new TransactionCallbackWithoutResult() {
+            protected void doInTransactionWithoutResult(TransactionStatus status) {
+                template.sendBody("direct:a", "<hello id='123'>A</hello>");
+                template.sendBody("direct:a", "<hello id='124'>B</hello>");
+                template.sendBody("direct:a", "<hello id='125'>C</hello>");
+            }
+        });
     }
 
     protected void sendBMessages() {
-        template.sendBody("direct:b", "<hello id='123'>A</hello>");
-        template.sendBody("direct:b", "<hello id='125'>C</hello>");
+        TransactionTemplate transaction = getMandatoryBean(TransactionTemplate.class, "transactionTemplate");
+        transaction.execute(new TransactionCallbackWithoutResult() {
+            protected void doInTransactionWithoutResult(TransactionStatus status) {
+                template.sendBody("direct:b", "<hello id='123'>A</hello>");
+                template.sendBody("direct:b", "<hello id='125'>C</hello>");
+            }
+        });
     }
 }