japod@49
|
1 |
package org.apidesign.apifest08.test;
|
japod@49
|
2 |
|
japod@49
|
3 |
import junit.framework.TestCase;
|
japod@49
|
4 |
import org.apidesign.apifest08.currency.Convertor;
|
japod@49
|
5 |
import org.apidesign.apifest08.currency.ConvertorFactory;
|
japod@49
|
6 |
import org.apidesign.apifest08.currency.CurrencyRate;
|
japod@49
|
7 |
import org.apidesign.apifest08.currency.CurrencyRateFactory;
|
japod@49
|
8 |
|
japod@49
|
9 |
/** There are many currencies around the world and many banks manipulate
|
japod@49
|
10 |
* with more than one or two at the same time. As banks are usually the
|
japod@49
|
11 |
* best paying clients, which is true even in case of your Convertor API,
|
japod@49
|
12 |
* it is reasonable to listen to their requests.
|
japod@49
|
13 |
* <p>
|
japod@49
|
14 |
* The quest for today is to enhance your existing convertor API to hold
|
japod@49
|
15 |
* information about many currencies and allow conversions between any of them.
|
japod@49
|
16 |
* Also, as conversion rates for diferent currencies usually arise from various
|
japod@49
|
17 |
* bank departments, there is another important need. There is a need to
|
japod@49
|
18 |
* compose two convertors into one by merging all the information about
|
japod@49
|
19 |
* currencies they know about.
|
japod@49
|
20 |
*/
|
japod@49
|
21 |
public class Task2Test extends TestCase {
|
japod@49
|
22 |
public Task2Test(String testName) {
|
japod@49
|
23 |
super(testName);
|
japod@49
|
24 |
}
|
japod@49
|
25 |
|
japod@49
|
26 |
@Override
|
japod@49
|
27 |
protected void setUp() throws Exception {
|
japod@49
|
28 |
}
|
japod@49
|
29 |
|
japod@49
|
30 |
@Override
|
japod@49
|
31 |
protected void tearDown() throws Exception {
|
japod@49
|
32 |
}
|
japod@49
|
33 |
|
japod@49
|
34 |
// As in Task1Test, keep in mind, that there are three parts
|
japod@49
|
35 |
// of the whole system:
|
japod@49
|
36 |
// 1. there is someone who knows the current exchange rate
|
japod@49
|
37 |
// 2. there is someone who wants to do the conversion
|
japod@49
|
38 |
// 3. there is the API between 1. and 2. which allows them to communicate
|
japod@49
|
39 |
//
|
japod@49
|
40 |
// Please backward compatibly enhance your existing API to support following
|
japod@49
|
41 |
// usecases:
|
japod@49
|
42 |
//
|
japod@49
|
43 |
|
japod@49
|
44 |
/** Create convertor that understands two currencies, CZK and
|
japod@49
|
45 |
* SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
|
japod@49
|
46 |
* knows the exchange rate, and needs to use the API to create objects
|
japod@49
|
47 |
* with the exchange rate. Anyone shall be ready to call this method without
|
japod@49
|
48 |
* any other method being called previously. The API itself shall know
|
japod@49
|
49 |
* nothing about any rates, before this method is called.
|
japod@49
|
50 |
*/
|
japod@49
|
51 |
public static Convertor createTripleConvertor() {
|
japod@49
|
52 |
// Rates: 1USD = 15CZK
|
japod@49
|
53 |
// Rates: 1USD = 20SKK
|
japod@49
|
54 |
// Rates: 75CZK = 100SKK
|
japod@49
|
55 |
CurrencyRate usdCzk = CurrencyRateFactory.getInstance().createCurrencyRate("USD", "CZK", 1, 15);
|
japod@49
|
56 |
CurrencyRate usdSkk = CurrencyRateFactory.getInstance().createCurrencyRate("USD", "SKK", 1, 20);
|
japod@49
|
57 |
CurrencyRate czkSkk = CurrencyRateFactory.getInstance().createCurrencyRate("CZK", "SKK", 75, 100);
|
japod@49
|
58 |
return ConvertorFactory.newInstance().createConvertor(usdCzk, usdSkk, czkSkk);
|
japod@49
|
59 |
}
|
japod@49
|
60 |
|
japod@49
|
61 |
/** Define convertor that understands three currencies. Use it.
|
japod@49
|
62 |
*/
|
japod@49
|
63 |
public void testConvertorForUSDandCZKandSKK() throws Exception {
|
japod@49
|
64 |
Convertor c = createTripleConvertor();
|
japod@49
|
65 |
|
japod@49
|
66 |
// convert $5 to CZK using c:
|
japod@49
|
67 |
// assertEquals("Result is 75 CZK");
|
japod@49
|
68 |
assertEquals("Result is 75 CZK", 75.0, c.convert("USD", "CZK", 5));
|
japod@49
|
69 |
|
japod@49
|
70 |
// convert $5 to SKK using c:
|
japod@49
|
71 |
// assertEquals("Result is 100 SKK");
|
japod@49
|
72 |
assertEquals("Result is 100 SKK", 100.0, c.convert("USD", "SKK", 5));
|
japod@49
|
73 |
|
japod@49
|
74 |
// convert 200SKK to CZK using c:
|
japod@49
|
75 |
// assertEquals("Result is 150 CZK");
|
japod@49
|
76 |
assertEquals("Result is 150 CZK", 150.0, c.convert("SKK", "CZK", 200));
|
japod@49
|
77 |
|
japod@49
|
78 |
// convert 200SKK to USD using c:
|
japod@49
|
79 |
// assertEquals("Result is 10 USD");
|
japod@49
|
80 |
assertEquals("Result is 10 USD", 10.0, c.convert("SKK", "USD", 200));
|
japod@49
|
81 |
}
|
japod@49
|
82 |
|
japod@49
|
83 |
/** Merge all currency rates of convertor 1 with convertor 2.
|
japod@49
|
84 |
* Implement this using your API, preferably this method just delegates
|
japod@49
|
85 |
* into some API method which does the actual work, without requiring
|
japod@49
|
86 |
* API clients to code anything complex.
|
japod@49
|
87 |
*/
|
japod@49
|
88 |
public static Convertor merge(Convertor one, Convertor two) {
|
jaroslav@67
|
89 |
return ConvertorFactory.newInstance().mergeConvertorsIgnoreEqualCurrencies(one,two);
|
japod@49
|
90 |
}
|
japod@49
|
91 |
|
japod@49
|
92 |
/** Join the convertors from previous task, Task1Test and show that it
|
japod@49
|
93 |
* can be used to do reasonable conversions.
|
japod@49
|
94 |
*/
|
japod@49
|
95 |
public void testConvertorComposition() throws Exception {
|
japod@49
|
96 |
Convertor c = merge(
|
japod@49
|
97 |
Task1Test.createCZKtoUSD(),
|
japod@49
|
98 |
Task1Test.createSKKtoCZK()
|
japod@49
|
99 |
);
|
japod@49
|
100 |
|
japod@49
|
101 |
// convert $5 to CZK using c:
|
japod@49
|
102 |
// assertEquals("Result is 85 CZK");
|
japod@49
|
103 |
assertEquals("Result is 85 CZK", 85.0, c.convert("USD", "CZK", 5));
|
japod@49
|
104 |
|
japod@49
|
105 |
// convert $8 to CZK using c:
|
japod@49
|
106 |
// assertEquals("Result is 136 CZK");
|
japod@49
|
107 |
assertEquals("Result is 136 CZK", 136.0, c.convert("USD", "CZK", 8));
|
japod@49
|
108 |
|
japod@49
|
109 |
// convert 1003CZK to USD using c:
|
japod@49
|
110 |
// assertEquals("Result is 59 USD");
|
japod@49
|
111 |
assertEquals("Result is 59 USD", 59.0, c.convert("CZK", "USD", 1003));
|
japod@49
|
112 |
|
japod@49
|
113 |
// convert 16CZK using c:
|
japod@49
|
114 |
// assertEquals("Result is 20 SKK");
|
japod@49
|
115 |
assertEquals("Result is 20 SKK", 20.0, c.convert("CZK", "SKK", 16));
|
japod@49
|
116 |
|
japod@49
|
117 |
// convert 500SKK to CZK using c:
|
japod@49
|
118 |
// assertEquals("Result is 400 CZK");
|
japod@49
|
119 |
assertEquals("Result is 400 CZK", 400.0, c.convert("SKK", "CZK", 500));
|
japod@49
|
120 |
|
japod@49
|
121 |
//test exceptions
|
japod@49
|
122 |
Convertor one = Task1Test.createCZKtoUSD();
|
japod@49
|
123 |
Convertor two = Task1Test.createSKKtoCZK();
|
japod@49
|
124 |
Convertor three = Task1Test.createSKKtoCZK();
|
japod@49
|
125 |
try {
|
japod@49
|
126 |
ConvertorFactory.newInstance().mergeConvertors(one,two,three);
|
japod@49
|
127 |
fail();
|
japod@49
|
128 |
} catch (IllegalArgumentException e) {
|
japod@49
|
129 |
//ok
|
japod@49
|
130 |
}
|
japod@49
|
131 |
|
japod@49
|
132 |
//test exceptions
|
japod@49
|
133 |
try {
|
japod@49
|
134 |
ConvertorFactory.newInstance().mergeConvertors(c, two);
|
japod@49
|
135 |
fail();
|
japod@49
|
136 |
} catch (IllegalArgumentException e) {
|
japod@49
|
137 |
//ok
|
japod@49
|
138 |
}
|
japod@49
|
139 |
|
japod@49
|
140 |
//try convertors from version 1
|
japod@49
|
141 |
Convertor v1one = ConvertorFactory.newInstance().createConvertor("CZE", "CZE", 1, 2);
|
japod@49
|
142 |
assertEquals("CZE->CZE 1:2 10 expects 20", 20.0, v1one.convert("CZE", "CZE", 10));
|
japod@49
|
143 |
try {
|
japod@49
|
144 |
ConvertorFactory.newInstance().mergeConvertors(v1one, two);
|
japod@49
|
145 |
fail();
|
japod@49
|
146 |
} catch (IllegalArgumentException e) {
|
japod@49
|
147 |
//ok
|
japod@49
|
148 |
}
|
japod@49
|
149 |
|
japod@49
|
150 |
Convertor v1two = ConvertorFactory.newInstance().createConvertor("EUR", "", 1, 2);
|
japod@49
|
151 |
assertEquals("EUR->'' 1:2 10 expects 20", 20.0, v1two.convert("EUR", "", 10));
|
japod@49
|
152 |
try {
|
japod@49
|
153 |
ConvertorFactory.newInstance().mergeConvertors(v1two, two);
|
japod@49
|
154 |
fail();
|
japod@49
|
155 |
} catch (IllegalArgumentException e) {
|
japod@49
|
156 |
//ok
|
japod@49
|
157 |
}
|
japod@49
|
158 |
|
japod@49
|
159 |
Convertor v1three = ConvertorFactory.newInstance().createConvertor("EUR", "", 1, 2);
|
japod@49
|
160 |
assertEquals("''->EUR 1:2 10 expects 5", 5.0, v1three.convert("", "EUR", 10));
|
japod@49
|
161 |
try {
|
japod@49
|
162 |
ConvertorFactory.newInstance().mergeConvertors(v1three, two);
|
japod@49
|
163 |
fail();
|
japod@49
|
164 |
} catch (IllegalArgumentException e) {
|
japod@49
|
165 |
//ok
|
japod@49
|
166 |
}
|
japod@49
|
167 |
|
japod@49
|
168 |
}
|
japod@49
|
169 |
}
|