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.it;
22  
23  import java.util.Calendar;
24  import java.util.Currency;
25  import java.util.Date;
26  import junit.framework.Assert;
27  import junit.framework.TestCase;
28  import org.jdtaus.banking.CurrencyDirectory;
29  
30  /**
31   * Testcase for {@code CurrencyDirectory} implementations.
32   *
33   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
34   * @version $JDTAUS: CurrencyDirectoryTest.java 8723 2012-10-04 20:09:53Z schulte $
35   */
36  public class CurrencyDirectoryTest extends TestCase
37  {
38  
39      /** Implementation to test. */
40      private CurrencyDirectory directory;
41  
42      /**
43       * Gets the {@code CurrencyDirectory} implementation tests are performed with.
44       *
45       * @return the {@code CurrencyDirectory} implementation tests are performed with.
46       */
47      public CurrencyDirectory getCurrencyDirectory()
48      {
49          return this.directory;
50      }
51  
52      /**
53       * Sets the {@code CurrencyDirectory} implementation tests are performed with.
54       *
55       * @param value the {@code CurrencyDirectory} implementation to perform tests with.
56       */
57      public final void setCurrencyDirectory( final CurrencyDirectory value )
58      {
59          this.directory = value;
60      }
61  
62      /**
63       * Checks a given array of currencies to contain an instance corresponding to a given ISO code.
64       *
65       * @param currencies The currencies to check.
66       * @param isoCode ISO code to check existence in {@code currencies} with.
67       *
68       * @throws NullPointerException if either {@code currencies} or {@code isoCode} is {@code null}.
69       */
70      protected void assertContainsCurrency( final Currency[] currencies, final String isoCode )
71      {
72          if ( currencies == null )
73          {
74              throw new NullPointerException( "currencies" );
75          }
76          if ( isoCode == null )
77          {
78              throw new NullPointerException( "isoCode" );
79          }
80  
81          boolean contains = false;
82          for ( int i = currencies.length - 1; i >= 0; i-- )
83          {
84              if ( currencies[i].getCurrencyCode().equals( isoCode ) )
85              {
86                  contains = true;
87                  break;
88              }
89          }
90  
91          if ( !contains )
92          {
93              throw new AssertionError( isoCode );
94          }
95      }
96  
97      /**
98       * Checks a given array of currencies to not contain an instance corresponding to a given ISO code.
99       *
100      * @param currencies The currencies to check.
101      * @param isoCode ISO code to check existence in {@code currencies} with.
102      *
103      * @throws NullPointerException if either {@code currencies} or {@code isoCode} is {@code null}.
104      */
105     protected void assertNotContainsCurrency( final Currency[] currencies, final String isoCode )
106     {
107         if ( currencies == null )
108         {
109             throw new NullPointerException( "currencies" );
110         }
111         if ( isoCode == null )
112         {
113             throw new NullPointerException( "isoCode" );
114         }
115 
116         boolean contains = false;
117         for ( int i = currencies.length - 1; i >= 0; i-- )
118         {
119             if ( currencies[i].getCurrencyCode().equals( isoCode ) )
120             {
121                 contains = true;
122                 break;
123             }
124         }
125 
126         if ( contains )
127         {
128             throw new AssertionError( isoCode );
129         }
130     }
131 
132     /**
133      * Test the {@link CurrencyDirectory#getDtausCurrencies(Date)} method to handle illegal arguments correctly by
134      * throwing a {@code NullPointerException}.
135      */
136     public void testGetDtausCurrenciesNull()
137     {
138         assert this.getCurrencyDirectory() != null;
139 
140         try
141         {
142             this.getCurrencyDirectory().getDtausCurrencies( 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 
152     /** Tests the {@link CurrencyDirectory#getDtausCurrencies(Date)} method to return sane values. */
153     public void testGetDtausCurrencies() throws Exception
154     {
155         assert this.getCurrencyDirectory() != null;
156         Assert.assertTrue( this.getCurrencyDirectory().getDtausCurrencies( new Date() ).length >= 0 );
157     }
158 
159     /**
160      * Tests the {@link CurrencyDirectory#getDtausCurrencies(Date)} method to return correct values for the EUR and DEM
161      * currency for various dates.
162      */
163     public void testCurrencyConstraints() throws Exception
164     {
165         assert this.getCurrencyDirectory() != null;
166 
167         final Calendar cal = Calendar.getInstance();
168         final Date now = cal.getTime();
169 
170         cal.set( Calendar.YEAR, 2001 );
171         cal.set( Calendar.MONTH, 11 );
172         cal.set( Calendar.DAY_OF_MONTH, 31 );
173 
174         final Date lastDayDEM = cal.getTime();
175 
176         cal.set( Calendar.YEAR, 2002 );
177         cal.set( Calendar.MONTH, 0 );
178         cal.set( Calendar.DAY_OF_MONTH, 1 );
179 
180         final Date firstDayEUR = cal.getTime();
181 
182         this.assertContainsCurrency( this.getCurrencyDirectory().getDtausCurrencies( now ), "EUR" );
183         this.assertNotContainsCurrency( this.getCurrencyDirectory().getDtausCurrencies( now ), "DEM" );
184         this.assertNotContainsCurrency( this.getCurrencyDirectory().getDtausCurrencies( lastDayDEM ), "EUR" );
185         this.assertContainsCurrency( this.getCurrencyDirectory().getDtausCurrencies( lastDayDEM ), "DEM" );
186         this.assertContainsCurrency( this.getCurrencyDirectory().getDtausCurrencies( firstDayEUR ), "EUR" );
187         this.assertNotContainsCurrency( this.getCurrencyDirectory().getDtausCurrencies( firstDayEUR ), "DEM" );
188     }
189 
190 }