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
     1 package org.apidesign.apifest08.test;
     2 
     3 import junit.framework.TestCase;
     4 import org.apidesign.apifest08.currency.Convertor;
     5 import org.apidesign.apifest08.currency.CurrencyValue;
     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<Integer, String> createCZKtoUSD() {
    35         return Convertor.getConvertorIntegerString(
    36                 CurrencyValue.getCurrencyValue(1, "USD"),
    37                 CurrencyValue.getCurrencyValue(17, "CZK")
    38         );
    39     }
    40 
    41     /** Create convertor that understands two currencies, CZK and
    42      *  SKK. Make 100 SKK == 80 CZK.
    43      *
    44      * Creation of the convertor shall not require subclassing of any class
    45      * or interface on the client side.
    46      * 
    47      * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    48      */
    49     public static Convertor<Integer, String> createSKKtoCZK() {
    50         return Convertor.getConvertorIntegerString(
    51                 CurrencyValue.getCurrencyValue(100, "SKK"),
    52                 CurrencyValue.getCurrencyValue(80, "CZK")
    53         );
    54     }
    55     
    56     /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    57      * with it.
    58      */
    59     public void testCurrencyCZKUSD() throws Exception {
    60         Convertor<Integer, String> c = createCZKtoUSD();
    61         
    62         CurrencyValue<Integer, String> result;
    63         
    64         // convert $5 to CZK using c:
    65         // assertEquals("Result is 85 CZK");
    66         result = c.convert(CurrencyValue.getCurrencyValue(5, "USD"));
    67         assertEquals(CurrencyValue.getCurrencyValue(85, "CZK"), result);
    68 
    69         // convert $8 to CZK
    70         // assertEquals("Result is 136 CZK");
    71         result = c.convert(CurrencyValue.getCurrencyValue(8, "USD"));
    72         assertEquals(CurrencyValue.getCurrencyValue(136, "CZK"), result);
    73 
    74         // convert 1003CZK to USD
    75         // assertEquals("Result is 59 USD");
    76         result = c.convert(CurrencyValue.getCurrencyValue(1003, "CZK"));
    77         assertEquals(CurrencyValue.getCurrencyValue(59, "USD"), result);
    78     }
    79 
    80     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    81      * with it.
    82      */
    83     public void testCurrencySKKCZK() throws Exception {
    84         Convertor<Integer, String> c = createSKKtoCZK();
    85         
    86         CurrencyValue<Integer, String> result;
    87         
    88         // convert 16CZK using c:
    89         // assertEquals("Result is 20 SKK");
    90         result = c.convert(CurrencyValue.getCurrencyValue(16, "CZK"));
    91         assertEquals(CurrencyValue.getCurrencyValue(20, "SKK"), result);
    92 
    93         // convert 500SKK to CZK
    94         // assertEquals("Result is 400 CZK");
    95         result = c.convert(CurrencyValue.getCurrencyValue(500, "SKK"));
    96         assertEquals(CurrencyValue.getCurrencyValue(400, "CZK"), result);
    97     }
    98 }
    99