Author: davsclaus
Date: Fri Feb 11 15:34:50 2011
New Revision: 1069839

URL: http://svn.apache.org/viewvc?rev=1069839&view=rev
Log:
CAMEL-1353: Added Spring Expression Language to camel-spring. Thanks to Richard 
Kettelerij for the patch. Great work.

Added:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/SpELExpression.java
    camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/SpEL.java
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/RootObject.java
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelExpression.java
    
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelLanguage.java
    
camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/language/
    
camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/language/spel
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelRouteTest.java
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelSpringTest.java
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelTest.java
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/bean/
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/bean/Dummy.java
    
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/
    
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/spel/
    
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/spel/SpelSpringTest-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/resources/org/apache/camel/model/language/jaxb.index
    camel/trunk/components/camel-spring/pom.xml

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=1069839&r1=1069838&r2=1069839&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 Feb 11 15:34:50 2011
@@ -346,6 +346,17 @@ public class ExpressionClause<T> extends
     }
 
     /**
+     * Evaluates a <a href="http://camel.apache.org/spel.html";>SpEL
+     * expression</a>
+     * 
+     * @param text the expression to be evaluated
+     * @return the builder to continue processing the DSL
+     */
+    public T spel(String text) {
+        return delegate.spel(text);
+    }
+    
+    /**
      * Evaluates a <a href="http://camel.apache.org/simple.html";>Simple
      * 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=1069839&r1=1069838&r2=1069839&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 Feb 11 15:34:50 2011
@@ -353,6 +353,17 @@ public class ExpressionClauseSupport<T> 
     }
 
     /**
+     * Evaluates an <a href="http://camel.apache.org/spel.html";>SpEL
+     * expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @return the builder to continue processing the DSL
+     */
+    public T spel(String text) {
+        return language("spel", text);
+    }
+    
+    /**
      * Evaluates an <a href="http://camel.apache.org/sql.html";>SQL
      * expression</a>
      *

Added: 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/SpELExpression.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/SpELExpression.java?rev=1069839&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/SpELExpression.java
 (added)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/SpELExpression.java
 Fri Feb 11 15:34:50 2011
@@ -0,0 +1,39 @@
+/**
+ * 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.model.language;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+/**
+ * For Spring Expression Language (SpEL) expressions and predicates
+ *
+ * @version $Revision$
+ */
+@XmlRootElement(name = "spel")
+public class SpELExpression extends ExpressionDefinition {
+
+    public SpELExpression() {
+    }
+
+    public SpELExpression(String expression) {
+        super(expression);
+    }
+
+    public String getLanguage() {
+        return "spel";
+    }
+}

Modified: 
camel/trunk/camel-core/src/main/resources/org/apache/camel/model/language/jaxb.index
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/language/jaxb.index?rev=1069839&r1=1069838&r2=1069839&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/resources/org/apache/camel/model/language/jaxb.index
 (original)
+++ 
camel/trunk/camel-core/src/main/resources/org/apache/camel/model/language/jaxb.index
 Fri Feb 11 15:34:50 2011
@@ -30,6 +30,7 @@ PropertyExpression
 PythonExpression
 RubyExpression
 SimpleExpression
+SpELExpression
 SqlExpression
 TokenizerExpression
 XPathExpression

Modified: camel/trunk/components/camel-spring/pom.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/pom.xml?rev=1069839&r1=1069838&r2=1069839&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/pom.xml (original)
+++ camel/trunk/components/camel-spring/pom.xml Fri Feb 11 15:34:50 2011
@@ -54,7 +54,9 @@
         
org.apache.camel.component.event;${camel.osgi.split.pkg};${camel.osgi.version},
         
org.apache.camel.component.test;${camel.osgi.split.pkg};${camel.osgi.version},
         
