You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/12/25 15:23:12 UTC

[1/2] camel git commit: CAMEL-9207: Add collate function to simplage language. It works on the message body only (for now)

Repository: camel
Updated Branches:
  refs/heads/master 2d2134725 -> 13e3a7e0f


CAMEL-9207: Add collate function to simplage language. It works on the message body only (for now)


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/982df580
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/982df580
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/982df580

Branch: refs/heads/master
Commit: 982df580dde606b5dc551ee1457cb4ed03076c4f
Parents: 2d21347
Author: Claus Ibsen <da...@apache.org>
Authored: Fri Dec 25 15:16:20 2015 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Fri Dec 25 15:16:20 2015 +0100

----------------------------------------------------------------------
 .../apache/camel/builder/ExpressionBuilder.java |  25 ++-
 .../simple/ast/SimpleFunctionExpression.java    |  54 +++---
 .../org/apache/camel/util/GroupIterator.java    |  73 ++------
 .../apache/camel/util/GroupTokenIterator.java   | 175 +++++++++++++++++++
 .../org/apache/camel/LanguageTestSupport.java   |  20 ++-
 .../camel/language/simple/SimpleTest.java       |  57 ++++++
 .../apache/camel/util/GroupIteratorTest.java    |  93 ----------
 .../camel/util/GroupTokenIteratorTest.java      |  93 ++++++++++
 8 files changed, 409 insertions(+), 181 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/982df580/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
index 790ca36..e0ef026 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
@@ -54,6 +54,7 @@ import org.apache.camel.support.XMLTokenExpressionIterator;
 import org.apache.camel.util.ExchangeHelper;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.GroupIterator;
+import org.apache.camel.util.GroupTokenIterator;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.OgnlHelper;
@@ -1344,7 +1345,11 @@ public final class ExpressionBuilder {
                 // evaluate expression as iterator
                 Iterator<?> it = expression.evaluate(exchange, Iterator.class);
                 ObjectHelper.notNull(it, "expression: " + expression + " evaluated on " + exchange + " must return an java.util.Iterator");
-                return new GroupIterator(exchange, it, token, group);
+                if (token != null) {
+                    return new GroupTokenIterator(exchange, it, token, group);
+                } else {
+                    return new GroupIterator(exchange, it, group);
+                }
             }
 
             @Override
