task3/solution04/test/org/apidesign/apifest08/test/Task3Test.java
author japod@localhost
Fri, 10 Oct 2008 22:05:15 +0200
changeset 55 14e78f48ac2b
parent 48 79a576394dd7
child 59 c1d43bc1e9c0
permissions -rw-r--r--
solution04 task3
     1 package org.apidesign.apifest08.test;
     2 
     3 import java.math.BigDecimal;
     4 import java.util.Currency;
     5 import junit.framework.TestCase;
     6 import org.apidesign.apifest08.currency.Convertor;
     7 import org.apidesign.apifest08.currency.InvalidConversionException;
     8 import org.apidesign.apifest08.currency.OnlineConvertor;
     9 
    10 /** The exchange rates are not always the same. They are changing. Day by day,
    11  * hour by hour, minute by minute. For every bank it is important to always
    12  * have the actual exchange rate available in the system. That is why let's
    13  * create a pluggable convertor that will always have up to date value of its
    14  * exchange rate.
    15  * <p>
    16  * The quest for today is to allow 3rd party developer to write a convertor
    17  * that adjusts its exchange rate everytime it is queried. This convertor is
    18  * written by independent vendor, the vendor knows only your Convertor API,
    19  * he does not know how the whole system looks and how the convertor is supposed
    20  * to be used.
    21  */
    22 public class Task3Test 
    23     extends TestCase
    24 {
    25     private final static Currency CZK;
    26     private final static Currency SKK;
    27     private final static Currency USD;
    28 
    29     static
    30     {
    31         CZK = Currency.getInstance("CZK");
    32         SKK = Currency.getInstance("SKK");
    33         USD = Currency.getInstance("USD");
    34     }
    35     
    36     public Task3Test(String testName)
    37     {
    38         super(testName);
    39     }
    40 
    41     @Override
    42     protected void setUp() throws Exception
    43     {
    44     }
    45 
    46     @Override
    47     protected void tearDown() throws Exception
    48     {
    49     }
    50 
    51     // Backward compatibly enhance your existing API to support following
    52     // usecases:
    53     //
    54 
    55 
    56     /** Without knowing anything about the surrounding system, write an
    57      * implementation of convertor that will return different rates everytime
    58      * it is queried. Convert USD to CZK and vice versa. Start with the rate of
    59      * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
    60      * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
    61      * until you reach 1USD = 16CZK
    62      *
    63      * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
    64      * @throws InvalidConversionException 
    65      */
    66     public static Convertor createOnlineCZKUSDConvertor() 
    67         throws InvalidConversionException
    68     {
    69         final Convertor convertor;
    70         
    71         convertor = new OnlineConvertor(USD,
    72                                         CZK,
    73                                         new TestExchangeRateFinder(new BigDecimal("15.00"), 
    74                                                                    new BigDecimal("16.00"),
    75                                                                    new BigDecimal("16.00"),
    76                                                                    new BigDecimal("0.01"),
    77                                                                    new BigDecimal("-0.01")));
    78 
    79         // initial rate: 1USD = 16CZK
    80         // 2nd query 1USD = 15.99CZK
    81         // 3rd query 1USD = 15.98CZK
    82         // until 1USD = 15.00CZK
    83         // then 1USD = 15.01CZK
    84         // then 1USD = 15.02CZK
    85         // and so on and on up to 1USD = 16CZK
    86         // and then another round to 15, etc.
    87         return convertor;
    88     }
    89 
    90     public void testFewQueriesForOnlineConvertor() 
    91         throws InvalidConversionException
    92     {
    93         if (Boolean.getBoolean("ignore.failing")) {
    94             // implement me!
    95             return;
    96         }
    97 
    98         Convertor c = createOnlineCZKUSDConvertor();
    99         doFewQueriesForOnlineConvertor(c);
   100     }
   101 
   102     static void doFewQueriesForOnlineConvertor(Convertor c) 
   103         throws InvalidConversionException            
   104     {
   105         BigDecimal amount;
   106 
   107         // convert $5 to CZK using c:
   108         //assertEquals("Result is 80 CZK");
   109         amount = c.convert(USD, CZK, new BigDecimal("5.00"));
   110         assertEquals(new BigDecimal("80.00"), amount);
   111 
   112         // convert $8 to CZK using c:
   113         //assertEquals("Result is 127.92 CZK");
   114         amount = c.convert(USD, CZK, new BigDecimal("8.00"));
   115         assertEquals(new BigDecimal("127.92"), amount);
   116 
   117         // convert $1 to CZK using c:
   118         //assertEquals("Result is 15.98 CZK");
   119         amount = c.convert(USD, CZK, new BigDecimal("1.00"));
   120         assertEquals(new BigDecimal("15.98"), amount);
   121 
   122         // convert 15.97CZK to USD using c:
   123         //assertEquals("Result is 1$");
   124         amount = c.convert(CZK, USD, new BigDecimal("15.97"));
   125         assertEquals(new BigDecimal("1.00"), amount);
   126     }
   127 
   128     /** Join the convertors and show they behave sane.
   129      */
   130     public void testOnlineConvertorComposition() throws Exception {
   131         BigDecimal amount;
   132         
   133         if (Boolean.getBoolean("ignore.failing")) {
   134             // implement me!
   135             return;
   136         }
   137         
   138         Convertor c = Task2Test.merge(
   139             createOnlineCZKUSDConvertor(),
   140             Task1Test.createSKKtoCZK()
   141         );
   142 
   143         // convert 16CZK to SKK using c:
   144         // assertEquals("Result is 20 SKK");
   145         amount = c.convert(CZK, SKK, new BigDecimal("16.00"));
   146         assertEquals(new BigDecimal("20.00"), amount);
   147         
   148         // convert 500SKK to CZK using c:
   149         // assertEquals("Result is 400 CZK");
   150         amount = c.convert(SKK, CZK, new BigDecimal("500.00"));
   151         assertEquals(new BigDecimal("400.00"), amount);
   152 
   153         doFewQueriesForOnlineConvertor(c);
   154     }
   155 }