task4/solution04/test/org/apidesign/apifest08/test/Task1Test.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 45 task3/solution04/test/org/apidesign/apifest08/test/Task1Test.java@251d0ed461fb
child 69 420baec87dc5
permissions -rw-r--r--
Copying structure for task4
japod@6
     1
package org.apidesign.apifest08.test;
japod@6
     2
japod@17
     3
japod@6
     4
import java.math.BigDecimal;
japod@6
     5
import java.util.Currency;
japod@35
     6
import java.util.Set;
japod@6
     7
import junit.framework.TestCase;
japod@6
     8
import org.apidesign.apifest08.currency.Convertor;
japod@6
     9
import org.apidesign.apifest08.currency.ConvertorFactory;
japod@17
    10
import org.apidesign.apifest08.currency.InvalidConversionException;
japod@17
    11
japod@6
    12
japod@6
    13
/** Finish the Convertor API, and then write bodies of methods inside
japod@6
    14
 * of this class to match the given tasks. To fullfil your task, use the
japod@6
    15
 * API define in the <code>org.apidesign.apifest08.currency</code> package.
japod@6
    16
 * Do not you reflection, or other hacks as your code
japod@6
    17
 * shall run without any runtime permissions.
japod@6
    18
 */
japod@6
    19
public class Task1Test extends TestCase {
japod@17
    20
    
japod@17
    21
    private final static Currency CZK;
japod@17
    22
    private final static Currency SKK;
japod@17
    23
    private final static Currency USD;
japod@17
    24
japod@17
    25
    static
japod@17
    26
    {
japod@17
    27
        CZK = Currency.getInstance("CZK");
japod@17
    28
        SKK = Currency.getInstance("SKK");
japod@17
    29
        USD = Currency.getInstance("USD");
japod@17
    30
    }
japod@17
    31
    
japod@6
    32
    public Task1Test(String testName) {
japod@6
    33
        super(testName);
japod@6
    34
    }
japod@6
    35
japod@6
    36
    @Override
japod@6
    37
    protected void setUp() throws Exception {
japod@6
    38
    }
japod@6
    39
japod@6
    40
    @Override
japod@6
    41
    protected void tearDown() throws Exception {
japod@6
    42
    }
japod@6
    43
japod@6
    44
    /** Create convertor that understands two currencies, CZK and
japod@6
    45
     *  USD. Make 1 USD == 17 CZK.
japod@6
    46
     *
japod@6
    47
     * Creation of the convertor shall not require subclassing of any class
japod@6
    48
     * or interface on the client side.
japod@6
    49
     *
japod@6
    50
     * @return prepared convertor ready for converting USD to CZK and CZK to USD
japod@6
    51
     */
japod@6
    52
    public static Convertor createCZKtoUSD()
japod@6
    53
    {
japod@35
    54
        return (ConvertorFactory.getConvertor("CZK", BigDecimal.valueOf(17.0),
japod@35
    55
                                              "USD", BigDecimal.valueOf(1)));
japod@6
    56
    }
japod@6
    57
japod@6
    58
    /** Create convertor that understands two currencies, CZK and
japod@6
    59
     *  SKK. Make 100 SKK == 80 CZK.
japod@6
    60
     *
japod@6
    61
     * Creation of the convertor shall not require subclassing of any class
japod@6
    62
     * or interface on the client side.
japod@6
    63
     * 
japod@6
    64
     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
japod@6
    65
     */
japod@6
    66
    public static Convertor createSKKtoCZK()
japod@6
    67
    {
japod@35
    68
        return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), BigDecimal.valueOf(100),
japod@35
    69
                                              Currency.getInstance("CZK"), BigDecimal.valueOf(80)));
japod@6
    70
    }
japod@6
    71
    
japod@6
    72
    /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
japod@6
    73
     * with it.
japod@6
    74
     */
