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