Added: mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/FoobarProto.java URL: http://svn.apache.org/viewvc/mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/FoobarProto.java?rev=732497&view=auto ============================================================================== --- mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/FoobarProto.java (added) +++ mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/FoobarProto.java Wed Jan 7 13:12:46 2009 @@ -0,0 +1,402 @@ +/* + * 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. + * + */ +// Generated by the protocol buffer compiler. DO NOT EDIT! + +package org.apache.mina.filter.codec.protobuf; + +/** + * @author Tomasz Blachowicz + * @author The Apache MINA Project ([email protected]) + * @version $Rev$, $Date$ + */ +public final class FoobarProto { + private FoobarProto() {} + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistry registry) { + registry.add(org.apache.mina.filter.codec.protobuf.FoobarProto.Bar.bar); + } + public static final class Foo extends + com.google.protobuf.GeneratedMessage.ExtendableMessage< + Foo> { + // Use Foo.newBuilder() to construct. + private Foo() {} + + private static final Foo defaultInstance = new Foo(); + public static Foo getDefaultInstance() { + return defaultInstance; + } + + public Foo getDefaultInstanceForType() { + return defaultInstance; + } + + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return org.apache.mina.filter.codec.protobuf.FoobarProto.internal_static_Foo_descriptor; + } + + @Override + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return org.apache.mina.filter.codec.protobuf.FoobarProto.internal_static_Foo_fieldAccessorTable; + } + + // required int32 foo = 1; + private boolean hasFoo; + private int foo_ = 0; + public boolean hasFoo() { return hasFoo; } + public int getFoo() { return foo_; } + + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Foo parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Foo parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistry extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Foo parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Foo parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistry extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Foo parseFrom(java.io.InputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Foo parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistry extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Foo parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Foo parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistry extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + + public static Builder newBuilder() { return new Builder(); } + public Builder newBuilderForType() { return new Builder(); } + public static Builder newBuilder(org.apache.mina.filter.codec.protobuf.FoobarProto.Foo prototype) { + return new Builder().mergeFrom(prototype); + } + + public static final class Builder extends + com.google.protobuf.GeneratedMessage.ExtendableBuilder< + org.apache.mina.filter.codec.protobuf.FoobarProto.Foo, Builder> { + // Construct using org.apache.mina.filter.codec.protobuf.TestProto.Foo.newBuilder() + private Builder() {} + + org.apache.mina.filter.codec.protobuf.FoobarProto.Foo result = new org.apache.mina.filter.codec.protobuf.FoobarProto.Foo(); + + @Override + protected org.apache.mina.filter.codec.protobuf.FoobarProto.Foo internalGetResult() { + return result; + } + + @Override + public Builder clear() { + result = new org.apache.mina.filter.codec.protobuf.FoobarProto.Foo(); + return this; + } + + @Override + public Builder clone() { + return new Builder().mergeFrom(result); + } + + @Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return org.apache.mina.filter.codec.protobuf.FoobarProto.Foo.getDescriptor(); + } + + public org.apache.mina.filter.codec.protobuf.FoobarProto.Foo getDefaultInstanceForType() { + return org.apache.mina.filter.codec.protobuf.FoobarProto.Foo.getDefaultInstance(); + } + + public org.apache.mina.filter.codec.protobuf.FoobarProto.Foo build() { + if (!isInitialized()) { + throw new com.google.protobuf.UninitializedMessageException( + result); + } + return buildPartial(); + } + + private org.apache.mina.filter.codec.protobuf.FoobarProto.Foo buildParsed() + throws com.google.protobuf.InvalidProtocolBufferException { + if (!isInitialized()) { + throw new com.google.protobuf.UninitializedMessageException( + result).asInvalidProtocolBufferException(); + } + return buildPartial(); + } + + public org.apache.mina.filter.codec.protobuf.FoobarProto.Foo buildPartial() { + org.apache.mina.filter.codec.protobuf.FoobarProto.Foo returnMe = result; + result = null; + return returnMe; + } + + + // required int32 foo = 1; + public boolean hasFoo() { + return result.hasFoo(); + } + public int getFoo() { + return result.getFoo(); + } + public Builder setFoo(int value) { + result.hasFoo = true; + result.foo_ = value; + return this; + } + public Builder clearFoo() { + result.hasFoo = false; + result.foo_ = 0; + return this; + } + } + + static { + org.apache.mina.filter.codec.protobuf.FoobarProto.getDescriptor(); + } + } + + public static final class Bar extends + com.google.protobuf.GeneratedMessage { + // Use Bar.newBuilder() to construct. + private Bar() {} + + private static final Bar defaultInstance = new Bar(); + public static Bar getDefaultInstance() { + return defaultInstance; + } + + public Bar getDefaultInstanceForType() { + return defaultInstance; + } + + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return org.apache.mina.filter.codec.protobuf.FoobarProto.internal_static_Bar_descriptor; + } + + @Override + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable + internalGetFieldAccessorTable() { + return org.apache.mina.filter.codec.protobuf.FoobarProto.internal_static_Bar_fieldAccessorTable; + } + + public static + com.google.protobuf.GeneratedMessage.GeneratedExtension< + org.apache.mina.filter.codec.protobuf.FoobarProto.Foo, + java.lang.String> bar; + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Bar parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Bar parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistry extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Bar parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data).buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Bar parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistry extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return newBuilder().mergeFrom(data, extensionRegistry) + .buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Bar parseFrom(java.io.InputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Bar parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistry extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Bar parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return newBuilder().mergeFrom(input).buildParsed(); + } + public static org.apache.mina.filter.codec.protobuf.FoobarProto.Bar parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistry extensionRegistry) + throws java.io.IOException { + return newBuilder().mergeFrom(input, extensionRegistry) + .buildParsed(); + } + + public static Builder newBuilder() { return new Builder(); } + public Builder newBuilderForType() { return new Builder(); } + public static Builder newBuilder(org.apache.mina.filter.codec.protobuf.FoobarProto.Bar prototype) { + return new Builder().mergeFrom(prototype); + } + + public static final class Builder extends + com.google.protobuf.GeneratedMessage.Builder<Builder> { + // Construct using org.apache.mina.filter.codec.protobuf.TestProto.Bar.newBuilder() + private Builder() {} + + org.apache.mina.filter.codec.protobuf.FoobarProto.Bar result = new org.apache.mina.filter.codec.protobuf.FoobarProto.Bar(); + + @Override + protected org.apache.mina.filter.codec.protobuf.FoobarProto.Bar internalGetResult() { + return result; + } + + @Override + public Builder clear() { + result = new org.apache.mina.filter.codec.protobuf.FoobarProto.Bar(); + return this; + } + + @Override + public Builder clone() { + return new Builder().mergeFrom(result); + } + + @Override + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return org.apache.mina.filter.codec.protobuf.FoobarProto.Bar.getDescriptor(); + } + + public org.apache.mina.filter.codec.protobuf.FoobarProto.Bar getDefaultInstanceForType() { + return org.apache.mina.filter.codec.protobuf.FoobarProto.Bar.getDefaultInstance(); + } + + public org.apache.mina.filter.codec.protobuf.FoobarProto.Bar build() { + if (!isInitialized()) { + throw new com.google.protobuf.UninitializedMessageException( + result); + } + return buildPartial(); + } + + private org.apache.mina.filter.codec.protobuf.FoobarProto.Bar buildParsed() + throws com.google.protobuf.InvalidProtocolBufferException { + if (!isInitialized()) { + throw new com.google.protobuf.UninitializedMessageException( + result).asInvalidProtocolBufferException(); + } + return buildPartial(); + } + + public org.apache.mina.filter.codec.protobuf.FoobarProto.Bar buildPartial() { + org.apache.mina.filter.codec.protobuf.FoobarProto.Bar returnMe = result; + result = null; + return returnMe; + } + + } + + static { + org.apache.mina.filter.codec.protobuf.FoobarProto.getDescriptor(); + } + } + + private static com.google.protobuf.Descriptors.Descriptor + internal_static_Foo_descriptor; + private static + com.google.protobuf.GeneratedMessage.FieldAccessorTable + internal_static_Foo_fieldAccessorTable; + private static com.google.protobuf.Descriptors.Descriptor + internal_static_Bar_descriptor; + private static + com.google.protobuf.GeneratedMessage.FieldAccessorTable + internal_static_Bar_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor + getDescriptor() { + return descriptor; + } + private static com.google.protobuf.Descriptors.FileDescriptor + descriptor; + static { + java.lang.String descriptorData = + "\n\007x.proto\"\030\n\003Foo\022\013\n\003foo\030\001 \002(\005*\004\010\005\020\013\"\030\n\003B" + + "ar2\021\n\003bar\022\004.Foo\030\005 \002(\tB2\n%org.apache.mina" + + ".filter.codec.protobufB\tTestProto"; + com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = + new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() { + public com.google.protobuf.ExtensionRegistry assignDescriptors( + com.google.protobuf.Descriptors.FileDescriptor root) { + descriptor = root; + internal_static_Foo_descriptor = + getDescriptor().getMessageTypes().get(0); + internal_static_Foo_fieldAccessorTable = new + com.google.protobuf.GeneratedMessage.FieldAccessorTable( + internal_static_Foo_descriptor, + new java.lang.String[] { "Foo", }, + org.apache.mina.filter.codec.protobuf.FoobarProto.Foo.class, + org.apache.mina.filter.codec.protobuf.FoobarProto.Foo.Builder.class); + internal_static_Bar_descriptor = + getDescriptor().getMessageTypes().get(1); + internal_static_Bar_fieldAccessorTable = new + com.google.protobuf.GeneratedMessage.FieldAccessorTable( + internal_static_Bar_descriptor, + new java.lang.String[] { }, + org.apache.mina.filter.codec.protobuf.FoobarProto.Bar.class, + org.apache.mina.filter.codec.protobuf.FoobarProto.Bar.Builder.class); + org.apache.mina.filter.codec.protobuf.FoobarProto.Bar.bar = + com.google.protobuf.GeneratedMessage.newGeneratedExtension( + org.apache.mina.filter.codec.protobuf.FoobarProto.Bar.getDescriptor().getExtensions().get(0), + java.lang.String.class); + return null; + } + }; + com.google.protobuf.Descriptors.FileDescriptor + .internalBuildGeneratedFileFrom(descriptorData, + new com.google.protobuf.Descriptors.FileDescriptor[] { + }, assigner); + } +}
Added: mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/ProtobufCodecFactoryTest.java URL: http://svn.apache.org/viewvc/mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/ProtobufCodecFactoryTest.java?rev=732497&view=auto ============================================================================== --- mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/ProtobufCodecFactoryTest.java (added) +++ mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/ProtobufCodecFactoryTest.java Wed Jan 7 13:12:46 2009 @@ -0,0 +1,100 @@ +/* + * 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.mina.filter.codec.protobuf; + +import static org.junit.Assert.*; +import static org.hamcrest.core.Is.*; +import static org.hamcrest.core.IsNot.*; +import static org.hamcrest.core.IsNull.*; +import static org.hamcrest.core.IsInstanceOf.*; +import static org.hamcrest.core.IsSame.*; + +import org.apache.mina.filter.codec.ProtocolDecoder; +import org.apache.mina.filter.codec.ProtocolEncoder; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * @author Tomasz Blachowicz + * @author The Apache MINA Project ([email protected]) + * @version $Rev$, $Date$ + */ +public class ProtobufCodecFactoryTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetEncoder() throws Exception { + ProtobufCodecFactory factory = ProtobufCodecFactory.newInstance(null); + ProtocolEncoder encoder = factory.getEncoder(null); + assertThat(encoder, is(not(nullValue()))); + assertThat(encoder, is(instanceOf(ProtobufEncoder.class))); + } + + @Test + public void testGetEncoderMultiple() throws Exception { + ProtobufCodecFactory factory = ProtobufCodecFactory.newInstance(null); + ProtocolEncoder encoder = factory.getEncoder(null); + ProtocolEncoder anotherEncoder = factory.getEncoder(null); + assertThat(anotherEncoder, is(sameInstance(encoder))); + } + + @Test + public void testGetEncoderSingleton() throws Exception { + ProtobufCodecFactory factory = ProtobufCodecFactory.newInstance(null); + ProtocolEncoder encoder = factory.getEncoder(null); + ProtobufCodecFactory anotherFactory = ProtobufCodecFactory.newInstance(null); + ProtocolEncoder anotherEncoder = anotherFactory.getEncoder(null); + assertThat(anotherEncoder, is(sameInstance(encoder))); + } + + @Test + public void testGetDecoder() throws Exception { + ProtobufCodecFactory factory = ProtobufCodecFactory.newInstance(null); + ProtocolDecoder decoder = factory.getDecoder(null); + assertThat(decoder, is(not(nullValue()))); + assertThat(decoder, is(instanceOf(ProtobufDecoder.class))); + } + + @Test + public void testGetDecoderMultiple() throws Exception { + ProtobufCodecFactory factory = ProtobufCodecFactory.newInstance(null); + ProtocolDecoder decoder = factory.getDecoder(null); + ProtocolDecoder anotherDecoder = factory.getDecoder(null); + assertThat(anotherDecoder, is(sameInstance(decoder))); + } + + @Test + public void testGetDecoderNoSingleton() throws Exception { + ProtobufCodecFactory factory = ProtobufCodecFactory.newInstance(null); + ProtocolDecoder decoder = factory.getDecoder(null); + ProtobufCodecFactory anotherFactory = ProtobufCodecFactory.newInstance(null); + ProtocolDecoder anotherDecoder = anotherFactory.getDecoder(null); + assertThat(anotherDecoder, is(not(sameInstance(decoder)))); + } + +} Added: mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/ProtobufDecoderTest.java URL: http://svn.apache.org/viewvc/mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/ProtobufDecoderTest.java?rev=732497&view=auto ============================================================================== --- mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/ProtobufDecoderTest.java (added) +++ mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/ProtobufDecoderTest.java Wed Jan 7 13:12:46 2009 @@ -0,0 +1,174 @@ +/* + * 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.mina.filter.codec.protobuf; + +import static org.junit.Assert.*; +import static org.hamcrest.core.Is.*; +import static org.hamcrest.core.IsNot.*; +import static org.hamcrest.core.IsNull.*; + +import java.io.IOException; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.mina.core.buffer.IoBuffer; +import org.apache.mina.core.filterchain.IoFilter.NextFilter; +import org.apache.mina.core.session.DummySession; +import org.apache.mina.core.session.IoSession; +import org.apache.mina.filter.codec.ProtocolDecoderOutput; +import org.junit.Test; + +import com.google.protobuf.ExtensionRegistry; + +/** + * @author Tomasz Blachowicz + * @author The Apache MINA Project ([email protected]) + * @version $Rev$, $Date$ + */ +public class ProtobufDecoderTest { + + @Test + public void testDecode() throws Exception { + + FoobarProto.Foo foo = FoobarProto.Foo.newBuilder().setFoo(666).build(); + IoBuffer in = IoBuffer.wrap(ProtobufEncoderTest.encode(foo)); + ProtobufDecoder decoder = new ProtobufDecoder(FoobarProto.Foo.getDefaultInstance()); + + IoSession session = new DummySession(); + final AtomicReference<FoobarProto.Foo> msg = new AtomicReference<FoobarProto.Foo>(); + decoder.decode(session, in, new ProtocolDecoderOutput() { + public void flush(NextFilter nextFilter, IoSession session) {} + public void write(Object message) { + msg.set((FoobarProto.Foo) message); + }}); + + assertThat(msg.get(), is(foo)); + assertThat(in.remaining(), is(0)); + assertThat(session.getAttribute(SizeContext.KEY), is(nullValue())); + + } + + @Test + public void testDecodeWithExtension() throws Exception { + + FoobarProto.Foo foo = FoobarProto.Foo.newBuilder() + .setFoo(666) + .setExtension(FoobarProto.Bar.bar, "Protocol Buffers").build(); + IoBuffer in = IoBuffer.wrap(ProtobufEncoderTest.encode(foo)); + ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance(); + extensionRegistry.add(FoobarProto.Bar.bar); + + IoSession session = new DummySession(); + ProtobufDecoder decoder = new ProtobufDecoder(FoobarProto.Foo.getDefaultInstance(), extensionRegistry); + final AtomicReference<FoobarProto.Foo> msg = new AtomicReference<FoobarProto.Foo>(); + decoder.decode(session, in, new ProtocolDecoderOutput() { + public void flush(NextFilter nextFilter, IoSession session) {} + public void write(Object message) { + msg.set((FoobarProto.Foo) message); + }}); + + assertThat(msg.get(), is(foo)); + assertThat(in.remaining(), is(0)); + assertThat(session.getAttribute(SizeContext.KEY), is(nullValue())); + } + + @Test + public void testDecodeMoreData() throws Exception { + + FoobarProto.Foo foo1 = FoobarProto.Foo.newBuilder().setFoo(666).build(); + FoobarProto.Foo foo2 = FoobarProto.Foo.newBuilder().setFoo(13).build(); + IoBuffer in = IoBuffer.allocate(1); + in.setAutoExpand(true); + in.put(ProtobufEncoderTest.encode(foo1)); + in.put(ProtobufEncoderTest.encode(foo2)); + in.flip(); + + IoSession session = new DummySession(); + ProtobufDecoder decoder = new ProtobufDecoder(FoobarProto.Foo.getDefaultInstance()); + final AtomicReference<FoobarProto.Foo> msg = new AtomicReference<FoobarProto.Foo>(); + decoder.decode(session, in, new ProtocolDecoderOutput() { + public void flush(NextFilter nextFilter, IoSession session) {} + public void write(Object message) { + msg.set((FoobarProto.Foo) message); + }}); + + assertThat(msg.get(), is(foo1)); + assertThat(in.remaining(), is(ProtobufEncoderTest.encode(foo2).length)); + assertThat(session.getAttribute(SizeContext.KEY), is(nullValue())); + + } + + @Test + public void testDecodeNotEnoughData() throws Exception { + + FoobarProto.Foo foo = FoobarProto.Foo.newBuilder().setFoo(482726716).build(); + IoBuffer in = IoBuffer.allocate(5); + in.put(ProtobufEncoderTest.encode(foo), 0, 5); + in.flip(); + + IoSession session = new DummySession(); + ProtobufDecoder decoder = new ProtobufDecoder(FoobarProto.Foo.getDefaultInstance()); + decoder.decode(session, in, new ProtocolDecoderOutput() { + public void flush(NextFilter nextFilter, IoSession session) {} + public void write(Object message) {}}); + + assertThat(in.remaining(), is(5)); + SizeContext ctx = (SizeContext) session.getAttribute(SizeContext.KEY); + assertThat(ctx, is(not(nullValue()))); + + } + + @Test + public void testDecodeSequence() throws Exception { + FoobarProto.Foo foo1 = FoobarProto.Foo.newBuilder() + .setFoo(666) + .setExtension(FoobarProto.Bar.bar, "Apache MINA").build(); + FoobarProto.Foo foo2 = FoobarProto.Foo.newBuilder().setFoo(1977).build(); + IoBuffer in = IoBuffer.allocate(1); + in.setAutoExpand(true); + in.put(ProtobufEncoderTest.encode(foo1)); + in.put(ProtobufEncoderTest.encode(foo2)); + in.flip(); + + ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance(); + extensionRegistry.add(FoobarProto.Bar.bar); + + IoSession session = new DummySession(); + ProtobufDecoder decoder = new ProtobufDecoder( + FoobarProto.Foo.getDefaultInstance(), + extensionRegistry); + final AtomicReference<FoobarProto.Foo> msg = new AtomicReference<FoobarProto.Foo>(); + + decoder.decode(session, in, new ProtocolDecoderOutput() { + public void flush(NextFilter nextFilter, IoSession session) {} + public void write(Object message) { + msg.set((FoobarProto.Foo) message); + }}); + assertThat(msg.get(), is(foo1)); + + decoder.decode(session, in, new ProtocolDecoderOutput() { + public void flush(NextFilter nextFilter, IoSession session) {} + public void write(Object message) { + msg.set((FoobarProto.Foo) message); + }}); + assertThat(msg.get(), is(foo2)); + + } + +} Added: mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/ProtobufEncoderTest.java URL: http://svn.apache.org/viewvc/mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/ProtobufEncoderTest.java?rev=732497&view=auto ============================================================================== --- mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/ProtobufEncoderTest.java (added) +++ mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/java/org/apache/mina/filter/codec/protobuf/ProtobufEncoderTest.java Wed Jan 7 13:12:46 2009 @@ -0,0 +1,84 @@ +/* + * 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.mina.filter.codec.protobuf; + +import static org.junit.Assert.*; +import static org.hamcrest.core.Is.*; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.mina.core.buffer.IoBuffer; +import org.apache.mina.core.future.WriteFuture; +import org.apache.mina.filter.codec.ProtocolEncoderOutput; +import org.junit.Test; + +import com.google.protobuf.CodedOutputStream; +import com.google.protobuf.Message; + +/** + * @author Tomasz Blachowicz + * @author The Apache MINA Project ([email protected]) + * @version $Rev$, $Date$ + */ +public class ProtobufEncoderTest { + + @Test + public void testEncode() throws Exception { + FoobarProto.Foo foo = FoobarProto.Foo.newBuilder().setFoo(666).build(); + final AtomicReference<IoBuffer> buf = new AtomicReference<IoBuffer>(); + ProtobufEncoder encoder = new ProtobufEncoder(); + encoder.encode(null, foo, new ProtocolEncoderOutput() { + public WriteFuture flush() { return null; } + public void mergeAll() { } + public void write(Object encodedMessage) { + buf.set((IoBuffer) encodedMessage); + }}); + assertThat(buf.get().array(), is(encode(foo))); + } + + @Test + public void testEncodeWithExtension() throws Exception { + FoobarProto.Foo foo = FoobarProto.Foo.newBuilder() + .setFoo(666) + .setExtension(FoobarProto.Bar.bar, "Apache MINA") + .build(); + final AtomicReference<IoBuffer> buf = new AtomicReference<IoBuffer>(); + ProtobufEncoder encoder = new ProtobufEncoder(); + encoder.encode(null, foo, new ProtocolEncoderOutput() { + public WriteFuture flush() { return null; } + public void mergeAll() { } + public void write(Object encodedMessage) { + buf.set((IoBuffer) encodedMessage); + }}); + assertThat(buf.get().array(), is(encode(foo))); + } + + static byte[] encode(Message message) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + CodedOutputStream os = CodedOutputStream.newInstance(baos); + os.writeRawVarint32(message.getSerializedSize()); + message.writeTo(os); + os.flush(); + return baos.toByteArray(); + } + +} Added: mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/proto/foobar.proto URL: http://svn.apache.org/viewvc/mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/proto/foobar.proto?rev=732497&view=auto ============================================================================== --- mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/proto/foobar.proto (added) +++ mina/sandbox/protocol-buffers/filter-codec-protobuf/src/test/proto/foobar.proto Wed Jan 7 13:12:46 2009 @@ -0,0 +1,13 @@ +option java_package = "org.apache.mina.filter.codec.protobuf"; +option java_outer_classname = "FoobarProto"; + +message Foo { + required int32 foo = 1; + extensions 5 to 10; +} + +message Bar { + extend Foo { + required string bar = 5; + } +} Modified: mina/sandbox/protocol-buffers/pom.xml URL: http://svn.apache.org/viewvc/mina/sandbox/protocol-buffers/pom.xml?rev=732497&r1=732496&r2=732497&view=diff ============================================================================== --- mina/sandbox/protocol-buffers/pom.xml (original) +++ mina/sandbox/protocol-buffers/pom.xml Wed Jan 7 13:12:46 2009 @@ -117,6 +117,24 @@ <dependency> <groupId>${groupId}</groupId> + <artifactId>mina-filter-codec-protobuf</artifactId> + <version>${pom.version}</version> + </dependency> + + <dependency> + <groupId>${groupId}</groupId> + <artifactId>mina-filter-codec-protobuf</artifactId> + <version>${pom.version}</version> + </dependency> + + <dependency> + <groupId>${groupId}</groupId> + <artifactId>mina-filter-codec-protobuf</artifactId> + <version>${pom.version}</version> + </dependency> + + <dependency> + <groupId>${groupId}</groupId> <artifactId>mina-integration-beans</artifactId> <version>${pom.version}</version> </dependency> @@ -204,6 +222,13 @@ <artifactId>commons-lang</artifactId> <version>2.4</version> </dependency> + + <!-- Protocol Buffer Codec --> + <dependency> + <groupId>com.google.protobuf</groupId> + <artifactId>protobuf-java</artifactId> + <version>2.0.3</version> + </dependency> <!-- Logging --> <dependency> @@ -318,6 +343,7 @@ <module>transport-apr</module> <module>filter-compression</module> <module>filter-codec-netty</module> + <module>filter-codec-protobuf</module> <module>statemachine</module> <module>integration-beans</module> <module>integration-xbean</module> @@ -527,4 +553,13 @@ </plugin> </plugins> </reporting> + + <repositories> + <repository> + <id>google-maven-repository</id> + <name>Google Maven Repository</name> + <url>http://google-maven-repository.googlecode.com/svn/repository/</url> + </repository> + </repositories> + </project>
