japod@6
|
1 |
package org.apidesign.apifest08.test;
|
japod@6
|
2 |
|
japod@6
|
3 |
import static org.apidesign.apifest08.currency.Currencies.CZK;
|
japod@6
|
4 |
import static org.apidesign.apifest08.currency.Currencies.SKK;
|
japod@6
|
5 |
import static org.apidesign.apifest08.currency.Currencies.USD;
|
japod@6
|
6 |
|
japod@6
|
7 |
import java.math.BigDecimal;
|
japod@6
|
8 |
|
japod@6
|
9 |
import junit.framework.TestCase;
|
japod@6
|
10 |
|
japod@6
|
11 |
import org.apidesign.apifest08.currency.Amount;
|
japod@6
|
12 |
import org.apidesign.apifest08.currency.Convertor;
|
japod@6
|
13 |
import org.apidesign.apifest08.currency.ConvertorFactory;
|
japod@6
|
14 |
import org.apidesign.apifest08.currency.UnsupportedConversionException;
|
japod@6
|
15 |
|
japod@6
|
16 |
/** Finish the Convertor API, and then write bodies of methods inside
|
japod@6
|
17 |
* of this class to match the given tasks. To fullfil your task, use the
|
japod@6
|
18 |
* API define in the <code>org.apidesign.apifest08.currency</code> package.
|
japod@6
|
19 |
* Do not you reflection, or other hacks as your code
|
japod@6
|
20 |
* shall run without any runtime permissions.
|
japod@6
|
21 |
*/
|
japod@6
|
22 |
public class Task1Test extends TestCase {
|
japod@6
|
23 |
public Task1Test(String testName) {
|
japod@6
|
24 |
super(testName);
|
japod@6
|
25 |
}
|
japod@6
|
26 |
|
japod@6
|
27 |
@Override
|
japod@6
|
28 |
protected void setUp() throws Exception {
|
japod@6
|
29 |
}
|
japod@6
|
30 |
|
japod@6
|
31 |
@Override
|
japod@6
|
32 |
protected void tearDown() throws Exception {
|
japod@6
|
33 |
}
|
japod@6
|
34 |
|
japod@6
|
35 |
/** Create convertor that understands two currencies, CZK and
|
japod@6
|
36 |
* USD. Make 1 USD == 17 CZK.
|
japod@6
|
37 |
*
|
japod@6
|
38 |
* Creation of the convertor shall not require subclassing of any class
|
japod@6
|
39 |
* or interface on the client side.
|
japod@6
|
40 |
*
|
japod@6
|
41 |
* @return prepared convertor ready for converting USD to CZK and CZK to USD
|
japod@6
|
42 |
*/
|
japod@6
|
43 |
public static Convertor createCZKtoUSD() {
|
japod@6
|
44 |
return ConvertorFactory.newInstance();
|
japod@6
|
45 |
}
|
japod@6
|
46 |
|
japod@6
|
47 |
/** Create convertor that understands two currencies, CZK and
|
japod@6
|
48 |
* SKK. Make 100 SKK == 80 CZK.
|
japod@6
|
49 |
*
|
japod@6
|
50 |
* Creation of the convertor shall not require subclassing of any class
|
japod@6
|
51 |
* or interface on the client side.
|
japod@6
|
52 |
*
|
japod@6
|
53 |
* @return prepared convertor ready for converting SKK to CZK and CZK to SKK
|
japod@6
|
54 |
*/
|
japod@6
|
55 |
public static Convertor createSKKtoCZK() {
|
japod@6
|
56 |
return ConvertorFactory.newInstance();
|
japod@6
|
57 |
}
|
japod@6
|
58 |
|
japod@6
|
59 |
/** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
|
japod@6
|
60 |
* with it.
|
japod@6
|
61 |
*/
|
japod@6
|
62 |
public void testCurrencyCZKUSD() throws Exception {
|
japod@6
|
63 |
Convertor c = createCZKtoUSD();
|
japod@6
|
64 |
// convert $5 to CZK using c:
|
japod@6
|
65 |
Amount result = c.convert(new BigDecimal(5), USD, CZK);
|
japod@6
|
66 |
assertEquals("Result is 85 CZK", 85, result.getValue().intValue());
|
japod@6
|
67 |
|
japod@6
|
68 |
// convert $8 to CZK
|
japod@6
|
69 |
result = c.convert(new BigDecimal(8), USD, CZK);
|
japod@6
|
70 |
assertEquals("Result is 136 CZK", 136, result.getValue().intValue());
|
japod@6
|
71 |
|
japod@6
|
72 |
// convert 1003CZK to USD
|
japod@6
|
73 |
result = c.convert(new BigDecimal(1003), CZK, USD);
|
japod@6
|
74 |
assertEquals("Result is 59 USD", 59, result.getValue().intValue());
|
japod@6
|
75 |
}
|
japod@6
|
76 |
|
japod@6
|
77 |
/** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
|
japod@6
|
78 |
* with it.
|
japod@6
|
79 |
*/
|
japod@6
|
80 |
public void testCurrencySKKCZK() throws Exception {
|
japod@6
|
81 |
Convertor c = createSKKtoCZK();
|
japod@6
|
82 |
// convert 16CZK using c:
|
japod@6
|
83 |
Amount result = c.convert(new BigDecimal(16), CZK, SKK);
|
japod@6
|
84 |
assertEquals("Result is 20 SKK", 20, result.getValue().intValue());
|
japod@6
|
85 |
|
japod@6
|
86 |
// convert 500SKK to CZK
|
japod@6
|
87 |
result = c.convert(new BigDecimal(500), SKK, CZK);
|
japod@6
|
88 |
assertEquals("Result is 400 CZK", 400, result.getValue().intValue());
|
japod@6
|
89 |
}
|
japod@6
|
90 |
|
japod@6
|
91 |
public void testUnssuportedConversion(){
|
japod@6
|
92 |
Convertor c = ConvertorFactory.newInstance();
|
japod@6
|
93 |
try {
|
japod@6
|
94 |
c.convert(new BigDecimal(5), USD, SKK);
|
japod@6
|
95 |
fail();
|
japod@6
|
96 |
} catch(UnsupportedConversionException e) {
|
japod@6
|
97 |
//expected
|
japod@6
|
98 |
}
|
japod@6
|
99 |
}
|
japod@6
|
100 |
}
|
japod@6
|
101 |
|