task2/solution08/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/solution08/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 java.util.Currency;
japod@6
     4
import junit.framework.TestCase;
japod@6
     5
import org.apidesign.apifest08.currency.Convertor;
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 createCZKtoUSD() {
japod@6
    35
        return Convertor.getInstanceFor(Currency.getInstance("CZK"), Currency.getInstance("USD"));
japod@6
    36
    }
japod@6
    37
japod@6
    38
    /** Create convertor that understands two currencies, CZK and
japod@6
    39
     *  SKK. Make 100 SKK == 80 CZK.
japod@6
    40
     *
japod@6
    41
     * Creation of the convertor shall not require subclassing of any class
japod@6
    42
     * or interface on the client side.
japod@6
    43
     * 
japod@6
    44
     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
japod@6
    45
     */
japod@6
    46
    public static Convertor createSKKtoCZK() {
japod@6
    47
        return Convertor.getInstanceFor(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
japod@6
    48
    }
japod@6
    49
    
japod@6
    50
    /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
japod@6
    51
     * with it.
japod@6
    52
     */
japod@6
    53
    public void testCurrencyCZKUSD() throws Exception {
japod@6
    54
        Convertor c = createCZKtoUSD();
japod@6
    55
        Currency usd = Currency.getInstance("USD");
japod@6
    56
        Currency czk = Currency.getInstance("CZK");
japod@6
    57
        // convert $5 to CZK using c:
japod@6
    58
        // assertEquals("Result is 85 CZK");
japod@6
    59
        assertEquals(85F, c.convert(5, usd));
japod@6
    60
japod@6
    61
        // convert $8 to CZK
japod@6
    62
        // assertEquals("Result is 136 CZK");
japod@6
    63
        assertEquals(136F, c.convert(8, usd));
japod@6
    64
japod@6
    65
        // convert 1003CZK to USD
japod@6
    66
        // assertEquals("Result is 59 USD");
japod@6
    67
        assertEquals(59F, c.convert(1003, czk));
japod@6
    68
    }
japod@6
    69
japod@6
    70
    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
japod@6
    71
     * with it.
japod@6
    72
     */
japod@6
    73
    public void testCurrencySKKCZK() throws Exception {
japod@6
    74
        Convertor c = createSKKtoCZK();
japod@6
    75
        Currency skk = Currency.getInstance("SKK");
japod@6
    76
        Currency czk = Currency.getInstance("CZK");
japod@6
    77
        // convert 16CZK using c:
japod@6
    78
        // assertEquals("Result is 20 SKK");
japod@6
    79
        assertEquals(20F, c.convert(16, skk));
japod@6
    80
japod@6
    81
        // convert 500SKK to CZK
japod@6
    82
        // assertEquals("Result is 400 CZK");
japod@6
    83
        assertEquals(400F, c.convert(500, czk));
japod@6
    84
    }
japod@6
    85
}
japod@6
    86