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