task3/solution14/test/org/apidesign/apifest08/test/Task3Test.java
author japod@localhost
Fri, 10 Oct 2008 22:02:31 +0200
changeset 54 1b300c79f4ce
parent 50 03c5c5dc94e7
child 59 c1d43bc1e9c0
permissions -rw-r--r--
solution14 task3
     1 package org.apidesign.apifest08.test;
     2 
     3 import junit.framework.TestCase;
     4 import org.apidesign.apifest08.currency.Convertor;
     5 import org.apidesign.apifest08.currency.ConvertorFactory;
     6 import org.apidesign.apifest08.currency.CurrencyRate;
     7 import org.apidesign.apifest08.currency.Rate;
     8 
     9 /** The exchange rates are not always the same. They are changing. Day by day,
    10  * hour by hour, minute by minute. For every bank it is important to always
    11  * have the actual exchange rate available in the system. That is why let's
    12  * create a pluggable convertor that will always have up to date value of its
    13  * exchange rate.
    14  * <p>
    15  * The quest for today is to allow 3rd party developer to write a convertor
    16  * that adjusts its exchange rate everytime it is queried. This convertor is
    17  * written by independent vendor, the vendor knows only your Convertor API,
    18  * he does not know how the whole system looks and how the convertor is supposed
    19  * to be used.
    20  */
    21 public class Task3Test extends TestCase {
    22     public Task3Test(String testName) {
    23         super(testName);
    24     }
    25 
    26     @Override
    27     protected void setUp() throws Exception {
    28     }
    29 
    30     @Override
    31     protected void tearDown() throws Exception {
    32     }
    33 
    34     // Backward compatibly enhance your existing API to support following
    35     // usecases:
    36     //
    37 
    38 
    39     /** Without knowing anything about the surrounding system, write an
    40      * implementation of convertor that will return different rates everytime
    41      * it is queried. Convert USD to CZK and vice versa. Start with the rate of
    42      * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
    43      * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
    44      * until you reach 1USD = 16CZK
    45      *
    46      * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
    47      */
    48     public static Convertor createOnlineCZKUSDConvertor() {
    49         // initial rate: 1USD = 16CZK
    50         // 2nd query 1USD = 15.99CZK
    51         // 3rd query 1USD = 15.98CZK
    52         // until 1USD = 15.00CZK
    53         // then 1USD = 15.01CZK
    54         // then 1USD = 15.02CZK
    55         // and so on and on up to 1USD = 16CZK
    56         // and then another round to 15, etc.
    57         CurrencyRate onlineCurrencyRate = new CurrencyRate() {
    58             private int usdAmount = 100;
    59             private int czkAmount = 1600;
    60             private boolean up = false;
    61             public String getCurrency1() {
    62                 return "USD";
    63             }
    64 
    65             public String getCurrency2() {
    66                 return "CZK";
    67             }
    68 
    69             public Rate getRate() { //return Rate according to online status
    70                 Rate rate = new Rate(usdAmount, czkAmount);
    71                 if (up) {
    72                     if (czkAmount < 1600) {
    73                         czkAmount++;
    74                     } else {
    75                         up = false;
    76                         czkAmount--;
    77                     }
    78                 } else { //down
    79                     if (czkAmount > 1500) {
    80                         czkAmount--;
    81                     } else {
    82                         up = true;
    83                         czkAmount++;
    84                     }
    85                 }
    86                 return rate;
    87             }
    88         };
    89 
    90         return ConvertorFactory.newInstance().createConvertor(onlineCurrencyRate);
    91     }
    92 
    93     public void testFewQueriesForOnlineConvertor() {
    94 //        if (Boolean.getBoolean("ignore.failing")) {
    95 //            // implement me!
    96 //            return;
    97 //        }
    98 
    99         Convertor c = createOnlineCZKUSDConvertor();
   100         doFewQueriesForOnlineConvertor(c);
   101     }
   102 
   103     static void doFewQueriesForOnlineConvertor(Convertor c) {
   104         // convert $5 to CZK using c:
   105         //assertEquals("Result is 80 CZK");
   106         assertEquals("Result is 80 CZK", 80.0, c.convert("USD", "CZK", 5), 0.001);
   107 
   108         // convert $8 to CZK using c:
   109         //assertEquals("Result is 127.92 CZK");
   110         assertEquals("Result is 127.92 CZK", 127.92, c.convert("USD", "CZK", 8), 0.001);
   111 
   112         // convert $1 to CZK using c:
   113         //assertEquals("Result is 15.98 CZK");
   114         assertEquals("Result is 15.98 CZK", 15.98, c.convert("USD", "CZK", 1), 0.001);
   115 
   116         // convert 15.97CZK to USD using c:
   117         //assertEquals("Result is 1$");
   118         assertEquals("Result is 1$", 1.0, c.convert("CZK", "USD", 15.97), 0.001);
   119         
   120     }
   121 
   122     /** Join the convertors and show they behave sane.
   123      */
   124     public void testOnlineConvertorComposition() throws Exception {
   125         Convertor c = Task2Test.merge(
   126             createOnlineCZKUSDConvertor(),
   127             Task1Test.createSKKtoCZK()
   128         );
   129 
   130         // convert 16CZK to SKK using c:
   131         // assertEquals("Result is 20 SKK");
   132         assertEquals("Result is 20 SKK", 20.0, c.convert("CZK", "SKK", 16), 0.001);
   133 
   134         // convert 500SKK to CZK using c:
   135         // assertEquals("Result is 400 CZK");
   136         assertEquals("Result is 400 CZK", 400.0, c.convert("SKK", "CZK", 500), 0.001);
   137 
   138         doFewQueriesForOnlineConvertor(c);
   139     }
   140 }