Source code: openfuture/util/misc/testcase/StripWhitespaceReaderTestCase.java
1 package openfuture.util.misc.testcase;
2 /*
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.<p>
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.<p>
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA<br>
16 * http://www.gnu.org/copyleft/lesser.html
17 */
18
19 import com.debis.ntlib.junit.TestCase;
20 import java.io.IOException;
21 import java.io.StringReader;
22 import openfuture.util.misc.StripWhitespaceReader;
23
24 // Configuration Management Information:
25 // -------------------------------------
26 // $Id: StripWhitespaceReaderTestCase.java,v 1.1.1.1 2001/07/08 18:29:34 wreissen Exp $
27 //
28 // Version History:
29 // ----------------
30 // $Log: StripWhitespaceReaderTestCase.java,v $
31 // Revision 1.1.1.1 2001/07/08 18:29:34 wreissen
32 // initial version registered ad SourceForge
33 //
34 // Revision 1.1 2001/05/09 20:07:28 wreissen
35 // initial version.
36 //
37 //
38 // ***********************************************************************************
39 /**
40 * Testsuite for {@link openfuture.util.misc.StripWhitespaceReader
41 * StripWhitespaceReader}.
42 *
43 *
44 * Created: Wed May 02 19:17:36 2001
45 *
46 * @author <a href="mailto:wolfgang@openfuture.de">Wolfgang Reissenberger</a>
47 * @version $Revision: 1.1.1.1 $
48 */
49
50 public class StripWhitespaceReaderTestCase extends TestCase {
51
52 /**
53 * Creates a new <code>StripWhitespaceReaderTestCase</code> instance.
54 *
55 * @param name a <code>String</code> value
56 */
57 public StripWhitespaceReaderTestCase(String name) {
58 super(name);
59 }
60
61 /**
62 * Test reading strings containing only whitespace.
63 *
64 * @exception Exception if an error occurs
65 */
66 public void testFilterStream() throws Exception {
67
68 String text = readAll(" ");
69 assert("Expected 0 character, received " + text.length() + ".", text.length() == 0);
70 text = readAll(" ");
71 assert("Expected 0 character, received " + text.length() + ".", text.length() == 0);
72 text = readAll("\n ");
73 assert("Expected 0 character, received " + text.length() + ".", text.length() == 0);
74 text = readAll("\n \t ");
75 assert("Expected 0 character, received " + text.length() + ".", text.length() == 0);
76
77 char[] buffer = new char[4];
78 long n = read("\n \t ", buffer, 0, 1);
79 assert("Expected 0 character, received " + n + ".", n == -1);
80 n = read(" ", buffer, 1, 1);
81 assert("Expected 0 character, received " + n + ".", n == -1);
82
83 text = readAll(" aa a ");
84 assert("Expected 4 character, received " + text.length() + ": \"" + text + "\"", text.length() == 4);
85 n = read(" aa a ", buffer, 0, 5);
86 assert("Expected 4 character, received " + n + ".", n == 4);
87 }
88
89
90 /**
91 * Read the complete content of the stream and count the
92 * number of characters occured. The <code>init</code> string
93 * is processed by {@link openfuture.util.misc.StripWhitespaceReader
94 * StripWhitespaceReader}.
95 *
96 * @param init String to be read
97 * @return read character
98 * @exception IOException if an error occurs
99 */
100 protected String readAll(String init) throws IOException {
101 StripWhitespaceReader reader = new StripWhitespaceReader(new StringReader(init), "<>".toCharArray());
102
103 StringBuffer buffer = new StringBuffer();
104
105 int c = 0;
106 while((c = reader.read()) != -1) buffer.append((char) c);
107
108 return buffer.toString();
109 }
110
111 /**
112 * Read maximal <code>len</code> characters into the
113 * character buffer <code>buffer[]</code>. The buffer is filled
114 * starting at <code>offset</code>. The <code>init</code> string
115 * is processed by {@link openfuture.util.misc.StripWhitespaceReader
116 * StripWhitespaceReader}.
117 *
118 * @param init a <code>String</code> value
119 * @param buffer[] a <code>char</code> value
120 * @param offset an <code>int</code> value
121 * @param len an <code>int</code> value
122 * @return an <code>int</code> value
123 * @exception IOException if an error occurs
124 */
125 protected int read(String init, char buffer[], int offset, int len)
126 throws IOException {
127 StripWhitespaceReader reader = new StripWhitespaceReader(new StringReader(init), "<>".toCharArray());
128
129 return (reader.read(buffer, offset, len));
130 }
131 } // StripWhitespaceReaderTestCase