org.apache.camel.component.validator;${camel.osgi.split.pkg};${camel.osgi.version},
-        
org.apache.camel.component.xslt;${camel.osgi.split.pkg};${camel.osgi.version}
+        
org.apache.camel.component.xslt;${camel.osgi.split.pkg};${camel.osgi.version},
+        
org.apache.camel.language;${camel.osgi.split.pkg};${camel.osgi.version},
+        
org.apache.camel.language.spel;${camel.osgi.split.pkg};${camel.osgi.version}
       </camel.osgi.export>
       <camel.osgi.private.pkg>
         org.apache.camel.core.osgi*,

Added: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/SpEL.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/SpEL.java?rev=1069839&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/SpEL.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/SpEL.java
 Fri Feb 11 15:34:50 2011
@@ -0,0 +1,39 @@
+/**
+ * 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;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * An annotation for injection of Spring Expression Language (SpEL) expressions
+ * into method parameters, fields or properties.
+ * 
+ * In the context of Camel this annotation serves a similar purpose as 
Spring's {@Value} annotation.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
+@LanguageAnnotation(language = "spel")
+public @interface SpEL {
+    
+    String value();
+    
+}
\ No newline at end of file

Added: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/RootObject.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/RootObject.java?rev=1069839&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/RootObject.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/RootObject.java
 Fri Feb 11 15:34:50 2011
@@ -0,0 +1,67 @@
+/**
+ * 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.spel;
+
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+
+public final class RootObject {
+    private final Exchange exchange;
+
+    public RootObject(Exchange exchange) {
+        this.exchange = exchange;
+    }
+
+    public Exchange getExchange() {
+        return exchange;
+    }
+
+    public CamelContext getContext() {
+        return exchange.getContext();
+    }
+
+    public Throwable getException() {
+        return exchange.getException();
+    }
+
+    public String getExchangeId() {
+        return exchange.getExchangeId();
+    }
+
+    public Message getRequest() {
+        return exchange.getIn();
+    }
+
+    public Message getResponse() {
+        return exchange.getOut();
+    }
+
+    public Map<String, Object> getProperties() {
+        return exchange.getProperties();
+    }
+
+    public Object getProperty(String name) {
+        return exchange.getProperty(name);
+    }
+
+    public <T> T getProperty(String name, Class<T> type) {
+        return exchange.getProperty(name, type);
+    }
+}

Added: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelExpression.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelExpression.java?rev=1069839&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelExpression.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelExpression.java
 Fri Feb 11 15:34:50 2011
@@ -0,0 +1,96 @@
+/**
+ * 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.spel;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.ExpressionEvaluationException;
+import org.apache.camel.impl.ExpressionSupport;
+import org.apache.camel.spring.SpringCamelContext;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.expression.BeanFactoryResolver;
+import org.springframework.expression.EvaluationContext;
+import org.springframework.expression.Expression;
+import org.springframework.expression.ParserContext;
+import org.springframework.expression.common.TemplateParserContext;
+import org.springframework.expression.spel.standard.SpelExpressionParser;
+import org.springframework.expression.spel.support.StandardEvaluationContext;
+
+/**
+ * Class responsible for evaluating <a href="http://static.springsource.org
+ * /spring/docs/current/spring-framework-reference/html/expressions.html">
+ * Spring Expression Language</a> in the context of Camel.
+ */
+public class SpelExpression extends ExpressionSupport {
+
+    private final String expressionString;
+    private final Class<?> type;
+
+    // SpelExpressionParser is thread-safe according to the docs
+    private final SpelExpressionParser expressionParser;
+
+    public SpelExpression(String expressionString, Class<?> type) {
+        this.expressionString = expressionString;
+        this.type = type;
+        this.expressionParser = new SpelExpressionParser();
+    }
+
+    public static SpelExpression spel(String expression) {
+        return new SpelExpression(expression, Object.class);
+    }
+
+    public <T> T evaluate(Exchange exchange, Class<T> tClass) {
+        try {
+            Expression expression = parseExpression();
+            EvaluationContext evaluationContext = 
createEvaluationContext(exchange);
+            Object value = expression.getValue(evaluationContext);
+            // Let Camel handle the type conversion
+            return exchange.getContext().getTypeConverter().convertTo(tClass, 
value);
+        } catch (Exception e) {
+            throw new ExpressionEvaluationException(this, exchange, e);
+        }
+    }
+
+    private EvaluationContext createEvaluationContext(Exchange exchange) {
+        StandardEvaluationContext evaluationContext = new 
StandardEvaluationContext(new RootObject(exchange));
+        if (exchange.getContext() instanceof SpringCamelContext) {
+            // Support references (like @foo) in expressions to beans defined 
in the Registry/ApplicationContext
+            ApplicationContext applicationContext = ((SpringCamelContext) 
exchange.getContext()).getApplicationContext();
+            evaluationContext.setBeanResolver(new 
BeanFactoryResolver(applicationContext));
+        }
+        return evaluationContext;
+    }
+
+    private Expression parseExpression() {
+        // Support template parsing with #{ } delimiters
+        ParserContext parserContext = new TemplateParserContext();
+        Expression expression = 
expressionParser.parseExpression(expressionString, parserContext);
+        return expression;
+    }
+
+    public Class<?> getType() {
+        return type;
+    }
+
+    protected String assertionFailureMessage(Exchange exchange) {
+        return expressionString;
+    }
+
+    @Override
+    public String toString() {
+        return "SpelExpression[" + expressionString + "]";
+    }
+}

