1 package org.apidesign.apifest08.test;
3 import java.math.BigDecimal;
4 import java.util.Currency;
5 import junit.framework.TestCase;
6 import org.apidesign.apifest08.currency.Convertor;
7 import org.apidesign.apifest08.currency.InvalidConversionException;
8 import org.apidesign.apifest08.currency.OnlineConvertor;
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
25 private final static Currency CZK;
26 private final static Currency SKK;
27 private final static Currency USD;
31 CZK = Currency.getInstance("CZK");
32 SKK = Currency.getInstance("SKK");
33 USD = Currency.getInstance("USD");
36 public Task3Test(String testName)
42 protected void setUp() throws Exception
47 protected void tearDown() throws Exception
51 // Backward compatibly enhance your existing API to support following
56 /** Without knowing anything about the surrounding system, write an
57 * implementation of convertor that will return different rates everytime
58 * it is queried. Convert USD to CZK and vice versa. Start with the rate of
59 * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
60 * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
61 * until you reach 1USD = 16CZK
63 * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
64 * @throws InvalidConversionException
66 public static Convertor createOnlineCZKUSDConvertor()
67 throws InvalidConversionException
69 final Convertor convertor;
71 convertor = new OnlineConvertor(USD,
73 new TestExchangeRateFinder(new BigDecimal("15.00"),
74 new BigDecimal("16.00"),
75 new BigDecimal("16.00"),
76 new BigDecimal("0.01"),
77 new BigDecimal("-0.01")));
79 // initial rate: 1USD = 16CZK
80 // 2nd query 1USD = 15.99CZK
81 // 3rd query 1USD = 15.98CZK
82 // until 1USD = 15.00CZK
83 // then 1USD = 15.01CZK
84 // then 1USD = 15.02CZK
85 // and so on and on up to 1USD = 16CZK
86 // and then another round to 15, etc.
90 public void testFewQueriesForOnlineConvertor()
91 throws InvalidConversionException
93 Convertor c = createOnlineCZKUSDConvertor();
94 doFewQueriesForOnlineConvertor(c);
97 static void doFewQueriesForOnlineConvertor(Convertor c)
98 throws InvalidConversionException
102 // convert $5 to CZK using c:
103 //assertEquals("Result is 80 CZK");
104 amount = c.convert(USD, CZK, new BigDecimal("5.00"));
105 assertEquals(new BigDecimal("80.00"), amount);
107 // convert $8 to CZK using c:
108 //assertEquals("Result is 127.92 CZK");
109 amount = c.convert(USD, CZK, new BigDecimal("8.00"));
110 assertEquals(new BigDecimal("127.92"), amount);
112 // convert $1 to CZK using c:
113 //assertEquals("Result is 15.98 CZK");
114 amount = c.convert(USD, CZK, new BigDecimal("1.00"));
115 assertEquals(new BigDecimal("15.98"), amount);
117 // convert 15.97CZK to USD using c:
118 //assertEquals("Result is 1$");
119 amount = c.convert(CZK, USD, new BigDecimal("15.97"));
120 assertEquals(new BigDecimal("1.00"), amount);
123 /** Join the convertors and show they behave sane.
125 public void testOnlineConvertorComposition() throws Exception {
128 Convertor c = Task2Test.merge(
129 createOnlineCZKUSDConvertor(),
130 Task1Test.createSKKtoCZK()
133 // convert 16CZK to SKK using c:
134 // assertEquals("Result is 20 SKK");
135 amount = c.convert(CZK, SKK, new BigDecimal("16.00"));
136 assertEquals(new BigDecimal("20.00"), amount);
138 // convert 500SKK to CZK using c:
139 // assertEquals("Result is 400 CZK");
140 amount = c.convert(SKK, CZK, new BigDecimal("500.00"));
141 assertEquals(new BigDecimal("400.00"), amount);
143 doFewQueriesForOnlineConvertor(c);