/*
* Copyright (C) Chaperon. All rights reserved.
* -------------------------------------------------------------------------
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package net.sourceforge.chaperon.test;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import net.sourceforge.chaperon.process.extended.CharBuffer;
public class CharBufferTestCase extends TestCase
{
public CharBufferTestCase(String name)
{
super(name);
}
public void testCharBuffer()
{
CharBuffer buffer = new CharBuffer();
assertTrue("Test if read is not possible", !buffer.available());
char[] chars1 = "abcde".toCharArray();
buffer.push(chars1, 1, 3); // "bcd"
assertTrue("Test if read is possible", buffer.available());
assertEquals("Test text", 'b', buffer.read());
char[] chars2 = "fghij".toCharArray();
buffer.push(chars2, 2, 3); // "hij"
assertEquals("Test text", 'c', buffer.read());
assertEquals("Test text", 'd', buffer.read());
assertEquals("Test text", 'h', buffer.read());
assertEquals("Test text", 'i', buffer.read());
assertTrue("Test if read is possible", buffer.available());
assertEquals("Test text", 'j', buffer.read());
assertTrue("Test if read is not possible", !buffer.available());
try
{
buffer.read();
fail("Test for IllegalStateException");
}
catch (IllegalStateException e) {}
char[] chars3 = "klmno".toCharArray();
buffer.push(chars3, 0, 2); // "kl"
assertTrue("Test if read is possible", buffer.available());
assertEquals("Test text", 'k', buffer.read());
assertEquals("Test text", 'l', buffer.read());
try
{
buffer.read();
fail("Test for IllegalStateException");
}
catch (IllegalStateException e) {}
buffer.back();
assertEquals("Test text", 'l', buffer.peek());
buffer.back();
assertEquals("Test text", 'k', buffer.peek());
assertTrue("Test if read is possible", buffer.available());
buffer.back();
assertEquals("Test text", 'j', buffer.peek());
buffer.back();
assertEquals("Test text", 'i', buffer.peek());
buffer.back();
assertEquals("Test text", 'h', buffer.peek());
buffer.back();
assertEquals("Test text", 'd', buffer.peek());
buffer.back();
assertEquals("Test text", 'c', buffer.peek());
buffer.back();
assertEquals("Test text", 'b', buffer.peek());
assertTrue("Test if read is possible", buffer.available());
try
{
buffer.back();
fail("Test for IllegalStateException");
}
catch (IllegalStateException e) {}
}
public void testSimple()
{
CharBuffer buffer = new CharBuffer();
assertTrue("Test if read is not possible", !buffer.available());
char[] chars1 = "abc".toCharArray();
buffer.push(chars1, 0, 3);
assertTrue("Test if read is possible", buffer.available());
assertEquals("Test text", 'a', buffer.read());
assertTrue("Test if read is possible", buffer.available());
assertEquals("Test text", 'b', buffer.read());
assertTrue("Test if read is possible", buffer.available());
assertEquals("Test text", 'c', buffer.read());
assertTrue("Test if read is not possible", !buffer.available());
// change direction
buffer.back();
assertEquals("Test text", 'c', buffer.peek());
}
public static Test suite()
{
return new TestSuite(CharBufferTestCase.class);
}
}