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