task1/solution11/test/org/apidesign/apifest08/test/Task1Test.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
child 19 45e6a2d1ffd1
permissions -rw-r--r--
Adding solutions received for task1
japod@6
     1
package org.apidesign.apifest08.test;
japod@6
     2
japod@6
     3
import junit.framework.TestCase;
japod@6
     4
import org.apidesign.apifest08.currency.Convertor;
japod@6
     5
import org.apidesign.apifest08.currency.CurrencyValue;
japod@6
     6
japod@6
     7
/** Finish the Convertor API, and then write bodies of methods inside
japod@6
     8
 * of this class to match the given tasks. To fullfil your task, use the
japod@6
     9
 * API define in the <code>org.apidesign.apifest08.currency</code> package.
japod@6
    10
 * Do not you reflection, or other hacks as your code
japod@6
    11
 * shall run without any runtime permissions.
japod@6
    12
 */
japod@6
    13
public class Task1Test extends TestCase {
japod@6
    14
    public Task1Test(String testName) {
japod@6
    15
        super(testName);
japod@6
    16
    }
japod@6
    17
japod@6
    18
    @Override
japod@6
    19
    protected void setUp() throws Exception {
japod@6
    20
    }
japod@6
    21
japod@6
    22
    @Override
japod@6
    23
    protected void tearDown() throws Exception {
japod@6
    24
    }
japod@6
    25
japod@6
    26
    /** Create convertor that understands two currencies, CZK and
japod@6
    27
     *  USD. Make 1 USD == 17 CZK.
japod@6
    28
     *
japod@6
    29
     * Creation of the convertor shall not require subclassing of any class
japod@6
    30
     * or interface on the client side.
japod@6
    31
     *
japod@6
    32
     * @return prepared convertor ready for converting USD to CZK and CZK to USD
japod@6
    33
     */
japod@6
    34
    public static Convertor<Integer, String> createCZKtoUSD() {
japod@6
    35
        return Convertor.getConvertorIntegerString(
japod@6
    36
                CurrencyValue.getCurrencyValue(1, "USD"),
japod@6
    37
                CurrencyValue.getCurrencyValue(17, "CZK")
japod@6
    38
        );
japod@6
    39
    }
japod@6
    40
japod@6
    41
    /** Create convertor that understands two currencies, CZK and
japod@6
    42
     *  SKK. Make 100 SKK == 80 CZK.
japod@6
    43
     *
japod@6
    44
     * Creation of the convertor shall not require subclassing of any class
japod@6
    45
     * or interface on the client side.
japod@6
    46
     * 
japod@6
    47
     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
japod@6
    48
     */
japod@6
    49
    public static Convertor<Integer, String> createSKKtoCZK() {
japod@6
    50
        return Convertor.getConvertorIntegerString(
japod@6
    51
                CurrencyValue.getCurrencyValue(100, "SKK"),
japod@6
    52
                CurrencyValue.getCurrencyValue(80, "CZK")
japod@6
    53
        );
japod@6
    54
    }
japod@6
    55
    
japod@6
    56
    /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
japod@6
    57
     * with it.
japod@6
    58
     */
japod@6
    59
    public void testCurrencyCZKUSD() throws Exception {
japod@6
    60
        Convertor<Integer, String> c = createCZKtoUSD();
japod@6
    61
        
japod@6
    62
        CurrencyValue<Integer, String> result;
japod@6
    63
        
japod@6
    64
        // convert $5 to CZK using c:
japod@6
    65
        // assertEquals("Result is 85 CZK");
japod@6
    66
        result = c.convert(CurrencyValue.getCurrencyValue(5, "USD"));
japod@6
    67
        assertEquals(CurrencyValue.getCurrencyValue(85, "CZK"), result);
japod@6
    68
japod@6
    69
        // convert $8 to CZK
japod@6
    70
        // assertEquals("Result is 136 CZK");
japod@6
    71
        result = c.convert(CurrencyValue.getCurrencyValue(8, "USD"));
japod@6
    72
        assertEquals(CurrencyValue.getCurrencyValue(136, "CZK"), result);
japod@6
    73
japod@6
    74
        // convert 1003CZK to USD
japod@6
    75
        // assertEquals("Result is 59 USD");
japod@6
    76
        result = c.convert(CurrencyValue.getCurrencyValue(1003, "CZK"));
japod@6
    77
        assertEquals(CurrencyValue.getCurrencyValue(59, "USD"), result);
japod@6
    78
    }
japod@6
    79
japod@6
    80
    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
japod@6
    81
     * with it.
japod@6
    82
     */
japod@6
    83
    public void testCurrencySKKCZK() throws Exception {
japod@6
    84
        Convertor<Integer, String> c = createSKKtoCZK();
japod@6
    85
        
japod@6
    86
        CurrencyValue<Integer, String> result;
japod@6
    87
        
japod@6
    88
        // convert 16CZK using c:
japod@6
    89
        // assertEquals("Result is 20 SKK");
japod@6
    90
        result = c.convert(CurrencyValue.getCurrencyValue(16, "CZK"));
japod@6
    91
        assertEquals(CurrencyValue.getCurrencyValue(20, "SKK"), result);
japod@6
    92
japod@6
    93
        // convert 500SKK to CZK
japod@6
    94
        // assertEquals("Result is 400 CZK");
japod@6
    95
        result = c.convert(CurrencyValue.getCurrencyValue(500, "SKK"));
japod@6
    96
        assertEquals(CurrencyValue.getCurrencyValue(400, "CZK"), result);
japod@6
    97
    }
japod@6
    98
}
japod@6
    99