001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.util;
018:
019: import java.io.IOException;
020:
021: import junit.framework.TestCase;
022: import org.compass.core.util.reader.StringWithSeparatorReader;
023:
024: /**
025: * @author kimchy
026: */
027: public class StringWithSeparatorReaderTests extends TestCase {
028:
029: public void testSingleRead() {
030: StringWithSeparatorReader reader = new StringWithSeparatorReader(
031: "0123456", ' ');
032: for (int i = 0; i < 7; i++) {
033: assertEquals(i + 48, reader.read());
034: }
035: assertEquals(32, reader.read());
036: assertEquals(-1, reader.read());
037: }
038:
039: public void testBufferReadWithSingleCharBuffer() throws IOException {
040: char[] buff = new char[1];
041: StringWithSeparatorReader reader = new StringWithSeparatorReader(
042: "0123456", ' ');
043: for (int i = 0; i < 7; i++) {
044: assertEquals(1, reader.read(buff));
045: assertEquals(i + 48, buff[0]);
046: }
047: assertEquals(1, reader.read(buff));
048: assertEquals(32, buff[0]);
049: assertEquals(-1, reader.read(buff));
050: }
051:
052: public void testBufferReadWithExactBuffer() throws IOException {
053: char[] buff = new char[8];
054: StringWithSeparatorReader reader = new StringWithSeparatorReader(
055: "0123456", ' ');
056: assertEquals(8, reader.read(buff));
057: for (int i = 0; i < 7; i++) {
058: assertEquals(i + 48, buff[i]);
059: }
060: assertEquals(32, buff[7]);
061: assertEquals(-1, reader.read(buff));
062: }
063:
064: public void testBufferReadWithExactNoSeparatorBuffer()
065: throws IOException {
066: char[] buff = new char[7];
067: StringWithSeparatorReader reader = new StringWithSeparatorReader(
068: "0123456", ' ');
069: assertEquals(7, reader.read(buff));
070: for (int i = 0; i < 7; i++) {
071: assertEquals(i + 48, buff[i]);
072: }
073: assertEquals(1, reader.read(buff));
074: assertEquals(32, buff[0]);
075: assertEquals(-1, reader.read(buff));
076: }
077:
078: public void testBufferReadWithHalfSizeBuffer() throws IOException {
079: char[] buff = new char[4];
080: StringWithSeparatorReader reader = new StringWithSeparatorReader(
081: "0123456", ' ');
082: assertEquals(4, reader.read(buff));
083: for (int i = 0; i < 4; i++) {
084: assertEquals(i + 48, buff[i]);
085: }
086: assertEquals(4, reader.read(buff));
087: for (int i = 0; i < 3; i++) {
088: assertEquals(i + 52, buff[i]);
089: }
090: assertEquals(32, buff[3]);
091: assertEquals(-1, reader.read(buff));
092: }
093:
094: public void testBufferReadWithExactBufferRepeatableRead()
095: throws IOException {
096: char[] buff = new char[8];
097: StringWithSeparatorReader reader = new StringWithSeparatorReader(
098: "0123456", ' ');
099: assertEquals(8, reader.read(buff));
100: for (int i = 0; i < 7; i++) {
101: assertEquals(i + 48, buff[i]);
102: }
103: assertEquals(32, buff[7]);
104: assertEquals(-1, reader.read(buff));
105: reader.close();
106: assertEquals(8, reader.read(buff));
107: for (int i = 0; i < 7; i++) {
108: assertEquals(i + 48, buff[i]);
109: }
110: assertEquals(32, buff[7]);
111: assertEquals(-1, reader.read(buff));
112: }
113: }
|