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  package org.apache.commons.csv;
20  
21  import junit.framework.TestCase;
22  
23  /**
24   * 
25   * @author Ortwin Glück
26   */
27  public class CharBufferTest extends TestCase {
28      public void testCreate() {
29          CharBuffer cb = new CharBuffer();
30          assertEquals(0, cb.length());
31          try {
32              cb = new CharBuffer(0);
33              fail("Should not be possible");
34          } catch(IllegalArgumentException e) {
35              // expected
36          }
37          
38          cb = new CharBuffer(128);
39          assertEquals(0, cb.length());
40      }
41      
42      public void testAppendChar() {
43          CharBuffer cb = new CharBuffer(1);
44          String expected = "";
45          for (char c = 'a'; c < 'z'; c++) {
46              cb.append(c);
47              expected += c;
48              assertEquals(expected, cb.toString());
49              assertEquals(expected.length(), cb.length());
50          }
51      }
52      
53      public void testAppendCharArray() {
54          CharBuffer cb = new CharBuffer(1);
55          char[] abcd = "abcd".toCharArray();
56          String expected = "";
57          for (int i=0; i<10; i++) {
58              cb.append(abcd);
59              expected += "abcd";
60              assertEquals(expected, cb.toString());
61              assertEquals(4*(i+1), cb.length());
62          }
63      }
64      
65      public void testAppendString() {
66          CharBuffer cb = new CharBuffer(1);
67          String abcd = "abcd";
68          String expected = "";
69          for (int i=0; i<10; i++) {
70              cb.append(abcd);
71              expected += abcd;
72              assertEquals(expected, cb.toString());
73              assertEquals(4*(i+1), cb.length());
74          }
75      }
76      
77      public void testAppendStringBuffer() {
78          CharBuffer cb = new CharBuffer(1);
79          StringBuffer abcd = new StringBuffer("abcd");
80          String expected = "";
81          for (int i=0; i<10; i++) {
82              cb.append(abcd);
83              expected += "abcd";
84              assertEquals(expected, cb.toString());
85              assertEquals(4*(i+1), cb.length());
86          }
87      }
88      
89      public void testAppendCharBuffer() {
90          CharBuffer cb = new CharBuffer(1);
91          CharBuffer abcd = new CharBuffer(17);
92          abcd.append("abcd");
93          String expected = "";
94          for (int i=0; i<10; i++) {
95              cb.append(abcd);
96              expected += "abcd";
97              assertEquals(expected, cb.toString());
98              assertEquals(4*(i+1), cb.length());
99          }
100     }
101     
102     public void testShrink() {
103         String data = "123456789012345678901234567890";
104         
105         CharBuffer cb = new CharBuffer(data.length() + 100);
106         assertEquals(data.length() + 100, cb.capacity());
107         cb.append(data);
108         assertEquals(data.length() + 100, cb.capacity());
109         assertEquals(data.length(), cb.length());
110         cb.shrink();
111         assertEquals(data.length(), cb.capacity());
112         assertEquals(data.length(), cb.length());
113         assertEquals(data, cb.toString());
114     }
115     
116     //-- the following test cases have been adapted from the HttpComponents project
117     //-- written by Oleg Kalnichevski
118     
119     public void testSimpleAppend() throws Exception {
120         CharBuffer buffer = new CharBuffer(16);
121         assertEquals(16, buffer.capacity()); 
122         assertEquals(0, buffer.length());
123         char[] b1 = buffer.getCharacters();
124         assertNotNull(b1);
125         assertEquals(0, b1.length);
126         assertEquals(0, buffer.length());
127         
128         char[] tmp = new char[] { '1', '2', '3', '4'};
129         buffer.append(tmp);
130         assertEquals(16, buffer.capacity()); 
131         assertEquals(4, buffer.length());
132         
133         char[] b2 = buffer.getCharacters();
134         assertNotNull(b2);
135         assertEquals(4, b2.length);
136         for (int i = 0; i < tmp.length; i++) {
137             assertEquals(tmp[i], b2[i]);
138         }
139         assertEquals("1234", buffer.toString());
140         
141         buffer.clear();
142         assertEquals(16, buffer.capacity()); 
143         assertEquals(0, buffer.length());
144     }
145     
146     public void testAppendString2() throws Exception {
147         CharBuffer buffer = new CharBuffer(8);
148         buffer.append("stuff");
149         buffer.append(" and more stuff");
150         assertEquals("stuff and more stuff", buffer.toString());
151     }
152     
153     public void testAppendNull() throws Exception {
154         CharBuffer buffer = new CharBuffer(8);
155         
156         buffer.append((StringBuffer)null);
157         assertEquals("", buffer.toString());
158         
159         buffer.append((String)null);
160         assertEquals("", buffer.toString());
161 
162         buffer.append((CharBuffer)null);
163         assertEquals("", buffer.toString());
164 
165         buffer.append((char[])null);
166         assertEquals("", buffer.toString());
167     }
168     
169     public void testAppendCharArrayBuffer() throws Exception {
170         CharBuffer buffer1 = new CharBuffer(8);
171         buffer1.append(" and more stuff");
172         CharBuffer buffer2 = new CharBuffer(8);
173         buffer2.append("stuff");
174         buffer2.append(buffer1);
175         assertEquals("stuff and more stuff", buffer2.toString());
176     }
177     
178     public void testAppendSingleChar() throws Exception {
179         CharBuffer buffer = new CharBuffer(4);
180         buffer.append('1');
181         buffer.append('2');
182         buffer.append('3');
183         buffer.append('4');
184         buffer.append('5');
185         buffer.append('6');
186         assertEquals("123456", buffer.toString());
187     }
188     
189     public void testProvideCapacity() throws Exception {
190         CharBuffer buffer = new CharBuffer(4);
191         buffer.provideCapacity(2);
192         assertEquals(4, buffer.capacity());
193         buffer.provideCapacity(8);
194         assertTrue(buffer.capacity() >= 8);
195     }
196 }