1 package org.apidesign.apifest08.test;
4 import java.math.BigDecimal;
5 import java.util.Currency;
7 import junit.framework.TestCase;
8 import org.apidesign.apifest08.currency.Convertor;
9 import org.apidesign.apifest08.currency.ConvertorFactory;
10 import org.apidesign.apifest08.currency.InvalidConversionException;
13 /** Finish the Convertor API, and then write bodies of methods inside
14 * of this class to match the given tasks. To fullfil your task, use the
15 * API define in the <code>org.apidesign.apifest08.currency</code> package.
16 * Do not you reflection, or other hacks as your code
17 * shall run without any runtime permissions.
19 public class Task1Test extends TestCase {
21 private final static Currency CZK;
22 private final static Currency SKK;
23 private final static Currency USD;
27 CZK = Currency.getInstance("CZK");
28 SKK = Currency.getInstance("SKK");
29 USD = Currency.getInstance("USD");
32 public Task1Test(String testName) {
37 protected void setUp() throws Exception {
41 protected void tearDown() throws Exception {
44 /** Create convertor that understands two currencies, CZK and
45 * USD. Make 1 USD == 17 CZK.
47 * Creation of the convertor shall not require subclassing of any class
48 * or interface on the client side.
50 * @return prepared convertor ready for converting USD to CZK and CZK to USD
52 public static Convertor createCZKtoUSD()
54 return (ConvertorFactory.getConvertor("CZK", BigDecimal.valueOf(17.0),
55 "USD", BigDecimal.valueOf(1)));
58 /** Create convertor that understands two currencies, CZK and
59 * SKK. Make 100 SKK == 80 CZK.
61 * Creation of the convertor shall not require subclassing of any class
62 * or interface on the client side.
64 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
66 public static Convertor createSKKtoCZK()
68 return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), BigDecimal.valueOf(100),
69 Currency.getInstance("CZK"), BigDecimal.valueOf(80)));
72 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
75 public void testCurrencyCZKUSD() throws Exception {
76 Convertor c = createCZKtoUSD();
79 // convert $5 to CZK using c:
80 // assertEquals("Result is 85 CZK");
81 result = c.convert(USD, CZK, BigDecimal.valueOf(5));
82 assertEquals(new BigDecimal("85.00"), result);
85 // assertEquals("Result is 136 CZK");
86 result = c.convert(USD, CZK, BigDecimal.valueOf(8));
87 assertEquals(new BigDecimal("136.00"), result);
89 // convert 1003CZK to USD
90 // assertEquals("Result is 59 USD");
91 result = c.convert(CZK, USD, BigDecimal.valueOf(1003));
92 assertEquals(new BigDecimal("59.00"), result);
95 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
98 public void testCurrencySKKCZK() throws Exception {
99 Convertor c = createSKKtoCZK();
102 // convert 16CZK using c:
103 // assertEquals("Result is 20 SKK");
104 result = c.convert(CZK, SKK, BigDecimal.valueOf(16));
105 assertEquals(new BigDecimal("20.00"), result);
107 // convert 500SKK to CZK
108 // assertEquals("Result is 400 CZK");
109 result = c.convert(SKK, CZK, BigDecimal.valueOf(500));
110 assertEquals(new BigDecimal("400.00"), result);
114 * Verify that the CZK to USD convertor knows nothing about SKK.
116 public void testCannotConvertToSKKwithCZKUSDConvertor()
119 Convertor c = createCZKtoUSD();
123 // convert $5 to SKK, the API shall say this is not possible
124 c.convert(USD, SKK, BigDecimal.valueOf(5));
125 fail("cannot use the CZKtoUSD converter to convert to SKK");
127 catch(InvalidConversionException ex)
129 assertEquals("cannot convert to: SKK", ex.getMessage());
130 assertEquals(SKK, ex.getBadCurrency());
131 assertEquals(CZK, ex.getCurrencyA());
132 assertEquals(USD, ex.getCurrencyB());
137 // convert 500 SKK to CZK, the API shall say this is not possible
138 c.convert(SKK, CZK, BigDecimal.valueOf(5));
139 fail("cannot use the CZKtoUSD converter to convert from SKK");
141 catch(InvalidConversionException ex)
143 assertEquals("cannot convert from: SKK", ex.getMessage());
144 assertEquals(SKK, ex.getBadCurrency());
145 assertEquals(CZK, ex.getCurrencyA());
146 assertEquals(USD, ex.getCurrencyB());
151 * Verify that the CZK to SKK convertor knows nothing about USD.
153 public void testCannotConvertToUSDwithSKKCZKConvertor()
156 Convertor c = createSKKtoCZK();
160 // convert $5 to SKK, the API shall say this is not possible
161 c.convert(USD, SKK, BigDecimal.valueOf(5));
162 fail("cannot use the CZKtoUSD converter to convert to SKK");
164 catch(InvalidConversionException ex)
166 assertEquals("cannot convert from: USD", ex.getMessage());
167 assertEquals(USD, ex.getBadCurrency());
168 assertEquals(SKK, ex.getCurrencyA());
169 assertEquals(CZK, ex.getCurrencyB());
174 // convert 500 CZK to USD, the API shall say this is not possible
175 c.convert(CZK, USD, BigDecimal.valueOf(500));
176 fail("cannot use the CZKtoUSD converter to convert from SKK");
178 catch(InvalidConversionException ex)
180 assertEquals("cannot convert to: USD", ex.getMessage());
181 assertEquals(USD, ex.getBadCurrency());
182 assertEquals(SKK, ex.getCurrencyA());
183 assertEquals(CZK, ex.getCurrencyB());
187 public void testGetCurrencies()
190 Set<Currency> currencies;
192 c = createSKKtoCZK();
193 currencies = c.getCurrencies();
194 assertEquals(2, currencies.size());
195 assertTrue(currencies.contains(Currency.getInstance("SKK")));
196 assertTrue(currencies.contains(Currency.getInstance("CZK")));
198 c = createCZKtoUSD();
199 currencies = c.getCurrencies();
200 assertEquals(2, currencies.size());
201 assertTrue(currencies.contains(Currency.getInstance("USD")));
202 assertTrue(currencies.contains(Currency.getInstance("CZK")));
205 public void testGetConverstionRate()
206 throws InvalidConversionException
210 c = createSKKtoCZK();
211 assertEquals(1.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("CZK")).doubleValue());
212 assertEquals(1.0, c.getConversionRate(Currency.getInstance("SKK"), Currency.getInstance("SKK")).doubleValue());
213 assertEquals(0.80, c.getConversionRate(Currency.getInstance("SKK"), Currency.getInstance("CZK")).doubleValue());
214 assertEquals(1.25, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("SKK")).doubleValue());
216 c = createCZKtoUSD();
217 assertEquals(1.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("CZK")).doubleValue());
218 assertEquals(1.0, c.getConversionRate(Currency.getInstance("USD"), Currency.getInstance("USD")).doubleValue());
219 assertEquals(1.0/17.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("USD")).doubleValue(), 0.00000000000000001);
220 assertEquals(17.0, c.getConversionRate(Currency.getInstance("USD"), Currency.getInstance("CZK")).doubleValue(), 0.00000000000000001);