1 | /* |
2 | * @(#) $Id: ByteBufferTest.java 327113 2005-10-21 06:59:15Z trustin $ |
3 | * |
4 | * Copyright 2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * 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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
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 The Apache Directory Project (dev@directory.apache.org) |
33 | * @version $Rev: 327113 $, $Date: 2005-10-21 15:59:15 +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 ) // increase by 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 ) // increase by 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 | // ignore |
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 | // ignore |
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 testPooledProperty() throws Exception |
151 | { |
152 | ByteBuffer buf = ByteBuffer.allocate( 16 ); |
153 | java.nio.ByteBuffer nioBuf = buf.buf(); |
154 | buf.release(); |
155 | Assert.assertSame( nioBuf, ByteBuffer.allocate( 16 ).buf() ); |
156 | buf.setPooled( false ); |
157 | buf.release(); |
158 | Assert.assertNotSame( nioBuf, ByteBuffer.allocate( 16 ).buf() ); |
159 | } |
160 | |
161 | public void testGetString() throws Exception |
162 | { |
163 | ByteBuffer buf = ByteBuffer.allocate( 16 ); |
164 | CharsetDecoder decoder; |
165 | |
166 | decoder = Charset.forName( "ISO-8859-1" ).newDecoder(); |
167 | buf.put( (byte) 'A' ); |
168 | buf.put( (byte) 'B' ); |
169 | buf.put( (byte) 'C' ); |
170 | buf.put( (byte) 0 ); |
171 | |
172 | buf.position( 0 ); |
173 | Assert.assertEquals( "ABC", buf.getString( decoder ) ); |
174 | Assert.assertEquals( 4, buf.position() ); |
175 | |
176 | buf.position( 0 ); |
177 | buf.limit( 1 ); |
178 | Assert.assertEquals( "A", buf.getString( decoder ) ); |
179 | Assert.assertEquals( 1, buf.position() ); |
180 | |
181 | buf.clear(); |
182 | Assert.assertEquals( "ABC", buf.getString( 10, decoder ) ); |
183 | Assert.assertEquals( 10, buf.position() ); |
184 | |
185 | buf.clear(); |
186 | Assert.assertEquals( "A", buf.getString( 1, decoder ) ); |
187 | Assert.assertEquals( 1, buf.position() ); |
188 | |
189 | buf.clear(); |
190 | buf.fillAndReset( buf.limit() ); |
191 | decoder = Charset.forName( "UTF-16" ).newDecoder(); |
192 | buf.put( (byte) 0 ); |
193 | buf.put( (byte) 'A' ); |
194 | buf.put( (byte) 0 ); |
195 | buf.put( (byte) 'B' ); |
196 | buf.put( (byte) 0 ); |
197 | buf.put( (byte) 'C' ); |
198 | buf.put( (byte) 0 ); |
199 | buf.put( (byte) 0 ); |
200 | |
201 | buf.position( 0 ); |
202 | Assert.assertEquals( "ABC", buf.getString( decoder ) ); |
203 | Assert.assertEquals( 8, buf.position() ); |
204 | |
205 | buf.position( 0 ); |
206 | buf.limit( 2 ); |
207 | Assert.assertEquals( "A", buf.getString( decoder ) ); |
208 | Assert.assertEquals( 2, buf.position() ); |
209 | |
210 | buf.position( 0 ); |
211 | buf.limit( 3 ); |
212 | Assert.assertEquals( "A", buf.getString( decoder ) ); |
213 | Assert.assertEquals( 2, buf.position() ); |
214 | |
215 | buf.clear(); |
216 | Assert.assertEquals( "ABC", buf.getString( 10, decoder ) ); |
217 | Assert.assertEquals( 10, buf.position() ); |
218 | |
219 | buf.clear(); |
220 | Assert.assertEquals( "A", buf.getString( 2, decoder ) ); |
221 | Assert.assertEquals( 2, buf.position() ); |
222 | |
223 | buf.clear(); |
224 | try |
225 | { |
226 | buf.getString( 1, decoder ); |
227 | Assert.fail(); |
228 | } |
229 | catch( IllegalArgumentException e ) |
230 | { |
231 | // ignore |
232 | } |
233 | |
234 | // Test getting strings from an empty buffer. |
235 | buf.clear(); |
236 | buf.limit( 0 ); |
237 | Assert.assertEquals( "", buf.getString( decoder ) ); |
238 | Assert.assertEquals( "", buf.getString( 2, decoder ) ); |
239 | } |
240 | |
241 | public void testPutString() throws Exception |
242 | { |
243 | CharsetEncoder encoder; |
244 | ByteBuffer buf = ByteBuffer.allocate( 16 ); |
245 | encoder = Charset.forName( "ISO-8859-1" ).newEncoder(); |
246 | |
247 | buf.putString( "ABC", encoder ); |
248 | Assert.assertEquals( 3, buf.position() ); |
249 | buf.clear(); |
250 | Assert.assertEquals( 'A', buf.get( 0 ) ); |
251 | Assert.assertEquals( 'B', buf.get( 1 ) ); |
252 | Assert.assertEquals( 'C', buf.get( 2 ) ); |
253 | |
254 | buf.putString( "D", 5, encoder ); |
255 | Assert.assertEquals( 5, buf.position() ); |
256 | buf.clear(); |
257 | Assert.assertEquals( 'D', buf.get( 0 ) ); |
258 | Assert.assertEquals( 0, buf.get( 1 ) ); |
259 | |
260 | buf.putString( "EFG", 2, encoder ); |
261 | Assert.assertEquals( 2, buf.position() ); |
262 | buf.clear(); |
263 | Assert.assertEquals( 'E', buf.get( 0 ) ); |
264 | Assert.assertEquals( 'F', buf.get( 1 ) ); |
265 | Assert.assertEquals( 'C', buf.get( 2 ) ); // C may not be overwritten |
266 | |
267 | // UTF-16: We specify byte order to omit BOM. |
268 | encoder = Charset.forName( "UTF-16BE" ).newEncoder(); |
269 | buf.clear(); |
270 | |
271 | buf.putString( "ABC", encoder ); |
272 | Assert.assertEquals( 6, buf.position() ); |
273 | buf.clear(); |
274 | |
275 | Assert.assertEquals( 0, buf.get( 0 ) ); |
276 | Assert.assertEquals( 'A', buf.get( 1 ) ); |
277 | Assert.assertEquals( 0, buf.get( 2 ) ); |
278 | Assert.assertEquals( 'B', buf.get( 3 ) ); |
279 | Assert.assertEquals( 0, buf.get( 4 ) ); |
280 | Assert.assertEquals( 'C', buf.get( 5 ) ); |
281 | |
282 | buf.putString( "D", 10, encoder ); |
283 | Assert.assertEquals( 10, buf.position() ); |
284 | buf.clear(); |
285 | Assert.assertEquals( 0, buf.get( 0 ) ); |
286 | Assert.assertEquals( 'D', buf.get( 1 ) ); |
287 | Assert.assertEquals( 0, buf.get( 2 ) ); |
288 | Assert.assertEquals( 0, buf.get( 3 ) ); |
289 | |
290 | buf.putString( "EFG", 4, encoder ); |
291 | Assert.assertEquals( 4, buf.position() ); |
292 | buf.clear(); |
293 | Assert.assertEquals( 0, buf.get( 0 ) ); |
294 | Assert.assertEquals( 'E', buf.get( 1 ) ); |
295 | Assert.assertEquals( 0, buf.get( 2 ) ); |
296 | Assert.assertEquals( 'F', buf.get( 3 ) ); |
297 | Assert.assertEquals( 0, buf.get( 4 ) ); // C may not be overwritten |
298 | Assert.assertEquals( 'C', buf.get( 5 ) ); // C may not be overwritten |
299 | |
300 | // Test putting an emptry string |
301 | buf.putString( "", encoder ); |
302 | Assert.assertEquals( 0, buf.position() ); |
303 | buf.putString( "", 4, encoder ); |
304 | Assert.assertEquals( 4, buf.position() ); |
305 | Assert.assertEquals( 0, buf.get( 0 ) ); |
306 | Assert.assertEquals( 0, buf.get( 1 ) ); |
307 | } |
308 | } |