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
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@43
    74
        Convertor c = createOnlineCZKUSDConvertor();
jaroslav@43
    75
        doFewQueriesForOnlineConvertor(c);
jaroslav@43
    76
    }
jaroslav@43
    77
jaroslav@43
    78
    static void doFewQueriesForOnlineConvertor(Convertor c) {
jaroslav@43
    79
        // convert $5 to CZK using c:
japod@58
    80
        assertEquals("Result is 80 CZK", new BigDecimal("80.00"), c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(5)).getConverted());
jaroslav@43
    81
jaroslav@43
    82
        // convert $8 to CZK using c:
japod@58
    83
        assertEquals("Result is 127.92 CZK", new BigDecimal("127.92"), c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(8)).getConverted());
jaroslav@43
    84
jaroslav@43
    85
        // convert $1 to CZK using c:
japod@58
    86
        assertEquals("Result is 15.98 CZK", new BigDecimal("15.98"), c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(1)).getConverted());
jaroslav@43
    87
jaroslav@43
    88
        // convert 15.97CZK to USD using c:
japod@58
    89
        assertEquals("Result is 15.98 CZK", new BigDecimal("1.00"), c.convertWithReversibleRates(currencyCZK, currencyUSD , new BigDecimal("15.97")).getConverted());
jaroslav@43
    90
    }
jaroslav@43
    91
jaroslav@43
    92
    /** Join the convertors and show they behave sane.
jaroslav@43
    93
     */
jaroslav@43
    94
    public void testOnlineConvertorComposition() throws Exception {
jaroslav@43
    95
        Convertor c = Task2Test.merge(
jaroslav@43
    96
            createOnlineCZKUSDConvertor(),
jaroslav@43
    97
            Task1Test.createSKKtoCZK()
jaroslav@43
    98
        );
jaroslav@43
    99
jaroslav@43
   100
        // convert 16CZK to SKK using c:
japod@58
   101
        assertEquals("Result is 20 CZK", new BigDecimal("20.00"), c.convertWithReversibleRates(currencyCZK, currencySKK , new BigDecimal("16")).getConverted());
jaroslav@43
   102
jaroslav@43
   103
        // convert 500SKK to CZK using c:
japod@58
   104
        assertEquals("Result is 400 CZK", new BigDecimal("400.00"), c.convertWithReversibleRates(currencySKK, currencyCZK , new BigDecimal("500")).getConverted());
jaroslav@43
   105
jaroslav@43
   106
        doFewQueriesForOnlineConvertor(c);
jaroslav@43
   107
    }
japod@58
   108
    
japod@58
   109
//    public void testBouncing() {
japod@58
   110
//        Convertor c=createOnlineCZKUSDConvertor();
japod@58
   111
//        
japod@58
   112
//        IExchangeRateEngine engine = BouncingExchangeRateEngine.create(
japod@58
   113
//                currencyUSD,  new BigDecimal(1),
japod@58
   114
//                currencyCZK, 
japod@58
   115
//                new BigDecimal("16.00"),new BigDecimal("15.00"),
japod@58
   116
//                new BigDecimal("-0.01"));        
japod@58
   117
//        
japod@58
   118
//        for (int i=0;i<300;i++) {
japod@58
   119
//            System.out.println(engine.getExchangeRate(currencyUSD, currencyCZK).getToValue());
japod@58
   120
//        }
japod@58
   121
//    }
jaroslav@43
   122
}