CAMEL-8346 Poilsh the test code Conflicts: components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathSourceTest.java
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8db720c6 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8db720c6 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8db720c6 Branch: refs/heads/camel-2.14.x Commit: 8db720c62449586eeba21ab21f66828f930e46bf Parents: 81b4527 Author: Willem Jiang <willem.ji...@gmail.com> Authored: Mon Mar 2 13:37:40 2015 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Mon Mar 2 13:58:11 2015 +0800 ---------------------------------------------------------------------- .../apache/camel/jsonpath/JsonPathEngine.java | 9 +- .../camel/jsonpath/JsonPathSourceTest.java | 149 +++++++++++-------- 2 files changed, 90 insertions(+), 68 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/8db720c6/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 ccda30e..c0fb720 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 @@ -20,6 +20,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.nio.charset.Charset; import org.apache.camel.Exchange; import org.apache.camel.InvalidPayloadException; @@ -46,10 +47,10 @@ public class JsonPathEngine { if (json instanceof GenericFile) { try { - json = GenericFileConverter.genericFileToInputStream(((GenericFile<?>) json), exchange); - } catch (NoTypeConversionAvailableException e) { - json = ((WrappedFile<?>) json).getFile(); - } + 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/8db720c6/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 index db0a273..561db09 100644 --- 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 @@ -23,72 +23,93 @@ 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.AfterClass; 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) { - } - } +public class JsonPathSourceTest extends CamelTestSupport { + private static final String MESSAGE1 = "Joseph und seine Br\u00fcder"; + private static final String MESSAGE2 = "G\u00f6tzend\u00e4mmerung"; + private static final Charset DEFAULT_CHARSET = Charset.defaultCharset(); + + @AfterClass + public static void setDefaultCharsetBack() { + switchToDefaultCharset(DEFAULT_CHARSET.displayName()); + } + + @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(MESSAGE1); + getMockEndpoint("mock:title").message(1).body().isEqualTo(MESSAGE2); + + 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(MESSAGE1); + getMockEndpoint("mock:title").message(1).body().isEqualTo(MESSAGE2); + + 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("ISO-8859-1"); + getMockEndpoint("mock:title").expectedMessageCount(2); + getMockEndpoint("mock:title").message(0).body().isEqualTo(MESSAGE1); + getMockEndpoint("mock:title").message(1).body().isEqualTo(MESSAGE2); + + 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 static void switchToDefaultCharset(String charset) { + try { + Field defaultCharset = Charset.class.getDeclaredField("defaultCharset"); + defaultCharset.setAccessible(true); + defaultCharset.set(null, Charset.forName(charset)); + } catch (Exception e) { + // Do nothing here + } + } }