1 package org.apidesign.apifest08.test;
3 import junit.framework.TestCase;
4 import org.apidesign.apifest08.currency.Amount;
5 import org.apidesign.apifest08.currency.Convertor;
6 import org.apidesign.apifest08.currency.ConvertorFactory;
8 /** Finish the Convertor API, and then write bodies of methods inside
9 * of this class to match the given tasks. To fullfil your task, use the
10 * API define in the <code>org.apidesign.apifest08.currency</code> package.
11 * Do not you reflection, or other hacks as your code
12 * shall run without any runtime permissions.
14 public class Task1Test extends TestCase {
15 public Task1Test(String testName) {
20 protected void setUp() throws Exception {
24 protected void tearDown() throws Exception {
27 /** Create convertor that understands two currencies, CZK and
28 * USD. Make 1 USD == 17 CZK.
30 * Creation of the convertor shall not require subclassing of any class
31 * or interface on the client side.
33 * @return prepared convertor ready for converting USD to CZK and CZK to USD
35 public static Convertor createCZKtoUSD() {
36 return ConvertorFactory.createConvertor(17);
39 /** Create convertor that understands two currencies, CZK and
40 * SKK. Make 100 SKK == 80 CZK.
42 * Creation of the convertor shall not require subclassing of any class
43 * or interface on the client side.
45 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
47 public static Convertor createSKKtoCZK() {
48 return ConvertorFactory.createConvertor(0.8);
51 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
54 public void testCurrencyCZKUSD() throws Exception {
55 Convertor c = createCZKtoUSD();
56 // convert $5 to CZK using c:
57 assertEquals("Result is 85 CZK", 85l, c.convert(new Amount(5)).getAmount());
60 assertEquals("Result is 136 CZK", 136l, c.convert(new Amount(8)).getAmount());
62 // convert 1003CZK to USD
63 assertEquals("Result is 136 CZK", 59l, c.convertBack(new Amount(1003)).getAmount());
64 // assertEquals("Result is 59 USD");
67 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
70 public void testCurrencySKKCZK() throws Exception {
71 Convertor c = createSKKtoCZK();
72 // convert 16CZK using c:
73 assertEquals("Result is 20 SKK", 20l, c.convertBack(new Amount(16)).getAmount());
75 // convert 500SKK to CZK
76 assertEquals("Result is 400 CZK", 400l, c.convert(new Amount(500)).getAmount());
79 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
82 public void testCurrencySKKCZKwithPences() throws Exception {
83 Convertor c = createSKKtoCZK();
84 // convert 16CZK 16h using c:
85 assertEquals("Result is 20 SKK 20h", new Amount(20, 20), c.convertBack(new Amount(16, 16)));
87 // convert 500SKK 80h to CZK
88 assertEquals("Result is 400 CZK 64h", new Amount(400, 64), c.convert(new Amount(500, 80)));
90 // convert 400CZK 80h to SKK
91 assertEquals("Result is 501 CZK", new Amount(501), c.convertBack(new Amount(400, 80)));
94 public void testNegativeSKKCZ() throws Exception{
95 Convertor c = createSKKtoCZK();
96 // convert -16CZK using c:
97 assertEquals("Result is -20", new Amount(-20), c.convertBack(new Amount(-16)));
99 // convert -500SKK 80h to CZK
100 assertEquals("Result is -400CZK 64h", new Amount(-400, 64), c.convert(new Amount(-500, 80)));