001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.test;
010:
011: import junit.framework.Test;
012: import junit.framework.TestCase;
013: import junit.framework.TestSuite;
014:
015: import net.sourceforge.chaperon.process.extended.CharBuffer;
016:
017: public class CharBufferTestCase extends TestCase {
018: public CharBufferTestCase(String name) {
019: super (name);
020: }
021:
022: public void testCharBuffer() {
023: CharBuffer buffer = new CharBuffer();
024:
025: assertTrue("Test if read is not possible", !buffer.available());
026:
027: char[] chars1 = "abcde".toCharArray();
028: buffer.push(chars1, 1, 3); // "bcd"
029:
030: assertTrue("Test if read is possible", buffer.available());
031:
032: assertEquals("Test text", 'b', buffer.read());
033:
034: char[] chars2 = "fghij".toCharArray();
035: buffer.push(chars2, 2, 3); // "hij"
036:
037: assertEquals("Test text", 'c', buffer.read());
038: assertEquals("Test text", 'd', buffer.read());
039: assertEquals("Test text", 'h', buffer.read());
040: assertEquals("Test text", 'i', buffer.read());
041:
042: assertTrue("Test if read is possible", buffer.available());
043:
044: assertEquals("Test text", 'j', buffer.read());
045:
046: assertTrue("Test if read is not possible", !buffer.available());
047:
048: try {
049: buffer.read();
050: fail("Test for IllegalStateException");
051: } catch (IllegalStateException e) {
052: }
053:
054: char[] chars3 = "klmno".toCharArray();
055: buffer.push(chars3, 0, 2); // "kl"
056:
057: assertTrue("Test if read is possible", buffer.available());
058:
059: assertEquals("Test text", 'k', buffer.read());
060: assertEquals("Test text", 'l', buffer.read());
061:
062: try {
063: buffer.read();
064: fail("Test for IllegalStateException");
065: } catch (IllegalStateException e) {
066: }
067:
068: buffer.back();
069: assertEquals("Test text", 'l', buffer.peek());
070: buffer.back();
071: assertEquals("Test text", 'k', buffer.peek());
072:
073: assertTrue("Test if read is possible", buffer.available());
074:
075: buffer.back();
076: assertEquals("Test text", 'j', buffer.peek());
077: buffer.back();
078: assertEquals("Test text", 'i', buffer.peek());
079: buffer.back();
080: assertEquals("Test text", 'h', buffer.peek());
081: buffer.back();
082: assertEquals("Test text", 'd', buffer.peek());
083: buffer.back();
084: assertEquals("Test text", 'c', buffer.peek());
085: buffer.back();
086: assertEquals("Test text", 'b', buffer.peek());
087:
088: assertTrue("Test if read is possible", buffer.available());
089:
090: try {
091: buffer.back();
092: fail("Test for IllegalStateException");
093: } catch (IllegalStateException e) {
094: }
095: }
096:
097: public void testSimple() {
098: CharBuffer buffer = new CharBuffer();
099:
100: assertTrue("Test if read is not possible", !buffer.available());
101:
102: char[] chars1 = "abc".toCharArray();
103: buffer.push(chars1, 0, 3);
104:
105: assertTrue("Test if read is possible", buffer.available());
106:
107: assertEquals("Test text", 'a', buffer.read());
108:
109: assertTrue("Test if read is possible", buffer.available());
110:
111: assertEquals("Test text", 'b', buffer.read());
112:
113: assertTrue("Test if read is possible", buffer.available());
114:
115: assertEquals("Test text", 'c', buffer.read());
116:
117: assertTrue("Test if read is not possible", !buffer.available());
118:
119: // change direction
120: buffer.back();
121: assertEquals("Test text", 'c', buffer.peek());
122: }
123:
124: public static Test suite() {
125: return new TestSuite(CharBufferTestCase.class);
126: }
127: }
|