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 2012/08/29 05:16:55 UTC

svn commit: r1378431 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/util/ExchangeBuilder.java test/java/org/apache/camel/util/ExchangeBuilderTest.java

Author: ningjiang
Date: Wed Aug 29 03:16:54 2012
New Revision: 1378431

URL: http://svn.apache.org/viewvc?rev=1378431&view=rev
Log:
CAMEL-3104 ExchangeBuilder to create messages using fluent builder style with thanks to Bilgin

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeBuilder.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeBuilderTest.java

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeBuilder.java?rev=1378431&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeBuilder.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeBuilder.java Wed Aug 29 03:16:54 2012
@@ -0,0 +1,78 @@
+/**
+ * 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.util;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.Message;
+import org.apache.camel.impl.DefaultExchange;
+
+public class ExchangeBuilder {
+    private CamelContext context;
+    private ExchangePattern pattern;
+    private Object body;
+    private Map<String, Object> headers = new HashMap<String, Object>();
+    private Map<String, Object> properties = new HashMap<String, Object>();
+
+    public ExchangeBuilder(CamelContext context) {
+        this.context = context;
+    }
+
+    public static ExchangeBuilder anExchange(CamelContext context) {
+        return new ExchangeBuilder(context);
+    }
+
+    public ExchangeBuilder withBody(Object body) {
+        this.body = body;
+        return this;
+    }
+
+    public ExchangeBuilder withHeader(String key, Object value) {
+        headers.put(key, value);
+        return this;
+    }
+
+    public ExchangeBuilder withPattern(ExchangePattern pattern) {
+        this.pattern = pattern;
+        return this;
+    }
+    
+    public ExchangeBuilder withProperty(String key, Object value) {
+        properties.put(key, value);
+        return this;
+    }
+
+    public Exchange build() {
+        Exchange exchange = new DefaultExchange(context);
+        Message message = exchange.getIn();
+        message.setBody(body);
+        message.setHeaders(headers);
+        // setup the properties on the exchange
+        for (Map.Entry<String, Object> entry : properties.entrySet()) {
+            exchange.setProperty(entry.getKey(), entry.getValue());
+        }
+        if (pattern != null) {
+            exchange.setPattern(pattern);
+        }
+
+        return exchange;
+    }
+}

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeBuilderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeBuilderTest.java?rev=1378431&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeBuilderTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/ExchangeBuilderTest.java Wed Aug 29 03:16:54 2012
@@ -0,0 +1,59 @@
+/**
+ * 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.util;
+
+import junit.framework.TestCase;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.DefaultExchange;
+
+
+public class ExchangeBuilderTest extends TestCase {
+    private static final DefaultCamelContext CONTEXT = new DefaultCamelContext();
+    private static final String BODY = "Message Body";
+    private static final String KEY = "Header key";
+    private static final String VALUE = "Header value";
+    private static final String PROPERTY_KEY = "Property key";
+    private static final String PROPERTY_VALUE = "Property value";
+
+    public void testBuildAnExchangeWithDefaultPattern() {
+        Exchange exchange = new DefaultExchange(CONTEXT);
+        Exchange builtExchange = ExchangeBuilder.anExchange(CONTEXT).build();
+
+        assertEquals(exchange.getPattern(), builtExchange.getPattern());
+    }
+
+
+    public void testBuildAnExchangeWithBodyHeaderAndPattern() throws Exception {
+
+        Exchange exchange = ExchangeBuilder.anExchange(CONTEXT)
+                .withBody(BODY)
+                .withHeader(KEY, VALUE)
+                .withProperty(PROPERTY_KEY, PROPERTY_VALUE)
+                .withPattern(ExchangePattern.OutIn)
+                .build();
+
+        assertEquals(exchange.getIn().getBody(), BODY);
+        assertEquals(exchange.getIn().getHeader(KEY), VALUE);
+        assertEquals(exchange.getPattern(), ExchangePattern.OutIn);
+        assertEquals(exchange.getProperty(PROPERTY_KEY), PROPERTY_VALUE);
+    }
+
+}
+