camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [1/4] camel git commit: CAMEL-8356 corrections to default charset
Date Mon, 02 Mar 2015 05:58:48 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.14.x 7ecd45225 -> 8db720c62


CAMEL-8356 corrections to default charset 

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

Branch: refs/heads/camel-2.14.x
Commit: 7f6538cb1b30a80eef3f743d0f9e98b3e7b8f1bc
Parents: 7ecd452
Author: Stefan Mandel <mandelst@gmail.com>
Authored: Sun Mar 1 12:26:15 2015 +0100
Committer: Stefan Mandel <mandelst@gmail.com>
Committed: Sun Mar 1 12:32:55 2015 +0100

----------------------------------------------------------------------
 .../org/apache/camel/converter/IOConverter.java |  2 +-
 .../camel/converter/IOConverterCharsetTest.java | 41 ++++++++-
 .../apache/camel/jsonpath/JsonPathEngine.java   | 16 +++-
 .../camel/jsonpath/JsonPathSourceTest.java      | 94 ++++++++++++++++++++
 .../test/resources/germanbooks-iso-8859-1.json  | 23 +++++
 .../src/test/resources/germanbooks-utf8.json    | 23 +++++
 6 files changed, 191 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7f6538cb/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java b/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
index cf33390..24a296f 100644
--- a/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
+++ b/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java
@@ -80,7 +80,7 @@ public final class IOConverter {
     public static InputStream toInputStream(File file, String charset) throws IOException
{
         if (charset != null) {
             final BufferedReader reader = toReader(file, charset);
-            final Charset defaultStreamCharset = Charset.forName("UTF-8");
+            final Charset defaultStreamCharset = Charset.defaultCharset();
             return new InputStream() {
                 private ByteBuffer bufferBytes;
                 private CharBuffer bufferedChars = CharBuffer.allocate(4096);

http://git-wip-us.apache.org/repos/asf/camel/blob/7f6538cb/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java
b/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java
index fe74d75..52f2d10 100644
--- a/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java
+++ b/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java
@@ -21,6 +21,8 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.lang.reflect.Field;
+import java.nio.charset.Charset;
 import java.util.Arrays;
 
 import org.apache.camel.ContextTestSupport;
@@ -29,10 +31,30 @@ public class IOConverterCharsetTest extends ContextTestSupport {
     private static final String CONTENT = "G\u00f6tzend\u00e4mmerung,Joseph und seine Br\u00fcder";
 
     public void testToInputStreamFileWithCharsetUTF8() throws Exception {
+    	switchToDefaultCharset("UTF-8");
         File file = new File("src/test/resources/org/apache/camel/converter/german.utf-8.txt");
         InputStream in = IOConverter.toInputStream(file, "UTF-8");
-        // need to specify the encoding of the input stream bytes
-        BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
+        // do read with default charset!
+        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+        BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),
"UTF-8"));
+        try {   
+            String line = reader.readLine();
+            String naiveLine = naiveReader.readLine();
+            assertEquals(naiveLine, line);
+            assertEquals(CONTENT, line);
+        } finally {
+            reader.close();
+            naiveReader.close();
+        }
+        
+    }
+
+    public void testToInputStreamFileWithCharsetUTF8withOtherDefaultEncoding() throws Exception
{
+    	switchToDefaultCharset("ISO-8859-1");
+        File file = new File("src/test/resources/org/apache/camel/converter/german.utf-8.txt");
+        InputStream in = IOConverter.toInputStream(file, "UTF-8");
+        // do read with default charset!
+        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
         BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),
"UTF-8"));
         try {   
             String line = reader.readLine();
@@ -47,10 +69,11 @@ public class IOConverterCharsetTest extends ContextTestSupport {
     }
 
     public void testToInputStreamFileWithCharsetLatin1() throws Exception {
+    	switchToDefaultCharset("UTF-8");
         File file = new File("src/test/resources/org/apache/camel/converter/german.iso-8859-1.txt");
         InputStream in = IOConverter.toInputStream(file, "ISO-8859-1");
-        // need to specify the encoding of the input stream bytes
-        BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
+        // do read with default charset!
+        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
         BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file),
"ISO-8859-1"));
         try {
             String line = reader.readLine();
@@ -64,6 +87,7 @@ public class IOConverterCharsetTest extends ContextTestSupport {
     }
 
     public void testToInputStreamFileDirectByteDumpWithCharsetLatin1() throws Exception {
+    	switchToDefaultCharset("UTF-8");
         File file = new File("src/test/resources/org/apache/camel/converter/german.iso-8859-1.txt");
         InputStream in = IOConverter.toInputStream(file, "ISO-8859-1");
         InputStream naiveIn = new FileInputStream(file);
@@ -109,4 +133,13 @@ public class IOConverterCharsetTest extends ContextTestSupport {
         }
     }
 
+
+	private void switchToDefaultCharset(String charset) {
+		try {
+			Field defaultCharset = Charset.class.getDeclaredField("defaultCharset");
+			defaultCharset.setAccessible(true);
+			defaultCharset.set(null, Charset.forName(charset));
+		} catch (Exception e) {
+		}
+	}
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/7f6538cb/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
index 3616dbc..ccda30e 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
@@ -21,11 +21,15 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 
-import com.jayway.jsonpath.Configuration;
-import com.jayway.jsonpath.JsonPath;
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.WrappedFile;
+import org.apache.camel.component.file.GenericFile;
+import org.apache.camel.component.file.GenericFileConverter;
+
+import com.jayway.jsonpath.Configuration;
+import com.jayway.jsonpath.JsonPath;
 
 public class JsonPathEngine {
 
@@ -40,7 +44,13 @@ public class JsonPathEngine {
     public Object read(Exchange exchange) throws IOException, InvalidPayloadException {
         Object json = exchange.getIn().getBody();
 
-        if (json instanceof WrappedFile) {
+        if (json instanceof GenericFile) {
+            try {
+				json = GenericFileConverter.genericFileToInputStream(((GenericFile<?>) json), exchange);
+			} catch (NoTypeConversionAvailableException e) {
+	            json = ((WrappedFile<?>) json).getFile();
+			}
+        } else if (json instanceof WrappedFile) {
             json = ((WrappedFile<?>) json).getFile();
         }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/7f6538cb/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathSourceTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathSourceTest.java
b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathSourceTest.java
new file mode 100644
index 0000000..db0a273
--- /dev/null
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathSourceTest.java
@@ -0,0 +1,94 @@
+/**
+ * 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.jsonpath;
+
+import java.io.File;
+import java.lang.reflect.Field;
+import java.nio.charset.Charset;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.file.FileConsumer;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JsonPathSourceTest extends CamelTestSupport {
+
+	@Override
+	protected RouteBuilder createRouteBuilder() throws Exception {
+		return new RouteBuilder() {
+			@Override
+			public void configure() throws Exception {
+				from("direct:start")
+					.transform().jsonpath("$.store.book[0].title", String.class)
+					.to("mock:title");
+
+				from("direct:second")
+					.transform().jsonpath("$.store.book[1].title", String.class)
+					.to("mock:title");
+			}
+		};
+	}
+
+	@Test
+	public void testPriceResultTypeOnGenericFileUTF8() throws Exception {
+		switchToDefaultCharset("UTF-8");
+		getMockEndpoint("mock:title").expectedMessageCount(2);
+		getMockEndpoint("mock:title").message(0).body().isEqualTo("Joseph und seine Brüder");
+		getMockEndpoint("mock:title").message(1).body().isEqualTo("Götzendämmerung");
+
+		template.sendBody("direct:start", FileConsumer.asGenericFile("src/test/resources/germanbooks-utf8.json",
new File("src/test/resources/germanbooks-utf8.json"), "UTF-8"));
+		template.sendBody("direct:second", FileConsumer.asGenericFile("src/test/resources/germanbooks-utf8.json",
new File("src/test/resources/germanbooks-utf8.json"), "UTF-8"));
+
+		assertMockEndpointsSatisfied();
+	}
+
+	@Test
+	public void testPriceResultTypeOnGenericFileUTF8OnWindows() throws Exception {
+		switchToDefaultCharset("windows-1252");
+		getMockEndpoint("mock:title").expectedMessageCount(2);
+		getMockEndpoint("mock:title").message(0).body().isEqualTo("Joseph und seine Brüder");
+		getMockEndpoint("mock:title").message(1).body().isEqualTo("Götzendämmerung");
+
+		template.sendBody("direct:start", FileConsumer.asGenericFile("src/test/resources/germanbooks-utf8.json",
new File("src/test/resources/germanbooks-utf8.json"), "UTF-8"));
+		template.sendBody("direct:second", FileConsumer.asGenericFile("src/test/resources/germanbooks-utf8.json",
new File("src/test/resources/germanbooks-utf8.json"), "UTF-8"));
+
+		assertMockEndpointsSatisfied();
+	}
+
+	@Test
+	public void testPriceResultTypeOnGenericFileISO88591() throws Exception {
+		switchToDefaultCharset("UTF-8");
+		getMockEndpoint("mock:title").expectedMessageCount(2);
+		getMockEndpoint("mock:title").message(0).body().isEqualTo("Joseph und seine Brüder");
+		getMockEndpoint("mock:title").message(1).body().isEqualTo("Götzendämmerung");
+
+		template.sendBody("direct:start", FileConsumer.asGenericFile("src/test/resources/germanbooks-iso-8859-1.json",
new File("src/test/resources/germanbooks-iso-8859-1.json"), "ISO-8859-1"));
+		template.sendBody("direct:second", FileConsumer.asGenericFile("src/test/resources/germanbooks-iso-8859-1.json",
new File("src/test/resources/germanbooks-iso-8859-1.json"), "ISO-8859-1"));
+
+		assertMockEndpointsSatisfied();
+	}
+
+	private void switchToDefaultCharset(String charset) {
+		try {
+			Field defaultCharset = Charset.class.getDeclaredField("defaultCharset");
+			defaultCharset.setAccessible(true);
+			defaultCharset.set(null, Charset.forName(charset));
+		} catch (Exception e) {
+		}
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/7f6538cb/components/camel-jsonpath/src/test/resources/germanbooks-iso-8859-1.json
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/resources/germanbooks-iso-8859-1.json b/components/camel-jsonpath/src/test/resources/germanbooks-iso-8859-1.json
new file mode 100644
index 0000000..74759da
--- /dev/null
+++ b/components/camel-jsonpath/src/test/resources/germanbooks-iso-8859-1.json
@@ -0,0 +1,23 @@
+{
+    "store": {
+        "book": [
+            {
+                "category": "novel",
+                "author": "Thomas Mann",
+                "title": "Joseph und seine Br�der",
+                "price": 25.00
+            },
+            {
+                "category": "fiction",
+                "author": "Friedrich Nietzsche",
+                "title": "G�tzend�mmerung",
+                "price": 9.00,
+                "isbn": "3-458-32522-0"
+            }
+        ],
+        "bicycle": {
+            "color": "red",
+            "price": 19.95
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/7f6538cb/components/camel-jsonpath/src/test/resources/germanbooks-utf8.json
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/resources/germanbooks-utf8.json b/components/camel-jsonpath/src/test/resources/germanbooks-utf8.json
new file mode 100644
index 0000000..ca84e6f
--- /dev/null
+++ b/components/camel-jsonpath/src/test/resources/germanbooks-utf8.json
@@ -0,0 +1,23 @@
+{
+    "store": {
+        "book": [
+            {
+                "category": "novel",
+                "author": "Thomas Mann",
+                "title": "Joseph und seine Brüder",
+                "price": 25.00
+            },
+            {
+                "category": "fiction",
+                "author": "Friedrich Nietzsche",
+                "title": "Götzendämmerung",
+                "price": 9.00,
+                "isbn": "3-458-32522-0"
+            }
+        ],
+        "bicycle": {
+            "color": "red",
+            "price": 19.95
+        }
+    }
+}
\ No newline at end of file


Mime
View raw message