Author: davsclaus
Date: Wed May 11 15:50:49 2011
New Revision: 1101929

URL: http://svn.apache.org/viewvc?rev=1101929&view=rev
Log:
CAMEL-3963: Added RefDataFormat to make it consistent to use data format. As 
ref is just like any other data format.

Added:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/RefDataFormat.java
      - copied, changed from r1101887, 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/StringDataFormat.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RefDataFormatTest.java
      - copied, changed from r1101887, 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StringDataFormatTest.java
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/impl/
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/impl/SpringRefDataFormatTest.java
    
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/
    
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/SpringRefDataFormatTest.xml
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java
    
camel/trunk/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java?rev=1101929&r1=1101928&r2=1101929&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DataFormatClause.java
 Wed May 11 15:50:49 2011
@@ -32,6 +32,7 @@ import org.apache.camel.model.dataformat
 import org.apache.camel.model.dataformat.JsonDataFormat;
 import org.apache.camel.model.dataformat.JsonLibrary;
 import org.apache.camel.model.dataformat.ProtobufDataFormat;
+import org.apache.camel.model.dataformat.RefDataFormat;
 import org.apache.camel.model.dataformat.RssDataFormat;
 import org.apache.camel.model.dataformat.SerializationDataFormat;
 import org.apache.camel.model.dataformat.SoapJaxbDataFormat;
