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