1   /***
2    * 
3    * Copyright 2004 Hiram Chirino
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License. You may obtain a copy of
7    * the License at
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package org.activeio.packet;
18  
19  import java.util.Arrays;
20  
21  import junit.framework.TestCase;
22  
23  import org.activeio.Packet;
24  
25  /***
26   */
27  abstract public class PacketTestSupport extends TestCase {
28      abstract Packet createTestPacket(int capacity);
29      
30      public void testInit() {
31          Packet packet = createTestPacket(100);
32          assertEquals( 100, packet.capacity() );        
33          assertEquals( 0, packet.position());        
34          assertEquals( 100, packet.limit() );        
35          assertEquals( 100, packet.remaining() );        
36          assertTrue( packet.hasRemaining() );        
37      }
38      
39      public void testPosition() {
40          Packet packet = createTestPacket(100);
41          packet.position(10);
42          assertEquals( 10, packet.position() );        
43      }
44  
45      public void testLimit() {
46          Packet packet = createTestPacket(100);
47          packet.limit(10);
48          assertEquals( 10, packet.limit() );        
49      }
50  
51      public void testRemaining() {
52          Packet packet = createTestPacket(100);
53          packet.position(5);
54          packet.limit(95);
55          assertEquals(90, packet.remaining());
56          assertTrue(packet.hasRemaining());
57  
58          packet.position(5);
59          packet.limit(5);
60          assertEquals(0, packet.remaining());
61          assertFalse(packet.hasRemaining());
62      }
63      
64      public void testFlip() {
65          Packet packet = createTestPacket(100);
66          packet.position(95);
67          packet.flip();        
68          assertEquals(0, packet.position());
69          assertEquals(95, packet.limit());
70      }
71      
72      public void testClear() {
73          Packet packet = createTestPacket(100);
74          packet.position(5);
75          packet.limit(95);
76          packet.clear();        
77          assertEquals(0, packet.position());
78          assertEquals(100, packet.limit());
79      }
80  
81      public void testDuplicate() {
82          Packet packet = createTestPacket(100);
83          packet.position(5);
84          packet.limit(95);
85          Packet packet2 = packet.duplicate();
86          packet2.position(10);
87          packet2.limit(20);
88          
89          assertEquals(5, packet.position());
90          assertEquals(95, packet.limit());
91          assertEquals(10, packet2.position());
92          assertEquals(20, packet2.limit());
93      }
94  
95      public void testRewind() {
96          Packet packet = createTestPacket(100);
97          packet.position(5);
98          packet.limit(95);
99          packet.rewind();
100         
101         assertEquals(0, packet.position());
102         assertEquals(95, packet.limit());
103     }
104     
105     public void testSlice() {
106         Packet packet = createTestPacket(100);
107         packet.position(5);
108         packet.limit(95);
109         Packet packet2 = packet.slice();
110         
111         assertEquals(0, packet2.position());
112         assertEquals(90, packet2.capacity());
113         assertEquals(90, packet2.limit());
114     }
115 
116     public void testWriteAndReadByte() {
117         
118         Packet packet = createTestPacket(256);
119         for(int i=0; i < 256; i++) {
120             assertTrue(packet.write(i));
121         }
122         assertFalse(packet.write(0));
123         
124         packet.flip();
125         for(int i=0; i < 256; i++) {
126             assertEquals(i, packet.read());
127         }       
128         assertEquals(-1, packet.read());        
129     }
130     
131     public void testWriteAndReadBulkByte() {
132         
133         byte data[] = new byte[10];        
134         Packet packet = createTestPacket(data.length*10);
135         for(int i=0; i < 10; i++) {
136             Arrays.fill(data,(byte)i);
137             assertEquals(data.length, packet.write(data,0,data.length));
138         }
139         assertEquals(-1, packet.write(data,0,data.length));
140         
141         byte buffer[] = new byte[data.length];
142         packet.flip();
143         for(int i=0; i < 10; i++) {
144             assertEquals(buffer.length, packet.read(buffer,0,buffer.length));
145             Arrays.fill(data,(byte)i);
146             assertEquals(buffer, data);
147         }       
148         assertEquals(-1, packet.read(buffer,0,buffer.length));
149     }
150  
151     public void assertEquals(byte buffer[], byte data[]) {
152         assertEquals(buffer.length, data.length);
153         for (int i = 0; i < data.length; i++) {
154             assertEquals(buffer[i], data[i]);
155         }
156     }
157 }