KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > test > CharBufferTestCase


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.test;
10
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14
15 import net.sourceforge.chaperon.process.extended.CharBuffer;
16
17 public class CharBufferTestCase extends TestCase
18 {
19   public CharBufferTestCase(String JavaDoc name)
20   {
21     super(name);
22   }
23
24   public void testCharBuffer()
25   {
26     CharBuffer buffer = new CharBuffer();
27
28     assertTrue("Test if read is not possible", !buffer.available());
29
30     char[] chars1 = "abcde".toCharArray();
31     buffer.push(chars1, 1, 3); // "bcd"
32

33     assertTrue("Test if read is possible", buffer.available());
34
35     assertEquals("Test text", 'b', buffer.read());
36
37     char[] chars2 = "fghij".toCharArray();
38     buffer.push(chars2, 2, 3); // "hij"
39

40     assertEquals("Test text", 'c', buffer.read());
41     assertEquals("Test text", 'd', buffer.read());
42     assertEquals("Test text", 'h', buffer.read());
43     assertEquals("Test text", 'i', buffer.read());
44
45     assertTrue("Test if read is possible", buffer.available());
46
47     assertEquals("Test text", 'j', buffer.read());
48
49     assertTrue("Test if read is not possible", !buffer.available());
50
51     try
52     {
53       buffer.read();
54       fail("Test for IllegalStateException");
55     }
56     catch (IllegalStateException JavaDoc e) {}
57
58     char[] chars3 = "klmno".toCharArray();
59     buffer.push(chars3, 0, 2); // "kl"
60

61     assertTrue("Test if read is possible", buffer.available());
62
63     assertEquals("Test text", 'k', buffer.read());
64     assertEquals("Test text", 'l', buffer.read());
65
66     try
67     {
68       buffer.read();
69       fail("Test for IllegalStateException");
70     }
71     catch (IllegalStateException JavaDoc e) {}
72
73     buffer.back();
74     assertEquals("Test text", 'l', buffer.peek());
75     buffer.back();
76     assertEquals("Test text", 'k', buffer.peek());
77
78     assertTrue("Test if read is possible", buffer.available());
79
80     buffer.back();
81     assertEquals("Test text", 'j', buffer.peek());
82     buffer.back();
83     assertEquals("Test text", 'i', buffer.peek());
84     buffer.back();
85     assertEquals("Test text", 'h', buffer.peek());
86     buffer.back();
87     assertEquals("Test text", 'd', buffer.peek());
88     buffer.back();
89     assertEquals("Test text", 'c', buffer.peek());
90     buffer.back();
91     assertEquals("Test text", 'b', buffer.peek());
92
93     assertTrue("Test if read is possible", buffer.available());
94
95     try
96     {
97       buffer.back();
98       fail("Test for IllegalStateException");
99     }
100     catch (IllegalStateException JavaDoc e) {}
101   }
102
103   public void testSimple()
104   {
105     CharBuffer buffer = new CharBuffer();
106
107     assertTrue("Test if read is not possible", !buffer.available());
108
109     char[] chars1 = "abc".toCharArray();
110     buffer.push(chars1, 0, 3);
111
112     assertTrue("Test if read is possible", buffer.available());
113
114     assertEquals("Test text", 'a', buffer.read());
115
116     assertTrue("Test if read is possible", buffer.available());
117
118     assertEquals("Test text", 'b', buffer.read());
119
120     assertTrue("Test if read is possible", buffer.available());
121
122     assertEquals("Test text", 'c', buffer.read());
123
124     assertTrue("Test if read is not possible", !buffer.available());
125
126     // change direction
127
buffer.back();
128     assertEquals("Test text", 'c', buffer.peek());
129   }
130
131   public static Test suite()
132   {
133     return new TestSuite(CharBufferTestCase.class);
134   }
135 }
136
Popular Tags