Added: 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelLanguage.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelLanguage.java?rev=1069839&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelLanguage.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/main/java/org/apache/camel/language/spel/SpelLanguage.java
 Fri Feb 11 15:34:50 2011
@@ -0,0 +1,40 @@
+/**
+ * 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.spel;
+
+import org.apache.camel.Expression;
+import org.apache.camel.IsSingleton;
+import org.apache.camel.Predicate;
+import org.apache.camel.spi.Language;
+
+/**
+ * A Spring Expression {@link Language} plugin
+ */
+public class SpelLanguage implements Language, IsSingleton {
+
+    public Predicate createPredicate(String expression) {
+        return new SpelExpression(expression, Boolean.class);
+    }
+
+    public Expression createExpression(String expression) {
+        return new SpelExpression(expression, Object.class);
+    }
+
+    public boolean isSingleton() {
+        return true;
+    }
+}

Added: 
camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/language/spel
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/language/spel?rev=1069839&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/language/spel
 (added)
+++ 
camel/trunk/components/camel-spring/src/main/resources/META-INF/services/org/apache/camel/language/spel
 Fri Feb 11 15:34:50 2011
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+class=org.apache.camel.language.spel.SpelLanguage
\ No newline at end of file

Added: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelRouteTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelRouteTest.java?rev=1069839&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelRouteTest.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelRouteTest.java
 Fri Feb 11 15:34:50 2011
@@ -0,0 +1,42 @@
+/**
+ * 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.spel;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import static org.apache.camel.language.spel.SpelExpression.spel;
+
+public class SpelRouteTest extends ContextTestSupport {
+    
+    public void testSpelRoute() throws Exception {
+        MockEndpoint resultEndpoint = getMockEndpoint("mock:result");
+        resultEndpoint.expectedBodiesReceived("Hello World! What a beautiful 
day");
+        template.sendBodyAndHeader("direct:test", "World", "dayOrNight", 
"day");
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:test").setBody(spel("Hello #{request.body}! What 
a beautiful #{request.headers['dayOrNight']}")).to("mock:result");
+            }
+        };
+    }
+}

Added: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelSpringTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelSpringTest.java?rev=1069839&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelSpringTest.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelSpringTest.java
 Fri Feb 11 15:34:50 2011
@@ -0,0 +1,48 @@
+/**
+ * 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.spel;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.LanguageTestSupport;
+import org.apache.camel.spring.processor.SpringTestHelper;
+
+/**
+ * Test access to beans defined in Spring XML file from SpEL 
expressions/predicates.
+ */
+public class SpelSpringTest extends LanguageTestSupport {
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        return SpringTestHelper.createSpringCamelContext(this, 
"org/apache/camel/language/spel/SpelSpringTest-context.xml");
+    }
+
+    public void testSpelBeanExpressions() throws Exception {
+        assertExpression("#{@myDummy.foo == 'xyz'}", true);
+        assertExpression("#{@myDummy.bar == 789}", true);
+        assertExpression("#{@myDummy.bar.toString()}", "789");
+    }
+    
+    public void testSpelBeanPredicates() throws Exception {
+        assertPredicate("@myDummy.foo == 'xyz'");
+        assertPredicate("@myDummy.bar == 789");
+        assertPredicate("@myDummy.bar instanceof T(Integer)");
+    }
+
+    protected String getLanguageName() {
+        return "spel";
+    }
+}

