View Javadoc
1   /*
2    *  jDTAUS Banking Test Suite
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.banking.dtaus.it;
22  
23  import java.io.File;
24  import junit.framework.Assert;
25  import junit.framework.TestCase;
26  import org.jdtaus.banking.dtaus.PhysicalFile;
27  import org.jdtaus.banking.dtaus.PhysicalFileException;
28  import org.jdtaus.banking.dtaus.PhysicalFileFactory;
29  import org.jdtaus.core.io.FileOperations;
30  import org.jdtaus.core.io.util.MemoryFileOperations;
31  
32  /**
33   * Testcase for {@code BankleitzahlenVerzeichnis} implementations.
34   *
35   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
36   * @version $JDTAUS: PhysicalFileFactoryTest.java 8723 2012-10-04 20:09:53Z schulte $
37   */
38  public class PhysicalFileFactoryTest extends TestCase
39  {
40  
41      /** Implementation to test. */
42      private PhysicalFileFactory factory;
43  
44      /**
45       * Gets the {@code PhysicalFileFactory} implementation tests are performed with.
46       *
47       * @return the {@code PhysicalFileFactory} implementation tests are performed with.
48       */
49      public PhysicalFileFactory getPhysicalFileFactory()
50      {
51          return this.factory;
52      }
53  
54      /**
55       * Sets the {@code PhysicalFileFactory} implementation tests are performed with.
56       *
57       * @param value the {@code PhysicalFileFactory} implementation to perform tests with.
58       */
59      public final void setPhysicalFileFactory( final PhysicalFileFactory value )
60      {
61          this.factory = value;
62      }
63  
64      /**
65       * Tests the {@link PhysicalFileFactory#createPhysicalFile(FileOperations,int)} method to handle illegal arguments
66       * correctly by throwing a corresponding {@code NullPointerException} or {@code IllegalArgumentException}.
67       */
68      public void testCreateIllegalArguments() throws Exception
69      {
70          assert this.getPhysicalFileFactory() != null;
71  
72          try
73          {
74              this.getPhysicalFileFactory().createPhysicalFile( (FileOperations) null, PhysicalFileFactory.FORMAT_DISK );
75              throw new AssertionError();
76          }
77          catch ( NullPointerException e )
78          {
79              Assert.assertNotNull( e.getMessage() );
80              System.out.println( e.toString() );
81          }
82  
83          try
84          {
85              this.getPhysicalFileFactory().createPhysicalFile( (File) null, PhysicalFileFactory.FORMAT_DISK );
86              throw new AssertionError();
87          }
88          catch ( NullPointerException e )
89          {
90              Assert.assertNotNull( e.getMessage() );
91              System.out.println( e.toString() );
92          }
93  
94          try
95          {
96              this.getPhysicalFileFactory().createPhysicalFile( new MemoryFileOperations(), Integer.MIN_VALUE );
97              throw new AssertionError();
98          }
99          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 }