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.ByteArrayOutputStream; 024import java.io.File; 025import java.io.IOException; 026import java.io.RandomAccessFile; 027import org.jdtaus.core.io.FileOperations; 028import org.jdtaus.core.io.it.FileOperationsTest; 029import org.jdtaus.core.io.util.RandomAccessFileOperations; 030 031/** 032 * Testcase for {@code RandomAccessFileOperations} implementations. 033 * 034 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 035 * @version $JDTAUS: RandomAccessFileOperationsTest.java 8641 2012-09-27 06:45:17Z schulte $ 036 */ 037public class RandomAccessFileOperationsTest extends FileOperationsTest 038{ 039 //--FileOperationsTest------------------------------------------------------ 040 041 /** Temporary random access file. */ 042 private File tmp; 043 044 public FileOperations getFileOperations() 045 { 046 try 047 { 048 if ( this.tmp != null ) 049 { 050 this.tmp.delete(); 051 } 052 053 this.tmp = File.createTempFile( "jdtaus", "tmp" ); 054 this.tmp.deleteOnExit(); 055 return new RandomAccessFileOperations( 056 new RandomAccessFile( this.tmp, "rw" ) ); 057 058 } 059 catch ( IOException e ) 060 { 061 throw new AssertionError( e ); 062 } 063 } 064 065 //------------------------------------------------------FileOperationsTest-- 066 //--TestCase---------------------------------------------------------------- 067 068 protected void runTest() throws Throwable 069 { 070 super.runTest(); 071 this.testStreaming(); 072 } 073 074 //----------------------------------------------------------------TestCase-- 075 //--RandomAccessFileOperationsTest------------------------------------------ 076 077 /** 078 * Tests the {@link FileOperations#read(OutputStream)} and 079 * {@link FileOperations#write(InputStream)} methods. 080 * <p><ol> 081 * <li>Writes a testfile from an {@code InputStream} to the file, then reads 082 * the file contents into a {@code ByteArrayOutputStream} and checks 083 * that the read data matches the written data.</li> 084 * </ol></p> 085 */ 086 public void testStreaming() throws Exception 087 { 088 final File testFile = File.createTempFile( "jdtaus", "tmp" ); 089 final RandomAccessFileOperations ops = new RandomAccessFileOperations( 090 new RandomAccessFile( testFile, "rw" ) ); 091 092 final ByteArrayOutputStream out = new ByteArrayOutputStream(); 093 094 ops.write( this.getTestFile() ); 095 ops.read( out ); 096 out.close(); 097 this.assertValidTestFile( new String( out.toByteArray(), "UTF-8" ) ); 098 testFile.delete(); 099 } 100 101 //------------------------------------------RandomAccessFileOperationsTest-- 102 //--Object------------------------------------------------------------------ 103 104 public void finalize() 105 { 106 if ( this.tmp != null && this.tmp.exists() ) 107 { 108 this.tmp.delete(); 109 } 110 } 111 112 //------------------------------------------------------------------Object-- 113}