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 junit.framework.Assert;
24  import org.jdtaus.core.io.it.FileOperationsTest;
25  import org.jdtaus.core.io.util.FlushableFileOperations;
26  import org.jdtaus.core.io.util.MemoryFileOperations;
27  
28  /**
29   * Testcase for {@code FlushableFileOperations} implementations.
30   *
31   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
32   * @version $JDTAUS: FlushableFileOperationsTest.java 8641 2012-09-27 06:45:17Z schulte $
33   */
34  public abstract class FlushableFileOperationsTest extends FileOperationsTest
35  {
36      //--FlushableFileOperationsTest---------------------------------------------
37  
38      /** {@code MemoryFileOperations} backing the test instance. */
39      private MemoryFileOperations memoryOps;
40  
41      /**
42       * Gets the {@code MemoryFileOperations} backing the test instance.
43       *
44       * @return the {@code MemoryFileOperations} backing the test instance.
45       */
46      public final MemoryFileOperations getMemoryFileOperations()
47      {
48          if ( this.memoryOps == null )
49          {
50              this.memoryOps = new MemoryFileOperations();
51          }
52  
53          return this.memoryOps;
54      }
55  
56      /**
57       * Gets the {@code FlushableFileOperations} implementation tests are
58       * performed with.
59       *
60       * @return the {@code FlushableFileOperations} implementation tests are
61       * performed with.
62       */
63      public final FlushableFileOperations getFlushableFileOperations()
64      {
65          return ( FlushableFileOperations ) this.getFileOperations();
66      }
67  
68      //---------------------------------------------FlushableFileOperationsTest--
69      //--TestCase----------------------------------------------------------------
70  
71      protected void runTest() throws Throwable
72      {
73          super.runTest();
74          this.testFlush();
75      }
76  
77      //----------------------------------------------------------------TestCase--
78      //--Tests-------------------------------------------------------------------
79  
80      /**
81       * Tests the {@link FlushableFileOperations#flush()} method.
82       */
83      public void testFlush() throws Exception
84      {
85          assert this.getFlushableFileOperations() != null;
86  
87          final FlushableFileOperations ops = this.getFlushableFileOperations();
88  
89          ops.write( this.getTestFile() );
90          ops.flush();
91  
92          final byte[] x = new byte[ 1 ];
93          x[0] = "X".getBytes( "US-ASCII" )[0];
94          ops.setFilePointer( 0L );
95          ops.write( x, 0, 1 );
96          ops.setFilePointer( ops.getLength() - 1L );
97          ops.write( x, 0, 1 );
98          ops.flush();
99  
100         final String flushed = new String( this.getMemoryFileOperations().
101                                             getData(), "US-ASCII" );
102 
103         Assert.assertEquals( "XBCDEFGHIJKLMNOPQRSTUVWXYX", flushed );
104         ops.setLength( 0L );
105     }
106 
107     //-------------------------------------------------------------------Tests--
108 }