View Javadoc

1   package org.apache.mina.codec.delimited;
2   
3   /*
4    *  Licensed to the Apache Software Foundation (ASF) under one
5    *  or more contributor license agreements.  See the NOTICE file
6    *  distributed with this work for additional information
7    *  regarding copyright ownership.  The ASF licenses this file
8    *  to you under the Apache License, Version 2.0 (the
9    *  "License"); you may not use this file except in compliance
10   *  with the License.  You may obtain a copy of the License at
11   *
12   *    http://www.apache.org/licenses/LICENSE-2.0
13   *
14   *  Unless required by applicable law or agreed to in writing,
15   *  software distributed under the License is distributed on an
16   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   *  KIND, either express or implied.  See the License for the
18   *  specific language governing permissions and limitations
19   *  under the License.
20   *
21   */
22  
23  import java.nio.ByteBuffer;
24  import java.util.LinkedList;
25  import java.util.List;
26  
27  import org.apache.mina.generated.thrift.UserProfile;
28  import org.apache.thrift.TSerializer;
29  import org.apache.thrift.transport.TFramedTransport;
30  import org.apache.thrift.transport.TMemoryBuffer;
31  
32  /**
33   * A {@link ThriftEncoder} and {@link ThriftDecoder} test.
34   * 
35   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
36   */
37  public class ThriftTest extends DelimitTest<UserProfile> {
38  
39      @Override
40      public List<UserProfile> getObjects() {
41  
42          List<UserProfile> list = new LinkedList<UserProfile>();
43  
44          list.add(new UserProfile().setUid(1).setName("Jean Dupond"));
45          list.add(new UserProfile().setUid(2).setName("Marie Blanc"));
46  
47          return list;
48      }
49  
50      @Override
51      protected ByteBuffer delimitWithOriginal() throws Exception {
52  
53          TMemoryBuffer m = new TMemoryBuffer(1000000);
54          TFramedTransport t = new TFramedTransport(m);
55          TSerializer tt = new TSerializer();
56          for (UserProfile up : getObjects()) {
57              t.write(tt.serialize(up));
58              t.flush();
59          }
60          return ByteBuffer.wrap(m.getArray(), 0, m.length());
61  
62      }
63  
64      @Override
65      public SizePrefixedEncoder<UserProfile> getSerializer() throws SecurityException, NoSuchMethodException {
66          return ThriftEncoder.newInstance(UserProfile.class);
67      }
68  
69  }