1 package org.apidesign.apifest08.test;
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;
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
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
22 public class Task3Test extends TestCase {
24 private static ConvertorCurrency currencyCZK = ConvertorCurrency.getInstance("CZK");
25 private static ConvertorCurrency currencySKK = ConvertorCurrency.getInstance("SKK");
26 private static ConvertorCurrency currencyUSD = ConvertorCurrency.getInstance("USD");
28 public Task3Test(String testName) {
33 protected void setUp() throws Exception {
37 protected void tearDown() throws Exception {
40 // Backward compatibly enhance your existing API to support following
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
52 * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
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),
66 new BigDecimal(16),new BigDecimal(15),
67 new BigDecimal("-0.01"));
68 ExchangeRateProvider exRateProvider = ExchangeRateProvider.createExchangeRateProvider(engine);
69 Convertor convertor = Convertor.createConvertor(exRateProvider);
73 public void testFewQueriesForOnlineConvertor() {
74 Convertor c = createOnlineCZKUSDConvertor();
75 doFewQueriesForOnlineConvertor(c);
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());
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());
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());
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());
92 /** Join the convertors and show they behave sane.
94 public void testOnlineConvertorComposition() throws Exception {
95 Convertor c = Task2Test.merge(
96 createOnlineCZKUSDConvertor(),
97 Task1Test.createSKKtoCZK()
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());
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());
106 doFewQueriesForOnlineConvertor(c);
109 // public void testBouncing() {
110 // Convertor c=createOnlineCZKUSDConvertor();
112 // IExchangeRateEngine engine = BouncingExchangeRateEngine.create(
113 // currencyUSD, new BigDecimal(1),
115 // new BigDecimal("16.00"),new BigDecimal("15.00"),
116 // new BigDecimal("-0.01"));
118 // for (int i=0;i<300;i++) {
119 // System.out.println(engine.getExchangeRate(currencyUSD, currencyCZK).getToValue());