You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ge...@apache.org on 2010/10/11 13:14:31 UTC

svn commit: r1021305 - in /camel/trunk: camel-core/src/test/java/org/apache/camel/processor/ components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/ components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/ components/camel-sc...

Author: gertv
Date: Mon Oct 11 11:14:30 2010
New Revision: 1021305

URL: http://svn.apache.org/viewvc?rev=1021305&view=rev
Log:
CAMEL-463: Adding support for validate() DSL method

Added:
    camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/SValidateTest.scala
Modified:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateRegExpTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateSimpleTest.java
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractDefinition.scala
    camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateRegExpTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateRegExpTest.java?rev=1021305&r1=1021304&r2=1021305&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateRegExpTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateRegExpTest.java Mon Oct 11 11:14:30 2010
@@ -28,8 +28,8 @@ import org.apache.camel.processor.valida
  */
 public class ValidateRegExpTest extends ContextTestSupport {
     
-    private Endpoint startEndpoint;
-    private MockEndpoint resultEndpoint;
+    protected Endpoint startEndpoint;
+    protected MockEndpoint resultEndpoint;
     
     @Override
     protected void setUp() throws Exception {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateSimpleTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateSimpleTest.java?rev=1021305&r1=1021304&r2=1021305&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateSimpleTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ValidateSimpleTest.java Mon Oct 11 11:14:30 2010
@@ -28,8 +28,8 @@ import org.apache.camel.processor.valida
  */
 public class ValidateSimpleTest extends ContextTestSupport {
 
-    private Endpoint startEndpoint;
-    private MockEndpoint resultEndpoint;
+    protected Endpoint startEndpoint;
+    protected MockEndpoint resultEndpoint;
 
     @Override
     protected void setUp() throws Exception {

Modified: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala?rev=1021305&r1=1021304&r2=1021305&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala (original)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/DSL.scala Mon Oct 11 11:14:30 2010
@@ -92,6 +92,9 @@ trait DSL {
   def transform(expression: Exchange => Any) : DSL
 
   def unmarshal(format: DataFormatDefinition) : DSL
+
+  def validate(expression: Exchange => Any) : DSL
+
   def when(filter: Exchange => Any) : DSL with Block
   
   def wiretap(uri: String) : DSL

Modified: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractDefinition.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractDefinition.scala?rev=1021305&r1=1021304&r2=1021305&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractDefinition.scala (original)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/SAbstractDefinition.scala Mon Oct 11 11:14:30 2010
@@ -156,7 +156,9 @@ abstract class SAbstractDefinition[P <: 
   def transform(expression: Exchange => Any) = wrap(target.transform(expression))
   
   def unmarshal(format: DataFormatDefinition) = wrap(target.unmarshal(format))
-  
+
+  def validate(expression: Exchange => Any) = wrap(target.validate(predicateBuilder(expression)))
+
   def wiretap(uri: String) = wrap(target.wireTap(uri))
   def wiretap(uri: String, expression: Exchange => Any) = wrap(target.wireTap(uri, expression))
   

Modified: camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala?rev=1021305&r1=1021304&r2=1021305&view=diff
==============================================================================
--- camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala (original)
+++ camel/trunk/components/camel-scala/src/main/scala/org/apache/camel/scala/dsl/builder/RouteBuilder.scala Mon Oct 11 11:14:30 2010
@@ -137,6 +137,7 @@ class RouteBuilder extends Preamble with
   def transacted(uri: String) = stack.top.transacted
   def transform(expression: Exchange => Any) = stack.top.transform(expression)
   def unmarshal(format: DataFormatDefinition) = stack.top.unmarshal(format)
+  def validate(expression: (Exchange) => Any) = stack.top.validate(expression)
   def wiretap(uri: String) = stack.top.wiretap(uri)
   def wiretap(uri: String, expression: Exchange => Any) = stack.top.wiretap(uri, expression)
   def aggregate(expression: Exchange => Any, strategy: AggregationStrategy) = stack.top.aggregate(expression, strategy)

Added: camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/SValidateTest.scala
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/SValidateTest.scala?rev=1021305&view=auto
==============================================================================
--- camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/SValidateTest.scala (added)
+++ camel/trunk/components/camel-scala/src/test/scala/org/apache/camel/scala/dsl/SValidateTest.scala Mon Oct 11 11:14:30 2010
@@ -0,0 +1,93 @@
+/**
+ * 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.scala.dsl;
+
+import builder.{RouteBuilder, RouteBuilderSupport}
+import org.apache.camel.processor.{ValidateRegExpTest, ValidateSimpleTest}
+import org.apache.camel.processor.validation.PredicateValidationException
+import org.apache.camel.CamelExecutionException
+import org.apache.camel.TestSupport.{assertIsInstanceOf}
+
+import junit.framework.Assert.{fail, assertTrue}
+
+/**
+ * Scala DSL equivalent for the ValidateSimpleTest, using simple one-line Scala DSL syntax
+ */
+class SValidateSimpleTest extends ValidateSimpleTest with RouteBuilderSupport {
+
+  // we need to override the test method because the validation exception looks slightly different in Scala
+  override def testSendNotMatchingMessage = {
+    resultEndpoint.expectedMessageCount(0);
+
+    try {
+      template.sendBody(startEndpoint, "1.1.2010");
+      fail("CamelExecutionException expected");
+    } catch {
+      case e: CamelExecutionException => {
+        // expected
+        assertIsInstanceOf(classOf[PredicateValidationException], e.getCause())
+        // as the Expression could be different between the DSL and simple language, here we just check part of the message
+        assertTrue("Get a wrong exception message",
+                   e.getCause().getMessage().startsWith("Validation failed for Predicate[org.apache.camel.scala.dsl.ScalaPredicate"));
+        assertTrue("Get a wrong exception message",
+                   e.getCause().getMessage().endsWith("Exchange[Message: 1.1.2010]"));
+      }
+    }
+
+    assertMockEndpointsSatisfied();
+  }  
+
+  override def createRouteBuilder = new RouteBuilder {
+    "direct:start" validate(simple("${body} contains 'Camel'")) to("mock:result")
+  }
+}
+
+/**
+ * Scala DSL equivalent for the ValidateRegExpTest, using the Scala DSL block syntax
+ */
+class SValidateRegExpTest extends ValidateRegExpTest with RouteBuilderSupport {
+
+  // we need to override the test method because the validation exception looks slightly different in Scala
+  override def testSendNotMatchingMessage = {
+    resultEndpoint.expectedMessageCount(0);
+
+    try {
+      template.sendBody(startEndpoint, "1.1.2010");
+      fail("CamelExecutionException expected");
+    } catch {
+      case e: CamelExecutionException => {
+        // expected
+        assertIsInstanceOf(classOf[PredicateValidationException], e.getCause())
+        // as the Expression could be different between the DSL and simple language, here we just check part of the message
+        assertTrue("Get a wrong exception message",
+                   e.getCause().getMessage().startsWith("Validation failed for Predicate[org.apache.camel.scala.dsl.ScalaPredicate"));
+        assertTrue("Get a wrong exception message",
+                   e.getCause().getMessage().endsWith("Exchange[Message: 1.1.2010]"));
+      }
+    }
+
+    assertMockEndpointsSatisfied();
+  }
+
+  override def createRouteBuilder = new RouteBuilder {
+
+    "direct:start" ==> {
+      validate(_.in[String].matches("^\\d{2}\\.\\d{2}\\.\\d{4}$"))
+      to("mock:result")
+    }
+  }
+}