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