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