Author: bvahdat Date: Thu Jan 26 21:51:58 2012 New Revision: 1236403 URL: http://svn.apache.org/viewvc?rev=1236403&view=rev Log: CAMEL-4942: Jaxb-FallbackTypeConverter should hark to the contract of TypeConverter and not throw any unchecked exception if the conversion fails
Modified: camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbFallbackConverterTest.java Modified: camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java?rev=1236403&r1=1236402&r2=1236403&view=diff ============================================================================== --- camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java (original) +++ camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java Thu Jan 26 21:51:58 2012 @@ -46,7 +46,6 @@ import org.apache.camel.TypeConverter; import org.apache.camel.component.bean.BeanInvocation; 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; @@ -95,11 +94,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/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbFallbackConverterTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbFallbackConverterTest.java?rev=1236403&r1=1236402&r2=1236403&view=diff ============================================================================== --- camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbFallbackConverterTest.java (original) +++ camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/jaxb/CamelJaxbFallbackConverterTest.java Thu Jan 26 21:51:58 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); - + } }