Author: bvahdat Date: Fri Jan 27 19:53:16 2012 New Revision: 1236862 URL: http://svn.apache.org/viewvc?rev=1236862&view=rev Log: CAMEL-4942: Jaxb-FallbackTypeConverter should hark to the contract of TypeConverter and not throw any unchecked exception if the conversion fails
Added: camel/branches/camel-2.8.x/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/FallbackTypeConverterShouldNotThrowExceptionTest.java - copied unchanged from r1236663, camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/FallbackTypeConverterShouldNotThrowExceptionTest.java Modified: camel/branches/camel-2.8.x/components/camel-jaxb/ (props changed) camel/branches/camel-2.8.x/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java camel/branches/camel-2.8.x/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbFallbackConverterTest.java Propchange: camel/branches/camel-2.8.x/components/camel-jaxb/ ------------------------------------------------------------------------------ --- svn:mergeinfo (added) +++ svn:mergeinfo Fri Jan 27 19:53:16 2012 @@ -0,0 +1,2 @@ +/camel/branches/camel-2.9.x/components/camel-jaxb:1227549,1228229,1229567,1234054,1236672 +/camel/trunk/components/camel-jaxb:1226860,1227540,1228223,1229565,1234043,1236403-1236663,1236667 Modified: camel/branches/camel-2.8.x/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java?rev=1236862&r1=1236861&r2=1236862&view=diff ============================================================================== --- camel/branches/camel-2.8.x/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java (original) +++ camel/branches/camel-2.8.x/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java Fri Jan 27 19:53:16 2012 @@ -47,7 +47,6 @@ import org.apache.camel.component.bean.B import org.apache.camel.converter.IOConverter; import org.apache.camel.spi.TypeConverterAware; import org.apache.camel.util.IOHelper; -import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -96,11 +95,13 @@ public class FallbackTypeConverter imple return marshall(type, exchange, value); } } - return null; } catch (Exception e) { - throw ObjectHelper.wrapCamelExecutionException(exchange, e); + // do only warn about the failed conversion but don't rethrow it as unchecked + LOG.warn("Type conversion for '" + value + "' to the type '" + type.getCanonicalName() + "' failed", e); } + // should return null if didn't even try to convert at all or for whatever reason the conversion is failed + return null; } public <T> T mandatoryConvertTo(Class<T> type, Object value) throws NoTypeConversionAvailableException { Modified: camel/branches/camel-2.8.x/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbFallbackConverterTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbFallbackConverterTest.java?rev=1236862&r1=1236861&r2=1236862&view=diff ============================================================================== --- camel/branches/camel-2.8.x/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbFallbackConverterTest.java (original) +++ camel/branches/camel-2.8.x/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbFallbackConverterTest.java Fri Jan 27 19:53:16 2012 @@ -29,55 +29,60 @@ import org.apache.camel.test.junit4.Came import org.junit.Test; public class CamelJaxbFallbackConverterTest extends CamelTestSupport { - + @Test public void testFallbackConverterWithoutObjectFactory() throws Exception { TypeConverter converter = context.getTypeConverter(); - Foo foo = converter.convertTo(Foo.class, - "<foo><zot name=\"bar1\" value=\"value\" otherValue=\"otherValue\"/></foo>"); + Foo foo = converter.convertTo(Foo.class, "<foo><zot name=\"bar1\" value=\"value\" otherValue=\"otherValue\"/></foo>"); assertNotNull("foo should not be null", foo); assertEquals("value", foo.getBarRefs().get(0).getValue()); - + foo.getBarRefs().clear(); Bar bar = new Bar(); bar.setName("myName"); bar.setValue("myValue"); foo.getBarRefs().add(bar); - + Exchange exchange = new DefaultExchange(context); exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8"); - + String value = converter.convertTo(String.class, exchange, foo); - + assertTrue("Should get a right marshalled string", value.indexOf("<bar name=\"myName\" value=\"myValue\"/>") > 0); } - + @Test - public void testConvertor() throws Exception { + public void testFallbackConverterUnmarshalWithNonJAXBComplaintValue() throws Exception { TypeConverter converter = context.getTypeConverter(); - PersonType person = converter.convertTo(PersonType.class, - "<Person><firstName>FOO</firstName><lastName>BAR</lastName></Person>"); + + Foo foo = converter.convertTo(Foo.class, "Not every String is XML"); + assertNull("Should not be able to convert non XML String", foo); + + Bar bar = converter.convertTo(Bar.class, "<bar></bar"); + assertNull("Should not be able to convert misspelled XML String", bar); + } + + @Test + public void testConverter() throws Exception { + TypeConverter converter = context.getTypeConverter(); + PersonType person = converter.convertTo(PersonType.class, "<Person><firstName>FOO</firstName><lastName>BAR</lastName></Person>"); assertNotNull("Person should not be null ", person); assertEquals("Get the wrong first name ", "FOO", person.getFirstName()); assertEquals("Get the wrong second name ", "BAR", person.getLastName()); Exchange exchange = new DefaultExchange(context); exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8"); - + String value = converter.convertTo(String.class, exchange, person); assertTrue("Should get a right marshalled string", value.indexOf("<lastName>BAR</lastName>") > 0); - - try { - byte[] buffers = "<Person><firstName>FOO</firstName><lastName>BAR\u0008</lastName></Person>".getBytes("UTF-8"); - InputStream is = new ByteArrayInputStream(buffers); - person = converter.convertTo(PersonType.class, exchange, is); - fail("expect the exception here"); - } catch (Exception ex) { - assertTrue("The exception should be CamelExecutionException", ex instanceof org.apache.camel.CamelExecutionException); - } + + byte[] buffers = "<Person><firstName>FOO</firstName><lastName>BAR\u0008</lastName></Person>".getBytes("UTF-8"); + InputStream is = new ByteArrayInputStream(buffers); + person = converter.convertTo(PersonType.class, exchange, is); + assertNull("Should not be able to convert as FILTER_NON_XML_CHARS property is not enabled", person); } - + @Test - public void testFilteringConvertor() throws Exception { + public void testFilteringConverter() throws Exception { byte[] buffers = "<Person><firstName>FOO</firstName><lastName>BAR\u0008</lastName></Person>".getBytes("UTF-8"); InputStream is = new ByteArrayInputStream(buffers); Exchange exchange = new DefaultExchange(context); @@ -88,17 +93,16 @@ public class CamelJaxbFallbackConverterT assertNotNull("Person should not be null ", person); assertEquals("Get the wrong first name ", "FOO", person.getFirstName()); assertEquals("Get the wrong second name ", "BAR ", person.getLastName()); - - + person.setLastName("BAR\u0008\uD8FF"); String value = converter.convertTo(String.class, exchange, person); assertTrue("Didn't filter the non-xml chars", value.indexOf("<lastName>BAR </lastName>") > 0); - + exchange.setProperty(Exchange.FILTER_NON_XML_CHARS, false); - + value = converter.convertTo(String.class, exchange, person); assertTrue("Should not filter the non-xml chars", value.indexOf("<lastName>BAR\uD8FF</lastName>") > 0); - + } }