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.ConvertorFactory;
9 import org.apidesign.apifest08.currency.MoneyImpl;
11 /** Finish the Convertor API, and then write bodies of methods inside
12 * of this class to match the given tasks. To fullfil your task, use the
13 * API define in the <code>org.apidesign.apifest08.currency</code> package.
14 * Do not you reflection, or other hacks as your code
15 * shall run without any runtime permissions.
17 public class Task1Test extends TestCase {
19 public static final Currency USD = Currency.getInstance("USD");
20 public static final Currency CZK = Currency.getInstance("CZK");
21 public static final Currency SKK = Currency.getInstance("SKK");
23 public Task1Test(String testName) {
28 protected void setUp() throws Exception {
32 protected void tearDown() throws Exception {
36 // Imagine that there are three parts 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
40 // Please design such API
43 /** Create convertor that understands two currencies, CZK and
44 * USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
45 * e.g. those that know the exchange rate. They somehow need to create
46 * the objects from the API and tell them the exchange rate. The API itself
47 * knows nothing about any rates, before the createCZKtoUSD method is called.
49 * Creation of the convertor shall not require subclassing of any class
50 * or interface on the client side.
52 * @return prepared convertor ready for converting USD to CZK and CZK to USD
54 public static Convertor createCZKtoUSD() {
55 return ConvertorFactory.createConvertor(new MoneyImpl(17,CZK), new MoneyImpl(1,USD));
58 /** Create convertor that understands two currencies, CZK and
59 * SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
60 * it knows the exchange rate, and needs to use the API to create objects
61 * with the exchange rate. Anyone shall be ready to call this method without
62 * any other method being called previously. The API itself shall know
63 * nothing about any rates, before this method is called.
65 * Creation of the convertor shall not require subclassing of any class
66 * or interface on the client side.
68 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
70 public static Convertor createSKKtoCZK() {
71 return ConvertorFactory.createConvertor(new MoneyImpl(100,SKK), new MoneyImpl(80,CZK));
75 // now the methods for group #2 follow:
76 // this group knows nothing about exchange rates, but knows how to use
77 // the API to do conversions. It somehow (by calling one of the factory
78 // methods) gets objects from the API and uses them to do the conversions.
81 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
84 public void testCurrencyCZKUSD() throws Exception {
85 Convertor c = createCZKtoUSD();
86 // convert $5 to CZK using c:
87 assertEquals("Result is 85 CZK",new MoneyImpl(85,CZK), c.convert(new MoneyImpl(5,USD),CZK));
90 assertEquals("Result is 136 CZK",new MoneyImpl(136,CZK), c.convert(new MoneyImpl(8,USD),CZK));
92 // convert 1003CZK to USD
93 assertEquals("Result is 59 USD", new MoneyImpl(59,USD), c.convert(new MoneyImpl(1003,CZK),USD));
97 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
100 public void testCurrencySKKCZK() throws Exception {
101 Convertor c = createSKKtoCZK();
102 // convert 16CZK using c:
103 assertEquals("Result is 20 SKK", new MoneyImpl(20,SKK), c.convert(new MoneyImpl(16,CZK),SKK));
105 // convert 500SKK to CZK
106 assertEquals("Result is 400 CZK", new MoneyImpl(400,CZK), c.convert(new MoneyImpl(500,SKK),CZK));
109 /** Verify that the CZK to USD convertor knows nothing about SKK.
111 public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
112 Convertor c = createCZKtoUSD();
113 // convert $5 to SKK, the API shall say this is not possible
116 c.convert(new MoneyImpl(5, USD), SKK);
117 fail("Exception expected");
119 catch(IllegalArgumentException e)
121 assertTrue("Ok",true);
123 // convert 500 SKK to CZK, the API shall say this is not possible
126 c.convert(new MoneyImpl(500, SKK), CZK);
127 fail("Exception expected");
129 catch(IllegalArgumentException e)
131 assertTrue("Ok",true);
135 /** Verify that the CZK to SKK convertor knows nothing about USD.
137 public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception {
138 Convertor c = createSKKtoCZK();
139 // convert $5 to SKK, the API shall say this is not possible
142 c.convert(new MoneyImpl(5, USD), SKK);
143 fail("Exception expected");
145 catch(IllegalArgumentException e)
147 assertTrue("Ok",true);
149 // convert 500 CZK to USD, the API shall say this is not possible
152 c.convert(new MoneyImpl(500, CZK), USD);
153 fail("Exception expected");
155 catch(IllegalArgumentException e)
157 assertTrue("Ok",true);