camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [1/3] git commit: CAMEL-7876 ensure GroupIterator uses the exchange's CHARSET_NAME property
Date Sun, 28 Sep 2014 03:30:30 GMT
Repository: camel
Updated Branches:
  refs/heads/master bd06a4a5f -> c9e1bc87e


CAMEL-7876 ensure GroupIterator uses the exchange's CHARSET_NAME property


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

Branch: refs/heads/master
Commit: 80518e29d93745bcad341e5c02bef8a0e31ece61
Parents: bd06a4a
Author: Tom Ellis <tellisnz@gmail.com>
Authored: Sat Sep 27 17:27:33 2014 +0100
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Sun Sep 28 11:17:55 2014 +0800

----------------------------------------------------------------------
 .../apache/camel/builder/ExpressionBuilder.java |  2 +-
 .../org/apache/camel/util/GroupIterator.java    | 14 ++++----
 .../apache/camel/util/GroupIteratorTest.java    | 35 +++++++++++++++++++-
 3 files changed, 42 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/80518e29/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 4473fe7..20f00b9 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
@@ -1294,7 +1294,7 @@ 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.getContext(), it, token, group);
+                return new GroupIterator(exchange, it, token, group);
             }
 
             @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/80518e29/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 158dbed..4b6f9ad 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
@@ -23,7 +23,7 @@ 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;
 
 /**
@@ -37,7 +37,7 @@ import org.apache.camel.NoTypeConversionAvailableException;
  */
 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;
@@ -53,8 +53,8 @@ public final class GroupIterator implements Iterator<Object>, Closeable
{
      * @param group         number of parts to group together
      * @throws IllegalArgumentException is thrown if group is not a positive number
      */
-    public GroupIterator(CamelContext camelContext, Iterator<?> it, String token, int
group) {
-        this.camelContext = camelContext;
+    public GroupIterator(Exchange exchange, Iterator<?> it, String token, int group)
{
+        this.exchange = exchange;
         this.it = it;
         this.token = token;
         this.group = group;
@@ -130,15 +130,15 @@ public final class GroupIterator implements Iterator<Object>,
Closeable {
                 bos.write(bytes);
             } else if (data != null) {
                 // convert to input stream
-                InputStream is = camelContext.getTypeConverter().mandatoryConvertTo(InputStream.class,
data);
+                InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class,
data);
                 IOHelper.copy(is, bos);
             }
 
             count++;
         }
 
-        // prepare and return answer as String
-        String answer = bos.toString();
+        // prepare and return answer as String using exchange's charset
+        String answer = bos.toString(IOHelper.getCharsetName(exchange));
         bos.reset();
         return answer;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/80518e29/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
index 4dbbac5..c5c3e33 100644
--- a/camel-core/src/test/java/org/apache/camel/util/GroupIteratorTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/GroupIteratorTest.java
@@ -16,11 +16,17 @@
  */
 package org.apache.camel.util;
 
+import java.io.ByteArrayInputStream;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
 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;
 
 /**
  *
@@ -28,12 +34,15 @@ import org.apache.camel.impl.DefaultCamelContext;
 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
@@ -47,7 +56,7 @@ public class GroupIteratorTest extends TestSupport {
         Scanner scanner = new Scanner(s);
         scanner.useDelimiter("\n");
 
-        GroupIterator gi = new GroupIterator(context, scanner, "\n", 3);
+        GroupIterator gi = new GroupIterator(exchange, scanner, "\n", 3);
 
         assertTrue(gi.hasNext());
         assertEquals("ABC\nDEF\nGHI", gi.next());
@@ -58,4 +67,28 @@ public class GroupIteratorTest extends TestSupport {
         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 = "£1\n£2\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("£1", gi.next());
+        assertEquals("£2", gi.next());
+        assertFalse(gi.hasNext());
+
+        IOHelper.close(gi);
+    }
+
 }


Mime
View raw message