camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1195142 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/model/language/ main/java/org/apache/camel/support/ test/java/org/apache/camel/language/
Date Sun, 30 Oct 2011 10:12:34 GMT
Author: davsclaus
Date: Sun Oct 30 10:12:33 2011
New Revision: 1195142

URL: http://svn.apache.org/viewvc?rev=1195142&view=rev
Log:
CAMEL-4595: Polished

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/TokenPairPredicateTest.java
      - copied, changed from r1195125, camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathFromFileExceptionTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/TokenizerExpression.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/support/TokenPairExpressionIterator.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/TokenizerTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/TokenizerExpression.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/TokenizerExpression.java?rev=1195142&r1=1195141&r2=1195142&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/TokenizerExpression.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/TokenizerExpression.java
Sun Oct 30 10:12:33 2011
@@ -23,7 +23,9 @@ import javax.xml.bind.annotation.XmlRoot
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
 import org.apache.camel.language.tokenizer.TokenizeLanguage;
+import org.apache.camel.util.ExpressionToPredicateAdapter;
 
 /**
  * For expressions and predicates using a body or header tokenizer.
@@ -95,6 +97,12 @@ public class TokenizerExpression extends
     }
 
     @Override
+    public Predicate createPredicate(CamelContext camelContext) {
+        Expression exp = createExpression(camelContext);
+        return ExpressionToPredicateAdapter.toPredicate(exp);
+    }
+
+    @Override
     public String toString() {
         if (endToken != null) {
             return "tokenize{body() using tokens: " + token + "..." + endToken + "}";

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/support/TokenPairExpressionIterator.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/support/TokenPairExpressionIterator.java?rev=1195142&r1=1195141&r2=1195142&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/support/TokenPairExpressionIterator.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/support/TokenPairExpressionIterator.java
Sun Oct 30 10:12:33 2011
@@ -24,6 +24,7 @@ import java.util.Scanner;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 
 /**
@@ -51,7 +52,9 @@ public class TokenPairExpressionIterator
     public Object evaluate(Exchange exchange) {
         try {
             InputStream in = exchange.getIn().getMandatoryBody(InputStream.class);
-            return new TokenPairIterator(startToken, endToken, in);
+            // we may read from a file, and want to support custom charset defined on the
exchange
+            String charset = IOHelper.getCharsetName(exchange);
+            return new TokenPairIterator(startToken, endToken, in, charset);
         } catch (InvalidPayloadException e) {
             exchange.setException(e);
             return null;
@@ -73,11 +76,11 @@ public class TokenPairExpressionIterator
         private final Scanner scanner;
         private Object image;
 
-        private TokenPairIterator(String startToken, String endToken, InputStream in) {
+        private TokenPairIterator(String startToken, String endToken, InputStream in, String
charset) {
             this.startToken = startToken;
             this.endToken = endToken;
             // use end token as delimiter
-            this.scanner = new Scanner(in).useDelimiter(endToken);
+            this.scanner = new Scanner(in, charset).useDelimiter(endToken);
             // this iterator will do look ahead as we may have data
             // after the last end token, which the scanner would find
             // so we need to be one step ahead of the scanner

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/language/TokenPairPredicateTest.java
(from r1195125, camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathFromFileExceptionTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/TokenPairPredicateTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/language/TokenPairPredicateTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathFromFileExceptionTest.java&r1=1195125&r2=1195142&rev=1195142&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/XPathFromFileExceptionTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/TokenPairPredicateTest.java
Sun Oct 30 10:12:33 2011
@@ -25,46 +25,27 @@ import org.apache.camel.builder.RouteBui
 /**
  *
  */
-public class XPathFromFileExceptionTest extends ContextTestSupport {
+public class TokenPairPredicateTest extends ContextTestSupport {
 
     @Override
     protected void setUp() throws Exception {
-        deleteDirectory("target/xpath");
+        deleteDirectory("target/pair");
         super.setUp();
     }
 
-    public void testXPathFromFileExceptionOk() throws Exception {
+    public void testTokenPairPredicate() throws Exception {
         getMockEndpoint("mock:result").expectedMessageCount(1);
-        getMockEndpoint("mock:error").expectedMessageCount(0);
 
-        template.sendBodyAndHeader("file:target/xpath", "<hello>world!</hello>",
Exchange.FILE_NAME, "hello.xml");
+        template.sendBodyAndHeader("file:target/pair", "<hello>world!</hello>",
Exchange.FILE_NAME, "hello.xml");
 
         assertMockEndpointsSatisfied();
 
         oneExchangeDone.matchesMockWaitTime();
 
-        File file = new File("target/xpath/hello.xml");
+        File file = new File("target/pair/hello.xml");
         assertFalse("File should not exists " + file, file.exists());
 
-        file = new File("target/xpath/ok/hello.xml");
-        assertTrue("File should exists " + file, file.exists());
-    }
-
-    public void testXPathFromFileExceptionFail() throws Exception {
-        getMockEndpoint("mock:result").expectedMessageCount(0);
-        getMockEndpoint("mock:error").expectedMessageCount(1);
-
-        // the last tag is not ended properly
-        template.sendBodyAndHeader("file:target/xpath", "<hello>world!</hello",
Exchange.FILE_NAME, "hello2.xml");
-
-        assertMockEndpointsSatisfied();
-
-        oneExchangeDone.matchesMockWaitTime();
-        
-        File file = new File("target/xpath/hello2.xml");
-        assertFalse("File should not exists " + file, file.exists());
-
-        file = new File("target/xpath/error/hello2.xml");
+        file = new File("target/pair/ok/hello.xml");
         assertTrue("File should exists " + file, file.exists());
     }
 
@@ -73,12 +54,11 @@ public class XPathFromFileExceptionTest 
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("file:target/xpath?moveFailed=error&move=ok")
-                    .onException(Exception.class)
-                        .to("mock:error")
-                    .end()
+                from("file:target/pair?move=ok")
                     .choice()
-                        .when().xpath("/hello").to("mock:result")
+                        // does not make so much sense to use a tokenPair in a predicate
+                        // but you can do it nevertheless
+                        .when().tokenizePair("<hello>", "</hello>").to("mock:result")
                     .end();
             }
         };

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/language/TokenizerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/TokenizerTest.java?rev=1195142&r1=1195141&r2=1195142&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/TokenizerTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/TokenizerTest.java Sun
Oct 30 10:12:33 2011
@@ -143,6 +143,15 @@ public class TokenizerTest extends Excha
         exchange.getIn().setBody("");
 
         List names = exp.evaluate(exchange, List.class);
+        assertEquals(0, names.size());
+    }
+
+    public void testTokenizePairNullData() throws Exception {
+        Expression exp = TokenizeLanguage.tokenizePair("<person>", "</person>");
+
+        exchange.getIn().setBody(null);
+
+        List names = exp.evaluate(exchange, List.class);
         assertNull(names);
     }
 



Mime
View raw message