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;
    21
    22  import java.io.IOException;
    23  import java.nio.ByteBuffer;
    24  import java.util.LinkedList;
    25  import java.util.List;
    26
    27  import org.apache.mina.generated.protoc.AddressBookProtos.Person;
    28  import org.apache.mina.util.ByteBufferOutputStream;
    29
    30  /**
    31   * A {@link ProtobufEncoder} and {@link ProtobufDecoder} test.
    32   *
    33   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    34   */
    35  public class ProtobufTest extends DelimitTest<Person> {
    36
    37      @Override
    38      public List<Person> getObjects() {
    39
    40          List<Person> list = new LinkedList<Person>();
    41
    42          list.add(Person.newBuilder().setId(1).setName("Jean Dupond").setEmail("john.white@bigcorp.com").build());
    43          list.add(Person.newBuilder().setId(2).setName("Marie Blanc").setEmail("marie.blanc@bigcorp.com").build());
    44
    45          return list;
    46      }
    47
    48      @Override
    49      protected ByteBuffer delimitWithOriginal() throws IOException {
    50          ByteBufferOutputStream bbos = new ByteBufferOutputStream();
    51          bbos.setElastic(true);
    52
    53          for (Person p : getObjects()) {
    54              p.writeDelimitedTo(bbos);
    55          }
    56          return bbos.getByteBuffer();
    57      }
    58
    59      @Override
    60      public SizePrefixedEncoder<Person> getSerializer() {
    61          return ProtobufEncoder.newInstance(Person.class);
    62      }
    63  }