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.InvalidCurrencyException;
9 import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
12 * Finish the Convertor API, and then write bodies of methods inside of this class to match the given tasks. To fullfil
13 * your task, use the API define in the <code>org.apidesign.apifest08.currency</code> package. Do not you reflection, or
14 * other hacks as your code shall run without any runtime permissions.
16 public class Task1Test extends TestCase {
17 public Task1Test(String testName) {
22 protected void setUp() throws Exception {
26 protected void tearDown() throws Exception {
30 // Imagine that there are three parts of the whole system:
31 // 1. there is someone who knows the current exchange rate
32 // 2. there is someone who wants to do the conversion
33 // 3. there is the API between 1. and 2. which allows them to communicate
34 // Please design such API
38 * Create convertor that understands two currencies, CZK and USD. Make 1 USD == 17 CZK. This is a method provided for
39 * #1 group - e.g. those that know the exchange rate. They somehow need to create the objects from the API and tell
40 * them the exchange rate. The API itself knows nothing about any rates, before the createCZKtoUSD method is called.
41 * Creation of the convertor shall not require subclassing of any class or interface on the client side.
42 * @return prepared convertor ready for converting USD to CZK and CZK to USD
44 public static Convertor createCZKtoUSD() {
46 Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), 17d, 1d);
48 // create new instance
49 Convertor convertor = null;
51 convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("CZK"));
52 } catch (UnknownConvertorException e) {
60 * Create convertor that understands two currencies, CZK and SKK. Make 100 SKK == 80 CZK. Again this is method for the
61 * #1 group - it knows the exchange rate, and needs to use the API to create objects with the exchange rate. Anyone
62 * shall be ready to call this method without any other method being called previously. The API itself shall know
63 * nothing about any rates, before this method is called. Creation of the convertor shall not require subclassing of
64 * any class or interface on the client side.
65 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
67 public static Convertor createSKKtoCZK() {
69 Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 80d, 100d);
71 // create new instance
72 Convertor convertor = null;
74 convertor = Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
75 } catch (UnknownConvertorException e) {
83 // now the methods for group #2 follow:
84 // this group knows nothing about exchange rates, but knows how to use
85 // the API to do conversions. It somehow (by calling one of the factory
86 // methods) gets objects from the API and uses them to do the conversions.
90 * Use the convertor from <code>createCZKtoUSD</code> method and do few conversions with it.
92 public void testCurrencyCZKUSD() throws Exception {
93 Convertor c = createCZKtoUSD();
94 // convert $5 to CZK using c:
95 double result = c.convert(5, Currency.getInstance("USD"), Currency.getInstance("CZK"));
96 assertEquals("Result is not 85 CZK", 85.0, result);
99 result = c.convert(8, Currency.getInstance("USD"), Currency.getInstance("CZK"));
100 assertEquals("Result is not 136 CZK", 136.0, result);
102 // convert 1003CZK to USD
103 result = c.convert(1003, Currency.getInstance("CZK"), Currency.getInstance("USD"));
104 assertEquals("Result is not 59 USD", 59.0, result);
108 * Use the convertor from <code>createSKKtoCZK</code> method and do few conversions with it.
110 public void testCurrencySKKCZK() throws Exception {
111 Convertor c = createSKKtoCZK();
113 // convert 16CZK using c:
114 double result = c.convert(16, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
115 assertEquals("Result is not 20 SKK", 20.0, result);
117 // convert 500SKK to CZK
118 result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
119 assertEquals("Result is not 400 CZK", 400.0, result);
123 * Verify that the CZK to USD convertor knows nothing about SKK.
125 public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
126 Convertor c = createCZKtoUSD();
127 boolean exceptionThrown = false;
129 // convert $5 to SKK, the API shall say this is not possible
131 c.convert(5, Currency.getInstance("USD"), Currency.getInstance("SKK"));
132 exceptionThrown = false;
133 } catch (InvalidCurrencyException e) {
134 exceptionThrown = true;
136 assertEquals("It should be impossible to convert to SKK with USD->CZK convertor", true, exceptionThrown);
138 // convert 500 SKK to CZK, the API shall say this is not possible
140 c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
141 exceptionThrown = false;
142 } catch (InvalidCurrencyException e) {
143 exceptionThrown = true;
145 assertEquals("It should be impossible to convert from SKK with USD->CZK convertor", true, exceptionThrown);
150 * Verify that the CZK to SKK convertor knows nothing about USD.
152 public void testCannotConvertToSKKwithSKKCZKConvertor() throws Exception {
153 Convertor c = createSKKtoCZK();
154 boolean exceptionThrown = false;
156 // convert $5 to SKK, the API shall say this is not possible
158 c.convert(5, Currency.getInstance("USD"), Currency.getInstance("SKK"));
159 exceptionThrown = false;
160 } catch (InvalidCurrencyException e) {
161 exceptionThrown = true;
163 assertEquals("It should be impossible to convert form USD with SKK->CZK convertor", true, exceptionThrown);
165 // convert 500 CZK to USD, the API shall say this is not possible
167 c.convert(500, Currency.getInstance("CZK"), Currency.getInstance("USD"));
168 exceptionThrown = false;
169 } catch (InvalidCurrencyException e) {
170 exceptionThrown = true;
172 assertEquals("It should be impossible to convert to USD with SKK->CZK convertor", true, exceptionThrown);