task4/solution02/test/org/apidesign/apifest08/test/Task3Test.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 56 task3/solution02/test/org/apidesign/apifest08/test/Task3Test.java@a3144e7f9c90
permissions -rw-r--r--
Copying structure for task4
     1 package org.apidesign.apifest08.test;
     2 
     3 import static org.apidesign.apifest08.currency.MoneyImpl.money;
     4 import static org.apidesign.apifest08.test.Task1Test.CZK;
     5 import static org.apidesign.apifest08.test.Task1Test.SKK;
     6 import static org.apidesign.apifest08.test.Task1Test.USD;
     7 import junit.framework.TestCase;
     8 
     9 import org.apidesign.apifest08.currency.Convertor;
    10 
    11 /** The exchange rates are not always the same. They are changing. Day by day,
    12  * hour by hour, minute by minute. For every bank it is important to always
    13  * have the actual exchange rate available in the system. That is why let's
    14  * create a pluggable convertor that will always have up to date value of its
    15  * exchange rate.
    16  * <p>
    17  * The quest for today is to allow 3rd party developer to write a convertor
    18  * that adjusts its exchange rate everytime it is queried. This convertor is
    19  * written by independent vendor, the vendor knows only your Convertor API,
    20  * he does not know how the whole system looks and how the convertor is supposed
    21  * to be used.
    22  */
    23 public class Task3Test extends TestCase {
    24     public Task3Test(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     // Backward compatibly enhance your existing API to support following
    37     // usecases:
    38     //
    39 
    40 
    41     /** Without knowing anything about the surrounding system, write an
    42      * implementation of convertor that will return different rates everytime
    43      * it is queried. Convert USD to CZK and vice versa. Start with the rate of
    44      * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
    45      * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
    46      * until you reach 1USD = 16CZK
    47      *
    48      * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
    49      */
    50     public static Convertor createOnlineCZKUSDConvertor() {
    51         // initial rate: 1USD = 16CZK
    52         // 2nd query 1USD = 15.99CZK
    53         // 3rd query 1USD = 15.98CZK
    54         // until 1USD = 15.00CZK
    55         // then 1USD = 15.01CZK
    56         // then 1USD = 15.02CZK
    57         // and so on and on up to 1USD = 16CZK
    58         // and then another round to 15, etc.
    59         return new OnlineConvertor();
    60     }
    61 
    62     public void testFewQueriesForOnlineConvertor() {
    63         Convertor c = createOnlineCZKUSDConvertor();
    64         doFewQueriesForOnlineConvertor(c);
    65     }
    66 
    67     static void doFewQueriesForOnlineConvertor(Convertor c) {
    68         // convert $5 to CZK using c:
    69         assertEquals("Result is 80 CZK", money(80,CZK), c.convert(money(5,USD), CZK));
    70 
    71         // convert $8 to CZK using c:
    72         assertEquals("Result is 127.92 CZK", money(127.92,CZK), c.convert(money(8,USD), CZK));
    73 
    74         // convert $1 to CZK using c:
    75         assertEquals("Result is 15.98 CZK", money(15.98,CZK), c.convert(money(1,USD), CZK));
    76 
    77         // convert 15.97CZK to USD using c:
    78         assertEquals("Result is 1 USD", money(1,USD), c.convert(money(15.97,CZK), USD));
    79         //assertEquals("Result is 1$");
    80 
    81     }
    82 
    83     /** Join the convertors and show they behave sane.
    84      */
    85     public void testOnlineConvertorComposition() throws Exception {
    86         
    87         Convertor c = Task2Test.merge(
    88             createOnlineCZKUSDConvertor(),
    89             Task1Test.createSKKtoCZK()
    90         );
    91 
    92         // convert 16CZK to SKK using c:
    93         assertEquals("Result is 20 SKK", money(20,SKK), c.convert(money(16,CZK), SKK));
    94 
    95         // convert 500SKK to CZK using c:
    96         assertEquals("Result is 400 CZK", money(400,CZK), c.convert(money(500,SKK), CZK));
    97 
    98         doFewQueriesForOnlineConvertor(c);
    99     }
   100 }