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 junit.framework.Assert;
024import org.jdtaus.core.io.it.FileOperationsTest;
025import org.jdtaus.core.io.util.FlushableFileOperations;
026import org.jdtaus.core.io.util.MemoryFileOperations;
027
028/**
029 * Testcase for {@code FlushableFileOperations} implementations.
030 *
031 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
032 * @version $JDTAUS: FlushableFileOperationsTest.java 8641 2012-09-27 06:45:17Z schulte $
033 */
034public abstract class FlushableFileOperationsTest extends FileOperationsTest
035{
036    //--FlushableFileOperationsTest---------------------------------------------
037
038    /** {@code MemoryFileOperations} backing the test instance. */
039    private MemoryFileOperations memoryOps;
040
041    /**
042     * Gets the {@code MemoryFileOperations} backing the test instance.
043     *
044     * @return the {@code MemoryFileOperations} backing the test instance.
045     */
046    public final MemoryFileOperations getMemoryFileOperations()
047    {
048        if ( this.memoryOps == null )
049        {
050            this.memoryOps = new MemoryFileOperations();
051        }
052
053        return this.memoryOps;
054    }
055
056    /**
057     * Gets the {@code FlushableFileOperations} implementation tests are
058     * performed with.
059     *
060     * @return the {@code FlushableFileOperations} implementation tests are
061     * performed with.
062     */
063    public final FlushableFileOperations getFlushableFileOperations()
064    {
065        return ( FlushableFileOperations ) this.getFileOperations();
066    }
067
068    //---------------------------------------------FlushableFileOperationsTest--
069    //--TestCase----------------------------------------------------------------
070
071    protected void runTest() throws Throwable
072    {
073        super.runTest();
074        this.testFlush();
075    }
076
077    //----------------------------------------------------------------TestCase--
078    //--Tests-------------------------------------------------------------------
079
080    /**
081     * Tests the {@link FlushableFileOperations#flush()} method.
082     */
083    public void testFlush() throws Exception
084    {
085        assert this.getFlushableFileOperations() != null;
086
087        final FlushableFileOperations ops = this.getFlushableFileOperations();
088
089        ops.write( this.getTestFile() );
090        ops.flush();
091
092        final byte[] x = new byte[ 1 ];
093        x[0] = "X".getBytes( "US-ASCII" )[0];
094        ops.setFilePointer( 0L );
095        ops.write( x, 0, 1 );
096        ops.setFilePointer( ops.getLength() - 1L );
097        ops.write( x, 0, 1 );
098        ops.flush();
099
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}