Hi,

I have found an issue with the camel-csv component. The fix itself is pretty 
trivial (a one-liner to use the IOHelper.getCharsetName(exchange)) for an 
OutputStreamWriter in the marhaller (the InputStreamWriter in the unmarshaller 
already does this).

However I have some difficulties with the unit test.

1.       The issue will only happen if there are non-ASCII characters in the 
code. As far as I understand it is not exactly desired to have those in the 
coding (on the other hand I just copied the appropriate test for the 
unmarshaller and that one does also have german umlaut characters in the code.

2.       The test only fails (with the original code) if the default encoding 
on the JVM that executes this is not UTF-8. There is some very hacky way to 
change the JVM default encoding for a test but I would not want to contribute 
that.

I put the test into this mail as an example (because it is short enough to 
demonstrate the issues). How could I test this properly? Wouldn't it be a good 
idea in general to run tests with an ASCII default charset to find issues with 
default encodings better (set file.encoding to "ASCII" in the surefire plugin 
configuration)?

Best regards
Stephan

public class CsvMarshalCharsetTest extends CamelTestSupport {

    @BeforeClass
    public static void hackEncoding() throws NoSuchFieldException, 
SecurityException, IllegalArgumentException, IllegalAccessException {
        System.setProperty("file.encoding", "ASCII");
        Field charset = Charset.class.getDeclaredField("defaultCharset");
        charset.setAccessible(true);
        charset.set(null, null);
    }

    @Test
    public void testMarshal() throws Exception {
        MockEndpoint endpoint = getMockEndpoint("mock:daltons");
        endpoint.expectedMessageCount(1);

        List<List<String>> data = new ArrayList<List<String>>();
        data.add(0, new ArrayList<String>());
        data.get(0).add(0, "Lücky Luke");
        template.sendBody("direct:start", data);

        endpoint.assertIsSatisfied();

        Exchange exchange = endpoint.getExchanges().get(0);
        String body = exchange.getIn().getBody(String.class);
        assertThat(body, startsWith("Lücky Luke"));
    }

    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            public void configure() {
                from("direct:start").marshal().csv().to("mock:daltons");
            }
        };
    }
}

Reply via email to