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: 162104 $, $Date: 2005-04-21 10:34:06 +0900 (목, 21 4월 2005) $
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 public void testPutString() throws Exception
225 {
226 CharsetEncoder encoder;
227 ByteBuffer buf = ByteBuffer.allocate( 16 );
228 encoder = Charset.forName( "ISO-8859-1" ).newEncoder();
229
230 buf.putString( "ABC", encoder );
231 Assert.assertEquals( 3, buf.position() );
232 buf.clear();
233 Assert.assertEquals( 'A', buf.get( 0 ) );
234 Assert.assertEquals( 'B', buf.get( 1 ) );
235 Assert.assertEquals( 'C', buf.get( 2 ) );
236
237 buf.putString( "D", 5, encoder );
238 Assert.assertEquals( 5, buf.position() );
239 buf.clear();
240 Assert.assertEquals( 'D', buf.get( 0 ) );
241 Assert.assertEquals( 0, buf.get( 1 ) );
242
243 buf.putString( "EFG", 2, encoder );
244 Assert.assertEquals( 2, buf.position() );
245 buf.clear();
246 Assert.assertEquals( 'E', buf.get( 0 ) );
247 Assert.assertEquals( 'F', buf.get( 1 ) );
248 Assert.assertEquals( 'C', buf.get( 2 ) );
249
250
251 encoder = Charset.forName( "UTF-16BE" ).newEncoder();
252 buf.clear();
253
254 buf.putString( "ABC", encoder );
255 Assert.assertEquals( 6, buf.position() );
256 buf.clear();
257
258 Assert.assertEquals( 0, buf.get( 0 ) );
259 Assert.assertEquals( 'A', buf.get( 1 ) );
260 Assert.assertEquals( 0, buf.get( 2 ) );
261 Assert.assertEquals( 'B', buf.get( 3 ) );
262 Assert.assertEquals( 0, buf.get( 4 ) );
263 Assert.assertEquals( 'C', buf.get( 5 ) );
264
265 buf.putString( "D", 10, encoder );
266 Assert.assertEquals( 10, buf.position() );
267 buf.clear();
268 Assert.assertEquals( 0, buf.get( 0 ) );
269 Assert.assertEquals( 'D', buf.get( 1 ) );
270 Assert.assertEquals( 0, buf.get( 2 ) );
271 Assert.assertEquals( 0, buf.get( 3 ) );
272
273 buf.putString( "EFG", 4, encoder );
274 Assert.assertEquals( 4, buf.position() );
275 buf.clear();
276 Assert.assertEquals( 0, buf.get( 0 ) );
277 Assert.assertEquals( 'E', buf.get( 1 ) );
278 Assert.assertEquals( 0, buf.get( 2 ) );
279 Assert.assertEquals( 'F', buf.get( 3 ) );
280 Assert.assertEquals( 0, buf.get( 4 ) );
281 Assert.assertEquals( 'C', buf.get( 5 ) );
282 }
283 }