1 package org.apidesign.apifest08.test;
3 import static org.apidesign.apifest08.currency.Currencies.CZK;
4 import static org.apidesign.apifest08.currency.Currencies.SKK;
5 import static org.apidesign.apifest08.currency.Currencies.USD;
7 import java.math.BigDecimal;
9 import junit.framework.TestCase;
11 import org.apidesign.apifest08.currency.Amount;
12 import org.apidesign.apifest08.currency.Convertor;
13 import org.apidesign.apifest08.currency.ConvertorFactory;
14 import org.apidesign.apifest08.currency.UnsupportedConversionException;
16 /** Finish the Convertor API, and then write bodies of methods inside
17 * of this class to match the given tasks. To fullfil your task, use the
18 * API define in the <code>org.apidesign.apifest08.currency</code> package.
19 * Do not you reflection, or other hacks as your code
20 * shall run without any runtime permissions.
22 public class Task1Test extends TestCase {
23 public Task1Test(String testName) {
28 protected void setUp() throws Exception {
32 protected void tearDown() throws Exception {
35 /** Create convertor that understands two currencies, CZK and
36 * USD. Make 1 USD == 17 CZK.
38 * Creation of the convertor shall not require subclassing of any class
39 * or interface on the client side.
41 * @return prepared convertor ready for converting USD to CZK and CZK to USD
43 public static Convertor createCZKtoUSD() {
44 return ConvertorFactory.newInstance();
47 /** Create convertor that understands two currencies, CZK and
48 * SKK. Make 100 SKK == 80 CZK.
50 * Creation of the convertor shall not require subclassing of any class
51 * or interface on the client side.
53 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
55 public static Convertor createSKKtoCZK() {
56 return ConvertorFactory.newInstance();
59 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
62 public void testCurrencyCZKUSD() throws Exception {
63 Convertor c = createCZKtoUSD();
64 // convert $5 to CZK using c:
65 Amount result = c.convert(new BigDecimal(5), USD, CZK);
66 assertEquals("Result is 85 CZK", 85, result.getValue().intValue());
69 result = c.convert(new BigDecimal(8), USD, CZK);
70 assertEquals("Result is 136 CZK", 136, result.getValue().intValue());
72 // convert 1003CZK to USD
73 result = c.convert(new BigDecimal(1003), CZK, USD);
74 assertEquals("Result is 59 USD", 59, result.getValue().intValue());
77 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
80 public void testCurrencySKKCZK() throws Exception {
81 Convertor c = createSKKtoCZK();
82 // convert 16CZK using c:
83 Amount result = c.convert(new BigDecimal(16), CZK, SKK);
84 assertEquals("Result is 20 SKK", 20, result.getValue().intValue());
86 // convert 500SKK to CZK
87 result = c.convert(new BigDecimal(500), SKK, CZK);
88 assertEquals("Result is 400 CZK", 400, result.getValue().intValue());
91 public void testUnssuportedConversion(){
92 Convertor c = ConvertorFactory.newInstance();
94 c.convert(new BigDecimal(5), USD, SKK);
96 } catch(UnsupportedConversionException e) {