You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2010/01/13 04:47:58 UTC

svn commit: r898640 - in /camel/trunk/components/camel-jaxb/src: main/java/org/apache/camel/converter/jaxb/ test/java/org/apache/camel/converter/jaxb/ test/java/org/apache/camel/jaxb/

Author: ningjiang
Date: Wed Jan 13 03:47:57 2010
New Revision: 898640

URL: http://svn.apache.org/viewvc?rev=898640&view=rev
Log:
CAMEL-2330 enhance the unit test with the suggestion of Pavel

Modified:
    camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java
    camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
    camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbConverterTest.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=898640&r1=898639&r2=898640&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 Wed Jan 13 03:47:57 2010
@@ -18,29 +18,32 @@
 
 import java.io.Closeable;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 import java.util.HashMap;
 import java.util.Map;
+
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.bind.util.JAXBSource;
 import javax.xml.stream.FactoryConfigurationError;
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.Source;
 
+import org.apache.camel.CamelExecutionException;
 import org.apache.camel.Exchange;
 import org.apache.camel.NoTypeConversionAvailableException;
-import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.StreamCache;
 import org.apache.camel.TypeConverter;
+import org.apache.camel.converter.IOConverter;
 import org.apache.camel.spi.TypeConverterAware;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.commons.logging.Log;
@@ -87,7 +90,7 @@
             }
             return null;
         } catch (Exception e) {
-            throw new RuntimeCamelException(e);
+            throw ObjectHelper.wrapCamelExecutionException(exchange, e);
         }
         
     }
@@ -112,7 +115,7 @@
     /**
      * Lets try parse via JAXB
      */
-    protected <T> T unmarshall(Class<T> type, Exchange exchange, Object value) throws JAXBException {
+    protected <T> T unmarshall(Class<T> type, Exchange exchange, Object value) throws Exception {
         if (value == null) {
             throw new IllegalArgumentException("Cannot convert from null value to JAXBSource");
         }
@@ -122,20 +125,19 @@
         Unmarshaller unmarshaller = context.createUnmarshaller();
 
         if (parentTypeConverter != null) {
-            // Prefer to use the Reader which can skip the control characters and other non-xml characters
-            Reader reader = parentTypeConverter.convertTo(Reader.class, value);
-            if (reader != null) {
-                Object unmarshalled = unmarshal(unmarshaller, reader);
-                return type.cast(unmarshalled);
-            }
             InputStream inputStream = parentTypeConverter.convertTo(InputStream.class, value);
             if (inputStream != null) {
-                Object unmarshalled = unmarshal(unmarshaller, inputStream);
+                Object unmarshalled = unmarshal(unmarshaller, exchange, inputStream);
+                return type.cast(unmarshalled);
+            }
+            Reader reader = parentTypeConverter.convertTo(Reader.class, value);
+            if (reader != null) {
+                Object unmarshalled = unmarshal(unmarshaller, exchange, reader);
                 return type.cast(unmarshalled);
             }
             Source source = parentTypeConverter.convertTo(Source.class, value);
             if (source != null) {
-                Object unmarshalled = unmarshal(unmarshaller, source);
+                Object unmarshalled = unmarshal(unmarshaller, exchange, source);
                 return type.cast(unmarshalled);
             }
         }
@@ -144,7 +146,7 @@
             value = new StringReader((String) value);
         }
         if (value instanceof InputStream || value instanceof Reader) {
-            Object unmarshalled = unmarshal(unmarshaller, value);
+            Object unmarshalled = unmarshal(unmarshaller, exchange, value);
             return type.cast(unmarshalled);
         }
 
@@ -164,17 +166,14 @@
             if (exchange != null && exchange.getProperty(Exchange.CHARSET_NAME, String.class) != null) {
                 marshaller.setProperty(Marshaller.JAXB_ENCODING, exchange.getProperty(Exchange.CHARSET_NAME, String.class));
             }
-            if (answer == null) {
-                if (exchange != null
-                    && exchange.getProperty(Exchange.FILTER_NON_XML_CHARS, Boolean.FALSE, Boolean.class)) {
-                    XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer);
-                    FilteringXmlStreamWriter filteringWriter = new FilteringXmlStreamWriter(writer);
-                    marshaller.marshal(value, filteringWriter);
-                } else {
-                    marshaller.marshal(value, buffer);
-                }
-                answer = parentTypeConverter.convertTo(type, buffer.toString());
+            if (needFiltering(exchange)) {
+                XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer);
+                FilteringXmlStreamWriter filteringWriter = new FilteringXmlStreamWriter(writer);
+                marshaller.marshal(value, filteringWriter);
+            } else {
+                marshaller.marshal(value, buffer);
             }
+            answer = parentTypeConverter.convertTo(type, buffer.toString());
         }
 
         return answer;
@@ -184,33 +183,43 @@
      * Unmarshals the given value with the unmarshaller
      *
      * @param unmarshaller  the unmarshaller
+     * @param exchange the exchange 
      * @param value  the stream to unmarshal (will close it after use, also if exception is thrown)
      * @return  the value
      * @throws JAXBException is thrown if an exception occur while unmarshalling
+     * @throws UnsupportedEncodingException 
      */
