View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  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,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  
21  package org.apache.mina.util;
22  
23  import static org.junit.Assert.*;
24  
25  import java.io.IOException;
26  import java.nio.BufferOverflowException;
27  import java.nio.ByteBuffer;
28  import java.util.LinkedList;
29  import java.util.List;
30  
31  import org.junit.Test;
32  
33  /**
34   * A {@link ByteBufferOutputStream} test.
35   * 
36   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
37   */
38  public class ByteBufferOutputStreamTest {
39      @Test
40      public void testEmpty() throws IOException {
41          ByteBufferOutputStream bbos = new ByteBufferOutputStream();
42  
43          bbos.close();
44  
45          assertEquals(0, bbos.getByteBuffer().remaining());
46      }
47  
48      @Test
49      public void testSingleWrite() throws IOException {
50          ByteBufferOutputStream bbos = new ByteBufferOutputStream();
51          bbos.write(86);
52          bbos.close();
53  
54          assertEquals(1, bbos.getByteBuffer().remaining());
55          assertEquals(86, bbos.getByteBuffer().get());
56      }
57  
58      @Test
59      public void testWrite() throws IOException {
60          byte[] src = "HELLO MINA!".getBytes();
61  
62          ByteBufferOutputStream bbos = new ByteBufferOutputStream(1024);
63          bbos.write(src);
64          bbos.close();
65  
66          assertEquals(src.length, bbos.getByteBuffer().remaining());
67          assertEquals(ByteBuffer.wrap(src), bbos.getByteBuffer());
68      }
69  
70      @Test
71      public void testElasticity() throws IOException {
72          final int allocatedSize = 1024;
73          List<ByteBufferOutputStream> list = new LinkedList<ByteBufferOutputStream>();
74          ByteBufferOutputStream elastic = new ByteBufferOutputStream(allocatedSize);
75          elastic.setElastic(true);
76          ByteBufferOutputStream nonElastic = new ByteBufferOutputStream(allocatedSize);
77          nonElastic.setElastic(false);
78          list.add(elastic);
79          list.add(nonElastic);
80          byte[] src = new byte[321];
81          for (int i = 0; i < src.length; i++)
82              src[i] = (byte) (0xff & (i / 7));
83  
84          for (ByteBufferOutputStream bbos : list) {
85              for (int j = 0; j < allocatedSize * 10; j += src.length) {
86                  try {
87                      bbos.write(src);
88                      if (!bbos.isElastic() && j > allocatedSize)
89                          fail("Overflooded buffer without any exception!");
90                  } catch (BufferOverflowException boe) {
91                      if (bbos.isElastic())
92                          fail("Elastic buffer overflooded! " + boe);
93                  }
94              }
95              bbos.close();
96          }
97      }
98  
99  }