task3/solution13/test/org/apidesign/apifest08/test/Task3Test.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 11 Oct 2008 10:49:25 +0200
changeset 59 c1d43bc1e9c0
parent 58 07c16ec15a25
permissions -rw-r--r--
Removing all 'implement me' messages
     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         Convertor c = createOnlineCZKUSDConvertor();
    75         doFewQueriesForOnlineConvertor(c);
    76     }
    77 
    78     static void doFewQueriesForOnlineConvertor(Convertor c) {
    79         // convert $5 to CZK using c:
    80         assertEquals("Result is 80 CZK", new BigDecimal("80.00"), c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(5)).getConverted());
    81 
    82         // convert $8 to CZK using c:
    83         assertEquals("Result is 127.92 CZK", new BigDecimal("127.92"), c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(8)).getConverted());
    84 
    85         // convert $1 to CZK using c:
    86         assertEquals("Result is 15.98 CZK", new BigDecimal("15.98"), c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(1)).getConverted());
    87 
    88         // convert 15.97CZK to USD using c:
    89         assertEquals("Result is 15.98 CZK", new BigDecimal("1.00"), c.convertWithReversibleRates(currencyCZK, currencyUSD , new BigDecimal("15.97")).getConverted());
    90     }
    91 
    92     /** Join the convertors and show they behave sane.
    93      */
    94     public void testOnlineConvertorComposition() throws Exception {
    95         Convertor c = Task2Test.merge(
    96             createOnlineCZKUSDConvertor(),
    97             Task1Test.createSKKtoCZK()
    98         );
    99 
   100         // convert 16CZK to SKK using c:
   101         assertEquals("Result is 20 CZK", new BigDecimal("20.00"), c.convertWithReversibleRates(currencyCZK, currencySKK , new BigDecimal("16")).getConverted());
   102 
   103         // convert 500SKK to CZK using c:
   104         assertEquals("Result is 400 CZK", new BigDecimal("400.00"), c.convertWithReversibleRates(currencySKK, currencyCZK , new BigDecimal("500")).getConverted());
   105 
   106         doFewQueriesForOnlineConvertor(c);
   107     }
   108     
   109 //    public void testBouncing() {
   110 //        Convertor c=createOnlineCZKUSDConvertor();
   111 //        
   112 //        IExchangeRateEngine engine = BouncingExchangeRateEngine.create(
   113 //                currencyUSD,  new BigDecimal(1),
   114 //                currencyCZK, 
   115 //                new BigDecimal("16.00"),new BigDecimal("15.00"),
   116 //                new BigDecimal("-0.01"));        
   117 //        
   118 //        for (int i=0;i<300;i++) {
   119 //            System.out.println(engine.getExchangeRate(currencyUSD, currencyCZK).getToValue());
   120 //        }
   121 //    }
   122 }