Author: davsclaus
Date: Fri Mar  8 07:49:54 2013
New Revision: 1454269

URL: http://svn.apache.org/r1454269
Log:
CAMEL-6145: Added lenient option to jxpath language.

Added:
    
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterLenientTest.java
      - copied, changed from r1454254, 
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterTest.java
    
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterNotLenientTest.java
    
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/SpringJXPathFilterLenientTest.java
    
camel/trunk/components/camel-jxpath/src/test/resources/org/apache/camel/language/jxpath/SpringJXPathFilterLenientTest-context.xml
      - copied, changed from r1454254, 
camel/trunk/components/camel-jxpath/src/test/resources/org/apache/camel/language/jxpath/SpringJXPathFilterTest-context.xml
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/JXPathExpression.java
    
camel/trunk/components/camel-jxpath/src/main/java/org/apache/camel/language/jxpath/JXPathExpression.java
    
camel/trunk/components/camel-jxpath/src/main/java/org/apache/camel/language/jxpath/JXPathLanguage.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java?rev=1454269&r1=1454268&r2=1454269&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
 Fri Mar  8 07:49:54 2013
@@ -287,6 +287,17 @@ public class ExpressionClause<T> extends
     }
 
     /**
+     * Evaluates a <a href="http://commons.apache.org/jxpath/";>JXPath 
expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @param lenient to configure whether lenient is in use or not
+     * @return the builder to continue processing the DSL
+     */
+    public T jxpath(String text, boolean lenient) {
+        return delegate.jxpath(text, lenient);
+    }
+
+    /**
      * Evaluates an <a href="http://camel.apache.org/ognl.html";>OGNL
      * expression</a>
      * 

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java?rev=1454269&r1=1454268&r2=1454269&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
 Fri Mar  8 07:49:54 2013
@@ -318,7 +318,20 @@ public class ExpressionClauseSupport<T> 
      * @return the builder to continue processing the DSL
      */
     public T jxpath(String text) {
-        return expression(new JXPathExpression(text));
+        return jxpath(text, false);
+    }
+
+    /**
+     * Evaluates a <a href="http://commons.apache.org/jxpath/";>JXPath 
expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @param lenient to configure whether lenient is in use or not
+     * @return the builder to continue processing the DSL
+     */
+    public T jxpath(String text, boolean lenient) {
+        JXPathExpression answer = new JXPathExpression(text);
+        answer.setLenient(lenient);
+        return expression(answer);
     }
 
     /**

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/JXPathExpression.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/JXPathExpression.java?rev=1454269&r1=1454268&r2=1454269&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/JXPathExpression.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/JXPathExpression.java
 Fri Mar  8 07:49:54 2013
@@ -18,8 +18,13 @@ package org.apache.camel.model.language;
 
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
 
+import org.apache.camel.CamelContext;
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
+
 /**
  * For <a href="http://commons.apache.org/jxpath/";>JXPath</a> expressions and 
predicates
  *
@@ -29,6 +34,9 @@ import javax.xml.bind.annotation.XmlRoot
 @XmlAccessorType(XmlAccessType.FIELD)
 public class JXPathExpression extends ExpressionDefinition {
 
+    @XmlAttribute
+    private Boolean lenient;
+
     public JXPathExpression() {
     }
 
@@ -39,4 +47,28 @@ public class JXPathExpression extends Ex
     public String getLanguage() {
         return "jxpath";
     }
+
+    public Boolean getLenient() {
+        return lenient;
+    }
+
+    public void setLenient(Boolean lenient) {
+        this.lenient = lenient;
+    }
+
+    @Override
+    protected void configureExpression(CamelContext camelContext, Expression 
expression) {
+        if (lenient != null) {
+            setProperty(expression, "lenient", lenient);
+        }
+        super.configureExpression(camelContext, expression);
+    }
+
+    @Override
+    protected void configurePredicate(CamelContext camelContext, Predicate 
predicate) {
+        if (lenient != null) {
+            setProperty(predicate, "lenient", lenient);
+        }
+        super.configurePredicate(camelContext, predicate);
+    }
 }

Modified: 
camel/trunk/components/camel-jxpath/src/main/java/org/apache/camel/language/jxpath/JXPathExpression.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jxpath/src/main/java/org/apache/camel/language/jxpath/JXPathExpression.java?rev=1454269&r1=1454268&r2=1454269&view=diff
==============================================================================
--- 
camel/trunk/components/camel-jxpath/src/main/java/org/apache/camel/language/jxpath/JXPathExpression.java
 (original)
+++ 
camel/trunk/components/camel-jxpath/src/main/java/org/apache/camel/language/jxpath/JXPathExpression.java
 Fri Mar  8 07:49:54 2013
@@ -31,21 +31,43 @@ public class JXPathExpression extends Ex
     private final String expression;
     private CompiledExpression compiledExpression;
     private final Class<?> type;
+    private boolean lenient;
 
     /**
-     * Creates a new JXPathExpression instance
+     * Creates a new JXPathExpression instance (lenient is disabled)
      * 
      * @param expression the JXPath expression to be evaluated
      * @param type the expected result type
      */
     public JXPathExpression(String expression, Class<?> type) {
+        this(expression, type, false);
+    }
+
+    /**
+     * Creates a new JXPathExpression instance
+     *
+     * @param expression the JXPath expression to be evaluated
+     * @param type the expected result type
+     * @param lenient to configure lenient
+     */
+    public JXPathExpression(String expression, Class<?> type, boolean lenient) 
{
         this.expression = expression;
         this.type = type;
+        this.lenient = lenient;
+    }
+
+    public boolean isLenient() {
+        return lenient;
+    }
+
+    public void setLenient(boolean lenient) {
+        this.lenient = lenient;
     }
 
     public <T> T evaluate(Exchange exchange, Class<T> tClass) {
         try {
             JXPathContext context = JXPathContext.newContext(exchange);
+            context.setLenient(lenient);
             Object result = getJXPathExpression().getValue(context, type);
             assertResultType(exchange, result);
             return exchange.getContext().getTypeConverter().convertTo(tClass, 
result);

Modified: 
camel/trunk/components/camel-jxpath/src/main/java/org/apache/camel/language/jxpath/JXPathLanguage.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jxpath/src/main/java/org/apache/camel/language/jxpath/JXPathLanguage.java?rev=1454269&r1=1454268&r2=1454269&view=diff
==============================================================================
--- 
camel/trunk/components/camel-jxpath/src/main/java/org/apache/camel/language/jxpath/JXPathLanguage.java
 (original)
+++ 
camel/trunk/components/camel-jxpath/src/main/java/org/apache/camel/language/jxpath/JXPathLanguage.java
 Fri Mar  8 07:49:54 2013
@@ -27,14 +27,29 @@ import org.apache.camel.support.Language
  */
 public class JXPathLanguage extends LanguageSupport {
 
+    private boolean lenient;
+
+    public boolean isLenient() {
+        return lenient;
+    }
+
+    public void setLenient(boolean lenient) {
+        this.lenient = lenient;
+    }
+
     public Expression createExpression(String expression) {
         expression = loadResource(expression);
-        return new JXPathExpression(expression, Object.class);
+        return new JXPathExpression(expression, Object.class, lenient);
     }
 
     public Predicate createPredicate(String predicate) {
         predicate = loadResource(predicate);
-        return new JXPathExpression(predicate, Boolean.class);
+        return new JXPathExpression(predicate, Boolean.class, lenient);
     }
 
+    @Override
+    public boolean isSingleton() {
+        // cannot be singleton due lenient option
+        return false;
+    }
 }

Copied: 
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterLenientTest.java
 (from r1454254, 
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterLenientTest.java?p2=camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterLenientTest.java&p1=camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterTest.java&r1=1454254&r2=1454269&rev=1454269&view=diff
==============================================================================
--- 
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterTest.java
 (original)
+++ 
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterLenientTest.java
 Fri Mar  8 07:49:54 2013
@@ -23,31 +23,13 @@ import org.junit.Test;
 /**
  * @version 
  */
-public class JXPathFilterTest extends CamelTestSupport {
+public class JXPathFilterLenientTest extends CamelTestSupport {
 
     @Test
-    public void testSendMatchingMessage() throws Exception {
-        getMockEndpoint("mock:result").expectedMessageCount(1);
-
-        sendBody("direct:start", new PersonBean("James", "London"));
-
-        assertMockEndpointsSatisfied();
-    }
-
-    @Test
-    public void testSendNotMatchingMessage() throws Exception {
-        getMockEndpoint("mock:result").expectedMessageCount(0);
-
-        sendBody("direct:start", new PersonBean("Hiram", "Tampa"));
-
-        assertMockEndpointsSatisfied();
-    }
-    
-    @Test
-    public void testSendNullMessage() throws Exception {
+    public void testLenient() throws Exception {
         getMockEndpoint("mock:result").expectedMessageCount(0);
 
-        sendBody("direct:start", new PersonBean(null, "Tampa"));
+        template.sendBody("direct:start", new PersonBean("James", "London"));
 
         assertMockEndpointsSatisfied();
     }
@@ -55,11 +37,9 @@ public class JXPathFilterTest extends Ca
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
-                // START SNIPPET: example
                 from("direct:start").
-                        filter().jxpath("in/body/name='James'").
+                        filter().jxpath("in/body/name2", true).
                         to("mock:result");
-                // END SNIPPET: example
             }
         };
     }

Added: 
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterNotLenientTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterNotLenientTest.java?rev=1454269&view=auto
==============================================================================
--- 
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterNotLenientTest.java
 (added)
+++ 
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/JXPathFilterNotLenientTest.java
 Fri Mar  8 07:49:54 2013
@@ -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.language.jxpath;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.commons.jxpath.JXPathNotFoundException;
+import org.junit.Test;
+
+/**
+ * @version 
+ */
+public class JXPathFilterNotLenientTest extends CamelTestSupport {
+
+    @Test
+    public void testNotLenient() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        try {
+            template.sendBody("direct:start", new PersonBean("James", 
"London"));
+            fail("Should have thrown exception");
+        } catch (Exception e) {
+            JXPathNotFoundException cause = 
assertIsInstanceOf(JXPathNotFoundException.class, e.getCause().getCause());
+            assertEquals("No value for xpath: in/body/name2", 
cause.getMessage());
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start").
+                        filter().jxpath("in/body/name2", false).
+                        to("mock:result");
+            }
+        };
+    }
+}

Added: 
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/SpringJXPathFilterLenientTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/SpringJXPathFilterLenientTest.java?rev=1454269&view=auto
==============================================================================
--- 
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/SpringJXPathFilterLenientTest.java
 (added)
+++ 
camel/trunk/components/camel-jxpath/src/test/java/org/apache/camel/language/jxpath/SpringJXPathFilterLenientTest.java
 Fri Mar  8 07:49:54 2013
@@ -0,0 +1,43 @@
+/**
+ * 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.language.jxpath;
+
+import org.apache.camel.test.spring.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version 
+ */
+public class SpringJXPathFilterLenientTest extends CamelSpringTestSupport {
+
+    @Test
+    public void testLenient() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        template.sendBody("direct:start", new PersonBean("James", "London"));
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected AbstractApplicationContext createApplicationContext() {
+        return new 
ClassPathXmlApplicationContext("org/apache/camel/language/jxpath/SpringJXPathFilterLenientTest-context.xml");
+    }
+
+}

Copied: 
camel/trunk/components/camel-jxpath/src/test/resources/org/apache/camel/language/jxpath/SpringJXPathFilterLenientTest-context.xml
 (from r1454254, 
camel/trunk/components/camel-jxpath/src/test/resources/org/apache/camel/language/jxpath/SpringJXPathFilterTest-context.xml)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jxpath/src/test/resources/org/apache/camel/language/jxpath/SpringJXPathFilterLenientTest-context.xml?p2=camel/trunk/components/camel-jxpath/src/test/resources/org/apache/camel/language/jxpath/SpringJXPathFilterLenientTest-context.xml&p1=camel/trunk/components/camel-jxpath/src/test/resources/org/apache/camel/language/jxpath/SpringJXPathFilterTest-context.xml&r1=1454254&r2=1454269&rev=1454269&view=diff
==============================================================================
--- 
camel/trunk/components/camel-jxpath/src/test/resources/org/apache/camel/language/jxpath/SpringJXPathFilterTest-context.xml
 (original)
+++ 
camel/trunk/components/camel-jxpath/src/test/resources/org/apache/camel/language/jxpath/SpringJXPathFilterLenientTest-context.xml
 Fri Mar  8 07:49:54 2013
@@ -28,7 +28,7 @@
     <route>
       <from uri="direct:start"/>
       <filter>
-        <jxpath>in/body/name='James'</jxpath>
+        <jxpath lenient="true">in/body/name2</jxpath>
         <to uri="mock:results"/>
       </filter>
     </route>


Reply via email to