1   /*
2    *   @(#) $Id: ByteBufferTest.java 357871 2005-12-20 01:56:40Z 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: 357871 $, $Date: 2005-12-20 10:56:40 +0900 (Tue, 20 Dec 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 ) // 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         // Test getting strings from non-empty buffer which is filled with 0x00
241         buf.clear();
242         buf.putInt( 0 );
243         buf.clear();
244         buf.limit( 4 );
245         Assert.assertEquals( "", buf.getString( decoder ) );
246         Assert.assertEquals( 2, buf.position() );
247         Assert.assertEquals( 4, buf.limit() );
248         
249         buf.position( 0 );
250         Assert.assertEquals( "", buf.getString( 2, decoder ) );
251         Assert.assertEquals( 2, buf.position() );
252         Assert.assertEquals( 4, buf.limit() );
253     }
254     
255     public void testPutString() throws Exception
256     {
257         CharsetEncoder encoder;
258         ByteBuffer buf = ByteBuffer.allocate( 16 );
259         encoder = Charset.forName( "ISO-8859-1" ).newEncoder();
260         
261         buf.putString( "ABC", encoder );
262         Assert.assertEquals( 3, buf.position() );
263         buf.clear();
264         Assert.assertEquals( 'A', buf.get( 0 ) );
265         Assert.assertEquals( 'B', buf.get( 1 ) );
266         Assert.assertEquals( 'C', buf.get( 2 ) );
267         
268         buf.putString( "D", 5, encoder );
269         Assert.assertEquals( 5, buf.position() );
270         buf.clear();
271         Assert.assertEquals( 'D', buf.get( 0 ) );
272         Assert.assertEquals( 0, buf.get( 1 ) );
273         
274         buf.putString( "EFG", 2, encoder );
275         Assert.assertEquals( 2, buf.position() );
276         buf.clear();
277         Assert.assertEquals( 'E', buf.get( 0 ) );
278         Assert.assertEquals( 'F', buf.get( 1 ) );
279         Assert.assertEquals( 'C', buf.get( 2 ) ); // C may not be overwritten
280 
281         // UTF-16: We specify byte order to omit BOM.
282         encoder = Charset.forName( "UTF-16BE" ).newEncoder();
283         buf.clear();
284         
285         buf.putString( "ABC", encoder );
286         Assert.assertEquals( 6, buf.position() );
287         buf.clear();
288         
289         Assert.assertEquals( 0, buf.get( 0 ) );
290         Assert.assertEquals( 'A', buf.get( 1 ) );
291         Assert.assertEquals( 0, buf.get( 2 ) );
292         Assert.assertEquals( 'B', buf.get( 3 ) );
293         Assert.assertEquals( 0, buf.get( 4 ) );
294         Assert.assertEquals( 'C', buf.get( 5 ) );
295         
296         buf.putString( "D", 10, encoder );
297         Assert.assertEquals( 10, buf.position() );
298         buf.clear();
299         Assert.assertEquals( 0, buf.get( 0 ) );
300         Assert.assertEquals( 'D', buf.get( 1 ) );
301         Assert.assertEquals( 0, buf.get( 2 ) );
302         Assert.assertEquals( 0, buf.get( 3 ) );
303         
304         buf.putString( "EFG", 4, encoder );
305         Assert.assertEquals( 4, buf.position() );
306         buf.clear();
307         Assert.assertEquals( 0, buf.get( 0 ) );
308         Assert.assertEquals( 'E', buf.get( 1 ) );
309         Assert.assertEquals( 0, buf.get( 2 ) );
310         Assert.assertEquals( 'F', buf.get( 3 ) );
311         Assert.assertEquals( 0, buf.get( 4 ) );   // C may not be overwritten
312         Assert.assertEquals( 'C', buf.get( 5 ) ); // C may not be overwritten
313 
314         // Test putting an emptry string
315         buf.putString( "", encoder );
316         Assert.assertEquals( 0, buf.position() );
317         buf.putString( "", 4, encoder );
318         Assert.assertEquals( 4, buf.position() );
319         Assert.assertEquals( 0, buf.get( 0 ) );
320         Assert.assertEquals( 0, buf.get( 1 ) );
321     }
322 }