01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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:
17: package org.compass.core.util;
18:
19: import java.io.Reader;
20: import java.io.StringReader;
21:
22: import junit.framework.TestCase;
23: import org.compass.core.util.reader.MultiIOReader;
24:
25: /**
26: * @author kimchy
27: */
28: public class MultiReaderTests extends TestCase {
29:
30: public void testRead() throws Exception {
31: Reader[] readers = new Reader[] { new StringReader("01234"),
32: new StringReader("5678"), new StringReader("9") };
33: Reader r = new MultiIOReader(readers);
34: for (int i = 0; i < 10; i++) {
35: assertEquals(i + 48, r.read());
36: }
37: assertEquals(-1, r.read());
38:
39: readers = new Reader[] { new StringReader("0"),
40: new StringReader("1"), new StringReader("2"),
41: new StringReader("3"), new StringReader("4"),
42: new StringReader("5"), new StringReader("6"),
43: new StringReader("7") };
44: r = new MultiIOReader(readers);
45: for (int i = 0; i < 8; i++) {
46: assertEquals(i + 48, r.read());
47: }
48: assertEquals(-1, r.read());
49:
50: readers = new Reader[] { new StringReader("01234"),
51: new StringReader(""), null, new StringReader("") };
52: r = new MultiIOReader(readers);
53: for (int i = 0; i < 5; i++) {
54: assertEquals(i + 48, r.read());
55: }
56: assertEquals(-1, r.read());
57: }
58:
59: public void testReadBuffer() throws Exception {
60: Reader[] readers = new Reader[] { new StringReader("01234"),
61: new StringReader("5678"), new StringReader("9") };
62: Reader r = new MultiIOReader(readers);
63: char[] buffer = new char[3];
64: int found = 0;
65: int index = 0;
66: while (found != -1) {
67: found = r.read(buffer);
68: for (int i = 0; i < found; i++) {
69: assertEquals(index + 48, buffer[i]);
70: index++;
71: }
72: }
73: }
74: }
|