task2/solution03/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/solution03/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.Convertor;
     5 
     6 /** Finish the Convertor API, and then write bodies of methods inside
     7  * of this class to match the given tasks. To fullfil your task, use the
     8  * API define in the <code>org.apidesign.apifest08.currency</code> package.
     9  * Do not you reflection, or other hacks as your code
    10  * shall run without any runtime permissions.
    11  */
    12 public class Task1Test extends TestCase {
    13 
    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 new Convertor(17, 1);
    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 new Convertor(100, 80);
    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         // convert $5 to CZK using c:
    56         // assertEquals("Result is 85 CZK");
    57         assertEquals(c.convert(5, Convertor.SECOND_TO_FIRST), (double) 85);
    58 
    59         // convert $8 to CZK
    60         // assertEquals("Result is 136 CZK");
    61         assertEquals(c.convert(8, Convertor.SECOND_TO_FIRST), (double) 136);
    62 
    63         // convert 1003CZK to USD
    64         // assertEquals("Result is 59 USD");
    65         assertEquals(c.convert(1003, Convertor.FIRST_TO_SECOND), (double) 59);
    66     }
    67 
    68     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    69      * with it.
    70      */
    71     public void testCurrencySKKCZK() throws Exception {
    72         Convertor c = createSKKtoCZK();
    73         // convert 16CZK using c:
    74         // assertEquals("Result is 20 SKK");
    75         assertEquals(c.convert(16, Convertor.SECOND_TO_FIRST), (double) 20);
    76 
    77         // convert 500SKK to CZK
    78         // assertEquals("Result is 400 CZK");
    79         assertEquals(c.convert(500, Convertor.FIRST_TO_SECOND), (double) 400);
    80     }
    81 }
    82