task4/solution02/test/org/apidesign/apifest08/test/Task3Test.java
changeset 61 58ec6da75f6f
parent 56 a3144e7f9c90
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution02/test/org/apidesign/apifest08/test/Task3Test.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,100 @@
     1.4 +package org.apidesign.apifest08.test;
     1.5 +
     1.6 +import static org.apidesign.apifest08.currency.MoneyImpl.money;
     1.7 +import static org.apidesign.apifest08.test.Task1Test.CZK;
     1.8 +import static org.apidesign.apifest08.test.Task1Test.SKK;
     1.9 +import static org.apidesign.apifest08.test.Task1Test.USD;
    1.10 +import junit.framework.TestCase;
    1.11 +
    1.12 +import org.apidesign.apifest08.currency.Convertor;
    1.13 +
    1.14 +/** The exchange rates are not always the same. They are changing. Day by day,
    1.15 + * hour by hour, minute by minute. For every bank it is important to always
    1.16 + * have the actual exchange rate available in the system. That is why let's
    1.17 + * create a pluggable convertor that will always have up to date value of its
    1.18 + * exchange rate.
    1.19 + * <p>
    1.20 + * The quest for today is to allow 3rd party developer to write a convertor
    1.21 + * that adjusts its exchange rate everytime it is queried. This convertor is
    1.22 + * written by independent vendor, the vendor knows only your Convertor API,
    1.23 + * he does not know how the whole system looks and how the convertor is supposed
    1.24 + * to be used.
    1.25 + */
    1.26 +public class Task3Test extends TestCase {
    1.27 +    public Task3Test(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 +    // Backward compatibly enhance your existing API to support following
    1.40 +    // usecases:
    1.41 +    //
    1.42 +
    1.43 +
    1.44 +    /** Without knowing anything about the surrounding system, write an
    1.45 +     * implementation of convertor that will return different rates everytime
    1.46 +     * it is queried. Convert USD to CZK and vice versa. Start with the rate of
    1.47 +     * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
    1.48 +     * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
    1.49 +     * until you reach 1USD = 16CZK
    1.50 +     *
    1.51 +     * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
    1.52 +     */
    1.53 +    public static Convertor createOnlineCZKUSDConvertor() {
    1.54 +        // initial rate: 1USD = 16CZK
    1.55 +        // 2nd query 1USD = 15.99CZK
    1.56 +        // 3rd query 1USD = 15.98CZK
    1.57 +        // until 1USD = 15.00CZK
    1.58 +        // then 1USD = 15.01CZK
    1.59 +        // then 1USD = 15.02CZK
    1.60 +        // and so on and on up to 1USD = 16CZK
    1.61 +        // and then another round to 15, etc.
    1.62 +        return new OnlineConvertor();
    1.63 +    }
    1.64 +
    1.65 +    public void testFewQueriesForOnlineConvertor() {
    1.66 +        Convertor c = createOnlineCZKUSDConvertor();
    1.67 +        doFewQueriesForOnlineConvertor(c);
    1.68 +    }
    1.69 +
    1.70 +    static void doFewQueriesForOnlineConvertor(Convertor c) {
    1.71 +        // convert $5 to CZK using c:
    1.72 +        assertEquals("Result is 80 CZK", money(80,CZK), c.convert(money(5,USD), CZK));
    1.73 +
    1.74 +        // convert $8 to CZK using c:
    1.75 +        assertEquals("Result is 127.92 CZK", money(127.92,CZK), c.convert(money(8,USD), CZK));
    1.76 +
    1.77 +        // convert $1 to CZK using c:
    1.78 +        assertEquals("Result is 15.98 CZK", money(15.98,CZK), c.convert(money(1,USD), CZK));
    1.79 +
    1.80 +        // convert 15.97CZK to USD using c:
    1.81 +        assertEquals("Result is 1 USD", money(1,USD), c.convert(money(15.97,CZK), USD));
    1.82 +        //assertEquals("Result is 1$");
    1.83 +
    1.84 +    }
    1.85 +
    1.86 +    /** Join the convertors and show they behave sane.
    1.87 +     */
    1.88 +    public void testOnlineConvertorComposition() throws Exception {
    1.89 +        
    1.90 +        Convertor c = Task2Test.merge(
    1.91 +            createOnlineCZKUSDConvertor(),
    1.92 +            Task1Test.createSKKtoCZK()
    1.93 +        );
    1.94 +
    1.95 +        // convert 16CZK to SKK using c:
    1.96 +        assertEquals("Result is 20 SKK", money(20,SKK), c.convert(money(16,CZK), SKK));
    1.97 +
    1.98 +        // convert 500SKK to CZK using c:
    1.99 +        assertEquals("Result is 400 CZK", money(400,CZK), c.convert(money(500,SKK), CZK));
   1.100 +
   1.101 +        doFewQueriesForOnlineConvertor(c);
   1.102 +    }
   1.103 +}