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