1
package org.codehaus.groovy.runtime;
7
* Test File append and left shift methods in Groovy
9
* @author <a href="mailto:joachim.baumann@xinaris.de">Joachim Baumann</a>
10
* @version $Revision: 7320 $
12
class FileAppendTest extends GroovyTestCase {
14
* The following instances are used in testing the file writes
19
<thing>Jelly Beans</thing>
22
<tune>The 59th Street Bridge Song</tune>
25
<character name="Austin Powers">
26
<enemy>Dr. Evil</enemy>
27
<enemy>Mini Me</enemy>
32
static gPathResult = new XmlSlurper().parseText(text)
35
StringWriter sw = new StringWriter()
36
gPathResult.writeTo(sw)
37
gPathWriteTo = sw.toString()
40
// see below for class definition
41
def testInstance = new TestClass()
47
// Setup guarantees us that we use a non-existent file
48
file = File.createTempFile("unitTest", ".txt")
49
assert file.exists() == true
50
//println file.canonicalPath
51
assert file.length() == 0L
54
// we remove our temporary file
58
void testAppendString(){
64
assert hasContents(file, expected)
69
assert hasContents(file, expected)
72
void testAppendObjectToString(){
76
file.append(testInstance)
77
expected = testInstance.toString()
78
assert hasContents(file, expected)
81
file.append(testInstance)
82
expected += testInstance.toString()
83
assert hasContents(file, expected)
86
void testappendWritable(){
90
file.append(gPathResult)
91
expected = gPathWriteTo
92
assert hasContents(file, expected)
95
file.append(gPathResult)
96
expected += gPathWriteTo
97
assert hasContents(file, expected)
100
void testappendMixed(){
106
assert hasContents(file, expected)
108
file.append(testInstance)
109
expected += testInstance.toString()
110
assert hasContents(file, expected)
112
file.append(gPathResult)
113
expected += gPathWriteTo
114
assert hasContents(file, expected)
117
file.append(gPathResult)
118
expected += gPathWriteTo
119
assert hasContents(file, expected)
121
file.append(testInstance)
122
expected += testInstance.toString()
123
assert hasContents(file, expected)
127
assert hasContents(file, expected)
130
void testLeftShiftString(){
136
assert hasContents(file, expected)
141
assert hasContents(file, expected)
144
void testLeftShiftObjectToString(){
149
expected = testInstance.toString()
150
assert hasContents(file, expected)
154
expected += testInstance.toString()
155
assert hasContents(file, expected)
158
void testLeftShiftWritable(){
163
expected = gPathWriteTo
164
assert hasContents(file, expected)
168
expected += gPathWriteTo
169
assert hasContents(file, expected)
172
void testLeftShiftMixed(){
178
assert hasContents(file, expected)
181
expected += testInstance.toString()
182
assert hasContents(file, expected)
185
expected += gPathWriteTo
186
assert hasContents(file, expected)
190
expected += gPathWriteTo
191
assert hasContents(file, expected)
194
expected += testInstance.toString()
195
assert hasContents(file, expected)
199
assert hasContents(file, expected)
202
boolean hasContents(File f, String expected)
204
assert file.length() == expected.length()
205
// read contents the Java way
206
char[] cbuf = new char[expected.length()];
207
def fileReader = new FileReader(file)
208
fileReader.read(cbuf)
209
return expected == String.valueOf(cbuf)
214
def testString = "TestThis"
215
public String toString() {
216
super.toString() + ": " + testString
1
package org.codehaus.groovy.runtime;
7
* Test File append and left shift methods in Groovy
9
* @author <a href="mailto:joachim.baumann@xinaris.de">Joachim Baumann</a>
10
* @version $Revision: 15129 $
12
class FileAppendTest extends GroovyTestCase {
14
* The following instances are used in testing the file writes
19
<thing>Jelly Beans</thing>
22
<tune>The 59th Street Bridge Song</tune>
25
<character name="Austin Powers">
26
<enemy>Dr. Evil</enemy>
27
<enemy>Mini Me</enemy>
32
static gPathResult = new XmlSlurper().parseText(text)
35
public FileAppendTest ()
37
StringWriter sw = new StringWriter()
38
gPathResult.writeTo(sw)
39
gPathWriteTo = sw.toString()
42
// see below for class definition
43
def testInstance = new TestClass()
49
// Setup guarantees us that we use a non-existent file
50
file = File.createTempFile("unitTest", ".txt")
51
assert file.exists() == true
52
//println file.canonicalPath
53
assert file.length() == 0L
56
// we remove our temporary file
60
void testAppendString(){
66
assert hasContents(file, expected)
71
assert hasContents(file, expected)
74
void testAppendObjectToString(){
78
file.append(testInstance)
79
expected = testInstance.toString()
80
assert hasContents(file, expected)
83
file.append(testInstance)
84
expected += testInstance.toString()
85
assert hasContents(file, expected)
88
void testappendWritable(){
92
file.append(gPathResult)
93
expected = gPathWriteTo
94
assert hasContents(file, expected)
97
file.append(gPathResult)
98
expected += gPathWriteTo
99
assert hasContents(file, expected)
102
void testappendMixed(){
108
assert hasContents(file, expected)
110
file.append(testInstance)
111
expected += testInstance.toString()
112
assert hasContents(file, expected)
114
file.append(gPathResult)
115
expected += gPathWriteTo
116
assert hasContents(file, expected)
119
file.append(gPathResult)
120
expected += gPathWriteTo
121
assert hasContents(file, expected)
123
file.append(testInstance)
124
expected += testInstance.toString()
125
assert hasContents(file, expected)
129
assert hasContents(file, expected)
132
void testLeftShiftString(){
138
assert hasContents(file, expected)
143
assert hasContents(file, expected)
146
void testLeftShiftObjectToString(){
151
expected = testInstance.toString()
152
assert hasContents(file, expected)
156
expected += testInstance.toString()
157
assert hasContents(file, expected)
160
void testLeftShiftWritable(){
165
expected = gPathWriteTo
166
assert hasContents(file, expected)
170
expected += gPathWriteTo
171
assert hasContents(file, expected)
174
void testLeftShiftMixed(){
180
assert hasContents(file, expected)
183
expected += testInstance.toString()
184
assert hasContents(file, expected)
187
expected += gPathWriteTo
188
assert hasContents(file, expected)
192
expected += gPathWriteTo
193
assert hasContents(file, expected)
196
expected += testInstance.toString()
197
assert hasContents(file, expected)
201
assert hasContents(file, expected)
204
void testByteArrayAppend() {
207
def array = [0x0,0x1,0x2]
209
file.append array as byte[]
210
assert hasContents(file, total)
214
file.append array as byte[]
215
assert hasContents( file, total )
218
void testBinaryAppend() {
221
def array = [0x0,0x1,0x2]
224
file.append new ByteArrayInputStream( array as byte[] )
225
assert hasContents( file, total )
227
//test leftShift here as well, which should simply be an alias for 'append'
229
array = [0x5,0x6,0x7,0x8]
231
file << new ByteArrayInputStream( array as byte[] )
232
assert hasContents( file, total )
235
boolean hasContents(File f, String expected)
237
assert file.length() == expected.length()
238
// read contents the Java way
239
char[] cbuf = new char[expected.length()];
240
def fileReader = new FileReader(file)
242
fileReader.read(cbuf)
243
return expected == String.valueOf(cbuf)
245
finally { fileReader?.close() }
248
boolean hasContents(File f, List expected) // list of bytes
250
assert file.length() == expected.size()
251
byte[] buf = new byte[expected.size()];
252
def fis = new FileInputStream(file)
255
return (expected as byte[]) == buf
257
finally { fis?.close() }
262
def testString = "TestThis"
263
public String toString() {
264
super.toString() + ": " + testString