@@ -1962,6 +1967,24 @@ public final class ExpressionBuilder {
     }
 
     /**
+     * Returns an iterator to collate (iterate) the given expression
+     */
+    public static Expression collateExpression(final String expression, final int group) {
+        return new ExpressionAdapter() {
+            public Object evaluate(Exchange exchange) {
+                // use simple language
+                Expression exp = exchange.getContext().resolveLanguage("simple").createExpression(expression);
+                return ExpressionBuilder.groupIteratorExpression(exp, null, group).evaluate(exchange, Object.class);
+            }
+
+            @Override
+            public String toString() {
+                return "collate(" + expression + "," + group + ")";
+            }
+        };
+    }
+
+    /**
      * Expression adapter for OGNL expression from Message Header or Exchange property
      */
     private static class KeyedOgnlExpressionAdapter extends ExpressionAdapter {

http://git-wip-us.apache.org/repos/asf/camel/blob/982df580/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
index d2fd1ca..be5e4ac 100644
--- a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
+++ b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
@@ -206,6 +206,39 @@ public class SimpleFunctionExpression extends LiteralExpression {
             return ExpressionBuilder.cacheExpression(exp);
         }
 
+        // random
+        remainder = ifStartsWithReturnRemainder("random", function);
+        if (remainder != null) {
+            String values = ObjectHelper.between(remainder, "(", ")");
+            if (values == null || ObjectHelper.isEmpty(values)) {
+                throw new SimpleParserException("Valid syntax: ${random(min,max)} or ${random(max)} was: " + function, token.getIndex());
+            }
+            if (values.contains(",")) {
+                String[] tokens = values.split(",", -1);
+                if (tokens.length > 2) {
+                    throw new SimpleParserException("Valid syntax: ${random(min,max)} or ${random(max)} was: " + function, token.getIndex());
+                }
+                int min = Integer.parseInt(tokens[0].trim());
+                int max = Integer.parseInt(tokens[1].trim());
+                return ExpressionBuilder.randomExpression(min, max);
+            } else {
+                int max = Integer.parseInt(values.trim());
+                return ExpressionBuilder.randomExpression(max);
+            }
+        }
+
+        // collate function
+        remainder = ifStartsWithReturnRemainder("collate", function);
+        if (remainder != null) {
+            String values = ObjectHelper.between(remainder, "(", ")");
+            if (values == null || ObjectHelper.isEmpty(values)) {
+                throw new SimpleParserException("Valid syntax: ${collate(group)} was: " + function, token.getIndex());
+            }
+            String exp = "${body}";
+            int num = Integer.parseInt(values.trim());
+            return ExpressionBuilder.collateExpression(exp, num);
+        }
+
         if (strict) {
             throw new SimpleParserException("Unknown function: " + function, token.getIndex());
         } else {
@@ -321,27 +354,6 @@ public class SimpleFunctionExpression extends LiteralExpression {
         if (remainder != null) {
             return ExpressionBuilder.outHeaderExpression(remainder);
         }
-        
-        // random
-        remainder = ifStartsWithReturnRemainder("random", function);
-        if (remainder != null) {
-            String values = ObjectHelper.between(remainder, "(", ")");
-            if (values == null || ObjectHelper.isEmpty(values)) {
-                throw new SimpleParserException("Valid syntax: ${random(min,max)} or ${random(max)} was: " + function, token.getIndex());
-            }
-            if (values.contains(",")) {
-                String[] tokens = values.split(",", -1);
-                if (tokens.length > 2) {
-                    throw new SimpleParserException("Valid syntax: ${random(min,max)} or ${random(max)} was: " + function, token.getIndex());
-                }
-                int min = Integer.parseInt(tokens[0].trim());
-                int max = Integer.parseInt(tokens[1].trim());
-                return ExpressionBuilder.randomExpression(min, max);
-            } else {
-                int max = Integer.parseInt(values.trim());
-                return ExpressionBuilder.randomExpression(max);
-            }
-        }
 
         return null;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/982df580/camel-core/src/main/java/org/apache/camel/util/GroupIterator.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/GroupIterator.java b/camel-core/src/main/java/org/apache/camel/util/GroupIterator.java
index b241d18..0ccced1 100644
--- a/camel-core/src/main/java/org/apache/camel/util/GroupIterator.java
+++ b/camel-core/src/main/java/org/apache/camel/util/GroupIterator.java
@@ -16,72 +16,44 @@
  */
 package org.apache.camel.util;
 
-import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.IOException;
-import java.io.InputStream;
+import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Scanner;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
-import org.apache.camel.NoTypeConversionAvailableException;
 
 /**
  * Group based {@link Iterator} which groups the given {@link Iterator} a number of times
- * and then return a combined response as a String.
+ * and then return a combined response as a <tt>List</tt>.
  * <p/>
- * This implementation uses as internal byte array buffer, to combine the response.
- * The token is inserted between the individual parts.
- * <p/>
- * For example if you group by new line, then a new line token is inserted between the lines.
+ * This implementation uses a internal array list, to combine the response.
+ *
+ * @see GroupTokenIterator
  */
 public final class GroupIterator implements Iterator<Object>, Closeable {
 
     private final CamelContext camelContext;
     private final Exchange exchange;
     private final Iterator<?> it;
-    private final String token;
     private final int group;
     private boolean closed;
-    private final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-    
-    /**
-     * Creates a new group iterator
-     *
-     * @param camelContext  the camel context
-     * @param it            the iterator to group
-     * @param token         then token used to separate between the parts, use <tt>null</tt> to not add the token
-     * @param group         number of parts to group together
-     * @throws IllegalArgumentException is thrown if group is not a positive number
-     * @deprecated  Please use GroupIterator(Exchange exchange, Iterator<?> it, String token, int group) instead
-     */
-    @Deprecated 
-    public GroupIterator(CamelContext camelContext, Iterator<?> it, String token, int group) {
-        this.exchange = null;
-        this.camelContext = camelContext;
-        this.it = it;
-        this.token = token;
-        this.group = group;
-        if (group <= 0) {
-            throw new IllegalArgumentException("Group must be a positive number, was: " + group);
-        }
-    }
 
     /**
      * Creates a new group iterator
      *
      * @param exchange  the exchange used to create this group iterator
      * @param it            the iterator to group
-     * @param token         then token used to separate between the parts, use <tt>null</tt> to not add the token
      * @param group         number of parts to group together
      * @throws IllegalArgumentException is thrown if group is not a positive number
      */
-    public GroupIterator(Exchange exchange, Iterator<?> it, String token, int group) {
+    public GroupIterator(Exchange exchange, Iterator<?> it, int group) {
         this.exchange = exchange;
         this.camelContext = exchange.getContext();
         this.it = it;
-        this.token = token;
         this.group = group;
         if (group <= 0) {
             throw new IllegalArgumentException("Group must be a positive number, was: " + group);
@@ -103,8 +75,6 @@ public final class GroupIterator implements Iterator<Object>, Closeable {
                 IOHelper.closeWithException((Closeable) it);
             }
         } finally {
-            // close the buffer as well
-            bos.close();
             // we are now closed
             closed = true;
         }
@@ -137,35 +107,16 @@ public final class GroupIterator implements Iterator<Object>, Closeable {
         }
     }
 
-    private Object doNext() throws IOException, NoTypeConversionAvailableException {
+    private Object doNext() throws IOException {
+        List<Object> list = new ArrayList<Object>();
         int count = 0;
-        Object data = "";
         while (count < group && it.hasNext()) {
-            data = it.next();
-
-            // include token in between
-            if (data != null && count > 0 && token != null) {
-                bos.write(token.getBytes());
-            }
-            if (data instanceof InputStream) {
-                InputStream is = (InputStream) data;
-                IOHelper.copy(is, bos);
-            } else if (data instanceof byte[]) {
-                byte[] bytes = (byte[]) data;
-                bos.write(bytes);
-            } else if (data != null) {
-                // convert to input stream
-                InputStream is = camelContext.getTypeConverter().mandatoryConvertTo(InputStream.class, data);
-                IOHelper.copy(is, bos);
-            }
-
+            Object data = it.next();
+            list.add(data);
             count++;
         }
 
-        // prepare and return answer as String using exchange's charset
-        String answer = bos.toString(IOHelper.getCharsetName(exchange));
-        bos.reset();
-        return answer;
+        return list;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/982df580/camel-core/src/main/java/org/apache/camel/util/GroupTokenIterator.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/GroupTokenIterator.java b/camel-core/src/main/java/org/apache/camel/util/GroupTokenIterator.java
new file mode 100644
index 0000000..4eb50e1
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/util/GroupTokenIterator.java
@@ -0,0 +1,175 @@
+/**
+ * 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.util;
+
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Iterator;
+import java.util.Scanner;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.NoTypeConversionAvailableException;
+
+/**
+ * Group based {@link Iterator} which groups the given {@link Iterator} a number of times
+ * and then return a combined response as a String.
+ * <p/>
+ * This implementation uses an internal byte array buffer, to combine the response.
+ * The token is inserted between the individual parts.
+ * <p/>
+ * For example if you group by new line, then a new line token is inserted between the lines.
+ */
+public final class GroupTokenIterator implements Iterator<Object>, Closeable {
+
+    private final CamelContext camelContext;
+    private final Exchange exchange;
+    private final Iterator<?> it;
+    private final String token;
+    private final int group;
+    private boolean closed;
+    private final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+    
+    /**
+     * Creates a new token based group titerator
+     *
+     * @param camelContext  the camel context
+     * @param it            the iterator to group
+     * @param token         then token used to separate between the parts, use <tt>null</tt> to not add the token
+     * @param group         number of parts to group together
+     * @throws IllegalArgumentException is thrown if group is not a positive number
+     * @deprecated  Please use GroupIterator(Exchange exchange, Iterator<?> it, String token, int group) instead
+     */
+    @Deprecated 
+    public GroupTokenIterator(CamelContext camelContext, Iterator<?> it, String token, int group) {
+        this.exchange = null;
+        this.camelContext = camelContext;
+        this.it = it;
+        this.token = token;
+        this.group = group;
+        if (group <= 0) {
+            throw new IllegalArgumentException("Group must be a positive number, was: " + group);
+        }
+    }
+
+    /**
+     * Creates a new token based group iterator
+     *
+     * @param exchange      the exchange used to create this group iterator
+     * @param it            the iterator to group
+     * @param token         then token used to separate between the parts, use <tt>null</tt> to not add the token
+     * @param group         number of parts to group together
+     * @throws IllegalArgumentException is thrown if group is not a positive number
+     */
+    public GroupTokenIterator(Exchange exchange, Iterator<?> it, String token, int group) {
+        this.exchange = exchange;
+        this.camelContext = exchange.getContext();
+        this.it = it;
+        this.token = token;
+        this.group = group;
+        if (group <= 0) {
+            throw new IllegalArgumentException("Group must be a positive number, was: " + group);
+        }
+    }
+
+    @Override
+    public void close() throws IOException {
+        try {
+            if (it instanceof Scanner) {
+                // special for Scanner which implement the Closeable since JDK7 
+                Scanner scanner = (Scanner) it;
+                scanner.close();
+                IOException ioException = scanner.ioException();
+                if (ioException != null) {
+                    throw ioException;
+                }
+            } else if (it instanceof Closeable) {
+                IOHelper.closeWithException((Closeable) it);
+            }
+        } finally {
+            // close the buffer as well
+            bos.close();
+            // we are now closed
+            closed = true;
+        }
+    }
+
+    @Override
+    public boolean hasNext() {
+        if (closed) {
+            return false;
+        }
+
+        boolean answer = it.hasNext();
+        if (!answer) {
+            // auto close
+            try {
+                close();
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+        return answer;
+    }
+
+    @Override
+    public Object next() {
+        try {
+            return doNext();
+        } catch (Exception e) {
+            throw ObjectHelper.wrapRuntimeCamelException(e);
+        }
+    }
+
+    private Object doNext() throws IOException, NoTypeConversionAvailableException {
+        int count = 0;
+        Object data = "";
+        while (count < group && it.hasNext()) {
+            data = it.next();
+
+            // include token in between
+            if (data != null && count > 0 && token != null) {
+                bos.write(token.getBytes());
+            }
+            if (data instanceof InputStream) {
+                InputStream is = (InputStream) data;
+                IOHelper.copy(is, bos);
+            } else if (data instanceof byte[]) {
+                byte[] bytes = (byte[]) data;
+                bos.write(bytes);
+            } else if (data != null) {
+                // convert to input stream
+                InputStream is = camelContext.getTypeConverter().mandatoryConvertTo(InputStream.class, data);
+                IOHelper.copy(is, bos);
+            }
+
+            count++;
+        }
+
+        // prepare and return answer as String using exchange's charset
+        String answer = bos.toString(IOHelper.getCharsetName(exchange));
+        bos.reset();
+        return answer;
+    }
+
+    @Override
+    public void remove() {
+        it.remove();
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/982df580/camel-core/src/test/java/org/apache/camel/LanguageTestSupport.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/LanguageTestSupport.java b/camel-core/src/test/java/org/apache/camel/LanguageTestSupport.java
index 6b4764f..92ad5d1 100644
--- a/camel-core/src/test/java/org/apache/camel/LanguageTestSupport.java
+++ b/camel-core/src/test/java/org/apache/camel/LanguageTestSupport.java
@@ -68,16 +68,26 @@ public abstract class LanguageTestSupport extends ExchangeTestSupport {
     protected void assertExpression(String expressionText, Object expectedValue) {
         assertExpression(exchange, expressionText, expectedValue);
     }
-    
+
     /**
      * Asserts that the expression evaluates to one of the two given values
      */
     protected void assertExpression(String expressionText, String expectedValue, String orThisExpectedValue) {
+        Object value = evaluateExpression(expressionText, expectedValue);
+
+        assertTrue("Expression: " + expressionText + " on Exchange: " + exchange,
+                   expectedValue.equals(value) || orThisExpectedValue.equals(value));
+    }
+
+    /**
+     * Evaluates the expression
+     */
+    protected Object evaluateExpression(String expressionText, String expectedValue) {
         Language language = assertResolveLanguage(getLanguageName());
 
         Expression expression = language.createExpression(expressionText);
         assertNotNull("No Expression could be created for text: " + expressionText + " language: " + language, expression);
-        
+
         Object value;
         if (expectedValue != null) {
             value = expression.evaluate(exchange, expectedValue.getClass());
@@ -87,7 +97,7 @@ public abstract class LanguageTestSupport extends ExchangeTestSupport {
 
         log.debug("Evaluated expression: " + expression + " on exchange: " + exchange + " result: " + value);
 
-        assertTrue("Expression: " + expression + " on Exchange: " + exchange, 
-                   expectedValue.equals(value) || orThisExpectedValue.equals(value));
-    }    
+        return value;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/982df580/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index 95c7455..b13ccb1 100644
--- a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -1439,7 +1440,63 @@ public class SimpleTest extends LanguageTestSupport {
         exchange.getIn().setBody("87444549697");
         assertPredicate("${body} regex '^(tel:\\+)(974)(44)(\\d+)|^(974)(44)(\\d+)'", false);
     }
+
+    public void testCollateEven() throws Exception {
+        List<Object> data = new ArrayList<Object>();
+        data.add("A");
+        data.add("B");
+        data.add("C");
+        data.add("D");
+        data.add("E");
+        data.add("F");
+        exchange.getIn().setBody(data);
+
+        Iterator it = (Iterator) evaluateExpression("${collate(3)}", null);
+        List chunk = (List) it.next();
+        List chunk2 = (List) it.next();
+        assertFalse(it.hasNext());
+
+        assertEquals(3, chunk.size());
+        assertEquals(3, chunk2.size());
+
+        assertEquals("A", chunk.get(0));
+        assertEquals("B", chunk.get(1));
+        assertEquals("C", chunk.get(2));
+        assertEquals("D", chunk2.get(0));
+        assertEquals("E", chunk2.get(1));
+        assertEquals("F", chunk2.get(2));
+    }
     
+    public void testCollateOdd() throws Exception {
+        List<Object> data = new ArrayList<Object>();
+        data.add("A");
+        data.add("B");
+        data.add("C");
+        data.add("D");
+        data.add("E");
+        data.add("F");
+        data.add("G");
+        exchange.getIn().setBody(data);
+
+        Iterator it = (Iterator) evaluateExpression("${collate(3)}", null);
+        List chunk = (List) it.next();
+        List chunk2 = (List) it.next();
+        List chunk3 = (List) it.next();
+        assertFalse(it.hasNext());
+
+        assertEquals(3, chunk.size());
+        assertEquals(3, chunk2.size());
+        assertEquals(1, chunk3.size());
+
+        assertEquals("A", chunk.get(0));
+        assertEquals("B", chunk.get(1));
+        assertEquals("C", chunk.get(2));
+        assertEquals("D", chunk2.get(0));
+        assertEquals("E", chunk2.get(1));
+        assertEquals("F", chunk2.get(2));
+        assertEquals("G", chunk3.get(0));
+    }
+
     public void testRandomExpression() throws Exception {
         int min = 1;
         int max = 10;

http://git-wip-us.apache.org/repos/asf/camel/blob/982df580/camel-core/src/test/java/org/apache/camel/util/GroupIteratorTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/GroupIteratorTest.java b/camel-core/src/test/java/org/apache/camel/util/GroupIteratorTest.java
deleted file mode 100644
index 2aeef2f..0000000
--- a/camel-core/src/test/java/org/apache/camel/util/GroupIteratorTest.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/**
- * 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.util;
-
-import java.io.ByteArrayInputStream;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.util.Scanner;
-
-import org.apache.camel.CamelContext;
-import org.apache.camel.Exchange;
-import org.apache.camel.TestSupport;
-import org.apache.camel.impl.DefaultCamelContext;
-import org.apache.camel.impl.DefaultExchange;
-
-/**
- *
- */
-public class GroupIteratorTest extends TestSupport {
-
-    private CamelContext context;
-    private Exchange exchange;
-
-    @Override
-    public void setUp() throws Exception {
-        super.setUp();
-        context = new DefaultCamelContext();
-        context.start();
-        exchange = new DefaultExchange(context);
-
-    }
-
-    @Override
-    public void tearDown() throws Exception {
-        context.stop();
-        super.tearDown();
-    }
-
-    public void testGroupIterator() throws Exception {
-        String s = "ABC\nDEF\nGHI\nJKL\nMNO\nPQR\nSTU\nVW";
-        Scanner scanner = new Scanner(s);
-        scanner.useDelimiter("\n");
-
-        GroupIterator gi = new GroupIterator(exchange, scanner, "\n", 3);
-
-        assertTrue(gi.hasNext());
-        assertEquals("ABC\nDEF\nGHI", gi.next());
-        assertEquals("JKL\nMNO\nPQR", gi.next());
-        assertEquals("STU\nVW", gi.next());
-        assertFalse(gi.hasNext());
-
-        IOHelper.close(gi);
-    }
-
-    public void testGroupIteratorWithDifferentEncodingFromDefault() throws Exception {
-        if (Charset.defaultCharset() == StandardCharsets.UTF_8) {
-            // can't think of test case where having default charset set to UTF-8 is affected
-            return;
-        }
-
-        byte[] buf = "\u00A31\n\u00A32\n".getBytes(StandardCharsets.UTF_8);
-
-        ByteArrayInputStream in = new ByteArrayInputStream(buf);
-
-        Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.displayName());
-        scanner.useDelimiter("\n");
-
-        exchange.setProperty(Exchange.CHARSET_NAME, StandardCharsets.UTF_8.displayName());
-        GroupIterator gi = new GroupIterator(exchange, scanner, "\n", 1);
-
-        assertTrue(gi.hasNext());
-        assertEquals("\u00A31", gi.next());
-        assertEquals("\u00A32", gi.next());
-        assertFalse(gi.hasNext());
-
-        IOHelper.close(gi);
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/982df580/camel-core/src/test/java/org/apache/camel/util/GroupTokenIteratorTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/GroupTokenIteratorTest.java b/camel-core/src/test/java/org/apache/camel/util/GroupTokenIteratorTest.java
new file mode 100644
index 0000000..ddedb2c
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/util/GroupTokenIteratorTest.java
@@ -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.util;
+
+import java.io.ByteArrayInputStream;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Scanner;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.TestSupport;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.DefaultExchange;
+
+/**
+ *
+ */
+public class GroupTokenIteratorTest extends TestSupport {
+
+    private CamelContext context;
+    private Exchange exchange;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        context = new DefaultCamelContext();
+        context.start();
+        exchange = new DefaultExchange(context);
+
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        context.stop();
+        super.tearDown();
+    }
+
+    public void testGroupIterator() throws Exception {
+        String s = "ABC\nDEF\nGHI\nJKL\nMNO\nPQR\nSTU\nVW";
+        Scanner scanner = new Scanner(s);
+        scanner.useDelimiter("\n");
+
+        GroupTokenIterator gi = new GroupTokenIterator(exchange, scanner, "\n", 3);
+
+        assertTrue(gi.hasNext());
+        assertEquals("ABC\nDEF\nGHI", gi.next());
+        assertEquals("JKL\nMNO\nPQR", gi.next());
+        assertEquals("STU\nVW", gi.next());
+        assertFalse(gi.hasNext());
+
+        IOHelper.close(gi);
+    }
+
+    public void testGroupIteratorWithDifferentEncodingFromDefault() throws Exception {
+        if (Charset.defaultCharset() == StandardCharsets.UTF_8) {
+            // can't think of test case where having default charset set to UTF-8 is affected
+            return;
+        }
+
+        byte[] buf = "\u00A31\n\u00A32\n".getBytes(StandardCharsets.UTF_8);
+
+        ByteArrayInputStream in = new ByteArrayInputStream(buf);
+
+        Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.displayName());
+        scanner.useDelimiter("\n");
+
+        exchange.setProperty(Exchange.CHARSET_NAME, StandardCharsets.UTF_8.displayName());
+        GroupTokenIterator gi = new GroupTokenIterator(exchange, scanner, "\n", 1);
+
+        assertTrue(gi.hasNext());
+        assertEquals("\u00A31", gi.next());
+        assertEquals("\u00A32", gi.next());
+        assertFalse(gi.hasNext());
+
+        IOHelper.close(gi);
+    }
+
+}


[2/2] camel git commit: CAMEL-9207: Add collate function to simplage language. It works on the message body only (for now)

Posted by da...@apache.org.
CAMEL-9207: Add collate function to simplage language. It works on the message body only (for now)


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/13e3a7e0
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/13e3a7e0
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/13e3a7e0

Branch: refs/heads/master
Commit: 13e3a7e0f4bedccc0813b0efd46df41034265529
Parents: 982df58
Author: Claus Ibsen <da...@apache.org>
Authored: Fri Dec 25 15:21:50 2015 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Fri Dec 25 15:21:50 2015 +0100

----------------------------------------------------------------------
 .../camel/processor/SplitterCollateTest.java    | 64 ++++++++++++++++++++
 1 file changed, 64 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/13e3a7e0/camel-core/src/test/java/org/apache/camel/processor/SplitterCollateTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/SplitterCollateTest.java b/camel-core/src/test/java/org/apache/camel/processor/SplitterCollateTest.java
new file mode 100644
index 0000000..850b7c1
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/SplitterCollateTest.java
@@ -0,0 +1,64 @@
+/**
+ * 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.processor;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+public class SplitterCollateTest extends ContextTestSupport {
+
+    public void testSplitterCollate() throws Exception {
+        getMockEndpoint("mock:line").expectedMessageCount(2);
+
+        List<Object> data = new ArrayList<Object>();
+        data.add("A");
+        data.add("B");
+        data.add("C");
+        data.add("D");
+        data.add("E");
+        template.sendBody("direct:start", data);
+
+        assertMockEndpointsSatisfied();
+
+        List chunk = getMockEndpoint("mock:line").getReceivedExchanges().get(0).getIn().getBody(List.class);
+        List chunk2 = getMockEndpoint("mock:line").getReceivedExchanges().get(1).getIn().getBody(List.class);
+
+        assertEquals(3, chunk.size());
+        assertEquals(2, chunk2.size());
+
+        assertEquals("A", chunk.get(0));
+        assertEquals("B", chunk.get(1));
+        assertEquals("C", chunk.get(2));
+        assertEquals("D", chunk2.get(0));
+        assertEquals("E", chunk2.get(1));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .split(simple("${collate(3)}"))
+                        .to("mock:line");
+            }
+        };
+    }
+}