View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.codec.delimited.serialization;
    21
    22  import static org.junit.Assert.assertEquals;
    23
    24  import java.util.LinkedList;
    25  import java.util.List;
    26
    27  import org.apache.mina.codec.IoBuffer;
    28  import org.apache.mina.codec.delimited.ByteBufferEncoder;
    29  import org.apache.mina.codec.delimited.IoBufferDecoder;
    30  import org.apache.mina.codec.delimited.serialization.ProtobufDynamicMessageDecoder.ProtobufSerializedMessage;
    31  import org.apache.mina.generated.protoc.AddressBookProtos.Person;
    32  import org.junit.Test;
    33
    34  /**
    35   * A {@link ProtobufMessageEncoder} and {@link ProtobufMessageDecoder} test.
    36   *
    37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    38   */
    39  public class ProtobufTest extends GenericSerializerTest<Person> {
    40
    41  	@Override
    42  	public List<Person> getObjects() {
    43  		List<Person> list = new LinkedList<Person>();
    44
    45  		list.add(Person.newBuilder().setId(1).setName("Jean Dupond")
    46  				.setEmail("john.white@bigcorp.com").build());
    47  		list.add(Person.newBuilder().setId(2).setName("Marie Blanc")
    48  				.setEmail("marie.blanc@bigcorp.com").build());
    49
    50  		return list;
    51  	}
    52
    53  	@Override
    54  	public IoBufferDecoder<Person> getDecoder() throws Exception {
    55  		return ProtobufMessageDecoder.newInstance(Person.class);
    56  	}
    57
    58  	@Override
    59  	public ByteBufferEncoder<Person> getEncoder() throws Exception {
    60  		return new ProtobufMessageEncoder<Person>();
    61  	}
    62
    63  	@Test
    64  	public void testDynamic() throws Exception {
    65  		ByteBufferEncoder<Person> encoder = getEncoder();
    66  		ProtobufDynamicMessageDecoder decoder = new ProtobufDynamicMessageDecoder();
    67
    68  		for (Person object : getObjects()) {
    69  			ProtobufSerializedMessage message = decoder.decode(IoBuffer
    70  					.wrap(encoder.encode(object)));
    71  			assertEquals(object, message.get(Person.class));
    72  		}
    73  	}
    74  }