@@ -230,6 +231,13 @@ public class DataFormatClause<T extends 
     }
 
     /**
+     * Uses the ref data format
+     */
+    public T ref(String ref) {
+        return dataFormat(new RefDataFormat(ref));
+    }
+
+    /**
      * Uses the Java Serialization data format
      */
     public T serialization() {

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java?rev=1101929&r1=1101928&r2=1101929&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/MarshalDefinition.java
 Wed May 11 15:50:49 2011
@@ -35,6 +35,7 @@ import org.apache.camel.model.dataformat
 import org.apache.camel.model.dataformat.JibxDataFormat;
 import org.apache.camel.model.dataformat.JsonDataFormat;
 import org.apache.camel.model.dataformat.ProtobufDataFormat;
+import org.apache.camel.model.dataformat.RefDataFormat;
 import org.apache.camel.model.dataformat.RssDataFormat;
 import org.apache.camel.model.dataformat.SerializationDataFormat;
 import org.apache.camel.model.dataformat.SoapJaxbDataFormat;
@@ -73,6 +74,7 @@ public class MarshalDefinition extends O
     @XmlElement(required = false, name = "jibx", type = JibxDataFormat.class),
     @XmlElement(required = false, name = "json", type = JsonDataFormat.class),
     @XmlElement(required = false, name = "protobuf", type = 
ProtobufDataFormat.class),
+    @XmlElement(required = false, name = "ref", type = RefDataFormat.class),
     @XmlElement(required = false, name = "rss", type = RssDataFormat.class),
     @XmlElement(required = false, name = "secureXML", type = 
XMLSecurityDataFormat.class),
     @XmlElement(required = false, name = "serialization", type = 
SerializationDataFormat.class),

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java?rev=1101929&r1=1101928&r2=1101929&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/UnmarshalDefinition.java
 Wed May 11 15:50:49 2011
@@ -35,6 +35,7 @@ import org.apache.camel.model.dataformat
 import org.apache.camel.model.dataformat.JibxDataFormat;
 import org.apache.camel.model.dataformat.JsonDataFormat;
 import org.apache.camel.model.dataformat.ProtobufDataFormat;
+import org.apache.camel.model.dataformat.RefDataFormat;
 import org.apache.camel.model.dataformat.RssDataFormat;
 import org.apache.camel.model.dataformat.SerializationDataFormat;
 import org.apache.camel.model.dataformat.SoapJaxbDataFormat;
@@ -73,6 +74,7 @@ public class UnmarshalDefinition extends
     @XmlElement(required = false, name = "jibx", type = JibxDataFormat.class),
     @XmlElement(required = false, name = "json", type = JsonDataFormat.class),
     @XmlElement(required = false, name = "protobuf", type = 
ProtobufDataFormat.class),
+    @XmlElement(required = false, name = "ref", type = RefDataFormat.class),
     @XmlElement(required = false, name = "rss", type = RssDataFormat.class),
     @XmlElement(required = false, name = "secureXML", type = 
XMLSecurityDataFormat.class),
     @XmlElement(required = false, name = "serialization", type = 
SerializationDataFormat.class),

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java?rev=1101929&r1=1101928&r2=1101929&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/DataFormatsDefinition.java
 Wed May 11 15:50:49 2011
@@ -48,6 +48,7 @@ public class DataFormatsDefinition {
         @XmlElement(required = false, name = "jibx", type = 
JibxDataFormat.class),
         @XmlElement(required = false, name = "json", type = 
JsonDataFormat.class),
         @XmlElement(required = false, name = "protobuf", type = 
ProtobufDataFormat.class),
+        @XmlElement(required = false, name = "ref", type = 
RefDataFormat.class),
         @XmlElement(required = false, name = "rss", type = 
RssDataFormat.class),
         @XmlElement(required = false, name = "secureXML", type = 
XMLSecurityDataFormat.class),
         @XmlElement(required = false, name = "serialization", type = 
SerializationDataFormat.class),

Copied: 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/RefDataFormat.java
 (from r1101887, 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/StringDataFormat.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/RefDataFormat.java?p2=camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/RefDataFormat.java&p1=camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/StringDataFormat.java&r1=1101887&r2=1101929&rev=1101929&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/StringDataFormat.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/dataformat/RefDataFormat.java
 Wed May 11 15:50:49 2011
@@ -26,27 +26,33 @@ import org.apache.camel.spi.DataFormat;
 import org.apache.camel.spi.RouteContext;
 
 /**
- * Represents the String (text based) {@link DataFormat}
+ * Represents the custom {@link org.apache.camel.spi.DataFormat} to lookup 
using a reference
  *
- * @version 
+ * @version
  */
-@XmlRootElement(name = "string")
+@XmlRootElement(name = "ref")
 @XmlAccessorType(XmlAccessType.FIELD)
-public class StringDataFormat extends DataFormatDefinition {
+public class RefDataFormat extends DataFormatDefinition {
     @XmlAttribute
-    private String charset;
+    private String ref;
+
+    public RefDataFormat() {
+    }
+
+    public RefDataFormat(String ref) {
+        this.ref = ref;
+    }
 
     @Override
     protected DataFormat createDataFormat(RouteContext routeContext) {
-        return new org.apache.camel.impl.StringDataFormat(charset);
+        return DataFormatDefinition.getDataFormat(routeContext, null, ref);
     }
 
-    public String getCharset() {
-        return charset;
+    public String getRef() {
+        return ref;
     }
 
-    public void setCharset(String charset) {
-        this.charset = charset;
+    public void setRef(String ref) {
+        this.ref = ref;
     }
-
 }

Modified: 
camel/trunk/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index?rev=1101929&r1=1101928&r2=1101929&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index
 (original)
+++ 
camel/trunk/camel-core/src/main/resources/org/apache/camel/model/dataformat/jaxb.index
 Wed May 11 15:50:49 2011
@@ -27,6 +27,7 @@ JaxbDataFormat
 JsonDataFormat
 JsonLibrary
 ProtobufDataFormat
+RefDataFormat
 RssDataFormat
 SerializationDataFormat
 StringDataFormat

Copied: 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RefDataFormatTest.java
 (from r1101887, 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StringDataFormatTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RefDataFormatTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RefDataFormatTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StringDataFormatTest.java&r1=1101887&r2=1101929&rev=1101929&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StringDataFormatTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RefDataFormatTest.java
 Wed May 11 15:50:49 2011
@@ -16,156 +16,75 @@
  */
 package org.apache.camel.impl;
 
-import java.io.ByteArrayInputStream;
 import java.io.InputStream;
+import java.io.OutputStream;
 
-import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
-import org.apache.camel.ProducerTemplate;
-import org.apache.camel.TestSupport;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.DataFormat;
 
 /**
  * Unit test of the string data format.
  */
-public class StringDataFormatTest extends TestSupport {
+public class RefDataFormatTest extends ContextTestSupport {
 
-    private CamelContext context;
-    private ProducerTemplate template;
-
-    protected void setUp() throws Exception {
-        context = new DefaultCamelContext();
-        template = context.createProducerTemplate();
-        template.start();
-    }
-
-    protected void tearDown() throws Exception {
-        template.stop();
-        context.stop();
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("reverse", new MyReverseDataFormat());
+        return jndi;
     }
 
-    public void testMarshalUTF8() throws Exception {
-        // NOTE: We are using a processor to do the assertions as the mock 
endpoint (Camel) does not yet support
-        // type conversion using byte and strings where you can set a charset 
encoding
-
-        // include a UTF-8 char in the text \u0E08 is a Thai elephant
-        final String title = "Hello Thai Elephant \u0E08";
-
-        context.addRoutes(new RouteBuilder() {
-            public void configure() {
-                from("direct:start").marshal().string("UTF-8").process(new 
MyBookProcessor("UTF-8", title));
-            }
-        });
-        context.start();
+    public void testMarshalRef() throws Exception {
+        getMockEndpoint("mock:a").expectedBodiesReceived("CBA");
 
-        MyBook book = new MyBook();
-        book.setTitle(title);
+        template.sendBody("direct:a", "ABC");
 
-        template.sendBody("direct:start", book);
+        assertMockEndpointsSatisfied();
     }
 
-    public void testMarshalNoEncoding() throws Exception {
-        // NOTE: We are using a processor to do the assertions as the mock 
endpoint (Camel) does not yet support
-        // type conversion using byte and strings where you can set a charset 
encoding
-
-        final String title = "Hello World";
-
-        context.addRoutes(new RouteBuilder() {
-            public void configure() {
-                from("direct:start").marshal().string().process(new 
MyBookProcessor(null, title));
-            }
-        });
-        context.start();
+    public void testUnmarshalRef() throws Exception {
+        getMockEndpoint("mock:b").expectedBodiesReceived("ABC");
 
-        MyBook book = new MyBook();
-        book.setTitle(title);
+        template.sendBody("direct:b", "CBA");
 
-        template.sendBody("direct:start", book);
+        assertMockEndpointsSatisfied();
     }
 
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:a").marshal().ref("reverse").to("mock:a");
 
-    public void testUnmarshalUTF8() throws Exception {
-        // NOTE: Here we can use a MockEndpoint as we unmarshal the 
inputstream to String
-
-        // include a UTF-8 char in the text \u0E08 is a Thai elephant
-        final String title = "Hello Thai Elephant \u0E08";
-
-        context.addRoutes(new RouteBuilder() {
-            public void configure() {
-                
from("direct:start").unmarshal().string("UTF-8").to("mock:unmarshal");
+                from("direct:b").unmarshal().ref("reverse").to("mock:b");
             }
-        });
-        context.start();
-
-        byte[] bytes = title.getBytes("UTF-8");
-        InputStream in = new ByteArrayInputStream(bytes);
-
-        template.sendBody("direct:start", in);
-
-        MockEndpoint mock = context.getEndpoint("mock:unmarshal", 
MockEndpoint.class);
-        mock.setExpectedMessageCount(1);
-        mock.expectedBodiesReceived(title);
+        };
     }
 
-    public void testUnmarshalNoEncoding() throws Exception {
-        // NOTE: Here we can use a MockEndpoint as we unmarshal the 
inputstream to String
-
-        final String title = "Hello World";
-
-        context.addRoutes(new RouteBuilder() {
-            public void configure() {
-                from("direct:start").unmarshal().string().to("mock:unmarshal");
-            }
-        });
-        context.start();
-
-        byte[] bytes = title.getBytes();
-        InputStream in = new ByteArrayInputStream(bytes);
+    public static final class MyReverseDataFormat implements DataFormat {
 
-        template.sendBody("direct:start", in);
-
-        MockEndpoint mock = context.getEndpoint("mock:unmarshal", 
MockEndpoint.class);
-        mock.setExpectedMessageCount(1);
-        mock.expectedBodiesReceived(title);
-    }
-
-    private class MyBookProcessor implements Processor {
-
-        private String encoding;
-        private String title;
-
-        public MyBookProcessor(String encoding, String title) {
-            this.encoding = encoding;
-            this.title = title;
+        public void marshal(Exchange exchange, Object graph, OutputStream 
stream) throws Exception {
+            byte[] bytes = 
exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, 
graph);
+            String body = reverseBytes(bytes);
+            stream.write(body.getBytes());
         }
 
-        public void process(Exchange exchange) throws Exception {
-            byte[] body = exchange.getIn().getBody(byte[].class);
-
-            String text;
-            if (encoding != null) {
-                text = new String(body, encoding);
-            } else {
-                text = new String(body);
-            }
-
-            // does the testing
-            assertEquals(text, title);
-        }
-    }
-
-    private class MyBook {
-        private String title;
-
-        public void setTitle(String title) {
-            this.title = title;
+        public Object unmarshal(Exchange exchange, InputStream stream) throws 
Exception {
+            byte[] bytes = 
exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, 
stream);
+            String body = reverseBytes(bytes);
+            return body;
         }
 
-        public String toString() {
-            // Camel will fallback to object toString converter and thus we 
get this text
-            return title;
+        private String reverseBytes(byte[] data) {
+            StringBuilder sb = new StringBuilder(data.length);
+            for (int i = data.length - 1; i >= 0; i--) {
+                char ch = (char) data[i];
+                sb.append(ch);
+            }
+            return sb.toString();
         }
     }
 

Added: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/impl/SpringRefDataFormatTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/impl/SpringRefDataFormatTest.java?rev=1101929&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/impl/SpringRefDataFormatTest.java
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/impl/SpringRefDataFormatTest.java
 Wed May 11 15:50:49 2011
@@ -0,0 +1,33 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.spring.impl;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.RefDataFormatTest;
+
+import static 
org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
+
+/**
+ *
+ */
+public class SpringRefDataFormatTest extends RefDataFormatTest {
+
+    protected CamelContext createCamelContext() throws Exception {
+        return createSpringCamelContext(this, 
"org/apache/camel/spring/impl/SpringRefDataFormatTest.xml");
+    }
+
+}

Added: 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/SpringRefDataFormatTest.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/SpringRefDataFormatTest.xml?rev=1101929&view=auto
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/SpringRefDataFormatTest.xml
 (added)
+++ 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/impl/SpringRefDataFormatTest.xml
 Wed May 11 15:50:49 2011
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans";
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+    <bean id="reverse" 
class="org.apache.camel.impl.RefDataFormatTest$MyReverseDataFormat"/>
+
+    <camelContext xmlns="http://camel.apache.org/schema/spring";>
+        <route>
+            <from uri="direct:a"/>
+            <marshal>
+                <ref ref="reverse"/>
+            </marshal>
+            <to uri="mock:a"/>
+        </route>
+
+        <route>
+            <from uri="direct:b"/>
+            <unmarshal>
+                <ref ref="reverse"/>
+            </unmarshal>
+            <to uri="mock:b"/>
+        </route>
+    </camelContext>
+
+</beans>


Reply via email to