1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35
36
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 }