japod@6
    75
    public void testCurrencyCZKUSD() throws Exception {
japod@6
    76
        Convertor  c = createCZKtoUSD();
japod@6
    77
        BigDecimal result;
japod@6
    78
        
japod@6
    79
        // convert $5 to CZK using c:
japod@6
    80
        // assertEquals("Result is 85 CZK");
japod@17
    81
        result = c.convert(USD, CZK, BigDecimal.valueOf(5));
japod@6
    82
        assertEquals(new BigDecimal("85.00"), result);
japod@6
    83
japod@6
    84
        // convert $8 to CZK
japod@6
    85
        // assertEquals("Result is 136 CZK");
japod@17
    86
        result = c.convert(USD, CZK, BigDecimal.valueOf(8));
japod@6
    87
        assertEquals(new BigDecimal("136.00"), result);
japod@6
    88
japod@6
    89
        // convert 1003CZK to USD
japod@6
    90
        // assertEquals("Result is 59 USD");
japod@17
    91
        result = c.convert(CZK, USD, BigDecimal.valueOf(1003));
japod@6
    92
        assertEquals(new BigDecimal("59.00"), result);
japod@6
    93
    }
japod@6
    94
japod@6
    95
    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
japod@6
    96
     * with it.
japod@6
    97
     */
japod@6
    98
    public void testCurrencySKKCZK() throws Exception {
japod@6
    99
        Convertor c = createSKKtoCZK();
japod@6
   100
        BigDecimal result;
japod@6
   101
        
japod@6
   102
        // convert 16CZK using c:
japod@6
   103
        // assertEquals("Result is 20 SKK");
japod@17
   104
        result = c.convert(CZK, SKK, BigDecimal.valueOf(16));
japod@6
   105
        assertEquals(new BigDecimal("20.00"), result);
japod@6
   106
                        
japod@6
   107
        // convert 500SKK to CZK
japod@6
   108
        // assertEquals("Result is 400 CZK");
japod@17
   109
        result = c.convert(SKK, CZK, BigDecimal.valueOf(500));
japod@6
   110
        assertEquals(new BigDecimal("400.00"), result);
japod@6
   111
   }
japod@6
   112
japod@17
   113
    /** 
japod@17
   114
     * Verify that the CZK to USD convertor knows nothing about SKK.
japod@6
   115
     */
japod@17
   116
    public void testCannotConvertToSKKwithCZKUSDConvertor() 
japod@17
   117
        throws Exception 
japod@17
   118
    {
japod@17
   119
        Convertor c = createCZKtoUSD();
japod@6
   120
        
japod@17
   121
        try
japod@17
   122
        {
japod@17
   123
            // convert $5 to SKK, the API shall say this is not possible
japod@17
   124
            c.convert(USD, SKK, BigDecimal.valueOf(5));
japod@17
   125
            fail("cannot use the CZKtoUSD converter to convert to SKK");
japod@17
   126
        }
japod@17
   127
        catch(InvalidConversionException ex)
japod@17
   128
        {       
japod@17
   129
            assertEquals("cannot convert to: SKK", ex.getMessage());
japod@17
   130
            assertEquals(SKK, ex.getBadCurrency());
japod@17
   131
            assertEquals(CZK, ex.getCurrencyA());
japod@17
   132
            assertEquals(USD, ex.getCurrencyB());
japod@17
   133
        }
japod@17
   134
japod@17
   135
        try
japod@17
   136
        {
japod@17
   137
            // convert 500 SKK to CZK, the API shall say this is not possible
japod@17
   138
            c.convert(SKK, CZK, BigDecimal.valueOf(5));
japod@17
   139
            fail("cannot use the CZKtoUSD converter to convert from SKK");
japod@17
   140
        }
japod@17
   141
        catch(InvalidConversionException ex)
japod@17
   142
        {            
japod@17
   143
            assertEquals("cannot convert from: SKK", ex.getMessage());
japod@17
   144
            assertEquals(SKK, ex.getBadCurrency());
japod@17
   145
            assertEquals(CZK, ex.getCurrencyA());
japod@17
   146
            assertEquals(USD, ex.getCurrencyB());
japod@17
   147
        }
japod@17
   148
    }
japod@17
   149
    
japod@17
   150
    /** 
japod@17
   151
     * Verify that the CZK to SKK convertor knows nothing about USD.
japod@17
   152
     */
japod@17
   153
    public void testCannotConvertToUSDwithSKKCZKConvertor() 
japod@17
   154
        throws Exception 
