001/*
002 *  jDTAUS Core Utilities
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.core.io.util.test;
022
023import java.io.IOException;
024import java.util.Arrays;
025import junit.framework.Assert;
026import org.jdtaus.core.io.FileOperations;
027import org.jdtaus.core.io.util.ReadAheadFileOperations;
028
029/**
030 * Testcase for {@code ReadAheadFileOperations} implementations.
031 *
032 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
033 * @version $JDTAUS: ReadAheadFileOperationsTest.java 8641 2012-09-27 06:45:17Z schulte $
034 */
035public class ReadAheadFileOperationsTest extends FlushableFileOperationsTest
036{
037    //--FileOperationsTest------------------------------------------------------
038
039    public FileOperations getFileOperations()
040    {
041        try
042        {
043            return new ReadAheadFileOperations( this.getMemoryFileOperations() );
044        }
045        catch ( IOException e )
046        {
047            throw new AssertionError( e );
048        }
049    }
050
051    //------------------------------------------------------FileOperationsTest--
052    //--TestCase----------------------------------------------------------------
053
054    protected void runTest() throws Throwable
055    {
056        super.runTest();
057        this.testWriteUpdatesCache();
058    }
059
060    //----------------------------------------------------------------TestCase--
061    //--ReadAheadFileOperationsTest---------------------------------------------
062
063    /**
064     * Tests the {@link ReadAheadFileOperations#write(byte[],int,int)} to
065     * correctly update the cache.
066     */
067    public void testWriteUpdatesCache() throws Exception
068    {
069        final FileOperations ops = this.getFileOperations();
070        final byte[] testBuf = new byte[ 101 ];
071        final byte[] buf = new byte[ 101 ];
072
073        int totalRead = 0;
074        int toRead = 100;
075        int read = 0;
076
077        Arrays.fill( testBuf, ( byte ) 100 );
078        Arrays.fill( buf, ( byte ) 0 );
079        buf[100] = ( byte ) 100;
080
081        ops.setLength( 0L );
082        ops.write( testBuf, 0, 100 );
083
084        Assert.assertEquals( 100L, ops.getFilePointer() );
085
086        ops.setFilePointer( 0L );
087
088        do
089        {
090            read = ops.read( buf, totalRead, toRead );
091            assert read != -1;
092            totalRead += read;
093            toRead -= read;
094        }
095        while ( totalRead < 100 );
096
097        Assert.assertTrue( Arrays.equals( testBuf, buf ) );
098
099        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}