View Javadoc

1   /*
2    *  jDTAUS Core Utilities
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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   * Testcase for {@code ReadAheadFileOperations} implementations.
31   *
32   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
33   * @version $JDTAUS: ReadAheadFileOperationsUnevenTest.java 8641 2012-09-27 06:45:17Z schulte $
34   */
35  public class ReadAheadFileOperationsUnevenTest
36      extends ReadAheadFileOperationsTest
37  {
38      //--FileOperationsTest------------------------------------------------------
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      //------------------------------------------------------FileOperationsTest--
55      //--TestCase----------------------------------------------------------------
56  
57      protected void runTest() throws Throwable
58      {
59          super.runTest();
60          this.testReadBeyondCacheEof();
61          this.testReadBeyondCacheNoEof();
62      }
63  
64      //----------------------------------------------------------------TestCase--
65      //--ReadAheadFileOperationsUnevenTest---------------------------------------
66  
67      /**
68       * Tests the {@link ReadAheadFileOperations#read(byte[],int,int)} method
69       * to correctly read beyond the cache limit when more data to read is
70       * requested than available for the file.
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      * Tests the {@link ReadAheadFileOperations#read(byte[],int,int)} method
122      * to correctly read beyond the cache limit when less data to read is
123      * requested than available for the file.
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     //---------------------------------------ReadAheadFileOperationsUnevenTest--
174 }