-    protected Object unmarshal(Unmarshaller unmarshaller, Object value) throws JAXBException {
+    protected Object unmarshal(Unmarshaller unmarshaller, Exchange exchange, Object value)
+        throws JAXBException, UnsupportedEncodingException {
         try {
             if (value instanceof InputStream) {
-                return unmarshaller.unmarshal((InputStream) value);
+                if (needFiltering(exchange)) {
+                    return unmarshaller.unmarshal(new NonXmlFilterReader(new InputStreamReader((InputStream)value, IOConverter.getCharsetName(exchange))));
+                }
+                return unmarshaller.unmarshal((InputStream)value);
             } else if (value instanceof Reader) {
-                // using the FilterReader by default
-                NonXmlFilterReader filterReader;
-                if (value instanceof NonXmlFilterReader) {
-                    filterReader = (NonXmlFilterReader) value;
-                } else {
-                    filterReader = new NonXmlFilterReader((Reader)value);
+                Reader reader = (Reader)value;
+                if (needFiltering(exchange)) {
+                    if (!(value instanceof NonXmlFilterReader)) {
+                        reader = new NonXmlFilterReader((Reader)value);
+                    }
                 }
-                return unmarshaller.unmarshal(filterReader);
+                return unmarshaller.unmarshal(reader);
             } else if (value instanceof Source) {
-                return unmarshaller.unmarshal((Source) value);
+                return unmarshaller.unmarshal((Source)value);
             }
         } finally {
             if (value instanceof Closeable) {
-                ObjectHelper.close((Closeable) value, "Unmarshalling", LOG);
+                ObjectHelper.close((Closeable)value, "Unmarshalling", LOG);
             }
         }
         return null;
     }
+    
+    protected boolean needFiltering(Exchange exchange) {
+        // exchange property takes precedence over data format property
+        return exchange != null && exchange.getProperty(Exchange.FILTER_NON_XML_CHARS, Boolean.FALSE, Boolean.class);
+    }
 
     protected synchronized <T> JAXBContext createContext(Class<T> type) throws JAXBException {
         JAXBContext context = contexts.get(type);

Modified: camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java?rev=898640&r1=898639&r2=898640&view=diff
==============================================================================
--- camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java (original)
+++ camel/trunk/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java Wed Jan 13 03:47:57 2010
@@ -102,7 +102,7 @@
             Object answer;
             Unmarshaller unmarshaller = getContext().createUnmarshaller();
             if (needFiltering(exchange)) {
-                answer = unmarshaller.unmarshal(new NonXmlFilterReader(new InputStreamReader(stream)));
+                answer = unmarshaller.unmarshal(new NonXmlFilterReader(new InputStreamReader(stream, IOConverter.getCharsetName(exchange))));
             } else  {
                 answer = unmarshaller.unmarshal(stream);
             }
@@ -118,7 +118,7 @@
     
     protected boolean needFiltering(Exchange exchange) {
         // exchange property takes precedence over data format property
-        return exchange.getProperty(Exchange.FILTER_NON_XML_CHARS, filterNonXmlChars, Boolean.class);
+        return exchange == null ? filterNonXmlChars : exchange.getProperty(Exchange.FILTER_NON_XML_CHARS, filterNonXmlChars, Boolean.class);
     }
 
     // Properties

Modified: camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbConverterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbConverterTest.java?rev=898640&r1=898639&r2=898640&view=diff
==============================================================================
--- camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbConverterTest.java (original)
+++ camel/trunk/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/JaxbConverterTest.java Wed Jan 13 03:47:57 2010
@@ -58,7 +58,7 @@
         // this time the fall back converter will be use
         exchange.getIn().setBody(order);
         Source source = exchange.getIn().getBody(Source.class);
-        assertTrue("The result source should not be JAXBSource", source instanceof Source);
+        assertTrue("The result source should be Source instance", source instanceof Source);
         exchange.getIn().setBody(new CamelException("Test"));
         source = exchange.getIn().getBody(Source.class);
         assertNull("The result should be null", source);

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=898640&r1=898639&r2=898640&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 Wed Jan 13 03:47:57 2010
@@ -40,12 +40,21 @@
         exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
        
         String value = converter.convertTo(String.class, exchange, person);
-        assertTrue("Didn't filter the non-xml chars", value.indexOf("<lastName>BAR</lastName>") > 0);
+        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);
+        }
     }
     
     @Test
     public void testFilteringConvertor() throws Exception {
-        final byte[] buffers = "<Person><firstName>FOO</firstName><lastName>BAR\u0008</lastName></Person>".getBytes("UTF-8");
+        byte[] buffers = "<Person><firstName>FOO</firstName><lastName>BAR\u0008</lastName></Person>".getBytes("UTF-8");
         InputStream is = new ByteArrayInputStream(buffers);
         Exchange exchange = new DefaultExchange(context);
         exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
@@ -56,13 +65,16 @@
         assertEquals("Get the wrong first name ", person.getFirstName(), "FOO");
         assertEquals("Get the wrong second name ", person.getLastName(), "BAR ");
         
+        
         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("Didn't filter the non-xml chars", value.indexOf("<lastName>BAR\uD8FF</lastName>") > 0);
+        assertTrue("Should not filter the non-xml chars", value.indexOf("<lastName>BAR\uD8FF</lastName>") > 0);
+    
     }
 
 }