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.ByteArrayOutputStream;
24  import java.io.File;
25  import java.io.IOException;
26  import java.io.RandomAccessFile;
27  import org.jdtaus.core.io.FileOperations;
28  import org.jdtaus.core.io.it.FileOperationsTest;
29  import org.jdtaus.core.io.util.RandomAccessFileOperations;
30  
31  /**
32   * Testcase for {@code RandomAccessFileOperations} implementations.
33   *
34   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
35   * @version $JDTAUS: RandomAccessFileOperationsTest.java 8641 2012-09-27 06:45:17Z schulte $
36   */
37  public class RandomAccessFileOperationsTest extends FileOperationsTest
38  {
39      //--FileOperationsTest------------------------------------------------------
40  
41      /** Temporary random access file. */
42      private File tmp;
43  
44      public FileOperations getFileOperations()
45      {
46          try
47          {
48              if ( this.tmp != null )
49              {
50                  this.tmp.delete();
51              }
52  
53              this.tmp = File.createTempFile( "jdtaus", "tmp" );
54              this.tmp.deleteOnExit();
55              return new RandomAccessFileOperations(
56                  new RandomAccessFile( this.tmp, "rw" ) );
57  
58          }
59          catch ( IOException e )
60          {
61              throw new AssertionError( e );
62          }
63      }
64  
65      //------------------------------------------------------FileOperationsTest--
66      //--TestCase----------------------------------------------------------------
67  
68      protected void runTest() throws Throwable
69      {
70          super.runTest();
71          this.testStreaming();
72      }
73  
74      //----------------------------------------------------------------TestCase--
75      //--RandomAccessFileOperationsTest------------------------------------------
76  
77      /**
78       * Tests the {@link FileOperations#read(OutputStream)} and
79       * {@link FileOperations#write(InputStream)} methods.
80       * <p><ol>
81       * <li>Writes a testfile from an {@code InputStream} to the file, then reads
82       * the file contents into a {@code ByteArrayOutputStream} and checks
83       * that the read data matches the written data.</li>
84       * </ol></p>
85       */
86      public void testStreaming() throws Exception
87      {
88          final File testFile = File.createTempFile( "jdtaus", "tmp" );
89          final RandomAccessFileOperations ops = new RandomAccessFileOperations(
90              new RandomAccessFile( testFile, "rw" ) );
91  
92          final ByteArrayOutputStream out = new ByteArrayOutputStream();
93  
94          ops.write( this.getTestFile() );
95          ops.read( out );
96          out.close();
97          this.assertValidTestFile( new String( out.toByteArray(), "UTF-8" ) );
98          testFile.delete();
99      }
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 }