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