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.it;
022
023import java.util.Calendar;
024import java.util.Currency;
025import java.util.Date;
026import junit.framework.Assert;
027import junit.framework.TestCase;
028import org.jdtaus.banking.CurrencyDirectory;
029
030/**
031 * Testcase for {@code CurrencyDirectory} implementations.
032 *
033 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
034 * @version $JDTAUS: CurrencyDirectoryTest.java 8723 2012-10-04 20:09:53Z schulte $
035 */
036public class CurrencyDirectoryTest extends TestCase
037{
038
039    /** Implementation to test. */
040    private CurrencyDirectory directory;
041
042    /**
043     * Gets the {@code CurrencyDirectory} implementation tests are performed with.
044     *
045     * @return the {@code CurrencyDirectory} implementation tests are performed with.
046     */
047    public CurrencyDirectory getCurrencyDirectory()
048    {
049        return this.directory;
050    }
051
052    /**
053     * Sets the {@code CurrencyDirectory} implementation tests are performed with.
054     *
055     * @param value the {@code CurrencyDirectory} implementation to perform tests with.
056     */
057    public final void setCurrencyDirectory( final CurrencyDirectory value )
058    {
059        this.directory = value;
060    }
061
062    /**
063     * Checks a given array of currencies to contain an instance corresponding to a given ISO code.
064     *
065     * @param currencies The currencies to check.
066     * @param isoCode ISO code to check existence in {@code currencies} with.
067     *
068     * @throws NullPointerException if either {@code currencies} or {@code isoCode} is {@code null}.
069     */
070    protected void assertContainsCurrency( final Currency[] currencies, final String isoCode )
071    {
072        if ( currencies == null )
073        {
074            throw new NullPointerException( "currencies" );
075        }
076        if ( isoCode == null )
077        {
078            throw new NullPointerException( "isoCode" );
079        }
080
081        boolean contains = false;
082        for ( int i = currencies.length - 1; i >= 0; i-- )
083        {
084            if ( currencies[i].getCurrencyCode().equals( isoCode ) )
085            {
086                contains = true;
087                break;
088            }
089        }
090
091        if ( !contains )
092        {
093            throw new AssertionError( isoCode );
094        }
095    }
096
097    /**
098     * Checks a given array of currencies to not contain an instance corresponding to a given ISO code.
099     *
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}