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