currency/test/org/apidesign/apifest08/test/Task3Test.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 25 Oct 2008 20:53:00 +0200
changeset 84 2ae6e4aa7aef
parent 46 b61dcd332269
permissions -rw-r--r--
Solutions by Petr Smid
     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         if (Boolean.getBoolean("ignore.failing")) {
    59             // implement me!
    60             return;
    61         }
    62 
    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");
    70 
    71         // convert $8 to CZK using c:
    72         //assertEquals("Result is 127.92 CZK");
    73 
    74         // convert $1 to CZK using c:
    75         //assertEquals("Result is 15.98 CZK");
    76 
    77         // convert 15.97CZK to USD using c:
    78         //assertEquals("Result is 1$");
    79 
    80         fail("Implement me!");
    81     }
    82 
    83     /** Join the convertors and show they behave sane.
    84      */
    85     public void testOnlineConvertorComposition() throws Exception {
    86         if (Boolean.getBoolean("ignore.failing")) {
    87             // implement me!
    88             return;
    89         }
    90         
    91         Convertor c = Task2Test.merge(
    92             createOnlineCZKUSDConvertor(),
    93             Task1Test.createSKKtoCZK()
    94         );
    95 
    96         // convert 16CZK to SKK using c:
    97         // assertEquals("Result is 20 SKK");
    98 
    99         // convert 500SKK to CZK using c:
   100         // assertEquals("Result is 400 CZK");
   101 
   102         doFewQueriesForOnlineConvertor(c);
   103     }
   104 }