japod@17
   155
    {
japod@17
   156
        Convertor c = createSKKtoCZK();
japod@17
   157
        
japod@17
   158
        try
japod@17
   159
        {
japod@17
   160
            // convert $5 to SKK, the API shall say this is not possible
japod@17
   161
            c.convert(USD, SKK, BigDecimal.valueOf(5));
japod@17
   162
            fail("cannot use the CZKtoUSD converter to convert to SKK");
japod@17
   163
        }
japod@17
   164
        catch(InvalidConversionException ex)
japod@17
   165
        {       
japod@17
   166
            assertEquals("cannot convert from: USD", ex.getMessage());
japod@17
   167
            assertEquals(USD, ex.getBadCurrency());
japod@17
   168
            assertEquals(SKK, ex.getCurrencyA());
japod@17
   169
            assertEquals(CZK, ex.getCurrencyB());
japod@17
   170
        }
japod@17
   171
japod@17
   172
        try
japod@17
   173
        {
japod@17
   174
            // convert 500 CZK to USD, the API shall say this is not possible
japod@17
   175
            c.convert(CZK, USD, BigDecimal.valueOf(500));
japod@17
   176
            fail("cannot use the CZKtoUSD converter to convert from SKK");
japod@17
   177
        }
japod@17
   178
        catch(InvalidConversionException ex)
japod@17
   179
        {            
japod@17
   180
            assertEquals("cannot convert to: USD", ex.getMessage());
japod@17
   181
            assertEquals(USD, ex.getBadCurrency());
japod@17
   182
            assertEquals(SKK, ex.getCurrencyA());
japod@17
   183
            assertEquals(CZK, ex.getCurrencyB());
japod@17
   184
        }
japod@17
   185
    }
japod@35
   186
    
japod@35
   187
    public void testGetCurrencies()
japod@35
   188
    {
japod@35
   189
        Convertor           c;
japod@35
   190
        Set<Currency> currencies;
japod@35
   191
        
japod@35
   192
        c          = createSKKtoCZK();
japod@35
   193
        currencies = c.getCurrencies();        
japod@35
   194
        assertEquals(2, currencies.size());
japod@35
   195
        assertTrue(currencies.contains(Currency.getInstance("SKK")));
japod@35
   196
        assertTrue(currencies.contains(Currency.getInstance("CZK")));
japod@35
   197
japod@35
   198
        c          = createCZKtoUSD();
japod@35
   199
        currencies = c.getCurrencies();        
japod@35
   200
        assertEquals(2, currencies.size());
japod@35
   201
        assertTrue(currencies.contains(Currency.getInstance("USD")));
japod@35
   202
        assertTrue(currencies.contains(Currency.getInstance("CZK")));
japod@35
   203
    }
japod@35
   204
    
japod@35
   205
    public void testGetConverstionRate()
japod@35
   206
        throws InvalidConversionException
japod@35
   207
    {
japod@35
   208
        Convertor c;
japod@35
   209
       
japod@35
   210
        c = createSKKtoCZK();
japod@35
   211
        assertEquals(1.0,  c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("CZK")).doubleValue());
japod@35
   212
        assertEquals(1.0,  c.getConversionRate(Currency.getInstance("SKK"), Currency.getInstance("SKK")).doubleValue());
japod@35
   213
        assertEquals(0.80, c.getConversionRate(Currency.getInstance("SKK"), Currency.getInstance("CZK")).doubleValue());
japod@35
   214
        assertEquals(1.25, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("SKK")).doubleValue());
japod@35
   215
japod@35
   216
        c = createCZKtoUSD();
japod@35
   217
        assertEquals(1.0,      c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("CZK")).doubleValue());
japod@35
   218
        assertEquals(1.0,      c.getConversionRate(Currency.getInstance("USD"), Currency.getInstance("USD")).doubleValue());
japod@35
   219
        assertEquals(1.0/17.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("USD")).doubleValue(), 0.00000000000000001);
japod@35
   220
        assertEquals(17.0,     c.getConversionRate(Currency.getInstance("USD"), Currency.getInstance("CZK")).doubleValue(), 0.00000000000000001);
japod@35
   221
    }
japod@6
   222
}
japod@6
   223