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