1 package org.apidesign.apifest08.test;
4 import java.math.BigDecimal;
5 import java.util.Currency;
6 import junit.framework.TestCase;
7 import org.apidesign.apifest08.currency.Convertor;
8 import org.apidesign.apifest08.currency.ConvertorFactory;
9 import org.apidesign.apifest08.currency.InvalidConversionException;
12 /** Finish the Convertor API, and then write bodies of methods inside
13 * of this class to match the given tasks. To fullfil your task, use the
14 * API define in the <code>org.apidesign.apifest08.currency</code> package.
15 * Do not you reflection, or other hacks as your code
16 * shall run without any runtime permissions.
18 public class Task1Test extends TestCase {
20 private final static Currency CZK;
21 private final static Currency SKK;
22 private final static Currency USD;
26 CZK = Currency.getInstance("CZK");
27 SKK = Currency.getInstance("SKK");
28 USD = Currency.getInstance("USD");
31 public Task1Test(String testName) {
36 protected void setUp() throws Exception {
40 protected void tearDown() throws Exception {
43 /** Create convertor that understands two currencies, CZK and
44 * USD. Make 1 USD == 17 CZK.
46 * Creation of the convertor shall not require subclassing of any class
47 * or interface on the client side.
49 * @return prepared convertor ready for converting USD to CZK and CZK to USD
51 public static Convertor createCZKtoUSD()
53 return (ConvertorFactory.getConvertor("CZK", BigDecimal.valueOf(17.0), "USD", BigDecimal.valueOf(1)));
56 /** Create convertor that understands two currencies, CZK and
57 * SKK. Make 100 SKK == 80 CZK.
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 createSKKtoCZK()
66 return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), BigDecimal.valueOf(100), Currency.getInstance("CZK"), BigDecimal.valueOf(80)));
69 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
72 public void testCurrencyCZKUSD() throws Exception {
73 Convertor c = createCZKtoUSD();
76 // convert $5 to CZK using c:
77 // assertEquals("Result is 85 CZK");
78 result = c.convert(USD, CZK, BigDecimal.valueOf(5));
79 assertEquals(new BigDecimal("85.00"), result);
82 // assertEquals("Result is 136 CZK");
83 result = c.convert(USD, CZK, BigDecimal.valueOf(8));
84 assertEquals(new BigDecimal("136.00"), result);
86 // convert 1003CZK to USD
87 // assertEquals("Result is 59 USD");
88 result = c.convert(CZK, USD, BigDecimal.valueOf(1003));
89 assertEquals(new BigDecimal("59.00"), result);
92 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
95 public void testCurrencySKKCZK() throws Exception {
96 Convertor c = createSKKtoCZK();
99 // convert 16CZK using c:
100 // assertEquals("Result is 20 SKK");
101 result = c.convert(CZK, SKK, BigDecimal.valueOf(16));
102 assertEquals(new BigDecimal("20.00"), result);
104 // convert 500SKK to CZK
105 // assertEquals("Result is 400 CZK");
106 result = c.convert(SKK, CZK, BigDecimal.valueOf(500));
107 assertEquals(new BigDecimal("400.00"), result);
111 * Verify that the CZK to USD convertor knows nothing about SKK.
113 public void testCannotConvertToSKKwithCZKUSDConvertor()
116 Convertor c = createCZKtoUSD();
120 // convert $5 to SKK, the API shall say this is not possible
121 c.convert(USD, SKK, BigDecimal.valueOf(5));
122 fail("cannot use the CZKtoUSD converter to convert to SKK");
124 catch(InvalidConversionException ex)
126 assertEquals("cannot convert to: SKK", ex.getMessage());
127 assertEquals(SKK, ex.getBadCurrency());
128 assertEquals(CZK, ex.getCurrencyA());
129 assertEquals(USD, ex.getCurrencyB());
134 // convert 500 SKK to CZK, the API shall say this is not possible
135 c.convert(SKK, CZK, BigDecimal.valueOf(5));
136 fail("cannot use the CZKtoUSD converter to convert from SKK");
138 catch(InvalidConversionException ex)
140 assertEquals("cannot convert from: SKK", ex.getMessage());
141 assertEquals(SKK, ex.getBadCurrency());
142 assertEquals(CZK, ex.getCurrencyA());
143 assertEquals(USD, ex.getCurrencyB());
148 * Verify that the CZK to SKK convertor knows nothing about USD.
150 public void testCannotConvertToUSDwithSKKCZKConvertor()
153 Convertor c = createSKKtoCZK();
157 // convert $5 to SKK, the API shall say this is not possible
158 c.convert(USD, SKK, BigDecimal.valueOf(5));
159 fail("cannot use the CZKtoUSD converter to convert to SKK");
161 catch(InvalidConversionException ex)
163 assertEquals("cannot convert from: USD", ex.getMessage());
164 assertEquals(USD, ex.getBadCurrency());
165 assertEquals(SKK, ex.getCurrencyA());
166 assertEquals(CZK, ex.getCurrencyB());
171 // convert 500 CZK to USD, the API shall say this is not possible
172 c.convert(CZK, USD, BigDecimal.valueOf(500));
173 fail("cannot use the CZKtoUSD converter to convert from SKK");
175 catch(InvalidConversionException ex)
177 assertEquals("cannot convert to: USD", ex.getMessage());
178 assertEquals(USD, ex.getBadCurrency());
179 assertEquals(SKK, ex.getCurrencyA());
180 assertEquals(CZK, ex.getCurrencyB());