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: ReadAheadFileOperationsTest.java 8641 2012-09-27 06:45:17Z schulte $
34   */
35  public class ReadAheadFileOperationsTest extends FlushableFileOperationsTest
36  {
37      //--FileOperationsTest------------------------------------------------------
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      //------------------------------------------------------FileOperationsTest--
52      //--TestCase----------------------------------------------------------------
53  
54      protected void runTest() throws Throwable
55      {
56          super.runTest();
57          this.testWriteUpdatesCache();
58      }
59  
60      //----------------------------------------------------------------TestCase--
61      //--ReadAheadFileOperationsTest---------------------------------------------
62  
63      /**
64       * Tests the {@link ReadAheadFileOperations#write(byte[],int,int)} to
65       * correctly update the cache.
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     //---------------------------------------------ReadAheadFileOperationsTest--
128 }