task4/solution06/test/org/apidesign/apifest08/test/Task2Test.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 45 task3/solution06/test/org/apidesign/apifest08/test/Task2Test.java@251d0ed461fb
permissions -rw-r--r--
Copying structure for task4
japod@39
     1
package org.apidesign.apifest08.test;
japod@39
     2
japod@39
     3
import static org.apidesign.apifest08.test.Currencies.CZK;
japod@39
     4
import static org.apidesign.apifest08.test.Currencies.USD;
japod@39
     5
import static org.apidesign.apifest08.test.Currencies.SKK;
japod@39
     6
japod@39
     7
import java.math.BigDecimal;
japod@39
     8
japod@39
     9
import junit.framework.TestCase;
japod@39
    10
import org.apidesign.apifest08.currency.Convertor;
japod@39
    11
import org.apidesign.apifest08.test.Task1Test;
japod@39
    12
japod@39
    13
/** There are many currencies around the world and many banks manipulate
japod@39
    14
 * with more than one or two at the same time. As banks are usually the
japod@39
    15
 * best paying clients, which is true even in case of your Convertor API,
japod@39
    16
 * it is reasonable to listen to their requests.
japod@39
    17
 * <p>
japod@39
    18
 * The quest for today is to enhance your existing convertor API to hold
japod@39
    19
 * information about many currencies and allow conversions between any of them.
japod@39
    20
 * Also, as conversion rates for diferent currencies usually arise from various
japod@39
    21
 * bank departments, there is another important need. There is a need to
japod@39
    22
 * compose two convertors into one by merging all the information about
japod@39
    23
 * currencies they know about.
japod@39
    24
 */
japod@39
    25
public class Task2Test extends TestCase {
japod@39
    26
    public Task2Test(String testName) {
japod@39
    27
        super(testName);
japod@39
    28
    }
japod@39
    29
japod@39
    30
    @Override
japod@39
    31
    protected void setUp() throws Exception {
japod@39
    32
    }
japod@39
    33
japod@39
    34
    @Override
japod@39
    35
    protected void tearDown() throws Exception {
japod@39
    36
    }
japod@39
    37
japod@39
    38
    // As in Task1Test, keep in mind, that there are three parts
japod@39
    39
    // of the whole system:
japod@39
    40
    // 1. there is someone who knows the current exchange rate
japod@39
    41
    // 2. there is someone who wants to do the conversion
japod@39
    42
    // 3. there is the API between 1. and 2. which allows them to communicate
japod@39
    43
    // 
japod@39
    44
    // Please backward compatibly enhance your existing API to support following
japod@39
    45
    // usecases:
japod@39
    46
    //
japod@39
    47
    
japod@39
    48
    /** Create convertor that understands two currencies, CZK and
japod@39
    49
     *  SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
japod@39
    50
     *  knows the exchange rate, and needs to use the API to create objects
japod@39
    51
     *  with the exchange rate. Anyone shall be ready to call this method without
japod@39
    52
     *  any other method being called previously. The API itself shall know
japod@39
    53
     *  nothing about any rates, before this method is called.
japod@39
    54
     */
japod@39
    55
    public static Convertor createTripleConvertor() {
japod@39
    56
        // Rates: 1USD = 15CZK
japod@39
    57
        // Rates: 1USD = 20SKK
japod@39
    58
        // Rates: 75CZK = 100SKK
japod@39
    59
    	Convertor usdCzk = new Convertor(new BigDecimal(15), USD, CZK);
japod@39
    60
    	Convertor usdSkk = new Convertor(new BigDecimal(20), USD, SKK);
japod@39
    61
    	Convertor skkCzk = new Convertor(new BigDecimal("0.75"), SKK, CZK);
japod@39
    62
        return new Convertor(new Convertor[]{usdCzk, usdSkk, skkCzk});
japod@39
    63
    }
japod@39
    64
japod@39
    65
    /** Define convertor that understands three currencies. Use it.
japod@39
    66
     */
japod@39
    67
    public void testConvertorForUSDandCZKandSKK() throws Exception {
japod@39
    68
        Convertor c = createTripleConvertor();
japod@39
    69
japod@39
    70
        // convert $5 to CZK using c:
japod@39
    71
        assertEquals("Result is 75 CZK", 75 ,c.convert(new BigDecimal(5), USD, CZK).getValue().intValue());
japod@39
    72
japod@39
    73
        // convert $5 to SKK using c:
japod@39
    74
        assertEquals("Result is 100 SKK", 100, c.convert(new BigDecimal(5), USD, SKK).getValue().intValue());
japod@39
    75
japod@39
    76
        // convert 200SKK to CZK using c:
japod@39
    77
        assertEquals("Result is 150 CZK", 150, c.convert(new BigDecimal(200), SKK, CZK).getValue().intValue());
japod@39
    78
japod@39
    79
        // convert 200SKK to USK using c:
japod@39
    80
        assertEquals("Result is 10 USD", 10, c.convert(new BigDecimal(200), SKK, USD).getValue().intValue());
japod@39
    81
    }
japod@39
    82
japod@39
    83
    /** Merge all currency rates of convertor 1 with convertor 2.
japod@39
    84
     * Implement this using your API, preferably this method just delegates
japod@39
    85
     * into some API method which does the actual work, without requiring
japod@39
    86
     * API clients to code anything complex.
japod@39
    87
     */
japod@39
    88
    public static Convertor merge(Convertor one, Convertor two) {
japod@39
    89
        return new Convertor(new Convertor[]{one, two});
japod@39
    90
    }
japod@39
    91
japod@39
    92
    /** Join the convertors from previous task, Task1Test and show that it
japod@39
    93
     * can be used to do reasonable conversions.
japod@39
    94
     */
japod@39
    95
    public void testConvertorComposition() throws Exception {
japod@39
    96
        Convertor c = merge(
japod@39
    97
            Task1Test.createCZKtoUSD(),
japod@39
    98
            Task1Test.createSKKtoCZK()
japod@39
    99
        );
japod@39
   100
japod@39
   101
        // convert $5 to CZK using c:
japod@39
   102
        assertEquals("Result is 85 CZK", 85, c.convert(new BigDecimal(5), USD, CZK).getValue().intValue());
japod@39
   103
japod@39
   104
        // convert $8 to CZK using c:
japod@39
   105
        assertEquals("Result is 136 CZK", 136, c.convert(new BigDecimal(8), USD, CZK).getValue().intValue());
japod@39
   106
japod@39
   107
        // convert 1003CZK to USD using c:
japod@39
   108
        assertEquals("Result is 59 USD", 59, c.convert(new BigDecimal(1003), CZK, USD).getValue().intValue());
japod@39
   109
japod@39
   110
        // convert 16CZK using c:
japod@39
   111
        assertEquals("Result is 20 SKK", 20, c.convert(new BigDecimal(16), CZK, SKK).getValue().intValue());
japod@39
   112
japod@39
   113
        // convert 500SKK to CZK using c:
japod@39
   114
        assertEquals("Result is 400 CZK", 400, c.convert(new BigDecimal(500), SKK, CZK).getValue().intValue());
japod@39
   115
japod@39
   116
    }
japod@39
   117
}