1 package org.apidesign.apifest08.test;
3 import static org.apidesign.apifest08.test.Currencies.CZK;
4 import static org.apidesign.apifest08.test.Currencies.SKK;
5 import static org.apidesign.apifest08.test.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.ConversionException;
13 import org.apidesign.apifest08.currency.Convertor;
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 new Convertor(new BigDecimal(17), USD, CZK);
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 new Convertor(new BigDecimal("0.8"), SKK, CZK);
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());
93 * Verify that the CZK to USD convertor knows nothing about SKK.
95 public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
96 Convertor c = createCZKtoUSD();
97 // convert $5 to SKK, the API shall say this is not possible
99 c.convert(new BigDecimal(5), USD, SKK);
100 fail("convert $5 to SKK, the API shall say this is not possible");
101 } catch (ConversionException e) {
105 // convert 500 SKK to CZK, the API shall say this is not possible
108 c.convert(new BigDecimal("500"), SKK, CZK);
109 fail("convert 500 SKK to CZK, the API shall say this is not possible");
110 } catch (ConversionException e) {
116 * Verify that the CZK to SKK convertor knows nothing about USD.
118 public void testCannotConvertToSKKwithCZKSKKConvertor() throws Exception {
119 Convertor c = createSKKtoCZK();
120 // convert $5 to SKK, the API shall say this is not possible
122 c.convert(new BigDecimal(5), USD, SKK);
123 fail("convert $5 to SKK, the API shall say this is not possible");
124 } catch(ConversionException e) {
129 c.convert(new BigDecimal(500), CZK, USD);
130 fail("convert 500 CZK to USD, the API shall say this is not possible");
131 } catch(ConversionException e) {