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