task2/solution05/test/org/apidesign/apifest08/test/Task1Test.java
changeset 29 f6073056b9fe
parent 6 97662396c0fd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task2/solution05/test/org/apidesign/apifest08/test/Task1Test.java	Wed Oct 01 10:43:05 2008 +0200
     1.3 @@ -0,0 +1,104 @@
     1.4 +package org.apidesign.apifest08.test;
     1.5 +
     1.6 +import junit.framework.TestCase;
     1.7 +import org.apidesign.apifest08.currency.Amount;
     1.8 +import org.apidesign.apifest08.currency.Convertor;
     1.9 +import org.apidesign.apifest08.currency.ConvertorFactory;
    1.10 +
    1.11 +/** Finish the Convertor API, and then write bodies of methods inside
    1.12 + * of this class to match the given tasks. To fullfil your task, use the
    1.13 + * API define in the <code>org.apidesign.apifest08.currency</code> package.
    1.14 + * Do not you reflection, or other hacks as your code
    1.15 + * shall run without any runtime permissions.
    1.16 + */
    1.17 +public class Task1Test extends TestCase {
    1.18 +    public Task1Test(String testName) {
    1.19 +        super(testName);
    1.20 +    }
    1.21 +
    1.22 +    @Override
    1.23 +    protected void setUp() throws Exception {
    1.24 +    }
    1.25 +
    1.26 +    @Override
    1.27 +    protected void tearDown() throws Exception {
    1.28 +    }
    1.29 +
    1.30 +    /** Create convertor that understands two currencies, CZK and
    1.31 +     *  USD. Make 1 USD == 17 CZK.
    1.32 +     *
    1.33 +     * Creation of the convertor shall not require subclassing of any class
    1.34 +     * or interface on the client side.
    1.35 +     *
    1.36 +     * @return prepared convertor ready for converting USD to CZK and CZK to USD
    1.37 +     */
    1.38 +    public static Convertor createCZKtoUSD() {
    1.39 +        return ConvertorFactory.createConvertor(17);
    1.40 +    }
    1.41 +
    1.42 +    /** Create convertor that understands two currencies, CZK and
    1.43 +     *  SKK. Make 100 SKK == 80 CZK.
    1.44 +     *
    1.45 +     * Creation of the convertor shall not require subclassing of any class
    1.46 +     * or interface on the client side.
    1.47 +     * 
    1.48 +     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    1.49 +     */
    1.50 +    public static Convertor createSKKtoCZK() {
    1.51 +        return ConvertorFactory.createConvertor(0.8);
    1.52 +    }
    1.53 +    
    1.54 +    /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    1.55 +     * with it.
    1.56 +     */
    1.57 +    public void testCurrencyCZKUSD() throws Exception {
    1.58 +        Convertor c = createCZKtoUSD();
    1.59 +        // convert $5 to CZK using c:
    1.60 +        assertEquals("Result is 85 CZK", 85l, c.convert(new Amount(5)).getAmount());
    1.61 +
    1.62 +        // convert $8 to CZK
    1.63 +        assertEquals("Result is 136 CZK", 136l, c.convert(new Amount(8)).getAmount());
    1.64 +
    1.65 +        // convert 1003CZK to USD
    1.66 +        assertEquals("Result is 136 CZK", 59l, c.convertBack(new Amount(1003)).getAmount());
    1.67 +        // assertEquals("Result is 59 USD");
    1.68 +    }
    1.69 +
    1.70 +    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    1.71 +     * with it.
    1.72 +     */
    1.73 +    public void testCurrencySKKCZK() throws Exception {
    1.74 +        Convertor c = createSKKtoCZK();
    1.75 +        // convert 16CZK using c:
    1.76 +        assertEquals("Result is 20 SKK", 20l, c.convertBack(new Amount(16)).getAmount());
    1.77 +
    1.78 +        // convert 500SKK to CZK
    1.79 +        assertEquals("Result is 400 CZK", 400l, c.convert(new Amount(500)).getAmount());
    1.80 +    }
    1.81 +
    1.82 +    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    1.83 +     * with it.
    1.84 +     */
    1.85 +    public void testCurrencySKKCZKwithPences() throws Exception {
    1.86 +        Convertor c = createSKKtoCZK();
    1.87 +        // convert 16CZK 16h using c:
    1.88 +        assertEquals("Result is 20 SKK 20h", new Amount(20, 20), c.convertBack(new Amount(16, 16)));
    1.89 +
    1.90 +        // convert 500SKK 80h to CZK
    1.91 +        assertEquals("Result is 400 CZK 64h", new Amount(400, 64), c.convert(new Amount(500, 80)));
    1.92 +
    1.93 +        // convert 400CZK 80h to SKK
    1.94 +        assertEquals("Result is 501 CZK", new Amount(501), c.convertBack(new Amount(400, 80)));
    1.95 +    }
    1.96 +
    1.97 +    public void testNegativeSKKCZ() throws Exception{
    1.98 +        Convertor c = createSKKtoCZK();
    1.99 +        // convert -16CZK using c:
   1.100 +        assertEquals("Result is -20", new Amount(-20), c.convertBack(new Amount(-16)));
   1.101 +
   1.102 +        // convert -500SKK 80h to CZK
   1.103 +        assertEquals("Result is -400CZK 64h", new Amount(-400, 64), c.convert(new Amount(-500, 80)));
   1.104 +
   1.105 +    }
   1.106 +}
   1.107 +