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: ReadAheadFileOperationsUnevenTest.java 8641 2012-09-27 06:45:17Z schulte $ 034 */ 035public class ReadAheadFileOperationsUnevenTest 036 extends ReadAheadFileOperationsTest 037{ 038 //--FileOperationsTest------------------------------------------------------ 039 040 public FileOperations getFileOperations() 041 { 042 try 043 { 044 return new ReadAheadFileOperations( 045 this.getMemoryFileOperations(), 3 ); 046 047 } 048 catch ( IOException e ) 049 { 050 throw new AssertionError( e ); 051 } 052 } 053 054 //------------------------------------------------------FileOperationsTest-- 055 //--TestCase---------------------------------------------------------------- 056 057 protected void runTest() throws Throwable 058 { 059 super.runTest(); 060 this.testReadBeyondCacheEof(); 061 this.testReadBeyondCacheNoEof(); 062 } 063 064 //----------------------------------------------------------------TestCase-- 065 //--ReadAheadFileOperationsUnevenTest--------------------------------------- 066 067 /** 068 * Tests the {@link ReadAheadFileOperations#read(byte[],int,int)} method 069 * to correctly read beyond the cache limit when more data to read is 070 * requested than available for the file. 071 */ 072 public void testReadBeyondCacheEof() throws Exception 073 { 074 final FileOperations ops = this.getFileOperations(); 075 final byte[] buf = new byte[ 100 ]; 076 final byte[] eofBuf = new byte[ 200 ]; 077 078 int toRead = buf.length; 079 int totalRead = 0; 080 int read = -1; 081 082 Arrays.fill( buf, ( byte ) 100 ); 083 ops.setLength( 0L ); 084 ops.setFilePointer( 0L ); 085 ops.write( buf, 0, buf.length ); 086 087 ops.setFilePointer( 0L ); 088 089 do 090 { 091 read = ops.read( buf, totalRead, toRead ); 092 assert read != -1; 093 totalRead += read; 094 toRead -= read; 095 } 096 while ( totalRead < buf.length ); 097 098 Assert.assertEquals( buf.length, totalRead ); 099 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}