currency/test/org/apidesign/apifest08/test/Task2Test.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 01 Oct 2008 11:23:11 +0200
changeset 32 2198184978d5
permissions -rw-r--r--
Task2
     1 package org.apidesign.apifest08.test;
     2 
     3 import junit.framework.TestCase;
     4 import org.apidesign.apifest08.currency.Convertor;
     5 
     6 /** There are many currencies around the world and many banks manipulate
     7  * with more than one or two at the same time. As banks are usually the
     8  * best paying clients, which is true even in case of your Convertor API,
     9  * it is reasonable to listen to their requests.
    10  * <p>
    11  * The quest for today is to enhance your existing convertor API to hold
    12  * information about many currencies and allow conversions between any of them.
    13  * Also, as conversion rates for diferent currencies usually arise from various
    14  * bank departments, there is another important need. There is a need to
    15  * compose two convertors into one by merging all the information about
    16  * currencies they know about.
    17  */
    18 public class Task2Test extends TestCase {
    19     public Task2Test(String testName) {
    20         super(testName);
    21     }
    22 
    23     @Override
    24     protected void setUp() throws Exception {
    25     }
    26 
    27     @Override
    28     protected void tearDown() throws Exception {
    29     }
    30 
    31     // As in Task1Test, keep in mind, that there are three parts
    32     // of the whole system:
    33     // 1. there is someone who knows the current exchange rate
    34     // 2. there is someone who wants to do the conversion
    35     // 3. there is the API between 1. and 2. which allows them to communicate
    36     // 
    37     // Please backward compatibly enhance your existing API to support following
    38     // usecases:
    39     //
    40     
    41     /** Create convertor that understands two currencies, CZK and
    42      *  SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
    43      *  knows the exchange rate, and needs to use the API to create objects
    44      *  with the exchange rate. Anyone shall be ready to call this method without
    45      *  any other method being called previously. The API itself shall know
    46      *  nothing about any rates, before this method is called.
    47      */
    48     public static Convertor createTripleConvertor() {
    49         // Rates: 1USD = 15CZK
    50         // Rates: 1USD = 20SKK
    51         // Rates: 75CZK = 100SKK
    52         return null;
    53     }
    54 
    55     /** Define convertor that understands three currencies. Use it.
    56      */
    57     public void testConvertorForUSDandCZKandSKK() throws Exception {
    58         Convertor c = createTripleConvertor();
    59 
    60         // convert $5 to CZK using c:
    61         // assertEquals("Result is 75 CZK");
    62 
    63         // convert $5 to SKK using c:
    64         // assertEquals("Result is 100 SKK");
    65 
    66         // convert 200SKK to CZK using c:
    67         // assertEquals("Result is 150 CZK");
    68 
    69         // convert 200SKK to USK using c:
    70         // assertEquals("Result is 10 USD");
    71     }
    72 
    73     /** Merge all currency rates of convertor 1 with convertor 2.
    74      * Implement this using your API, preferably this method just delegates
    75      * into some API method which does the actual work, without requiring
    76      * API clients to code anything complex.
    77      */
    78     public static Convertor merge(Convertor one, Convertor two) {
    79         return null;
    80     }
    81 
    82     /** Join the convertors from previous task, Task1Test and show that it
    83      * can be used to do reasonable conversions.
    84      */
    85     public void testConvertorComposition() throws Exception {
    86         Convertor c = merge(
    87             Task1Test.createCZKtoUSD(),
    88             Task1Test.createSKKtoCZK()
    89         );
    90 
    91         // convert $5 to CZK using c:
    92         // assertEquals("Result is 85 CZK");
    93 
    94         // convert $8 to CZK using c:
    95         // assertEquals("Result is 136 CZK");
    96 
    97         // convert 1003CZK to USD using c:
    98         // assertEquals("Result is 59 USD");
    99 
   100         // convert 16CZK using c:
   101         // assertEquals("Result is 20 SKK");
   102 
   103         // convert 500SKK to CZK using c:
   104         // assertEquals("Result is 400 CZK");
   105 
   106     }
   107 }