~ubuntu-branches/ubuntu/lucid/groovy/lucid

« back to all changes in this revision

Viewing changes to src/test/org/codehaus/groovy/runtime/FileAppendTest.groovy

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath, Torsten Werner, Varun Hiremath
  • Date: 2009-04-01 19:24:19 UTC
  • mfrom: (3.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090401192419-c5mpylqhcdkv3zuv
Tags: 1.6.0-1
[ Torsten Werner ]
* New upstream release (Closes: #521648)
* Remove Build-Depends: libclassworlds-java.
* Switch to source and target version 1.5.

[ Varun Hiremath ]
* Fix build.xml file
* Add ivy to Build-Depends
* Remove unnecessary Depends -- collections3, mx4j and xpp3 
* Add build.diff patch to fix a build error
* Use quilt to manage patches
* Update manpage (Closes: #507862)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.codehaus.groovy.runtime;
2
 
 
3
 
import java.io.File
4
 
import java.io.Reader
5
 
 
6
 
/** 
7
 
 * Test File append and left shift methods in Groovy
8
 
 * 
9
 
 * @author <a href="mailto:joachim.baumann@xinaris.de">Joachim Baumann</a>
10
 
 * @version $Revision: 7320 $
11
 
 */
12
 
class FileAppendTest extends GroovyTestCase {
13
 
        /**
14
 
         * The following instances are used in testing the file writes
15
 
         */
16
 
        static text = """
17
 
                        <groovy>
18
 
                          <things>
19
 
                            <thing>Jelly Beans</thing>
20
 
                          </things>
21
 
                          <music>
22
 
                            <tune>The 59th Street Bridge Song</tune>
23
 
                          </music>
24
 
                          <characters>
25
 
                            <character name="Austin Powers">
26
 
                               <enemy>Dr. Evil</enemy>
27
 
                               <enemy>Mini Me</enemy>
28
 
                            </character>
29
 
                          </characters>
30
 
                        </groovy>
31
 
                        """
32
 
        static gPathResult = new XmlSlurper().parseText(text)
33
 
        static gPathWriteTo;
34
 
        {
35
 
                StringWriter sw = new StringWriter()
36
 
                gPathResult.writeTo(sw)
37
 
                gPathWriteTo = sw.toString()
38
 
        }
39
 
        
40
 
        // see below for class definition
41
 
        def testInstance = new TestClass()
42
 
 
43
 
        // Our file instance
44
 
        def File file;
45
 
        
46
 
        void setUp() {
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
52
 
        }
53
 
        void tearDown() {
54
 
                // we remove our temporary file
55
 
                file.deleteOnExit()
56
 
        }
57
 
 
58
 
        void testAppendString(){
59
 
                def expected
60
 
                
61
 
                // test new
62
 
                file.append(text)
63
 
                expected = text
64
 
                assert hasContents(file, expected)
65
 
                
66
 
                // test existing
67
 
                file.append(text)
68
 
                expected += text
69
 
                assert hasContents(file, expected)
70
 
        }
71
 
                        
72
 
        void testAppendObjectToString(){
73
 
                def expected
74
 
                
75
 
                // test new
76
 
                file.append(testInstance)
77
 
                expected = testInstance.toString()
78
 
                assert hasContents(file, expected)
79
 
                
80
 
                // test existing
81
 
                file.append(testInstance)
82
 
                expected += testInstance.toString()
83
 
                assert hasContents(file, expected)
84
 
        }
85
 
 
86
 
        void testappendWritable(){
87
 
                def expected
88
 
                
89
 
                // test new
90
 
                file.append(gPathResult)
91
 
                expected = gPathWriteTo
92
 
                assert hasContents(file, expected)
93
 
                
94
 
                // test existing
95
 
                file.append(gPathResult)
96
 
                expected += gPathWriteTo
97
 
                assert hasContents(file, expected)
98
 
        }
99
 
 
100
 
        void testappendMixed(){
101
 
                def expected
102
 
                
103
 
                // test new
104
 
                file.append(text)
105
 
                expected = text
106
 
                assert hasContents(file, expected)
107
 
                
108
 
                file.append(testInstance)
109
 
                expected += testInstance.toString()
110
 
                assert hasContents(file, expected)
111
 
                
112
 
                file.append(gPathResult)
113
 
                expected += gPathWriteTo
114
 
                assert hasContents(file, expected)
115
 
                
116
 
                // test existing
117
 
                file.append(gPathResult)
118
 
                expected += gPathWriteTo
119
 
                assert hasContents(file, expected)
120
 
 
121
 
                file.append(testInstance)
122
 
                expected += testInstance.toString()
123
 
                assert hasContents(file, expected)
124
 
 
125
 
                file.append(text)
126
 
                expected += text
127
 
                assert hasContents(file, expected)
128
 
        }
129
 
 
130
 
        void testLeftShiftString(){
131
 
                def expected
132
 
                
133
 
                // test new
134
 
                file << text
135
 
                expected = text
136
 
                assert hasContents(file, expected)
137
 
                
138
 
                // test existing
139
 
                file << text
140
 
                expected += text
141
 
                assert hasContents(file, expected)
142
 
        }
143
 
                        
144
 
        void testLeftShiftObjectToString(){
145
 
                def expected
146
 
                
147
 
                // test new
148
 
                file << testInstance
149
 
                expected = testInstance.toString()
150
 
                assert hasContents(file, expected)
151
 
                
152
 
                // test existing
153
 
                file << testInstance
154
 
                expected += testInstance.toString()
155
 
                assert hasContents(file, expected)
156
 
        }
157
 
 
158
 
        void testLeftShiftWritable(){
159
 
                def expected
160
 
                
161
 
                // test new
162
 
                file << gPathResult
163
 
                expected = gPathWriteTo
164
 
                assert hasContents(file, expected)
165
 
                
166
 
                // test existing
167
 
                file << gPathResult
168
 
                expected += gPathWriteTo
169
 
                assert hasContents(file, expected)
170
 
        }                
171
 
 
172
 
        void testLeftShiftMixed(){
173
 
                def expected
174
 
                
175
 
                // test new
176
 
                file << text
177
 
                expected = text
178
 
                assert hasContents(file, expected)
179
 
                
180
 
                file << testInstance
181
 
                expected += testInstance.toString()
182
 
                assert hasContents(file, expected)
183
 
                
184
 
                file << gPathResult
185
 
                expected += gPathWriteTo
186
 
                assert hasContents(file, expected)
187
 
                
188
 
                // test existing
189
 
                file << gPathResult
190
 
                expected += gPathWriteTo
191
 
                assert hasContents(file, expected)
192
 
 
193
 
                file << testInstance
194
 
                expected += testInstance.toString()
195
 
                assert hasContents(file, expected)
196
 
 
197
 
                file << text
198
 
                expected += text
199
 
                assert hasContents(file, expected)
200
 
        }
201
 
 
202
 
        boolean hasContents(File f, String expected)
203
 
        {
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)
210
 
        }
211
 
}
212
 
 
213
 
class TestClass {
214
 
        def testString = "TestThis"
215
 
        public String toString() {
216
 
                super.toString() + ": " + testString
217
 
        }
218
 
}
 
1
package org.codehaus.groovy.runtime;
 
2
 
 
3
import java.io.File
 
4
import java.io.Reader
 
5
 
 
6
/** 
 
7
 * Test File append and left shift methods in Groovy
 
8
 * 
 
9
 * @author <a href="mailto:joachim.baumann@xinaris.de">Joachim Baumann</a>
 
10
 * @version $Revision: 15129 $
 
11
 */
 
12
class FileAppendTest extends GroovyTestCase {
 
13
        /**
 
14
         * The following instances are used in testing the file writes
 
15
         */
 
16
        static text = """
 
17
                        <groovy>
 
18
                          <things>
 
19
                            <thing>Jelly Beans</thing>
 
20
                          </things>
 
21
                          <music>
 
22
                            <tune>The 59th Street Bridge Song</tune>
 
23
                          </music>
 
24
                          <characters>
 
25
                            <character name="Austin Powers">
 
26
                               <enemy>Dr. Evil</enemy>
 
27
                               <enemy>Mini Me</enemy>
 
28
                            </character>
 
29
                          </characters>
 
30
                        </groovy>
 
31
                        """
 
32
        static gPathResult = new XmlSlurper().parseText(text)
 
33
        static gPathWriteTo;
 
34
 
 
35
    public FileAppendTest ()
 
36
    {
 
37
                StringWriter sw = new StringWriter()
 
38
                gPathResult.writeTo(sw)
 
39
                gPathWriteTo = sw.toString()
 
40
        }
 
41
        
 
42
        // see below for class definition
 
43
        def testInstance = new TestClass()
 
44
 
 
45
        // Our file instance
 
46
        def File file;
 
47
        
 
48
        void setUp() {
 
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
 
54
        }
 
55
        void tearDown() {
 
56
                // we remove our temporary file
 
57
                file.deleteOnExit()
 
58
        }
 
59
 
 
60
        void testAppendString(){
 
61
                def expected
 
62
                
 
63
                // test new
 
64
                file.append(text)
 
65
                expected = text
 
66
                assert hasContents(file, expected)
 
67
                
 
68
                // test existing
 
69
                file.append(text)
 
70
                expected += text
 
71
                assert hasContents(file, expected)
 
72
        }
 
73
                        
 
74
        void testAppendObjectToString(){
 
75
                def expected
 
76
                
 
77
                // test new
 
78
                file.append(testInstance)
 
79
                expected = testInstance.toString()
 
80
                assert hasContents(file, expected)
 
81
                
 
82
                // test existing
 
83
                file.append(testInstance)
 
84
                expected += testInstance.toString()
 
85
                assert hasContents(file, expected)
 
86
        }
 
87
 
 
88
        void testappendWritable(){
 
89
                def expected
 
90
                
 
91
                // test new
 
92
                file.append(gPathResult)
 
93
                expected = gPathWriteTo
 
94
                assert hasContents(file, expected)
 
95
                
 
96
                // test existing
 
97
                file.append(gPathResult)
 
98
                expected += gPathWriteTo
 
99
                assert hasContents(file, expected)
 
100
        }
 
101
 
 
102
        void testappendMixed(){
 
103
                def expected
 
104
                
 
105
                // test new
 
106
                file.append(text)
 
107
                expected = text
 
108
                assert hasContents(file, expected)
 
109
                
 
110
                file.append(testInstance)
 
111
                expected += testInstance.toString()
 
112
                assert hasContents(file, expected)
 
113
                
 
114
                file.append(gPathResult)
 
115
                expected += gPathWriteTo
 
116
                assert hasContents(file, expected)
 
117
                
 
118
                // test existing
 
119
                file.append(gPathResult)
 
120
                expected += gPathWriteTo
 
121
                assert hasContents(file, expected)
 
122
 
 
123
                file.append(testInstance)
 
124
                expected += testInstance.toString()
 
125
                assert hasContents(file, expected)
 
126
 
 
127
                file.append(text)
 
128
                expected += text
 
129
                assert hasContents(file, expected)
 
130
        }
 
131
 
 
132
        void testLeftShiftString(){
 
133
                def expected
 
134
                
 
135
                // test new
 
136
                file << text
 
137
                expected = text
 
138
                assert hasContents(file, expected)
 
139
                
 
140
                // test existing
 
141
                file << text
 
142
                expected += text
 
143
                assert hasContents(file, expected)
 
144
        }
 
145
                        
 
146
        void testLeftShiftObjectToString(){
 
147
                def expected
 
148
                
 
149
                // test new
 
150
                file << testInstance
 
151
                expected = testInstance.toString()
 
152
                assert hasContents(file, expected)
 
153
                
 
154
                // test existing
 
155
                file << testInstance
 
156
                expected += testInstance.toString()
 
157
                assert hasContents(file, expected)
 
158
        }
 
159
 
 
160
        void testLeftShiftWritable(){
 
161
                def expected
 
162
                
 
163
                // test new
 
164
                file << gPathResult
 
165
                expected = gPathWriteTo
 
166
                assert hasContents(file, expected)
 
167
                
 
168
                // test existing
 
169
                file << gPathResult
 
170
                expected += gPathWriteTo
 
171
                assert hasContents(file, expected)
 
172
        }                
 
173
 
 
174
        void testLeftShiftMixed(){
 
175
                def expected
 
176
                
 
177
                // test new
 
178
                file << text
 
179
                expected = text
 
180
                assert hasContents(file, expected)
 
181
                
 
182
                file << testInstance
 
183
                expected += testInstance.toString()
 
184
                assert hasContents(file, expected)
 
185
                
 
186
                file << gPathResult
 
187
                expected += gPathWriteTo
 
188
                assert hasContents(file, expected)
 
189
                
 
190
                // test existing
 
191
                file << gPathResult
 
192
                expected += gPathWriteTo
 
193
                assert hasContents(file, expected)
 
194
 
 
195
                file << testInstance
 
196
                expected += testInstance.toString()
 
197
                assert hasContents(file, expected)
 
198
 
 
199
                file << text
 
200
                expected += text
 
201
                assert hasContents(file, expected)
 
202
        }
 
203
        
 
204
        void testByteArrayAppend() {
 
205
                def total = []
 
206
 
 
207
                def array = [0x0,0x1,0x2] 
 
208
                total.addAll array
 
209
                file.append array as byte[]
 
210
                assert hasContents(file, total)
 
211
 
 
212
                array = [0x3,0x4] 
 
213
                total.addAll array
 
214
                file.append array as byte[]
 
215
                assert hasContents( file, total )
 
216
        }
 
217
 
 
218
        void testBinaryAppend() {
 
219
                def total = []
 
220
 
 
221
                def array = [0x0,0x1,0x2] 
 
222
                total.addAll array
 
223
 
 
224
                file.append new ByteArrayInputStream( array as byte[] )
 
225
                assert hasContents( file, total )
 
226
 
 
227
                //test leftShift here as well, which should simply be an alias for 'append'
 
228
 
 
229
                array = [0x5,0x6,0x7,0x8]
 
230
                total.addAll array
 
231
                file << new ByteArrayInputStream( array as byte[] )
 
232
                assert hasContents( file, total )
 
233
        }
 
234
 
 
235
        boolean hasContents(File f, String expected)
 
236
        {
 
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)
 
241
                try {
 
242
                        fileReader.read(cbuf)
 
243
                        return expected == String.valueOf(cbuf)
 
244
                }
 
245
                finally { fileReader?.close() }
 
246
        }
 
247
 
 
248
        boolean hasContents(File f, List expected) // list of bytes
 
249
        {
 
250
                assert file.length() == expected.size()
 
251
                byte[] buf = new byte[expected.size()];
 
252
                def fis = new FileInputStream(file)
 
253
                try {
 
254
                        fis.read(buf)
 
255
                        return (expected as byte[]) == buf
 
256
                }
 
257
                finally { fis?.close() }
 
258
        }
 
259
}
 
260
 
 
261
class TestClass {
 
262
        def testString = "TestThis"
 
263
        public String toString() {
 
264
                super.toString() + ": " + testString
 
265
        }
 
266
}