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