1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.mina.common;
20
21 import java.nio.BufferOverflowException;
22 import java.nio.charset.Charset;
23 import java.nio.charset.CharsetDecoder;
24 import java.nio.charset.CharsetEncoder;
25
26 import junit.framework.Assert;
27 import junit.framework.TestCase;
28
29 /***
30 * Tests {@link ByteBuffer}.
31 *
32 * @author Trustin Lee (trustin@apache.org)
33 * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
34 */
35 public class ByteBufferTest extends TestCase
36 {
37
38 public static void main( String[] args )
39 {
40 junit.textui.TestRunner.run( ByteBufferTest.class );
41 }
42
43 protected void setUp() throws Exception
44 {
45 }
46
47 protected void tearDown() throws Exception
48 {
49 }
50
51 public void testAllocate() throws Exception
52 {
53 for( int i = 10; i < 1048576 * 2; i = i * 11 / 10 )
54 {
55 ByteBuffer buf = ByteBuffer.allocate( i );
56 Assert.assertEquals( 0, buf.position() );
57 Assert.assertEquals( buf.capacity(), buf.remaining() );
58 Assert.assertTrue( buf.capacity() >= i );
59 Assert.assertTrue( buf.capacity() < i * 2 );
60 }
61 }
62
63 public void testRelease() throws Exception
64 {
65 for( int i = 10; i < 1048576 * 2; i = i * 11 / 10 )
66 {
67 ByteBuffer buf = ByteBuffer.allocate( i );
68 Assert.assertEquals( 0, buf.position() );
69 Assert.assertEquals( buf.capacity(), buf.remaining() );
70 Assert.assertTrue( buf.capacity() >= i );
71 Assert.assertTrue( buf.capacity() < i * 2 );
72 buf.release();
73 }
74 }
75
76 public void testLeakageDetection() throws Exception
77 {
78 ByteBuffer buf = ByteBuffer.allocate( 1024 );
79 buf.release();
80 try
81 {
82 buf.release();
83 Assert.fail( "Releasing a buffer twice should fail." );
84 }
85 catch( IllegalStateException e )
86 {
87
88 }
89 }
90
91 public void testAcquireRelease() throws Exception
92 {
93 ByteBuffer buf = ByteBuffer.allocate( 1024 );
94 buf.acquire();
95 buf.release();
96 buf.acquire();
97 buf.acquire();
98 buf.release();
99 buf.release();
100 buf.release();
101 try
102 {
103 buf.release();
104 Assert.fail( "Releasing a buffer twice should fail." );
105 }
106 catch( IllegalStateException e )
107 {
108 }
109 }
110
111 public void testAutoExpand() throws Exception
112 {
113 ByteBuffer buf = ByteBuffer.allocate( 1 );
114
115 buf.put( (byte) 0 );
116 try
117 {
118 buf.put( (byte) 0 );
119 Assert.fail();
120 }
121 catch( BufferOverflowException e )
122 {
123
124 }
125
126 buf.setAutoExpand( true );
127 buf.put( (byte) 0 );
128 Assert.assertEquals( 2, buf.position() );
129 Assert.assertEquals( 2, buf.limit() );
130 Assert.assertEquals( 2, buf.capacity() );
131
132 buf.setAutoExpand( false );
133 try
134 {
135 buf.put( 3, (byte) 0 );
136 Assert.fail();
137 }
138 catch( IndexOutOfBoundsException e )
139 {
140
141 }
142
143 buf.setAutoExpand( true );
144 buf.put( 3, (byte) 0 );
145 Assert.assertEquals( 2, buf.position() );
146 Assert.assertEquals( 4, buf.limit() );
147 Assert.assertEquals( 4, buf.capacity() );
148 }
149
150 public void testGetString() throws Exception
151 {
152 ByteBuffer buf = ByteBuffer.allocate( 16 );
153 CharsetDecoder decoder;
154
155 decoder = Charset.forName( "ISO-8859-1" ).newDecoder();
156 buf.put( (byte) 'A' );
157 buf.put( (byte) 'B' );
158 buf.put( (byte) 'C' );
159 buf.put( (byte) 0 );
160
161 buf.position( 0 );
162 Assert.assertEquals( "ABC", buf.getString( decoder ) );
163 Assert.assertEquals( 4, buf.position() );
164
165 buf.position( 0 );
166 buf.limit( 1 );
167 Assert.assertEquals( "A", buf.getString( decoder ) );
168 Assert.assertEquals( 1, buf.position() );
169
170 buf.clear();
171 Assert.assertEquals( "ABC", buf.getString( 10, decoder ) );
172 Assert.assertEquals( 10, buf.position() );
173
174 buf.clear();
175 Assert.assertEquals( "A", buf.getString( 1, decoder ) );
176 Assert.assertEquals( 1, buf.position() );
177
178 buf.clear();
179 buf.fillAndReset( buf.limit() );
180 decoder = Charset.forName( "UTF-16" ).newDecoder();
181 buf.put( (byte) 0 );
182 buf.put( (byte) 'A' );
183 buf.put( (byte) 0 );
184 buf.put( (byte) 'B' );
185 buf.put( (byte) 0 );
186 buf.put( (byte) 'C' );
187 buf.put( (byte) 0 );
188 buf.put( (byte) 0 );
189
190 buf.position( 0 );
191 Assert.assertEquals( "ABC", buf.getString( decoder ) );
192 Assert.assertEquals( 8, buf.position() );
193
194 buf.position( 0 );
195 buf.limit( 2 );
196 Assert.assertEquals( "A", buf.getString( decoder ) );
197 Assert.assertEquals( 2, buf.position() );
198
199 buf.position( 0 );
200 buf.limit( 3 );
201 Assert.assertEquals( "A", buf.getString( decoder ) );
202 Assert.assertEquals( 2, buf.position() );
203
204 buf.clear();
205 Assert.assertEquals( "ABC", buf.getString( 10, decoder ) );
206 Assert.assertEquals( 10, buf.position() );
207
208 buf.clear();
209 Assert.assertEquals( "A", buf.getString( 2, decoder ) );
210 Assert.assertEquals( 2, buf.position() );
211
212 buf.clear();
213 try
214 {
215 buf.getString( 1, decoder );
216 Assert.fail();
217 }
218 catch( IllegalArgumentException e )
219 {
220
221 }
222
223
224 buf.clear();
225 buf.limit( 0 );
226 Assert.assertEquals( "", buf.getString( decoder ) );
227 Assert.assertEquals( "", buf.getString( 2, decoder ) );
228 }
229
230 public void testPutString() throws Exception
231 {
232 CharsetEncoder encoder;
233 ByteBuffer buf = ByteBuffer.allocate( 16 );
234 encoder = Charset.forName( "ISO-8859-1" ).newEncoder();
235
236 buf.putString( "ABC", encoder );
237 Assert.assertEquals( 3, buf.position() );
238 buf.clear();
239 Assert.assertEquals( 'A', buf.get( 0 ) );
240 Assert.assertEquals( 'B', buf.get( 1 ) );
241 Assert.assertEquals( 'C', buf.get( 2 ) );
242
243 buf.putString( "D", 5, encoder );
244 Assert.assertEquals( 5, buf.position() );
245 buf.clear();
246 Assert.assertEquals( 'D', buf.get( 0 ) );
247 Assert.assertEquals( 0, buf.get( 1 ) );
248
249 buf.putString( "EFG", 2, encoder );
250 Assert.assertEquals( 2, buf.position() );
251 buf.clear();
252 Assert.assertEquals( 'E', buf.get( 0 ) );
253 Assert.assertEquals( 'F', buf.get( 1 ) );
254 Assert.assertEquals( 'C', buf.get( 2 ) );
255
256
257 encoder = Charset.forName( "UTF-16BE" ).newEncoder();
258 buf.clear();
259
260 buf.putString( "ABC", encoder );
261 Assert.assertEquals( 6, buf.position() );
262 buf.clear();
263
264 Assert.assertEquals( 0, buf.get( 0 ) );
265 Assert.assertEquals( 'A', buf.get( 1 ) );
266 Assert.assertEquals( 0, buf.get( 2 ) );
267 Assert.assertEquals( 'B', buf.get( 3 ) );
268 Assert.assertEquals( 0, buf.get( 4 ) );
269 Assert.assertEquals( 'C', buf.get( 5 ) );
270
271 buf.putString( "D", 10, encoder );
272 Assert.assertEquals( 10, buf.position() );
273 buf.clear();
274 Assert.assertEquals( 0, buf.get( 0 ) );
275 Assert.assertEquals( 'D', buf.get( 1 ) );
276 Assert.assertEquals( 0, buf.get( 2 ) );
277 Assert.assertEquals( 0, buf.get( 3 ) );
278
279 buf.putString( "EFG", 4, encoder );
280 Assert.assertEquals( 4, buf.position() );
281 buf.clear();
282 Assert.assertEquals( 0, buf.get( 0 ) );
283 Assert.assertEquals( 'E', buf.get( 1 ) );
284 Assert.assertEquals( 0, buf.get( 2 ) );
285 Assert.assertEquals( 'F', buf.get( 3 ) );
286 Assert.assertEquals( 0, buf.get( 4 ) );
287 Assert.assertEquals( 'C', buf.get( 5 ) );
288
289
290 buf.putString( "", encoder );
291 Assert.assertEquals( 0, buf.position() );
292 buf.putString( "", 4, encoder );
293 Assert.assertEquals( 4, buf.position() );
294 Assert.assertEquals( 0, buf.get( 0 ) );
295 Assert.assertEquals( 0, buf.get( 1 ) );
296 }
297 }