task4/solution12/test/org/apidesign/apifest08/test/Task3Test.java
changeset 61 58ec6da75f6f
parent 59 c1d43bc1e9c0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution12/test/org/apidesign/apifest08/test/Task3Test.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,145 @@
     1.4 +package org.apidesign.apifest08.test;
     1.5 +
     1.6 +import java.util.Currency;
     1.7 +
     1.8 +import junit.framework.TestCase;
     1.9 +import org.apidesign.apifest08.currency.Convertor;
    1.10 +import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
    1.11 +
    1.12 +/** The exchange rates are not always the same. They are changing. Day by day,
    1.13 + * hour by hour, minute by minute. For every bank it is important to always
    1.14 + * have the actual exchange rate available in the system. That is why let's
    1.15 + * create a pluggable convertor that will always have up to date value of its
    1.16 + * exchange rate.
    1.17 + * <p>
    1.18 + * The quest for today is to allow 3rd party developer to write a convertor
    1.19 + * that adjusts its exchange rate everytime it is queried. This convertor is
    1.20 + * written by independent vendor, the vendor knows only your Convertor API,
    1.21 + * he does not know how the whole system looks and how the convertor is supposed
    1.22 + * to be used.
    1.23 + */
    1.24 +public class Task3Test extends TestCase {
    1.25 +  
    1.26 +    private static double actualRate;
    1.27 +    private static boolean increasing;
    1.28 +    
    1.29 +    private static final double EXCHANGE_RATE_MAX = 16.0;
    1.30 +    private static final double EXCHANGE_RATE_MIN = 15.0;
    1.31 +  
    1.32 +    public Task3Test(String testName) {
    1.33 +        super(testName);
    1.34 +    }
    1.35 +
    1.36 +    @Override
    1.37 +    protected void setUp() throws Exception {
    1.38 +    }
    1.39 +
    1.40 +    @Override
    1.41 +    protected void tearDown() throws Exception {
    1.42 +    }
    1.43 +
    1.44 +    // Backward compatibly enhance your existing API to support following
    1.45 +    // usecases:
    1.46 +    //
    1.47 +
    1.48 +
    1.49 +    /** Without knowing anything about the surrounding system, write an
    1.50 +     * implementation of convertor that will return different rates everytime
    1.51 +     * it is queried. Convert USD to CZK and vice versa. Start with the rate of
    1.52 +     * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
    1.53 +     * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
    1.54 +     * until you reach 1USD = 16CZK
    1.55 +     *
    1.56 +     * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
    1.57 +     */
    1.58 +    public static Convertor createOnlineCZKUSDConvertor() {
    1.59 +      actualRate = 16.01;
    1.60 +      increasing = false;
    1.61 +      
    1.62 +      // sets actual exchange rates
    1.63 +      setRates();
    1.64 +      
    1.65 +      // create new instance
    1.66 +      Convertor convertor = null;
    1.67 +      try {
    1.68 +        convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("CZK"));
    1.69 +      } catch (UnknownConvertorException e) {
    1.70 +        e.printStackTrace();
    1.71 +      }
    1.72 +
    1.73 +      return convertor;
    1.74 +    }
    1.75 +
    1.76 +    public void testFewQueriesForOnlineConvertor() throws Exception {
    1.77 +        Convertor c = createOnlineCZKUSDConvertor();
    1.78 +        doFewQueriesForOnlineConvertor(c);
    1.79 +    }
    1.80 +
    1.81 +    static void doFewQueriesForOnlineConvertor(Convertor c) throws Exception {
    1.82 +        // convert $5 to CZK using c:
    1.83 +        double result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    1.84 +        double expectedResult = actualRate * 5;
    1.85 +        assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result);
    1.86 +
    1.87 +        // change exchange rates
    1.88 +        setRates();
    1.89 +        
    1.90 +        // convert $8 to CZK using c:
    1.91 +        result = c.convert(8d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    1.92 +        expectedResult = actualRate * 8;
    1.93 +        assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result);
    1.94 +        
    1.95 +        // change exchange rates
    1.96 +        setRates();
    1.97 +
    1.98 +        // convert $1 to CZK using c:
    1.99 +        result = c.convert(1d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
   1.100 +        expectedResult = actualRate * 1;
   1.101 +        assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result);
   1.102 +        
   1.103 +        // change exchange rates
   1.104 +        setRates();
   1.105 +
   1.106 +        // convert 15.97CZK to USD using c:
   1.107 +        result = c.convert(15.97d, Currency.getInstance("CZK"), Currency.getInstance("USD"));
   1.108 +        expectedResult = 15.97 / actualRate ;
   1.109 +        assertEquals("Result is not " + expectedResult + " USD", expectedResult, result);
   1.110 +    }
   1.111 +
   1.112 +    /** Join the convertors and show they behave sane.
   1.113 +     */
   1.114 +    public void testOnlineConvertorComposition() throws Exception {
   1.115 +        Convertor c = Task2Test.merge(
   1.116 +            createOnlineCZKUSDConvertor(),
   1.117 +            Task1Test.createSKKtoCZK()
   1.118 +        );
   1.119 +
   1.120 +        // convert 16CZK to SKK using c:
   1.121 +        double result = c.convert(16d, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
   1.122 +        assertEquals("Result is not 20 SKK", 20d, result);
   1.123 +
   1.124 +        // convert 500SKK to CZK using c:
   1.125 +        result = c.convert(500d, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   1.126 +        assertEquals("Result is not 400 CZK", 400d, result);
   1.127 +
   1.128 +        doFewQueriesForOnlineConvertor(c);
   1.129 +    }
   1.130 +    
   1.131 +    private static void setRates() {
   1.132 +      // logic for change of actual exchange rate
   1.133 +      if(increasing) {
   1.134 +        actualRate += 0.01;
   1.135 +        if(actualRate == EXCHANGE_RATE_MAX){
   1.136 +          increasing = false;
   1.137 +        }
   1.138 +      } else {
   1.139 +        actualRate -= 0.01;
   1.140 +        if(actualRate == EXCHANGE_RATE_MIN){
   1.141 +          increasing = true;
   1.142 +        }
   1.143 +      }
   1.144 +      
   1.145 +      // set exchange rates
   1.146 +      Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), actualRate, 1d);
   1.147 +    }
   1.148 +}