task3/solution14/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 54 1b300c79f4ce
permissions -rw-r--r--
Removing all 'implement me' messages
jaroslav@43
     1
package org.apidesign.apifest08.test;
jaroslav@43
     2
jaroslav@43
     3
import junit.framework.TestCase;
jaroslav@43
     4
import org.apidesign.apifest08.currency.Convertor;
japod@54
     5
import org.apidesign.apifest08.currency.ConvertorFactory;
japod@54
     6
import org.apidesign.apifest08.currency.CurrencyRate;
japod@54
     7
import org.apidesign.apifest08.currency.Rate;
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 {
jaroslav@43
    22
    public Task3Test(String testName) {
jaroslav@43
    23
        super(testName);
jaroslav@43
    24
    }
jaroslav@43
    25
jaroslav@43
    26
    @Override
jaroslav@43
    27
    protected void setUp() throws Exception {
jaroslav@43
    28
    }
jaroslav@43
    29
jaroslav@43
    30
    @Override
jaroslav@43
    31
    protected void tearDown() throws Exception {
jaroslav@43
    32
    }
jaroslav@43
    33
jaroslav@43
    34
    // Backward compatibly enhance your existing API to support following
jaroslav@43
    35
    // usecases:
jaroslav@43
    36
    //
jaroslav@43
    37
jaroslav@43
    38
jaroslav@43
    39
    /** Without knowing anything about the surrounding system, write an
jaroslav@43
    40
     * implementation of convertor that will return different rates everytime
jaroslav@43
    41
     * it is queried. Convert USD to CZK and vice versa. Start with the rate of
jaroslav@43
    42
     * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
jaroslav@43
    43
     * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
jaroslav@43
    44
     * until you reach 1USD = 16CZK
jaroslav@43
    45
     *
jaroslav@43
    46
     * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
jaroslav@43
    47
     */
jaroslav@43
    48
    public static Convertor createOnlineCZKUSDConvertor() {
jaroslav@43
    49
        // initial rate: 1USD = 16CZK
jaroslav@43
    50
        // 2nd query 1USD = 15.99CZK
jaroslav@43
    51
        // 3rd query 1USD = 15.98CZK
jaroslav@43
    52
        // until 1USD = 15.00CZK
jaroslav@43
    53
        // then 1USD = 15.01CZK
jaroslav@43
    54
        // then 1USD = 15.02CZK
jaroslav@43
    55
        // and so on and on up to 1USD = 16CZK
jaroslav@43
    56
        // and then another round to 15, etc.
japod@54
    57
        CurrencyRate onlineCurrencyRate = new CurrencyRate() {
japod@54
    58
            private int usdAmount = 100;
japod@54
    59
            private int czkAmount = 1600;
japod@54
    60
            private boolean up = false;
japod@54
    61
            public String getCurrency1() {
japod@54
    62
                return "USD";
japod@54
    63
            }
japod@54
    64
japod@54
    65
            public String getCurrency2() {
japod@54
    66
                return "CZK";
japod@54
    67
            }
japod@54
    68
japod@54
    69
            public Rate getRate() { //return Rate according to online status
japod@54
    70
                Rate rate = new Rate(usdAmount, czkAmount);
japod@54
    71
                if (up) {
japod@54
    72
                    if (czkAmount < 1600) {
japod@54
    73
                        czkAmount++;
japod@54
    74
                    } else {
japod@54
    75
                        up = false;
japod@54
    76
                        czkAmount--;
japod@54
    77
                    }
japod@54
    78
                } else { //down
japod@54
    79
                    if (czkAmount > 1500) {
japod@54
    80
                        czkAmount--;
japod@54
    81
                    } else {
japod@54
    82
                        up = true;
japod@54
    83
                        czkAmount++;
japod@54
    84
                    }
japod@54
    85
                }
japod@54
    86
                return rate;
japod@54
    87
            }
japod@54
    88
        };
japod@54
    89
japod@54
    90
        return ConvertorFactory.newInstance().createConvertor(onlineCurrencyRate);
jaroslav@43
    91
    }
jaroslav@43
    92
jaroslav@43
    93
    public void testFewQueriesForOnlineConvertor() {
jaroslav@43
    94
        Convertor c = createOnlineCZKUSDConvertor();
jaroslav@43
    95
        doFewQueriesForOnlineConvertor(c);
jaroslav@43
    96
    }
jaroslav@43
    97
jaroslav@43
    98
    static void doFewQueriesForOnlineConvertor(Convertor c) {
jaroslav@43
    99
        // convert $5 to CZK using c:
jaroslav@43
   100
        //assertEquals("Result is 80 CZK");
japod@54
   101
        assertEquals("Result is 80 CZK", 80.0, c.convert("USD", "CZK", 5), 0.001);
jaroslav@43
   102
jaroslav@43
   103
        // convert $8 to CZK using c:
jaroslav@43
   104
        //assertEquals("Result is 127.92 CZK");
japod@54
   105
        assertEquals("Result is 127.92 CZK", 127.92, c.convert("USD", "CZK", 8), 0.001);
jaroslav@43
   106
jaroslav@43
   107
        // convert $1 to CZK using c:
jaroslav@43
   108
        //assertEquals("Result is 15.98 CZK");
japod@54
   109
        assertEquals("Result is 15.98 CZK", 15.98, c.convert("USD", "CZK", 1), 0.001);
jaroslav@43
   110
jaroslav@43
   111
        // convert 15.97CZK to USD using c:
jaroslav@43
   112
        //assertEquals("Result is 1$");
japod@54
   113
        assertEquals("Result is 1$", 1.0, c.convert("CZK", "USD", 15.97), 0.001);
japod@54
   114
        
jaroslav@43
   115
    }
jaroslav@43
   116
jaroslav@43
   117
    /** Join the convertors and show they behave sane.
jaroslav@43
   118
     */
jaroslav@43
   119
    public void testOnlineConvertorComposition() throws Exception {
jaroslav@43
   120
        Convertor c = Task2Test.merge(
jaroslav@43
   121
            createOnlineCZKUSDConvertor(),
jaroslav@43
   122
            Task1Test.createSKKtoCZK()
jaroslav@43
   123
        );
jaroslav@43
   124
jaroslav@43
   125
        // convert 16CZK to SKK using c:
jaroslav@43
   126
        // assertEquals("Result is 20 SKK");
japod@54
   127
        assertEquals("Result is 20 SKK", 20.0, c.convert("CZK", "SKK", 16), 0.001);
jaroslav@43
   128
jaroslav@43
   129
        // convert 500SKK to CZK using c:
jaroslav@43
   130
        // assertEquals("Result is 400 CZK");
japod@54
   131
        assertEquals("Result is 400 CZK", 400.0, c.convert("SKK", "CZK", 500), 0.001);
jaroslav@43
   132
jaroslav@43
   133
        doFewQueriesForOnlineConvertor(c);
jaroslav@43
   134
    }
jaroslav@43
   135
}