1 package org.apidesign.apifest08.test;
3 import junit.framework.TestCase;
4 import org.apidesign.apifest08.currency.Convertor;
5 import org.apidesign.apifest08.currency.CurrencyValue;
7 /** Finish the Convertor API, and then write bodies of methods inside
8 * of this class to match the given tasks. To fullfil your task, use the
9 * API define in the <code>org.apidesign.apifest08.currency</code> package.
10 * Do not you reflection, or other hacks as your code
11 * shall run without any runtime permissions.
13 public class Task1Test extends TestCase {
14 public Task1Test(String testName) {
19 protected void setUp() throws Exception {
23 protected void tearDown() throws Exception {
27 // Imagine that there are three parts of the whole system:
28 // 1. there is someone who knows the current exchange rate
29 // 2. there is someone who wants to do the conversion
30 // 3. there is the API between 1. and 2. which allows them to communicate
31 // Please design such API
34 /** Create convertor that understands two currencies, CZK and
35 * USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
36 * e.g. those that know the exchange rate. They somehow need to create
37 * the objects from the API and tell them the exchange rate. The API itself
38 * knows nothing about any rates, before the createCZKtoUSD method is called.
40 * Creation of the convertor shall not require subclassing of any class
41 * or interface on the client side.
43 * @return prepared convertor ready for converting USD to CZK and CZK to USD
45 public static Convertor<Double, String> createCZKtoUSD() {
46 return Convertor.getConvertorDoubleString(
47 CurrencyValue.getCurrencyValue(1d, "USD"),
48 CurrencyValue.getCurrencyValue(17d, "CZK")
52 /** Create convertor that understands two currencies, CZK and
53 * SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
54 * it knows the exchange rate, and needs to use the API to create objects
55 * with the exchange rate. Anyone shall be ready to call this method without
56 * any other method being called previously. The API itself shall know
57 * nothing about any rates, before this method is called.
59 * Creation of the convertor shall not require subclassing of any class
60 * or interface on the client side.
62 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
64 public static Convertor<Double, String> createSKKtoCZK() {
65 return Convertor.getConvertorDoubleString(
66 CurrencyValue.getCurrencyValue(100d, "SKK"),
67 CurrencyValue.getCurrencyValue(80d, "CZK")
72 // now the methods for group #2 follow:
73 // this group knows nothing about exchange rates, but knows how to use
74 // the API to do conversions. It somehow (by calling one of the factory
75 // methods) gets objects from the API and uses them to do the conversions.
78 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
81 public void testCurrencyCZKUSD() throws Exception {
82 Convertor<Double, String> c = createCZKtoUSD();
84 CurrencyValue<Double, String> result;
86 // convert $5 to CZK using c:
87 // assertEquals("Result is 85 CZK");
88 result = c.convert("CZK", CurrencyValue.getCurrencyValue(5d, "USD"));
89 assertEquals(CurrencyValue.getCurrencyValue(85d, "CZK"), result);
92 // assertEquals("Result is 136 CZK");
93 result = c.convert("CZK", CurrencyValue.getCurrencyValue(8d, "USD"));
94 assertEquals(CurrencyValue.getCurrencyValue(136d, "CZK"), result);
96 // convert 1003CZK to USD
97 // assertEquals("Result is 59 USD");
98 result = c.convert("USD", CurrencyValue.getCurrencyValue(1003d, "CZK"));
99 assertEquals(CurrencyValue.getCurrencyValue(59d, "USD"), result);
102 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
105 public void testCurrencySKKCZK() throws Exception {
106 Convertor<Double, String> c = createSKKtoCZK();
108 CurrencyValue<Double, String> result;
110 // convert 16CZK using c:
111 // assertEquals("Result is 20 SKK");
112 result = c.convert("SKK", CurrencyValue.getCurrencyValue(16d, "CZK"));
113 assertEquals(CurrencyValue.getCurrencyValue(20d, "SKK"), result);
115 // convert 500SKK to CZK
116 // assertEquals("Result is 400 CZK");
117 result = c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"));
118 assertEquals(CurrencyValue.getCurrencyValue(400d, "CZK"), result);
121 /** Verify that the CZK to USD convertor knows nothing about SKK.
123 public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
124 Convertor<Double, String> c = createCZKtoUSD();
126 // convert $5 to SKK, the API shall say this is not possible
127 c.convert("SKK", CurrencyValue.getCurrencyValue(16d, "CZK"));
128 fail("Should not convert");
129 } catch (Exception e) {
132 // convert 500 SKK to CZK, the API shall say this is not possible
133 c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"));
134 fail("Should not convert");
135 } catch (Exception e) {
140 /** Verify that the CZK to SKK convertor knows nothing about USD.
142 public void testCannotConvertToUSDwithSKKCZKConvertor() throws Exception {
143 Convertor<Double, String> c = createSKKtoCZK();
145 // convert $5 to SKK, the API shall say this is not possible
146 c.convert("SKK", CurrencyValue.getCurrencyValue(5d, "USD"));
147 fail("Should not convert");
148 } catch (Exception e) {
151 // convert 500 CZK to USD, the API shall say this is not possible
152 c.convert("USD", CurrencyValue.getCurrencyValue(500d, "CZK"));
153 fail("Should not convert");
154 } catch (Exception e) {