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 ReadAheadFileOperationsUnevenTest
36 extends ReadAheadFileOperationsTest
37 {
38
39
40 public FileOperations getFileOperations()
41 {
42 try
43 {
44 return new ReadAheadFileOperations(
45 this.getMemoryFileOperations(), 3 );
46
47 }
48 catch ( IOException e )
49 {
50 throw new AssertionError( e );
51 }
52 }
53
54
55
56
57 protected void runTest() throws Throwable
58 {
59 super.runTest();
60 this.testReadBeyondCacheEof();
61 this.testReadBeyondCacheNoEof();
62 }
63
64
65
66
67
68
69
70
71
72 public void testReadBeyondCacheEof() throws Exception
73 {
74 final FileOperations ops = this.getFileOperations();
75 final byte[] buf = new byte[ 100 ];
76 final byte[] eofBuf = new byte[ 200 ];
77
78 int toRead = buf.length;
79 int totalRead = 0;
80 int read = -1;
81
82 Arrays.fill( buf, ( byte ) 100 );
83 ops.setLength( 0L );
84 ops.setFilePointer( 0L );
85 ops.write( buf, 0, buf.length );
86
87 ops.setFilePointer( 0L );
88
89 do
90 {
91 read = ops.read( buf, totalRead, toRead );
92 assert read != -1;
93 totalRead += read;
94 toRead -= read;
95 }
96 while ( totalRead < buf.length );
97
98 Assert.assertEquals( buf.length, totalRead );
99
100 ops.setFilePointer( 0L );
101
102 totalRead = 0;
103 toRead = eofBuf.length;
104
105 do
106 {
107 read = ops.read( eofBuf, totalRead, toRead );
108
109 if ( read != -1 )
110 {
111 totalRead += read;
112 toRead -= read;
113 }
114 }
115 while ( totalRead < toRead && read != -1 );
116
117 Assert.assertEquals( 100L, totalRead );
118 }
119
120
121
122
123
124
125 public void testReadBeyondCacheNoEof() throws Exception
126 {
127 final FileOperations ops = this.getFileOperations();
128 final byte[] buf = new byte[ 100 ];
129 final byte[] noEofBuf = new byte[ 200 ];
130 Arrays.fill( buf, ( byte ) 100 );
131 ops.setLength( 0L );
132 ops.setFilePointer( 0L );
133 ops.write( buf, 0, buf.length );
134
135 int toRead = buf.length;
136 int totalRead = 0;
137 int read = -1;
138
139 ops.setFilePointer( 0L );
140 do
141 {
142 read = ops.read( buf, totalRead, toRead );
143 if ( read != -1 )
144 {
145 totalRead += read;
146 toRead -= read;
147 }
148 }
149 while ( totalRead < buf.length && read != -1 );
150
151 Assert.assertEquals( buf.length, totalRead );
152 Assert.assertEquals( -1L, ops.read( buf, 0, buf.length ) );
153 ops.setFilePointer( 0L );
154 ops.setLength( 10000L );
155
156 totalRead = 0;
157 toRead = noEofBuf.length;
158
159 do
160 {
161 read = ops.read( noEofBuf, totalRead, toRead );
162 if ( read != -1 )
163 {
164 totalRead += read;
165 toRead -= read;
166 }
167 }
168 while ( totalRead < noEofBuf.length && read != -1 );
169
170 Assert.assertEquals( noEofBuf.length, totalRead );
171 }
172
173
174 }