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;
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 createCZKtoUSD() {
46 return ConvertorFactory.newInstance().createConvertor("CZK", "USD", 17, 1);
49 /** Create convertor that understands two currencies, CZK and
50 * SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
51 * it knows the exchange rate, and needs to use the API to create objects
52 * with the exchange rate. Anyone shall be ready to call this method without
53 * any other method being called previously. The API itself shall know
54 * nothing about any rates, before this method is called.
56 * Creation of the convertor shall not require subclassing of any class
57 * or interface on the client side.
59 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
61 public static Convertor createSKKtoCZK() {
62 return ConvertorFactory.newInstance().createConvertor("SKK", "CZK", 100, 80);
66 // now the methods for group #2 follow:
67 // this group knows nothing about exchange rates, but knows how to use
68 // the API to do conversions. It somehow (by calling one of the factory
69 // methods) gets objects from the API and uses them to do the conversions.
72 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
75 public void testCurrencyCZKUSD() throws Exception {
76 Convertor c = createCZKtoUSD();
77 // convert $5 to CZK using c:
78 assertEquals("Result is 85 CZK", 85.0, c.convert("USD", "CZK", 5));
81 assertEquals("Result is 136 CZK", 136.0, c.convert("USD", "CZK", 8));
83 // convert 1003CZK to USD
84 assertEquals("Result is 59 CZK", 59.0, c.convert("CZK", "USD", 1003));
87 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
90 public void testCurrencySKKCZK() throws Exception {
91 Convertor c = createSKKtoCZK();
92 // convert 16CZK using c:
93 // assertEquals("Result is 20 SKK");
94 assertEquals("Result is 20 SKK", 20.0, c.convert("CZK", "SKK", 16));
96 // convert 500SKK to CZK
97 // assertEquals("Result is 400 CZK");
98 assertEquals("Result is 400 SKK", 400.0, c.convert("SKK", "CZK", 500));
101 /** Verify that the CZK to USD convertor knows nothing about SKK.
103 public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
104 Convertor c = createCZKtoUSD();
105 // convert $5 to SKK, the API shall say this is not possible
107 c.convert("USD", "SKK", 5);
108 fail("Converting SKK with CZKUSD convertor is impossible");
109 } catch (IllegalArgumentException e){
113 // convert 500 SKK to CZK, the API shall say this is not possible
115 c.convert("SKK", "CZK", 500);
116 fail("Converting SKK with CZKUSD convertor is impossible");
117 } catch (IllegalArgumentException e){
123 /** Verify that the CZK to SKK convertor knows nothing about USD.
125 public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception {
126 Convertor c = createSKKtoCZK();
127 // convert $5 to SKK, the API shall say this is not possible
129 c.convert("USD", "SKK", 5);
130 fail("Converting SKK with SKKCZK convertor is impossible");
131 } catch (IllegalArgumentException e){
135 // convert 500 CZK to USD, the API shall say this is not possible
137 c.convert("CZK", "USD", 500);
138 fail("Converting USD with SKKCZK convertor is impossible");
139 } catch (IllegalArgumentException e){