1 /************************************************************************* |
|
2 /* MarkRest.java -- Tests BufferedReader mark/reset functionality |
|
3 /* |
|
4 /* Copyright (c) 1998 Free Software Foundation, Inc. |
|
5 /* Written by Aaron M. Renn (arenn@urbanophile.com) |
|
6 /* |
|
7 /* This program is free software; you can redistribute it and/or modify |
|
8 /* it under the terms of the GNU General Public License as published |
|
9 /* by the Free Software Foundation, either version 2 of the License, or |
|
10 /* (at your option) any later version. |
|
11 /* |
|
12 /* This program is distributed in the hope that it will be useful, but |
|
13 /* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 /* GNU General Public License for more details. |
|
16 /* |
|
17 /* You should have received a copy of the GNU General Public License |
|
18 /* along with this program; if not, write to the Free Software Foundation |
|
19 /* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA |
|
20 /*************************************************************************/ |
|
21 |
|
22 // Tags: JDK1.1 |
|
23 |
|
24 package gnu.testlet.java.io.BufferedReader; |
|
25 |
|
26 import gnu.testlet.Testlet; |
|
27 import gnu.testlet.TestHarness; |
|
28 import java.io.*; |
|
29 |
|
30 public class MarkReset extends CharArrayReader implements Testlet |
|
31 { |
|
32 |
|
33 // Hehe. We override CharArrayReader.markSupported() in order to return |
|
34 // false so that we can test BufferedReader's handling of mark/reset in |
|
35 // both the case where the underlying stream does and does not support |
|
36 // mark/reset |
|
37 public boolean |
|
38 markSupported() |
|
39 { |
|
40 return(false); |
|
41 } |
|
42 |
|
43 public |
|
44 MarkReset(char[] buf) |
|
45 { |
|
46 super(buf); |
|
47 } |
|
48 |
|
49 // Constructor for test suite |
|
50 public |
|
51 MarkReset() |
|
52 { |
|
53 super(new char[1]); |
|
54 } |
|
55 |
|
56 public static int |
|
57 marktest(Reader ins, TestHarness harness) throws IOException |
|
58 { |
|
59 BufferedReader br = new BufferedReader(ins, 15); |
|
60 |
|
61 int chars_read; |
|
62 int total_read = 0; |
|
63 char[] buf = new char[12]; |
|
64 |
|
65 chars_read = br.read(buf); |
|
66 total_read += chars_read; |
|
67 harness.debug(new String(buf, 0, chars_read), false); |
|
68 |
|
69 chars_read = br.read(buf); |
|
70 total_read += chars_read; |
|
71 harness.debug(new String(buf, 0, chars_read), false); |
|
72 |
|
73 br.mark(75); |
|
74 br.read(); |
|
75 br.read(buf); |
|
76 br.read(buf); |
|
77 br.read(buf); |
|
78 br.reset(); |
|
79 |
|
80 chars_read = br.read(buf); |
|
81 total_read += chars_read; |
|
82 harness.debug(new String(buf, 0, chars_read), false); |
|
83 |
|
84 br.mark(555); |
|
85 |
|
86 chars_read = br.read(buf); |
|
87 total_read += chars_read; |
|
88 harness.debug(new String(buf, 0, chars_read), false); |
|
89 |
|
90 br.reset(); |
|
91 |
|
92 br.read(buf); |
|
93 chars_read = br.read(buf); |
|
94 total_read += chars_read; |
|
95 harness.debug(new String(buf, 0, chars_read), false); |
|
96 |
|
97 chars_read = br.read(buf); |
|
98 total_read += chars_read; |
|
99 harness.debug(new String(buf, 0, chars_read), false); |
|
100 |
|
101 br.mark(14); |
|
102 |
|
103 br.read(buf); |
|
104 |
|
105 br.reset(); |
|
106 |
|
107 chars_read = br.read(buf); |
|
108 total_read += chars_read; |
|
109 harness.debug(new String(buf, 0, chars_read), false); |
|
110 |
|
111 while ((chars_read = br.read(buf)) != -1) |
|
112 { |
|
113 harness.debug(new String(buf, 0, chars_read), false); |
|
114 total_read += chars_read; |
|
115 } |
|
116 |
|
117 return(total_read); |
|
118 } |
|
119 |
|
120 public void |
|
121 test(TestHarness harness) |
|
122 { |
|
123 try |
|
124 { |
|
125 harness.debug("First mark/reset test series"); |
|
126 harness.debug("Underlying reader supports mark/reset"); |
|
127 |
|
128 String str = "Growing up in a rural area brings such delights. One\n" + |
|
129 "time my uncle called me up and asked me to come over and help him\n" + |
|
130 "out with something. Since he lived right across the field, I\n" + |
|
131 "walked right over. Turned out he wanted me to come down to the\n" + |
|
132 "barn and help him castrate a calf. Oh, that was fun. Not.\n"; |
|
133 |
|
134 StringReader sr = new StringReader(str); |
|
135 BufferedReader br = new BufferedReader(sr); |
|
136 |
|
137 int total_read = marktest(br, harness); |
|
138 harness.check(total_read, str.length(), "total_read"); |
|
139 } |
|
140 catch (IOException e) |
|
141 { |
|
142 harness.debug(e); |
|
143 harness.check(false); |
|
144 } |
|
145 |
|
146 try |
|
147 { |
|
148 harness.debug("Second mark/reset test series"); |
|
149 harness.debug("Underlying reader does not support mark/reset"); |
|
150 |
|
151 String str = "Growing up we heated our house with a wood stove. That\n" + |
|
152 "thing could pump out some BTU's, let me tell you. No matter how\n" + |
|
153 "cold it got outside, it was always warm inside. Of course the\n" + |
|
154 "downside is that somebody had to chop the wood for the stove. That\n" + |
|
155 "somebody was me. I was slave labor. My uncle would go back and\n" + |
|
156 "chain saw up dead trees and I would load the wood in wagons and\n" + |
|
157 "split it with a maul. Somehow my no account brother always seemed\n" + |
|
158 "to get out of having to work.\n"; |
|
159 |
|
160 char[] buf = new char[str.length()]; |
|
161 str.getChars(0, str.length(), buf, 0); |
|
162 MarkReset mr = new MarkReset(buf); |
|
163 BufferedReader br = new BufferedReader(mr); |
|
164 |
|
165 int total_read = marktest(br, harness); |
|
166 harness.check(total_read, str.length(), "total_read"); |
|
167 } |
|
168 catch (IOException e) |
|
169 { |
|
170 harness.debug(e); |
|
171 harness.check(false); |
|
172 } |
|
173 } // main |
|
174 |
|
175 } // class MarkReset |
|
176 |
|