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