001/*
002 *  jDTAUS Banking Test Suite
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.banking.dtaus.it;
022
023import java.io.File;
024import junit.framework.Assert;
025import junit.framework.TestCase;
026import org.jdtaus.banking.dtaus.PhysicalFile;
027import org.jdtaus.banking.dtaus.PhysicalFileException;
028import org.jdtaus.banking.dtaus.PhysicalFileFactory;
029import org.jdtaus.core.io.FileOperations;
030import org.jdtaus.core.io.util.MemoryFileOperations;
031
032/**
033 * Testcase for {@code BankleitzahlenVerzeichnis} implementations.
034 *
035 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
036 * @version $JDTAUS: PhysicalFileFactoryTest.java 8723 2012-10-04 20:09:53Z schulte $
037 */
038public class PhysicalFileFactoryTest extends TestCase
039{
040
041    /** Implementation to test. */
042    private PhysicalFileFactory factory;
043
044    /**
045     * Gets the {@code PhysicalFileFactory} implementation tests are performed with.
046     *
047     * @return the {@code PhysicalFileFactory} implementation tests are performed with.
048     */
049    public PhysicalFileFactory getPhysicalFileFactory()
050    {
051        return this.factory;
052    }
053
054    /**
055     * Sets the {@code PhysicalFileFactory} implementation tests are performed with.
056     *
057     * @param value the {@code PhysicalFileFactory} implementation to perform tests with.
058     */
059    public final void setPhysicalFileFactory( final PhysicalFileFactory value )
060    {
061        this.factory = value;
062    }
063
064    /**
065     * Tests the {@link PhysicalFileFactory#createPhysicalFile(FileOperations,int)} method to handle illegal arguments
066     * correctly by throwing a corresponding {@code NullPointerException} or {@code IllegalArgumentException}.
067     */
068    public void testCreateIllegalArguments() throws Exception
069    {
070        assert this.getPhysicalFileFactory() != null;
071
072        try
073        {
074            this.getPhysicalFileFactory().createPhysicalFile( (FileOperations) null, PhysicalFileFactory.FORMAT_DISK );
075            throw new AssertionError();
076        }
077        catch ( NullPointerException e )
078        {
079            Assert.assertNotNull( e.getMessage() );
080            System.out.println( e.toString() );
081        }
082
083        try
084        {
085            this.getPhysicalFileFactory().createPhysicalFile( (File) null, PhysicalFileFactory.FORMAT_DISK );
086            throw new AssertionError();
087        }
088        catch ( NullPointerException e )
089        {
090            Assert.assertNotNull( e.getMessage() );
091            System.out.println( e.toString() );
092        }
093
094        try
095        {
096            this.getPhysicalFileFactory().createPhysicalFile( new MemoryFileOperations(), Integer.MIN_VALUE );
097            throw new AssertionError();
098        }
099        catch ( IllegalArgumentException e )
100        {
101            Assert.assertNotNull( e.getMessage() );
102            System.out.println( e.toString() );
103        }
104
105
106    }
107
108    /**
109     * Tests the {@link PhysicalFileFactory#createPhysicalFile(FileOperations,int)} method to return a working
110     * {@code PhysicalFile} instance for the tape and disk format.
111     */
112    public void testCreate() throws Exception
113    {
114        assert this.getPhysicalFileFactory() != null;
115
116        final FileOperations diskOps = new MemoryFileOperations();
117        final FileOperations tapeOps = new MemoryFileOperations();
118
119        final PhysicalFile diskFile =
120            this.getPhysicalFileFactory().createPhysicalFile( diskOps, PhysicalFileFactory.FORMAT_DISK );
121
122        final PhysicalFile tapeFile =
123            this.getPhysicalFileFactory().createPhysicalFile( tapeOps, PhysicalFileFactory.FORMAT_TAPE );
124
125        Assert.assertNotNull( diskFile );
126        Assert.assertNotNull( tapeFile );
127
128        Assert.assertTrue( diskFile.getLogicalFileCount() == 0 );
129        Assert.assertTrue( tapeFile.getLogicalFileCount() == 0 );
130    }
131
132    /**
133     * Test the {@link PhysicalFileFactory#getPhysicalFile(FileOperations)} method to handle illegal arguments
134     * correctly.
135     */
136    public void testGetPhysicalFileNull() throws Exception
137    {
138        assert this.getPhysicalFileFactory() != null;
139
140        try
141        {
142            this.getPhysicalFileFactory().getPhysicalFile( (FileOperations) null );
143            throw new AssertionError();
144        }
145        catch ( NullPointerException e )
146        {
147            Assert.assertNotNull( e.getMessage() );
148            System.out.println( e.toString() );
149        }
150
151        try
152        {
153            this.getPhysicalFileFactory().getPhysicalFile( (File) null );
154            throw new AssertionError();
155        }
156        catch ( NullPointerException e )
157        {
158            Assert.assertNotNull( e.getMessage() );
159            System.out.println( e.toString() );
160        }
161    }
162
163    /**
164     * Test the {@link PhysicalFileFactory#getPhysicalFile(FileOperations)} method to return en empty
165     * {@code PhysicalFile} for a given {@code FileOperations} instance of no length.
166     */
167    public void testGetPhysicalFileNoLength() throws Exception
168    {
169        assert this.getPhysicalFileFactory() != null;
170
171        final PhysicalFile pFile = this.getPhysicalFileFactory().getPhysicalFile( new MemoryFileOperations() );
172        Assert.assertNotNull( pFile );
173        Assert.assertTrue( pFile.getLogicalFileCount() == 0 );
174    }
175
176    /**
177     * Test the {@link PhysicalFileFactory#analyse(FileOperations)} method to handle illegal arguments correctly.
178     */
179    public void testAnalyseNull() throws Exception
180    {
181        assert this.getPhysicalFileFactory() != null;
182
183        try
184        {
185            this.getPhysicalFileFactory().analyse( (FileOperations) null );
186            throw new AssertionError();
187        }
188        catch ( NullPointerException e )
189        {
190            Assert.assertNotNull( e.getMessage() );
191            System.out.println( e.toString() );
192        }
193
194        try
195        {
196            this.getPhysicalFileFactory().analyse( (File) null );
197            throw new AssertionError();
198        }
199        catch ( NullPointerException e )
200        {
201            Assert.assertNotNull( e.getMessage() );
202            System.out.println( e.toString() );
203        }
204    }
205
206    /**
207     * Test the {@link PhysicalFileFactory#analyse(FileOperations)} method to correctly analyze an empty file by
208     * throwing a {@code PhysicalFileExceptin}.
209     */
210    public void testAnalyseNoLength() throws Exception
211    {
212        assert this.getPhysicalFileFactory() != null;
213
214        try
215        {
216            this.getPhysicalFileFactory().analyse( new MemoryFileOperations() );
217            throw new AssertionError();
218        }
219        catch ( PhysicalFileException e )
220        {
221            Assert.assertNotNull( e.getMessage() );
222            System.out.println( e.toString() );
223        }
224
225    }
226
227}