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.util.test;
22
23 import java.io.IOException;
24 import java.util.Arrays;
25 import junit.framework.Assert;
26 import org.jdtaus.core.io.FileOperations;
27 import org.jdtaus.core.io.util.ReadAheadFileOperations;
28
29
30
31
32
33
34
35 public class ReadAheadFileOperationsTest extends FlushableFileOperationsTest
36 {
37
38
39 public FileOperations getFileOperations()
40 {
41 try
42 {
43 return new ReadAheadFileOperations( this.getMemoryFileOperations() );
44 }
45 catch ( IOException e )
46 {
47 throw new AssertionError( e );
48 }
49 }
50
51
52
53
54 protected void runTest() throws Throwable
55 {
56 super.runTest();
57 this.testWriteUpdatesCache();
58 }
59
60
61
62
63
64
65
66
67 public void testWriteUpdatesCache() throws Exception
68 {
69 final FileOperations ops = this.getFileOperations();
70 final byte[] testBuf = new byte[ 101 ];
71 final byte[] buf = new byte[ 101 ];
72
73 int totalRead = 0;
74 int toRead = 100;
75 int read = 0;
76
77 Arrays.fill( testBuf, ( byte ) 100 );
78 Arrays.fill( buf, ( byte ) 0 );
79 buf[100] = ( byte ) 100;
80
81 ops.setLength( 0L );
82 ops.write( testBuf, 0, 100 );
83
84 Assert.assertEquals( 100L, ops.getFilePointer() );
85
86 ops.setFilePointer( 0L );
87
88 do
89 {
90 read = ops.read( buf, totalRead, toRead );
91 assert read != -1;
92 totalRead += read;
93 toRead -= read;
94 }
95 while ( totalRead < 100 );
96
97 Assert.assertTrue( Arrays.equals( testBuf, buf ) );
98
99 ops.setFilePointer( 0L );
100 ops.write( new byte[] { ( byte ) 1 }, 0, 1 );
101 ops.setFilePointer( 100L );
102 ops.write( new byte[] { ( byte ) 1 }, 0, 1 );
103
104 testBuf[0] = ( byte ) 1;
105 testBuf[100] = ( byte ) 1;
106
107 ops.setFilePointer( 0L );
108 Arrays.fill( buf, ( byte ) 0 );
109
110 toRead = buf.length;
111 totalRead = 0;
112
113 do
114 {
115 read = ops.read( buf, totalRead, toRead );
116 assert read != -1;
117 totalRead += read;
118 toRead -= read;
119 }
120 while ( totalRead < buf.length );
121
122 Assert.assertTrue( Arrays.equals( testBuf, buf ) );
123 Assert.assertEquals( -1, ops.read( buf, 0, buf.length ) );
124 ops.setLength( 0L );
125 }
126
127
128 }