1   /*
2    *  Copyright 2006 Simon Raess
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.beep4j.internal.util;
17  
18  import java.io.IOException;
19  import java.io.Reader;
20  import java.util.Arrays;
21  
22  import junit.framework.TestCase;
23  
24  public class CharSequenceReaderTest extends TestCase {
25  	
26  	private static final String INPUT = "abcdefghijklmnopqrstuvwxyz";
27  
28  	private void assertEndOfStream(Reader reader) throws IOException {
29  		assertEquals(-1, reader.read());
30  	}
31  	
32  	private void assertArrayEquals(char[] a1, char[] a2) {
33  		assertTrue(Arrays.equals(a1, a2));
34  	}
35  	
36  	public void testReadEmpty() throws Exception {
37  		Reader reader = new CharSequenceReader("");
38  		assertEndOfStream(reader);
39  	}
40  	
41  	public void testRead() throws Exception {
42  		Reader reader = new CharSequenceReader(INPUT);
43  		
44  		for (char c = 'a'; c <= 'z'; c++) {
45  			assertEquals(c, reader.read());
46  		}
47  		
48  		assertEndOfStream(reader);
49  	}
50  	
51  	public void testBulkReadEmpty() throws Exception {
52  		Reader reader = new CharSequenceReader("");
53  		assertEquals(-1, reader.read(new char[10]));
54  	}
55  	
56  	public void testBuldRead() throws Exception {
57  		Reader reader = new CharSequenceReader(INPUT);
58  		char[] buf = new char[10];
59  		
60  		assertEquals(10, reader.read(buf));
61  		assertArrayEquals(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }, buf);
62  		
63  		assertEquals(7, reader.read(buf, 2, 7));
64  		assertArrayEquals(new char[] { 'a', 'b', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'j' }, buf);
65  		
66  		assertEquals(9, reader.read(buf));
67  		assertArrayEquals(new char[] { 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'j' }, buf);
68  		
69  		assertEndOfStream(reader);
70  	}
71  	
72  }