Added: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelTest.java?rev=1069839&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelTest.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/SpelTest.java
 Fri Feb 11 15:34:50 2011
@@ -0,0 +1,55 @@
+/**
+ * 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.spel;
+
+import org.apache.camel.LanguageTestSupport;
+
+public class SpelTest extends LanguageTestSupport {
+
+    public void testSpelExpressions() throws Exception {
+        assertExpression("#{exchange}", exchange);
+        assertExpression("#{exchange.getIn().body}", "<hello 
id='m123'>world!</hello>");
+        assertExpression("#{getRequest().body}", "<hello 
id='m123'>world!</hello>");
+        assertExpression("#{request.body}", "<hello id='m123'>world!</hello>");
+        assertExpression("#{request.Headers['foo']}", "abc");
+        assertExpression("#{getRequest().Headers['foo']}", "abc");
+        assertExpression("#{request.Headers['foo'] == 'abc'}", true);
+        assertExpression("#{request.headers['bar'] == 123}", true);
+        assertExpression("#{request.headers['bar'] > 10}", true);
+        assertExpression("#{6 / -3}", -2);
+    }
+
+    public void testSpelPredicates() throws Exception {
+        assertPredicate("#{request.headers['foo'].startsWith('a')}");
+        assertPredicate("#{request.headers['foo'] == 'abc'}");
+        assertPredicateFails("#{request.headers['foo'] == 'badString'}");
+    }
+    
+    public void testGetOutFalseKeepsNullOutMessage() throws Exception {
+        assertExpression("exchange.hasOut()", false);
+        assertFalse(exchange.hasOut());
+    }
+
+    public void testResponseCreatesOutMessage() throws Exception {
+        assertExpression("#{response.body}", null);
+        assertTrue(exchange.hasOut());
+    }
+
+    protected String getLanguageName() {
+        return "spel";
+    }
+}

Added: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/bean/Dummy.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/bean/Dummy.java?rev=1069839&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/bean/Dummy.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/spel/bean/Dummy.java
 Fri Feb 11 15:34:50 2011
@@ -0,0 +1,38 @@
+/**
+ * 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.spel.bean;
+
+public class Dummy {
+    private String foo = "xyz";
+    private int bar = 789;
+
+    public String getFoo() {
+        return foo;
+    }
+
+    public void setFoo(String foo) {
+        this.foo = foo;
+    }
+
+    public int getBar() {
+        return bar;
+    }
+
+    public void setBar(int bar) {
+        this.bar = bar;
+    }
+}

Added: 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/spel/SpelSpringTest-context.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/spel/SpelSpringTest-context.xml?rev=1069839&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/spel/SpelSpringTest-context.xml
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/spel/SpelSpringTest-context.xml
 Fri Feb 11 15:34:50 2011
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd";>
+
+    <bean id="myDummy" class="org.apache.camel.language.spel.bean.Dummy"/>
+
+</beans>
\ No newline at end of file


Reply via email to