Updated Branches:
  refs/heads/master ca403331d -> e8f78a77d

Added camel-netty unit test for custom codec that uses 2x null delimiters in 
byte messages.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e8f78a77
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e8f78a77
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e8f78a77

Branch: refs/heads/master
Commit: e8f78a77d6b75d012d1ae25325f8e73729013b67
Parents: ca40333
Author: Claus Ibsen <davscl...@apache.org>
Authored: Tue Aug 27 16:23:26 2013 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Tue Aug 27 16:23:58 2013 +0200

----------------------------------------------------------------------
 .../camel/component/netty/MyCustomCodec.java    | 77 ++++++++++++++++++++
 .../component/netty/NettyCustomCodecTest.java   | 64 ++++++++++++++++
 2 files changed, 141 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e8f78a77/components/camel-netty/src/test/java/org/apache/camel/component/netty/MyCustomCodec.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/MyCustomCodec.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/MyCustomCodec.java
new file mode 100644
index 0000000..2c973c6
--- /dev/null
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/MyCustomCodec.java
@@ -0,0 +1,77 @@
+/**
+ * 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.component.netty;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.jboss.netty.channel.Channel;
+import org.jboss.netty.channel.ChannelHandler;
+import org.jboss.netty.channel.ChannelHandlerContext;
+import org.jboss.netty.handler.codec.oneone.OneToOneDecoder;
+import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
+
+import static org.jboss.netty.buffer.ChannelBuffers.copiedBuffer;
+
+public class MyCustomCodec {
+
+    private static ChannelBuffer nullDelimiter = 
ChannelBuffers.wrappedBuffer(new byte[]{0});
+
+    public static ChannelHandlerFactory createMyCustomDecoder() {
+        ChannelBuffer[] delimiters = new ChannelBuffer[]{nullDelimiter, 
nullDelimiter};
+        return ChannelHandlerFactories.newDelimiterBasedFrameDecoder(4096, 
delimiters);
+    }
+
+    public static ChannelHandler createMyCustomDecoder2() {
+        return new BytesDecoder();
+    }
+
+    public static ChannelHandler createMyCustomEncoder() {
+        return new BytesEncoder();
+    }
+
+    @ChannelHandler.Sharable
+    public static class BytesDecoder extends OneToOneDecoder {
+
+        @Override
+        protected Object decode(ChannelHandlerContext ctx, Channel channel, 
Object msg) throws Exception {
+            if (!(msg instanceof ChannelBuffer)) {
+                return msg;
+            } else {
+                // it may be empty, then return null
+                ChannelBuffer cb = (ChannelBuffer) msg;
+                if (cb.hasArray() && cb.readable()) {
+                    return cb.array();
+                } else {
+                    return null;
+                }
+            }
+        }
+
+    }
+
+    @ChannelHandler.Sharable
+    public static class BytesEncoder extends OneToOneEncoder {
+
+        @Override
+        protected Object encode(ChannelHandlerContext ctx, Channel channel, 
Object msg) throws Exception {
+            if (msg instanceof byte[]) {
+                return copiedBuffer((byte[]) msg);
+            }
+            return msg;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/e8f78a77/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyCustomCodecTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyCustomCodecTest.java
 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyCustomCodecTest.java
new file mode 100644
index 0000000..30fe250
--- /dev/null
+++ 
b/components/camel-netty/src/test/java/org/apache/camel/component/netty/NettyCustomCodecTest.java
@@ -0,0 +1,64 @@
+/**
+ * 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.component.netty;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.util.ObjectHelper;
+import org.junit.Test;
+
+public class NettyCustomCodecTest extends BaseNettyTest {
+
+    private String uri = 
"netty:tcp://localhost:{{port}}?disconnect=true&sync=false" +
+            
"&allowDefaultCodec=false&decoders=#myCustomDecoder,#myCustomDecoder2&encoder=#myCustomEncoder";
+
+    // use reaadble bytes
+    private byte[] data = new byte[]{65,66,67,68,69,70,71,72,73,0,0};
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("myCustomDecoder", MyCustomCodec.createMyCustomDecoder());
+        jndi.bind("myCustomDecoder2", MyCustomCodec.createMyCustomDecoder2());
+        jndi.bind("myCustomEncoder", MyCustomCodec.createMyCustomEncoder());
+        return jndi;
+    }
+
+    @Test
+    public void testCustomCodec() throws Exception {
+        getMockEndpoint("mock:input").expectedMessageCount(1);
+
+        template.sendBody(uri, data);
+
+        assertMockEndpointsSatisfied();
+
+        byte[] mockData = 
getMockEndpoint("mock:input").getReceivedExchanges().get(0).getIn().getBody(byte[].class);
+        ObjectHelper.equalByteArray(data, mockData);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from(uri)
+                    .to("log:input")
+                    .to("mock:input");
+            }
+        };
+    }
+}

Reply via email to