task4/solution11/test/org/apidesign/apifest08/test/Task2Test.java
changeset 61 58ec6da75f6f
parent 53 09d690bb97f6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution11/test/org/apidesign/apifest08/test/Task2Test.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,140 @@
     1.4 +package org.apidesign.apifest08.test;
     1.5 +
     1.6 +import java.util.ArrayList;
     1.7 +import java.util.Collection;
     1.8 +import junit.framework.TestCase;
     1.9 +import org.apidesign.apifest08.currency.Convertor;
    1.10 +import org.apidesign.apifest08.currency.CurrencyValue;
    1.11 +import org.apidesign.apifest08.currency.ExchangeRateValue;
    1.12 +
    1.13 +/** There are many currencies around the world and many banks manipulate
    1.14 + * with more than one or two at the same time. As banks are usually the
    1.15 + * best paying clients, which is true even in case of your Convertor API,
    1.16 + * it is reasonable to listen to their requests.
    1.17 + * <p>
    1.18 + * The quest for today is to enhance your existing convertor API to hold
    1.19 + * information about many currencies and allow conversions between any of them.
    1.20 + * Also, as conversion rates for diferent currencies usually arise from various
    1.21 + * bank departments, there is another important need. There is a need to
    1.22 + * compose two convertors into one by merging all the information about
    1.23 + * currencies they know about.
    1.24 + */
    1.25 +public class Task2Test extends TestCase {
    1.26 +
    1.27 +    public Task2Test(String testName) {
    1.28 +        super(testName);
    1.29 +    }
    1.30 +
    1.31 +    @Override
    1.32 +    protected void setUp() throws Exception {
    1.33 +    }
    1.34 +
    1.35 +    @Override
    1.36 +    protected void tearDown() throws Exception {
    1.37 +    }
    1.38 +
    1.39 +    // As in Task1Test, keep in mind, that there are three parts
    1.40 +    // of the whole system:
    1.41 +    // 1. there is someone who knows the current exchange rate
    1.42 +    // 2. there is someone who wants to do the conversion
    1.43 +    // 3. there is the API between 1. and 2. which allows them to communicate
    1.44 +    // 
    1.45 +    // Please backward compatibly enhance your existing API to support following
    1.46 +    // usecases:
    1.47 +    //
    1.48 +    /** Create convertor that understands two currencies, CZK and
    1.49 +     *  SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
    1.50 +     *  knows the exchange rate, and needs to use the API to create objects
    1.51 +     *  with the exchange rate. Anyone shall be ready to call this method without
    1.52 +     *  any other method being called previously. The API itself shall know
    1.53 +     *  nothing about any rates, before this method is called.
    1.54 +     */
    1.55 +    public static Convertor<Double, String> createTripleConvertor() {
    1.56 +        // Rates: 1USD = 15CZK
    1.57 +        // Rates: 1USD = 20SKK
    1.58 +        // Rates: 75CZK = 100SKK
    1.59 +        Collection<ExchangeRateValue<Double, String>> exchangeRates =
    1.60 +                new ArrayList<ExchangeRateValue<Double, String>>();
    1.61 +        exchangeRates.add(ExchangeRateValue.getExchangeRate(
    1.62 +                CurrencyValue.getCurrencyValue(1d, "USD"),
    1.63 +                CurrencyValue.getCurrencyValue(15d, "CZK")));
    1.64 +        exchangeRates.add(ExchangeRateValue.getExchangeRate(
    1.65 +                CurrencyValue.getCurrencyValue(1d, "USD"),
    1.66 +                CurrencyValue.getCurrencyValue(20d, "SKK")));
    1.67 +        exchangeRates.add(ExchangeRateValue.getExchangeRate(
    1.68 +                CurrencyValue.getCurrencyValue(75d, "CZK"),
    1.69 +                CurrencyValue.getCurrencyValue(100d, "SKK")));
    1.70 +        return Convertor.getConvertorDoubleString(exchangeRates);
    1.71 +    }
    1.72 +
    1.73 +    /** Define convertor that understands three currencies. Use it.
    1.74 +     */
    1.75 +    public void testConvertorForUSDandCZKandSKK() throws Exception {
    1.76 +        Convertor<Double, String> c = createTripleConvertor();
    1.77 +
    1.78 +        CurrencyValue<Double, String> result;
    1.79 +        // convert $5 to CZK using c:
    1.80 +        // assertEquals("Result is 75 CZK");
    1.81 +        result = c.convert("CZK", CurrencyValue.getCurrencyValue(5d, "USD"));
    1.82 +        assertEquals(CurrencyValue.getCurrencyValue(75d, "CZK"), result);
    1.83 +
    1.84 +        // convert $5 to SKK using c:
    1.85 +        // assertEquals("Result is 100 SKK");
    1.86 +        result = c.convert("SKK", CurrencyValue.getCurrencyValue(5d, "USD"));
    1.87 +        assertEquals(CurrencyValue.getCurrencyValue(100d, "SKK"), result);
    1.88 +
    1.89 +        // convert 200SKK to CZK using c:
    1.90 +        // assertEquals("Result is 150 CZK");
    1.91 +        result = c.convert("CZK", CurrencyValue.getCurrencyValue(200d, "SKK"));
    1.92 +        assertEquals(CurrencyValue.getCurrencyValue(150d, "CZK"), result);
    1.93 +
    1.94 +        // convert 200SKK to USK using c:
    1.95 +        // assertEquals("Result is 10 USD");
    1.96 +        result = c.convert("USD", CurrencyValue.getCurrencyValue(200d, "SKK"));
    1.97 +        assertEquals(CurrencyValue.getCurrencyValue(10d, "USD"), result);
    1.98 +    }
    1.99 +
   1.100 +    /** Merge all currency rates of convertor 1 with convertor 2.
   1.101 +     * Implement this using your API, preferably this method just delegates
   1.102 +     * into some API method which does the actual work, without requiring
   1.103 +     * API clients to code anything complex.
   1.104 +     */
   1.105 +    public static Convertor<Double, String> merge(Convertor<Double, String> one, Convertor<Double, String> two) {
   1.106 +        return Convertor.mergeConvertorsDoubleString(one, two);
   1.107 +    }
   1.108 +
   1.109 +    /** Join the convertors from previous task, Task1Test and show that it
   1.110 +     * can be used to do reasonable conversions.
   1.111 +     */
   1.112 +    public void testConvertorComposition() throws Exception {
   1.113 +        Convertor<Double, String> c = merge(
   1.114 +                Task1Test.createCZKtoUSD(),
   1.115 +                Task1Test.createSKKtoCZK());
   1.116 +
   1.117 +        CurrencyValue<Double, String> result;
   1.118 +        // convert $5 to CZK using c:
   1.119 +        // assertEquals("Result is 85 CZK");
   1.120 +        result = c.convert("CZK", CurrencyValue.getCurrencyValue(5d, "USD"));
   1.121 +        assertEquals(CurrencyValue.getCurrencyValue(85d, "CZK"), result);
   1.122 +
   1.123 +        // convert $8 to CZK using c:
   1.124 +        // assertEquals("Result is 136 CZK");
   1.125 +        result = c.convert("CZK", CurrencyValue.getCurrencyValue(8d, "USD"));
   1.126 +        assertEquals(CurrencyValue.getCurrencyValue(136d, "CZK"), result);
   1.127 +
   1.128 +        // convert 1003CZK to USD using c:
   1.129 +        // assertEquals("Result is 59 USD");
   1.130 +        result = c.convert("USD", CurrencyValue.getCurrencyValue(1003d, "CZK"));
   1.131 +        assertEquals(CurrencyValue.getCurrencyValue(59d, "USD"), result);
   1.132 +
   1.133 +        // convert 16CZK using c:
   1.134 +        // assertEquals("Result is 20 SKK");
   1.135 +        result = c.convert("SKK", CurrencyValue.getCurrencyValue(16d, "CZK"));
   1.136 +        assertEquals(CurrencyValue.getCurrencyValue(20d, "SKK"), result);
   1.137 +
   1.138 +        // convert 500SKK to CZK using c:
   1.139 +        // assertEquals("Result is 400 CZK");
   1.140 +        result = c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"));
   1.141 +        assertEquals(CurrencyValue.getCurrencyValue(400d, "CZK"), result);
   1.142 +    }
   1.143 +}