1 package org.apidesign.apifest08.test;
3 import java.util.Currency;
5 import junit.framework.TestCase;
7 import org.apidesign.apifest08.currency.Convertor;
8 import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
10 /** There are many currencies around the world and many banks manipulate
11 * with more than one or two at the same time. As banks are usually the
12 * best paying clients, which is true even in case of your Convertor API,
13 * it is reasonable to listen to their requests.
15 * The quest for today is to enhance your existing convertor API to hold
16 * information about many currencies and allow conversions between any of them.
17 * Also, as conversion rates for diferent currencies usually arise from various
18 * bank departments, there is another important need. There is a need to
19 * compose two convertors into one by merging all the information about
20 * currencies they know about.
22 public class Task2Test extends TestCase {
23 public Task2Test(String testName) {
28 protected void setUp() throws Exception {
32 protected void tearDown() throws Exception {
35 // As in Task1Test, keep in mind, that there are three parts
36 // of the whole system:
37 // 1. there is someone who knows the current exchange rate
38 // 2. there is someone who wants to do the conversion
39 // 3. there is the API between 1. and 2. which allows them to communicate
41 // Please backward compatibly enhance your existing API to support following
45 /** Create convertor that understands two currencies, CZK and
46 * SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
47 * knows the exchange rate, and needs to use the API to create objects
48 * with the exchange rate. Anyone shall be ready to call this method without
49 * any other method being called previously. The API itself shall know
50 * nothing about any rates, before this method is called.
52 public static Convertor createTripleConvertor() {
53 // Rates: 1USD = 15CZK
54 // Rates: 1USD = 20SKK
55 // Rates: 75CZK = 100SKK
57 Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), 15, 1);
58 Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("SKK"), 20, 1);
59 Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 75, 100);
61 // create new instance
62 Convertor convertor = null;
64 convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("SKK"), Currency.getInstance("CZK"));
65 } catch (UnknownConvertorException e) {
71 /** Define convertor that understands three currencies. Use it.
73 public void testConvertorForUSDandCZKandSKK() throws Exception {
74 Convertor c = createTripleConvertor();
76 // convert $5 to CZK using c:
77 double result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
78 assertEquals("Result is not 75 CZK", 75.0, result);
80 // convert $5 to SKK using c:
81 result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("SKK"));
82 assertEquals("Result is not 100 SKK", 100.0, result);
84 // convert 200SKK to CZK using c:
85 result = c.convert(200d, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
86 assertEquals("Result is not 150 CZK", 150.0, result);
88 // convert 200SKK to USK using c:
89 result = c.convert(200d, Currency.getInstance("SKK"), Currency.getInstance("USD"));
90 assertEquals("Result is not 10 USD", 10.0, result);
93 /** Merge all currency rates of convertor 1 with convertor 2.
94 * Implement this using your API, preferably this method just delegates
95 * into some API method which does the actual work, without requiring
96 * API clients to code anything complex.
98 public static Convertor merge(Convertor one, Convertor two) {
99 return one.merge(two);
102 /** Join the convertors from previous task, Task1Test and show that it
103 * can be used to do reasonable conversions.
105 public void testConvertorComposition() throws Exception {
107 Task1Test.createCZKtoUSD(),
108 Task1Test.createSKKtoCZK()
111 // convert $5 to CZK using c:
112 double result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
113 assertEquals("Result is not 85 CZK", 85.0, result);
115 // convert $8 to CZK using c:
116 result = c.convert(8d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
117 assertEquals("Result is not 136 CZK", 136.0, result);
119 // convert 1003CZK to USD using c:
120 result = c.convert(1003d, Currency.getInstance("CZK"), Currency.getInstance("USD"));
121 assertEquals("Result is not 59 USD", 59.0, result);
123 // convert 16CZK using c:
124 result = c.convert(16d, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
125 assertEquals("Result is not 20 SKK", 20.0, result);
127 // convert 500SKK to CZK using c:
128 result = c.convert(500d, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
129 assertEquals("Result is not 400 CZK", 400.0, result);