1 package org.apidesign.apifest08.test;
3 import java.math.BigDecimal;
4 import java.util.Currency;
5 import junit.framework.TestCase;
6 import org.apidesign.apifest08.currency.Convertor;
7 import org.apidesign.apifest08.currency.ConvertorFactory;
9 /** Finish the Convertor API, and then write bodies of methods inside
10 * of this class to match the given tasks. To fullfil your task, use the
11 * API define in the <code>org.apidesign.apifest08.currency</code> package.
12 * Do not you reflection, or other hacks as your code
13 * shall run without any runtime permissions.
15 public class Task1Test extends TestCase {
16 public Task1Test(String testName) {
21 protected void setUp() throws Exception {
25 protected void tearDown() throws Exception {
28 /** Create convertor that understands two currencies, CZK and
29 * USD. Make 1 USD == 17 CZK.
31 * Creation of the convertor shall not require subclassing of any class
32 * or interface on the client side.
34 * @return prepared convertor ready for converting USD to CZK and CZK to USD
36 public static Convertor createCZKtoUSD()
38 return (ConvertorFactory.getConvertor("CZK", "USD"));
41 /** Create convertor that understands two currencies, CZK and
42 * SKK. Make 100 SKK == 80 CZK.
44 * Creation of the convertor shall not require subclassing of any class
45 * or interface on the client side.
47 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
49 public static Convertor createSKKtoCZK()
51 return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), Currency.getInstance("CZK")));
54 public static Convertor createUSDtoUSD()
56 return (ConvertorFactory.getConvertor(Currency.getInstance("USD"), Currency.getInstance("USD")));
59 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
62 public void testCurrencyCZKUSD() throws Exception {
63 Convertor c = createCZKtoUSD();
66 // convert $5 to CZK using c:
67 // assertEquals("Result is 85 CZK");
68 result = c.convertFrom(BigDecimal.valueOf(5));
69 assertEquals(new BigDecimal("85.00"), result);
72 // assertEquals("Result is 136 CZK");
73 result = c.convertFrom(BigDecimal.valueOf(8));
74 assertEquals(new BigDecimal("136.00"), result);
76 // convert 1003CZK to USD
77 // assertEquals("Result is 59 USD");
78 result = c.convertTo(BigDecimal.valueOf(1003));
79 assertEquals(new BigDecimal("59.00"), result);
82 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
85 public void testCurrencySKKCZK() throws Exception {
86 Convertor c = createSKKtoCZK();
89 // convert 16CZK using c:
90 // assertEquals("Result is 20 SKK");
91 result = c.convertFrom(BigDecimal.valueOf(16));
92 assertEquals(new BigDecimal("20.00"), result);
94 // convert 500SKK to CZK
95 // assertEquals("Result is 400 CZK");
96 result = c.convertTo(BigDecimal.valueOf(500));
97 assertEquals(new BigDecimal("400.00"), result);
100 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
103 public void testCurrencyUSDUSD() throws Exception {
104 Convertor c = createUSDtoUSD();
107 // convert 1USD using c:
108 // assertEquals("Result is 1 USD");
109 result = c.convertFrom(BigDecimal.valueOf(1));
110 assertEquals(new BigDecimal("1.00"), result);
112 // convert 500USD to USD
113 // assertEquals("Result is 500 USD");
114 result = c.convertTo(BigDecimal.valueOf(500));
115 assertEquals(new BigDecimal("500.00"), result);