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