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