task3/solution04/test/org/apidesign/apifest08/test/Task3Test.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 11 Oct 2008 10:49:25 +0200
changeset 59 c1d43bc1e9c0
parent 55 14e78f48ac2b
permissions -rw-r--r--
Removing all 'implement me' messages
     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         Convertor c = createOnlineCZKUSDConvertor();
    94         doFewQueriesForOnlineConvertor(c);
    95     }
    96 
    97     static void doFewQueriesForOnlineConvertor(Convertor c) 
    98         throws InvalidConversionException            
    99     {
   100         BigDecimal amount;
   101 
   102         // convert $5 to CZK using c:
   103         //assertEquals("Result is 80 CZK");
   104         amount = c.convert(USD, CZK, new BigDecimal("5.00"));
   105         assertEquals(new BigDecimal("80.00"), amount);
   106 
   107         // convert $8 to CZK using c:
   108         //assertEquals("Result is 127.92 CZK");
   109         amount = c.convert(USD, CZK, new BigDecimal("8.00"));
   110         assertEquals(new BigDecimal("127.92"), amount);
   111 
   112         // convert $1 to CZK using c:
   113         //assertEquals("Result is 15.98 CZK");
   114         amount = c.convert(USD, CZK, new BigDecimal("1.00"));
   115         assertEquals(new BigDecimal("15.98"), amount);
   116 
   117         // convert 15.97CZK to USD using c:
   118         //assertEquals("Result is 1$");
   119         amount = c.convert(CZK, USD, new BigDecimal("15.97"));
   120         assertEquals(new BigDecimal("1.00"), amount);
   121     }
   122 
   123     /** Join the convertors and show they behave sane.
   124      */
   125     public void testOnlineConvertorComposition() throws Exception {
   126         BigDecimal amount;
   127         
   128         Convertor c = Task2Test.merge(
   129             createOnlineCZKUSDConvertor(),
   130             Task1Test.createSKKtoCZK()
   131         );
   132 
   133         // convert 16CZK to SKK using c:
   134         // assertEquals("Result is 20 SKK");
   135         amount = c.convert(CZK, SKK, new BigDecimal("16.00"));
   136         assertEquals(new BigDecimal("20.00"), amount);
   137         
   138         // convert 500SKK to CZK using c:
   139         // assertEquals("Result is 400 CZK");
   140         amount = c.convert(SKK, CZK, new BigDecimal("500.00"));
   141         assertEquals(new BigDecimal("400.00"), amount);
   142 
   143         doFewQueriesForOnlineConvertor(c);
   144     }
   145 }