task4/solution13/test/org/apidesign/apifest08/test/Task3Test.java
changeset 61 58ec6da75f6f
parent 59 c1d43bc1e9c0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution13/test/org/apidesign/apifest08/test/Task3Test.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,122 @@
     1.4 +package org.apidesign.apifest08.test;
     1.5 +
     1.6 +import java.math.BigDecimal;
     1.7 +import junit.framework.TestCase;
     1.8 +import org.apidesign.apifest08.currency.Convertor;
     1.9 +import org.apidesign.apifest08.currency.ConvertorCurrency;
    1.10 +import org.apidesign.apifest08.currency.ExchangeRateProvider;
    1.11 +import org.apidesign.apifest08.currency.IExchangeRateEngine;
    1.12 +
    1.13 +/** The exchange rates are not always the same. They are changing. Day by day,
    1.14 + * hour by hour, minute by minute. For every bank it is important to always
    1.15 + * have the actual exchange rate available in the system. That is why let's
    1.16 + * create a pluggable convertor that will always have up to date value of its
    1.17 + * exchange rate.
    1.18 + * <p>
    1.19 + * The quest for today is to allow 3rd party developer to write a convertor
    1.20 + * that adjusts its exchange rate everytime it is queried. This convertor is
    1.21 + * written by independent vendor, the vendor knows only your Convertor API,
    1.22 + * he does not know how the whole system looks and how the convertor is supposed
    1.23 + * to be used.
    1.24 + */
    1.25 +public class Task3Test extends TestCase {
    1.26 +    
    1.27 +    private static ConvertorCurrency currencyCZK = ConvertorCurrency.getInstance("CZK");
    1.28 +    private static ConvertorCurrency currencySKK = ConvertorCurrency.getInstance("SKK");
    1.29 +    private static ConvertorCurrency currencyUSD = ConvertorCurrency.getInstance("USD");
    1.30 +    
    1.31 +    public Task3Test(String testName) {
    1.32 +        super(testName);
    1.33 +    }
    1.34 +
    1.35 +    @Override
    1.36 +    protected void setUp() throws Exception {
    1.37 +    }
    1.38 +
    1.39 +    @Override
    1.40 +    protected void tearDown() throws Exception {
    1.41 +    }
    1.42 +
    1.43 +    // Backward compatibly enhance your existing API to support following
    1.44 +    // usecases:
    1.45 +    //
    1.46 +
    1.47 +
    1.48 +    /** Without knowing anything about the surrounding system, write an
    1.49 +     * implementation of convertor that will return different rates everytime
    1.50 +     * it is queried. Convert USD to CZK and vice versa. Start with the rate of
    1.51 +     * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
    1.52 +     * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
    1.53 +     * until you reach 1USD = 16CZK
    1.54 +     *
    1.55 +     * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
    1.56 +     */
    1.57 +    public static Convertor createOnlineCZKUSDConvertor() {
    1.58 +        // initial rate: 1USD = 16CZK
    1.59 +        // 2nd query 1USD = 15.99CZK
    1.60 +        // 3rd query 1USD = 15.98CZK
    1.61 +        // until 1USD = 15.00CZK
    1.62 +        // then 1USD = 15.01CZK
    1.63 +        // then 1USD = 15.02CZK
    1.64 +        // and so on and on up to 1USD = 16CZK
    1.65 +        // and then another round to 15, etc.
    1.66 +        IExchangeRateEngine engine = BouncingExchangeRateEngine.create(
    1.67 +                currencyUSD,  new BigDecimal(1),
    1.68 +                currencyCZK, 
    1.69 +                new BigDecimal(16),new BigDecimal(15),
    1.70 +                new BigDecimal("-0.01"));
    1.71 +        ExchangeRateProvider exRateProvider = ExchangeRateProvider.createExchangeRateProvider(engine);
    1.72 +        Convertor convertor = Convertor.createConvertor(exRateProvider);
    1.73 +        return convertor;
    1.74 +    }
    1.75 +
    1.76 +    public void testFewQueriesForOnlineConvertor() {
    1.77 +        Convertor c = createOnlineCZKUSDConvertor();
    1.78 +        doFewQueriesForOnlineConvertor(c);
    1.79 +    }
    1.80 +
    1.81 +    static void doFewQueriesForOnlineConvertor(Convertor c) {
    1.82 +        // convert $5 to CZK using c:
    1.83 +        assertEquals("Result is 80 CZK", new BigDecimal("80.00"), c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(5)).getConverted());
    1.84 +
    1.85 +        // convert $8 to CZK using c:
    1.86 +        assertEquals("Result is 127.92 CZK", new BigDecimal("127.92"), c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(8)).getConverted());
    1.87 +
    1.88 +        // convert $1 to CZK using c:
    1.89 +        assertEquals("Result is 15.98 CZK", new BigDecimal("15.98"), c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(1)).getConverted());
    1.90 +
    1.91 +        // convert 15.97CZK to USD using c:
    1.92 +        assertEquals("Result is 15.98 CZK", new BigDecimal("1.00"), c.convertWithReversibleRates(currencyCZK, currencyUSD , new BigDecimal("15.97")).getConverted());
    1.93 +    }
    1.94 +
    1.95 +    /** Join the convertors and show they behave sane.
    1.96 +     */
    1.97 +    public void testOnlineConvertorComposition() throws Exception {
    1.98 +        Convertor c = Task2Test.merge(
    1.99 +            createOnlineCZKUSDConvertor(),
   1.100 +            Task1Test.createSKKtoCZK()
   1.101 +        );
   1.102 +
   1.103 +        // convert 16CZK to SKK using c:
   1.104 +        assertEquals("Result is 20 CZK", new BigDecimal("20.00"), c.convertWithReversibleRates(currencyCZK, currencySKK , new BigDecimal("16")).getConverted());
   1.105 +
   1.106 +        // convert 500SKK to CZK using c:
   1.107 +        assertEquals("Result is 400 CZK", new BigDecimal("400.00"), c.convertWithReversibleRates(currencySKK, currencyCZK , new BigDecimal("500")).getConverted());
   1.108 +
   1.109 +        doFewQueriesForOnlineConvertor(c);
   1.110 +    }
   1.111 +    
   1.112 +//    public void testBouncing() {
   1.113 +//        Convertor c=createOnlineCZKUSDConvertor();
   1.114 +//        
   1.115 +//        IExchangeRateEngine engine = BouncingExchangeRateEngine.create(
   1.116 +//                currencyUSD,  new BigDecimal(1),
   1.117 +//                currencyCZK, 
   1.118 +//                new BigDecimal("16.00"),new BigDecimal("15.00"),
   1.119 +//                new BigDecimal("-0.01"));        
   1.120 +//        
   1.121 +//        for (int i=0;i<300;i++) {
   1.122 +//            System.out.println(engine.getExchangeRate(currencyUSD, currencyCZK).getToValue());
   1.123 +//        }
   1.124 +//    }
   1.125 +}