task4/solution11/test/org/apidesign/apifest08/test/Task2Test.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 53 task3/solution11/test/org/apidesign/apifest08/test/Task2Test.java@09d690bb97f6
permissions -rw-r--r--
Copying structure for task4
     1 package org.apidesign.apifest08.test;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Collection;
     5 import junit.framework.TestCase;
     6 import org.apidesign.apifest08.currency.Convertor;
     7 import org.apidesign.apifest08.currency.CurrencyValue;
     8 import org.apidesign.apifest08.currency.ExchangeRateValue;
     9 
    10 /** There are many currencies around the world and many banks manipulate
    11  * with more than one or two at the same time. As banks are usually the
    12  * best paying clients, which is true even in case of your Convertor API,
    13  * it is reasonable to listen to their requests.
    14  * <p>
    15  * The quest for today is to enhance your existing convertor API to hold
    16  * information about many currencies and allow conversions between any of them.
    17  * Also, as conversion rates for diferent currencies usually arise from various
    18  * bank departments, there is another important need. There is a need to
    19  * compose two convertors into one by merging all the information about
    20  * currencies they know about.
    21  */
    22 public class Task2Test extends TestCase {
    23 
    24     public Task2Test(String testName) {
    25         super(testName);
    26     }
    27 
    28     @Override
    29     protected void setUp() throws Exception {
    30     }
    31 
    32     @Override
    33     protected void tearDown() throws Exception {
    34     }
    35 
    36     // As in Task1Test, keep in mind, that there are three parts
    37     // of the whole system:
    38     // 1. there is someone who knows the current exchange rate
    39     // 2. there is someone who wants to do the conversion
    40     // 3. there is the API between 1. and 2. which allows them to communicate
    41     // 
    42     // Please backward compatibly enhance your existing API to support following
    43     // usecases:
    44     //
    45     /** Create convertor that understands two currencies, CZK and
    46      *  SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
    47      *  knows the exchange rate, and needs to use the API to create objects
    48      *  with the exchange rate. Anyone shall be ready to call this method without
    49      *  any other method being called previously. The API itself shall know
    50      *  nothing about any rates, before this method is called.
    51      */
    52     public static Convertor<Double, String> createTripleConvertor() {
    53         // Rates: 1USD = 15CZK
    54         // Rates: 1USD = 20SKK
    55         // Rates: 75CZK = 100SKK
    56         Collection<ExchangeRateValue<Double, String>> exchangeRates =
    57                 new ArrayList<ExchangeRateValue<Double, String>>();
    58         exchangeRates.add(ExchangeRateValue.getExchangeRate(
    59                 CurrencyValue.getCurrencyValue(1d, "USD"),
    60                 CurrencyValue.getCurrencyValue(15d, "CZK")));
    61         exchangeRates.add(ExchangeRateValue.getExchangeRate(
    62                 CurrencyValue.getCurrencyValue(1d, "USD"),
    63                 CurrencyValue.getCurrencyValue(20d, "SKK")));
    64         exchangeRates.add(ExchangeRateValue.getExchangeRate(
    65                 CurrencyValue.getCurrencyValue(75d, "CZK"),
    66                 CurrencyValue.getCurrencyValue(100d, "SKK")));
    67         return Convertor.getConvertorDoubleString(exchangeRates);
    68     }
    69 
    70     /** Define convertor that understands three currencies. Use it.
    71      */
    72     public void testConvertorForUSDandCZKandSKK() throws Exception {
    73         Convertor<Double, String> c = createTripleConvertor();
    74 
    75         CurrencyValue<Double, String> result;
    76         // convert $5 to CZK using c:
    77         // assertEquals("Result is 75 CZK");
    78         result = c.convert("CZK", CurrencyValue.getCurrencyValue(5d, "USD"));
    79         assertEquals(CurrencyValue.getCurrencyValue(75d, "CZK"), result);
    80 
    81         // convert $5 to SKK using c:
    82         // assertEquals("Result is 100 SKK");
    83         result = c.convert("SKK", CurrencyValue.getCurrencyValue(5d, "USD"));
    84         assertEquals(CurrencyValue.getCurrencyValue(100d, "SKK"), result);
    85 
    86         // convert 200SKK to CZK using c:
    87         // assertEquals("Result is 150 CZK");
    88         result = c.convert("CZK", CurrencyValue.getCurrencyValue(200d, "SKK"));
    89         assertEquals(CurrencyValue.getCurrencyValue(150d, "CZK"), result);
    90 
    91         // convert 200SKK to USK using c:
    92         // assertEquals("Result is 10 USD");
    93         result = c.convert("USD", CurrencyValue.getCurrencyValue(200d, "SKK"));
    94         assertEquals(CurrencyValue.getCurrencyValue(10d, "USD"), result);
    95     }
    96 
    97     /** Merge all currency rates of convertor 1 with convertor 2.
    98      * Implement this using your API, preferably this method just delegates
    99      * into some API method which does the actual work, without requiring
   100      * API clients to code anything complex.
   101      */
   102     public static Convertor<Double, String> merge(Convertor<Double, String> one, Convertor<Double, String> two) {
   103         return Convertor.mergeConvertorsDoubleString(one, two);
   104     }
   105 
   106     /** Join the convertors from previous task, Task1Test and show that it
   107      * can be used to do reasonable conversions.
   108      */
   109     public void testConvertorComposition() throws Exception {
   110         Convertor<Double, String> c = merge(
   111                 Task1Test.createCZKtoUSD(),
   112                 Task1Test.createSKKtoCZK());
   113 
   114         CurrencyValue<Double, String> result;
   115         // convert $5 to CZK using c:
   116         // assertEquals("Result is 85 CZK");
   117         result = c.convert("CZK", CurrencyValue.getCurrencyValue(5d, "USD"));
   118         assertEquals(CurrencyValue.getCurrencyValue(85d, "CZK"), result);
   119 
   120         // convert $8 to CZK using c:
   121         // assertEquals("Result is 136 CZK");
   122         result = c.convert("CZK", CurrencyValue.getCurrencyValue(8d, "USD"));
   123         assertEquals(CurrencyValue.getCurrencyValue(136d, "CZK"), result);
   124 
   125         // convert 1003CZK to USD using c:
   126         // assertEquals("Result is 59 USD");
   127         result = c.convert("USD", CurrencyValue.getCurrencyValue(1003d, "CZK"));
   128         assertEquals(CurrencyValue.getCurrencyValue(59d, "USD"), result);
   129 
   130         // convert 16CZK using c:
   131         // assertEquals("Result is 20 SKK");
   132         result = c.convert("SKK", CurrencyValue.getCurrencyValue(16d, "CZK"));
   133         assertEquals(CurrencyValue.getCurrencyValue(20d, "SKK"), result);
   134 
   135         // convert 500SKK to CZK using c:
   136         // assertEquals("Result is 400 CZK");
   137         result = c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"));
   138         assertEquals(CurrencyValue.getCurrencyValue(400d, "CZK"), result);
   139     }
   140 }