task3/solution12/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 51 221f1930cbfb
permissions -rw-r--r--
Removing all 'implement me' messages
     1 package org.apidesign.apifest08.test;
     2 
     3 import java.util.Currency;
     4 
     5 import junit.framework.TestCase;
     6 import org.apidesign.apifest08.currency.Convertor;
     7 import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
     8 
     9 /** The exchange rates are not always the same. They are changing. Day by day,
    10  * hour by hour, minute by minute. For every bank it is important to always
    11  * have the actual exchange rate available in the system. That is why let's
    12  * create a pluggable convertor that will always have up to date value of its
    13  * exchange rate.
    14  * <p>
    15  * The quest for today is to allow 3rd party developer to write a convertor
    16  * that adjusts its exchange rate everytime it is queried. This convertor is
    17  * written by independent vendor, the vendor knows only your Convertor API,
    18  * he does not know how the whole system looks and how the convertor is supposed
    19  * to be used.
    20  */
    21 public class Task3Test extends TestCase {
    22   
    23     private static double actualRate;
    24     private static boolean increasing;
    25     
    26     private static final double EXCHANGE_RATE_MAX = 16.0;
    27     private static final double EXCHANGE_RATE_MIN = 15.0;
    28   
    29     public Task3Test(String testName) {
    30         super(testName);
    31     }
    32 
    33     @Override
    34     protected void setUp() throws Exception {
    35     }
    36 
    37     @Override
    38     protected void tearDown() throws Exception {
    39     }
    40 
    41     // Backward compatibly enhance your existing API to support following
    42     // usecases:
    43     //
    44 
    45 
    46     /** Without knowing anything about the surrounding system, write an
    47      * implementation of convertor that will return different rates everytime
    48      * it is queried. Convert USD to CZK and vice versa. Start with the rate of
    49      * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
    50      * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
    51      * until you reach 1USD = 16CZK
    52      *
    53      * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
    54      */
    55     public static Convertor createOnlineCZKUSDConvertor() {
    56       actualRate = 16.01;
    57       increasing = false;
    58       
    59       // sets actual exchange rates
    60       setRates();
    61       
    62       // create new instance
    63       Convertor convertor = null;
    64       try {
    65         convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("CZK"));
    66       } catch (UnknownConvertorException e) {
    67         e.printStackTrace();
    68       }
    69 
    70       return convertor;
    71     }
    72 
    73     public void testFewQueriesForOnlineConvertor() throws Exception {
    74         Convertor c = createOnlineCZKUSDConvertor();
    75         doFewQueriesForOnlineConvertor(c);
    76     }
    77 
    78     static void doFewQueriesForOnlineConvertor(Convertor c) throws Exception {
    79         // convert $5 to CZK using c:
    80         double result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    81         double expectedResult = actualRate * 5;
    82         assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result);
    83 
    84         // change exchange rates
    85         setRates();
    86         
    87         // convert $8 to CZK using c:
    88         result = c.convert(8d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    89         expectedResult = actualRate * 8;
    90         assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result);
    91         
    92         // change exchange rates
    93         setRates();
    94 
    95         // convert $1 to CZK using c:
    96         result = c.convert(1d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    97         expectedResult = actualRate * 1;
    98         assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result);
    99         
   100         // change exchange rates
   101         setRates();
   102 
   103         // convert 15.97CZK to USD using c:
   104         result = c.convert(15.97d, Currency.getInstance("CZK"), Currency.getInstance("USD"));
   105         expectedResult = 15.97 / actualRate ;
   106         assertEquals("Result is not " + expectedResult + " USD", expectedResult, result);
   107     }
   108 
   109     /** Join the convertors and show they behave sane.
   110      */
   111     public void testOnlineConvertorComposition() throws Exception {
   112         Convertor c = Task2Test.merge(
   113             createOnlineCZKUSDConvertor(),
   114             Task1Test.createSKKtoCZK()
   115         );
   116 
   117         // convert 16CZK to SKK using c:
   118         double result = c.convert(16d, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
   119         assertEquals("Result is not 20 SKK", 20d, result);
   120 
   121         // convert 500SKK to CZK using c:
   122         result = c.convert(500d, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   123         assertEquals("Result is not 400 CZK", 400d, result);
   124 
   125         doFewQueriesForOnlineConvertor(c);
   126     }
   127     
   128     private static void setRates() {
   129       // logic for change of actual exchange rate
   130       if(increasing) {
   131         actualRate += 0.01;
   132         if(actualRate == EXCHANGE_RATE_MAX){
   133           increasing = false;
   134         }
   135       } else {
   136         actualRate -= 0.01;
   137         if(actualRate == EXCHANGE_RATE_MIN){
   138           increasing = true;
   139         }
   140       }
   141       
   142       // set exchange rates
   143       Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), actualRate, 1d);
   144     }
   145 }