1 /************************************************************************* |
|
2 /* BasicTests.java -- CharArrayWriter basic tests. |
|
3 /* |
|
4 /* Copyright (c) 2002 Free Software Foundation, Inc. |
|
5 /* Written by David J. King (david_king@softhome.net) |
|
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.CharArrayWriter; |
|
25 |
|
26 import gnu.testlet.Testlet; |
|
27 import gnu.testlet.TestHarness; |
|
28 import java.io.*; |
|
29 |
|
30 public class BasicTests implements Testlet |
|
31 { |
|
32 |
|
33 public void |
|
34 test(TestHarness harness) |
|
35 { |
|
36 /* |
|
37 * Use several methods to write to the buffer |
|
38 * and verify that the results are correct. |
|
39 */ |
|
40 String firstLines = "The first lines\n" + |
|
41 "of the test which include \uA000 inverted question\n" + |
|
42 "and \u6666 e-with-hat"; |
|
43 String thirdLine = "a third line"; |
|
44 String expected = firstLines + ' ' + "third"; |
|
45 CharArrayWriter writer = new CharArrayWriter(); |
|
46 if (writer.size() != 0) |
|
47 harness.check(writer.size(), 0, "empty size"); |
|
48 char[] thirdLineArray = new char[thirdLine.length()]; |
|
49 String extractedString; |
|
50 try |
|
51 { |
|
52 writer.write(firstLines, 0, firstLines.length()); |
|
53 writer.write(32); |
|
54 thirdLine.getChars(0, thirdLine.length(), thirdLineArray, 0); |
|
55 writer.write(thirdLineArray, 2, 5); |
|
56 extractedString = writer.toString(); |
|
57 harness.check(extractedString, expected, "basic string"); |
|
58 /* |
|
59 * Clear the buffer and write some more, then see if |
|
60 * toCharArray works. |
|
61 */ |
|
62 writer.reset(); |
|
63 writer.write(thirdLine, 0, thirdLine.length()); |
|
64 } |
|
65 catch (Throwable t) |
|
66 { |
|
67 harness.debug(t); |
|
68 harness.check(false, "Unexpected exception"); |
|
69 extractedString = ""; |
|
70 } |
|
71 char[] resultArray = writer.toCharArray(); |
|
72 boolean arrayEquals = resultArray.length == thirdLineArray.length; |
|
73 if (arrayEquals) |
|
74 for (int i=0; i < resultArray.length; i++) |
|
75 if (resultArray[i] != thirdLineArray[i]) |
|
76 arrayEquals = false; |
|
77 harness.checkPoint("reset string"); |
|
78 harness.check(arrayEquals); |
|
79 /* |
|
80 * Try flush and close and make sure they do nothing. |
|
81 */ |
|
82 try |
|
83 { |
|
84 writer.flush(); |
|
85 writer.close(); |
|
86 } |
|
87 catch (Throwable t) |
|
88 { |
|
89 harness.debug(t); |
|
90 harness.check(false, "Unexpected exception flush/close"); |
|
91 } |
|
92 extractedString = writer.toString(); |
|
93 harness.check(extractedString, thirdLine, "flush and close"); |
|
94 /* |
|
95 * Make another CharArrayWriter and writeTo it. |
|
96 */ |
|
97 CharArrayWriter secondWriter = new CharArrayWriter(); |
|
98 boolean pass = false; |
|
99 try { |
|
100 writer.writeTo(secondWriter); |
|
101 extractedString = secondWriter.toString(); |
|
102 if (extractedString.equals(thirdLine)) |
|
103 pass = true; |
|
104 } catch (IOException ie) { |
|
105 // Nothing need be done, it is enough for pass to remain false |
|
106 } |
|
107 harness.checkPoint("writeTo"); |
|
108 harness.check(pass); |
|
109 } |
|
110 |
|
111 } // BasicTests |
|