1 package org.apidesign.apifest08.test;
3 import junit.framework.*;
4 import org.apidesign.apifest08.currency.*;
8 /** Finish the Convertor API, and then write bodies of methods inside
9 * of this class to match the given tasks. To fullfil your task, use the
10 * API define in the <code>org.apidesign.apifest08.currency</code> package.
11 * Do not you reflection, or other hacks as your code
12 * shall run without any runtime permissions.
14 public class Task1Test extends TestCase {
16 private static final String
21 private static final Currency
22 USD2 = Currency.getInstance(USD),
23 CZK2 = Currency.getInstance(CZK),
24 SKK2 = Currency.getInstance(SKK);
26 public Task1Test(String testName) {
31 protected void setUp() throws Exception {
35 protected void tearDown() throws Exception {
38 /** Create convertor that understands two currencies, CZK and
39 * USD. Make 1 USD == 17 CZK.
41 * Creation of the convertor shall not require subclassing of any class
42 * or interface on the client side.
44 * @return prepared convertor ready for converting USD to CZK and CZK to USD
46 public static CurrencyConverter createCZKtoUSD() {
47 return OfflineConverterProvider.getInstance().getConverter(17, CZK, 1, USD);
50 public static CurrencyConverter createCZKtoUSD2() {
51 return OfflineConverterProvider.getInstance().getConverter(CZK, USD);
54 /** Create convertor that understands two currencies, CZK and
55 * SKK. Make 100 SKK == 80 CZK.
57 * Creation of the convertor shall not require subclassing of any class
58 * or interface on the client side.
60 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
62 public static CurrencyConverter createSKKtoCZK() {
63 return OfflineConverterProvider.getInstance().getConverter(100, SKK, 80, CZK);
66 public static CurrencyConverter createSKKtoCZK2() {
67 return OfflineConverterProvider.getInstance().getConverter(SKK, CZK);
70 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
73 public void testCurrencyCZKUSD() throws Exception {
74 CurrencyConverter c = createCZKtoUSD();
77 // test without specifying rates
78 c = createCZKtoUSD2();
82 private void testCZKUSD(CurrencyConverter c) {
84 // convert $5 to CZK using c:
85 // assertEquals("Result is 85 CZK");
86 czk = c.convert(5, USD, CZK);
87 assertEquals(85.0, czk, 0.0);
88 czk = c.convert(5, USD2, CZK2);
89 assertEquals(85.0, czk, 0.0);
92 // assertEquals("Result is 136 CZK");
93 czk = c.convert(8, USD, CZK);
94 assertEquals(136.0, czk, 0.0);
95 czk = c.convert(8, USD2, CZK2);
96 assertEquals(136.0, czk, 0.0);
98 // convert 1003CZK to USD
99 // assertEquals("Result is 59 USD");
100 usd = c.convert(1003, CZK, USD);
101 assertEquals(59.0, usd, 0.0);
102 usd = c.convert(1003, CZK2, USD2);
103 assertEquals(59.0, usd, 0.0);
106 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
109 public void testCurrencySKKCZK() throws Exception {
110 CurrencyConverter c = createSKKtoCZK();
113 // test without specifying rates
114 c = createSKKtoCZK2();
118 private void testCZKSKK(CurrencyConverter c) {
120 // convert 16CZK using c:
121 // assertEquals("Result is 20 SKK");
122 skk = c.convert(16, CZK, SKK);
123 assertEquals(20.0, skk, 0.0);
124 skk = c.convert(16, CZK2, SKK2);
125 assertEquals(20.0, skk, 0.0);
127 // convert 500SKK to CZK
128 // assertEquals("Result is 400 CZK");
129 czk = c.convert(500, SKK, CZK);
130 assertEquals(400.0, czk, 0.0);
131 czk = c.convert(500, SKK2, CZK2);
132 assertEquals(400.0, czk, 0.0);