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 static org.junit.Assert.assertEquals;
23
24 import java.nio.ByteBuffer;
25 import java.util.LinkedList;
26 import java.util.List;
27
28 import org.junit.Test;
29
30 /**
31 * An abstract {@link SizePrefixedEncoder} and {@link SizePrefixedDecoder} test.
32 *
33 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
34 */
35 public abstract class DelimitTest<T> {
36
37 public abstract List<T> getObjects();
38
39 protected abstract ByteBuffer delimitWithOriginal() throws Exception;
40
41 public abstract SizePrefixedEncoder<T> getSerializer() throws Exception;
42
43 final protected ByteBuffer delimitWithMina() throws Exception {
44 SizePrefixedEncoder<T> pe = getSerializer();
45
46 List<ByteBuffer> buffers = new LinkedList<ByteBuffer>();
47 for (T p : getObjects()) {
48 buffers.add(pe.encode(p, null));
49 }
50
51 int size = 0;
52 for (ByteBuffer b : buffers) {
53 size += b.remaining();
54 }
55
56 ByteBuffer buffer = ByteBuffer.allocate(size);
57 for (ByteBuffer b : buffers) {
58 buffer.put(b);
59 }
60 buffer.flip();
61 return buffer;
62 }
63
64 @Test
65 public void testDelimit() throws Exception {
66 assertEquals(delimitWithOriginal(), delimitWithMina());
67 }
68
69 }