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