1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.jdtaus.core.io.it;
22
23 import java.io.InputStream;
24 import java.util.Arrays;
25 import junit.framework.Assert;
26 import junit.framework.TestCase;
27 import org.jdtaus.core.io.FileOperations;
28
29
30
31
32
33
34
35 public class FileOperationsTest extends TestCase
36 {
37
38
39
40 private FileOperations fileOperations;
41
42
43
44
45
46
47
48 public FileOperations getFileOperations()
49 {
50 return this.fileOperations;
51 }
52
53
54
55
56
57
58 public final void setFileOperations( final FileOperations value )
59 {
60 this.fileOperations = value;
61 }
62
63
64
65
66
67
68 protected InputStream getTestFile()
69 {
70 final InputStream ret = Thread.currentThread().getContextClassLoader().
71 getResourceAsStream( "org/jdtaus/core/io/it/testfile" );
72
73 Assert.assertNotNull( ret );
74 return ret;
75 }
76
77
78
79
80
81
82
83
84
85
86 protected void assertValidTestFile( final String testfile )
87 {
88 Assert.assertEquals( "ABCDEFGHIJKLMNOPQRSTUVWXYZ", testfile );
89 }
90
91
92
93
94 protected void runTest() throws Throwable
95 {
96 this.testGetLength();
97 this.testSetLength();
98 this.testSetLengthUpdatesFilePointer();
99 this.testWriteBeyondIncreasesLength();
100 this.testEndOfFile();
101 this.testRead();
102 this.testWrite();
103 }
104
105
106
107
108
109
110
111
112
113
114 public void testGetLength() throws Exception
115 {
116 Assert.assertEquals( 0L, this.getFileOperations().getLength() );
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 public void testSetLength() throws Exception
133 {
134 final FileOperations ops = this.getFileOperations();
135 ops.setLength( 100L );
136 Assert.assertEquals( 100L, ops.getLength() );
137 ops.setLength( 0L );
138 Assert.assertEquals( 0L, ops.getLength() );
139
140 try
141 {
142 ops.setLength( Long.MIN_VALUE );
143 throw new AssertionError( String.valueOf( Long.MIN_VALUE ) );
144 }
145 catch ( IllegalArgumentException e )
146 {
147 Assert.assertNotNull( e.getMessage() );
148 System.out.println( e.toString() );
149 }
150 }
151
152
153
154
155
156 public void testSetLengthUpdatesFilePointer() throws Exception
157 {
158 final FileOperations ops = this.getFileOperations();
159
160 ops.setLength( 10000L );
161 ops.setFilePointer( 0L );
162 ops.setLength( 100L );
163 Assert.assertEquals( 0L, ops.getFilePointer() );
164 ops.setFilePointer( 50L );
165 ops.setLength( 10L );
166 Assert.assertEquals( 10L, ops.getFilePointer() );
167 ops.setLength( 0L );
168 }
169
170
171
172
173
174 public void testWriteBeyondIncreasesLength() throws Exception
175 {
176 final FileOperations ops = this.getFileOperations();
177
178 ops.setLength( 0L );
179 ops.setFilePointer( 0L );
180 ops.write( new byte[] { ( byte ) 1 }, 0, 1 );
181 Assert.assertEquals( 1L, ops.getLength() );
182 ops.setLength( 0L );
183
184 ops.setLength( 10L );
185 ops.setFilePointer( 10L );
186 ops.write( new byte[] { ( byte ) 1 }, 0, 1 );
187 Assert.assertEquals( 11L, ops.getLength() );
188 ops.setLength( 0L );
189
190 ops.setLength( 0L );
191 ops.setFilePointer( 0L );
192 ops.write( new byte[] {}, 0, 0 );
193 Assert.assertEquals( 0L, ops.getLength() );
194 ops.setLength( 0L );
195 ops.setFilePointer( 0L );
196
197 ops.write( new byte[ 0 ], 0, 0 );
198 Assert.assertEquals( 0L, ops.getLength() );
199 Assert.assertEquals( 0L, ops.getFilePointer() );
200
201 ops.setLength( 0L );
202 }
203
204
205
206
207
208
209
210
211 public void testEndOfFile() throws Exception
212 {
213 final FileOperations ops = this.getFileOperations();
214 ops.setLength( 1L );
215 ops.setFilePointer( 1L );
216
217 Assert.assertEquals( 0, ops.read( new byte[ 0 ], 0, 0 ) );
218 Assert.assertEquals( FileOperations.EOF,
219 ops.read( new byte[ 1 ], 0, 1 ) );
220
221 ops.setLength( 100000L );
222 ops.setFilePointer( 100000L );
223 Assert.assertEquals( 0, ops.read( new byte[ 0 ], 0, 0 ) );
224 Assert.assertEquals( FileOperations.EOF,
225 ops.read( new byte[ 1 ], 0, 1 ) );
226
227 ops.setLength( 0L );
228 }
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 public void testRead() throws Exception
247 {
248 final FileOperations ops = this.getFileOperations();
249 final byte[] digits = new byte[ 100 ];
250 final byte[] buf = new byte[ digits.length ];
251 Arrays.fill( digits, ( byte ) '1' );
252
253 int toRead = 0;
254 int totalRead = 0;
255 int read = 0;
256
257
258 Assert.assertEquals( FileOperations.EOF,
259 ops.read( new byte[ 100 ], 0, 100 ) );
260
261 ops.setFilePointer( 0L );
262 ops.write( digits, 0, digits.length );
263 Assert.assertEquals( digits.length, ops.getLength() );
264
265
266 ops.setFilePointer( digits.length / 2 );
267
268 toRead = digits.length;
269 do
270 {
271 read = ops.read( buf, totalRead, toRead );
272 if ( read != FileOperations.EOF )
273 {
274 totalRead += read;
275 toRead -= read;
276 }
277 }
278 while ( totalRead < digits.length && read != FileOperations.EOF );
279
280 Assert.assertEquals( digits.length / 2, totalRead );
281 Assert.assertEquals( FileOperations.EOF,
282 ops.read( buf, buf.length - 1, 1 ) );
283
284
285 ops.setFilePointer( 0L );
286 toRead = digits.length;
287 totalRead = 0;
288 do
289 {
290 read = ops.read( buf, totalRead, toRead );
291 if ( read != FileOperations.EOF )
292 {
293 totalRead += read;
294 toRead -= read;
295 }
296 }
297 while ( totalRead < digits.length && read != FileOperations.EOF );
298
299 Assert.assertEquals( digits.length, totalRead );
300 Assert.assertTrue( Arrays.equals( digits, buf ) );
301 ops.setLength( 0 );
302 }
303
304
305
306
307
308
309
310
311
312
313
314
315
316 public void testWrite() throws Exception
317 {
318 final long off;
319 final FileOperations ops = this.getFileOperations();
320 final byte[] digits = new byte[ 100 ];
321 final byte[] buf = new byte[ digits.length ];
322 int totalRead = 0;
323 int toRead = 0;
324 int read = 0;
325 Arrays.fill( digits, ( byte ) '1' );
326
327 ops.setFilePointer( 0L );
328 ops.write( digits, 0, 100 );
329 Assert.assertEquals( digits.length, ops.getLength() );
330
331 off = ops.getLength() * 2;
332 ops.setFilePointer( off );
333 ops.write( digits, 0, 100 );
334 ops.setFilePointer( off );
335
336 toRead = digits.length;
337 do
338 {
339 read = ops.read( buf, totalRead, toRead );
340 if ( read != FileOperations.EOF )
341 {
342 totalRead += read;
343 toRead -= read;
344 }
345 }
346 while ( totalRead < digits.length && read != FileOperations.EOF );
347 Assert.assertEquals( digits.length, totalRead );
348 Assert.assertTrue( Arrays.equals( digits, buf ) );
349 ops.setLength( 0 );
350 }
351
352
353 }