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