1 package org.apidesign.apifest08.test;
3 import static org.apidesign.apifest08.currency.MoneyImpl.money;
4 import static org.apidesign.apifest08.test.Task1Test.CZK;
5 import static org.apidesign.apifest08.test.Task1Test.SKK;
6 import static org.apidesign.apifest08.test.Task1Test.USD;
7 import junit.framework.TestCase;
9 import org.apidesign.apifest08.currency.Convertor;
11 /** The exchange rates are not always the same. They are changing. Day by day,
12 * hour by hour, minute by minute. For every bank it is important to always
13 * have the actual exchange rate available in the system. That is why let's
14 * create a pluggable convertor that will always have up to date value of its
17 * The quest for today is to allow 3rd party developer to write a convertor
18 * that adjusts its exchange rate everytime it is queried. This convertor is
19 * written by independent vendor, the vendor knows only your Convertor API,
20 * he does not know how the whole system looks and how the convertor is supposed
23 public class Task3Test extends TestCase {
24 public Task3Test(String testName) {
29 protected void setUp() throws Exception {
33 protected void tearDown() throws Exception {
36 // Backward compatibly enhance your existing API to support following
41 /** Without knowing anything about the surrounding system, write an
42 * implementation of convertor that will return different rates everytime
43 * it is queried. Convert USD to CZK and vice versa. Start with the rate of
44 * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
45 * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
46 * until you reach 1USD = 16CZK
48 * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
50 public static Convertor createOnlineCZKUSDConvertor() {
51 // initial rate: 1USD = 16CZK
52 // 2nd query 1USD = 15.99CZK
53 // 3rd query 1USD = 15.98CZK
54 // until 1USD = 15.00CZK
55 // then 1USD = 15.01CZK
56 // then 1USD = 15.02CZK
57 // and so on and on up to 1USD = 16CZK
58 // and then another round to 15, etc.
59 return new OnlineConvertor();
62 public void testFewQueriesForOnlineConvertor() {
63 Convertor c = createOnlineCZKUSDConvertor();
64 doFewQueriesForOnlineConvertor(c);
67 static void doFewQueriesForOnlineConvertor(Convertor c) {
68 // convert $5 to CZK using c:
69 assertEquals("Result is 80 CZK", money(80,CZK), c.convert(money(5,USD), CZK));
71 // convert $8 to CZK using c:
72 assertEquals("Result is 127.92 CZK", money(127.92,CZK), c.convert(money(8,USD), CZK));
74 // convert $1 to CZK using c:
75 assertEquals("Result is 15.98 CZK", money(15.98,CZK), c.convert(money(1,USD), CZK));
77 // convert 15.97CZK to USD using c:
78 assertEquals("Result is 1 USD", money(1,USD), c.convert(money(15.97,CZK), USD));
79 //assertEquals("Result is 1$");
83 /** Join the convertors and show they behave sane.
85 public void testOnlineConvertorComposition() throws Exception {
87 Convertor c = Task2Test.merge(
88 createOnlineCZKUSDConvertor(),
89 Task1Test.createSKKtoCZK()
92 // convert 16CZK to SKK using c:
93 assertEquals("Result is 20 SKK", money(20,SKK), c.convert(money(16,CZK), SKK));
95 // convert 500SKK to CZK using c:
96 assertEquals("Result is 400 CZK", money(400,CZK), c.convert(money(500,SKK), CZK));
98 doFewQueriesForOnlineConvertor(c);