task4/solution11/test/org/apidesign/apifest08/test/Task1Test.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 53 task3/solution11/test/org/apidesign/apifest08/test/Task1Test.java@09d690bb97f6
permissions -rw-r--r--
Copying structure for task4
japod@6
     1
package org.apidesign.apifest08.test;
japod@6
     2
japod@6
     3
import junit.framework.TestCase;
japod@6
     4
import org.apidesign.apifest08.currency.Convertor;
japod@6
     5
import org.apidesign.apifest08.currency.CurrencyValue;
japod@6
     6
japod@6
     7
/** Finish the Convertor API, and then write bodies of methods inside
japod@6
     8
 * of this class to match the given tasks. To fullfil your task, use the
japod@6
     9
 * API define in the <code>org.apidesign.apifest08.currency</code> package.
japod@6
    10
 * Do not you reflection, or other hacks as your code
japod@6
    11
 * shall run without any runtime permissions.
japod@6
    12
 */
japod@6
    13
public class Task1Test extends TestCase {
japod@6
    14
    public Task1Test(String testName) {
japod@6
    15
        super(testName);
japod@6
    16
    }
japod@6
    17
japod@6
    18
    @Override
japod@6
    19
    protected void setUp() throws Exception {
japod@6
    20
    }
japod@6
    21
japod@6
    22
    @Override
japod@6
    23
    protected void tearDown() throws Exception {
japod@6
    24
    }
japod@6
    25
japod@19
    26
    //
japod@19
    27
    // Imagine that there are three parts of the whole system:
japod@19
    28
    // 1. there is someone who knows the current exchange rate
japod@19
    29
    // 2. there is someone who wants to do the conversion
japod@19
    30
    // 3. there is the API between 1. and 2. which allows them to communicate
japod@19
    31
    // Please design such API
japod@19
    32
    //
japod@19
    33
japod@6
    34
    /** Create convertor that understands two currencies, CZK and
japod@19
    35
     *  USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
japod@19
    36
     *  e.g. those that know the exchange rate. They somehow need to create
japod@19
    37
     *  the objects from the API and tell them the exchange rate. The API itself
japod@19
    38
     *  knows nothing about any rates, before the createCZKtoUSD method is called.
japod@6
    39
     *
japod@6
    40
     * Creation of the convertor shall not require subclassing of any class
japod@6
    41
     * or interface on the client side.
japod@6
    42
     *
japod@6
    43
     * @return prepared convertor ready for converting USD to CZK and CZK to USD
japod@6
    44
     */
japod@53
    45
    public static Convertor<Double, String> createCZKtoUSD() {
japod@53
    46
        return Convertor.getConvertorDoubleString(
japod@53
    47
                CurrencyValue.getCurrencyValue(1d, "USD"),
japod@53
    48
                CurrencyValue.getCurrencyValue(17d, "CZK")
japod@6
    49
        );
japod@6
    50
    }
japod@6
    51
japod@6
    52
    /** Create convertor that understands two currencies, CZK and
japod@19
    53
     *  SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
japod@19
    54
     *  it knows the exchange rate, and needs to use the API to create objects
japod@19
    55
     *  with the exchange rate. Anyone shall be ready to call this method without
japod@19
    56
     *  any other method being called previously. The API itself shall know
japod@19
    57
     *  nothing about any rates, before this method is called.
japod@6
    58
     *
japod@6
    59
     * Creation of the convertor shall not require subclassing of any class
japod@6
    60
     * or interface on the client side.
japod@6
    61
     * 
japod@6
    62
     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
japod@6
    63
     */
japod@53
    64
    public static Convertor<Double, String> createSKKtoCZK() {
japod@53
    65
        return Convertor.getConvertorDoubleString(
japod@53
    66
                CurrencyValue.getCurrencyValue(100d, "SKK"),
japod@53
    67
                CurrencyValue.getCurrencyValue(80d, "CZK")
japod@6
    68
        );
japod@6
    69
    }
japod@19
    70
japod@19
    71
    //
japod@19
    72
    // now the methods for group #2 follow:
japod@19
    73
    // this group knows nothing about exchange rates, but knows how to use
japod@19
    74
    // the API to do conversions. It somehow (by calling one of the factory
japod@19
    75
    // methods) gets objects from the API and uses them to do the conversions.
japod@19
    76
    //
japod@6
    77
    
japod@6
    78
    /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
japod@6
    79
     * with it.
japod@6
    80
     */
japod@6
    81
    public void testCurrencyCZKUSD() throws Exception {
japod@53
    82
        Convertor<Double, String> c = createCZKtoUSD();
japod@6
    83
        
japod@53
    84
        CurrencyValue<Double, String> result;
japod@6
    85
        
japod@6
    86
        // convert $5 to CZK using c:
japod@6
    87
        // assertEquals("Result is 85 CZK");
japod@53
    88
        result = c.convert("CZK", CurrencyValue.getCurrencyValue(5d, "USD"));
japod@53
    89
        assertEquals(CurrencyValue.getCurrencyValue(85d, "CZK"), result);
japod@6
    90
japod@6
    91
        // convert $8 to CZK
japod@6
    92
        // assertEquals("Result is 136 CZK");
japod@53
    93
        result = c.convert("CZK", CurrencyValue.getCurrencyValue(8d, "USD"));
japod@53
    94
        assertEquals(CurrencyValue.getCurrencyValue(136d, "CZK"), result);
japod@6
    95
japod@6
    96
        // convert 1003CZK to USD
japod@6
    97
        // assertEquals("Result is 59 USD");
japod@53
    98
        result = c.convert("USD", CurrencyValue.getCurrencyValue(1003d, "CZK"));
japod@53
    99
        assertEquals(CurrencyValue.getCurrencyValue(59d, "USD"), result);
japod@6
   100
    }
japod@6
   101
japod@6
   102
    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
japod@6
   103
     * with it.
japod@6
   104
     */
japod@6
   105
    public void testCurrencySKKCZK() throws Exception {
japod@53
   106
        Convertor<Double, String> c = createSKKtoCZK();
japod@6
   107
        
japod@53
   108
        CurrencyValue<Double, String> result;
japod@6
   109
        
japod@6
   110
        // convert 16CZK using c:
japod@6
   111
        // assertEquals("Result is 20 SKK");
japod@53
   112
        result = c.convert("SKK", CurrencyValue.getCurrencyValue(16d, "CZK"));
japod@53
   113
        assertEquals(CurrencyValue.getCurrencyValue(20d, "SKK"), result);
japod@6
   114
japod@6
   115
        // convert 500SKK to CZK
japod@6
   116
        // assertEquals("Result is 400 CZK");
japod@53
   117
        result = c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"));
japod@53
   118
        assertEquals(CurrencyValue.getCurrencyValue(400d, "CZK"), result);
japod@6
   119
    }
japod@19
   120
japod@19
   121
    /** Verify that the CZK to USD convertor knows nothing about SKK.
japod@19
   122
     */
japod@19
   123
    public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
japod@53
   124
        Convertor<Double, String> c = createCZKtoUSD();
japod@19
   125
        try {
japod@19
   126
            // convert $5 to SKK, the API shall say this is not possible
japod@53
   127
            c.convert("SKK", CurrencyValue.getCurrencyValue(16d, "CZK"));
japod@53
   128
            fail("Should not convert");
japod@19
   129
        } catch (Exception e) {
japod@19
   130
        }
japod@19
   131
        try {
japod@19
   132
            // convert 500 SKK to CZK, the API shall say this is not possible
japod@53
   133
            c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"));
japod@53
   134
            fail("Should not convert");
japod@19
   135
        } catch (Exception e) {
japod@19
   136
        }
japod@19
   137
        
japod@19
   138
    }
japod@19
   139
japod@19
   140
    /** Verify that the CZK to SKK convertor knows nothing about USD.
japod@19
   141
     */
japod@19
   142
    public void testCannotConvertToUSDwithSKKCZKConvertor() throws Exception {
japod@53
   143
        Convertor<Double, String> c = createSKKtoCZK();
japod@19
   144
        try {
japod@19
   145
            // convert $5 to SKK, the API shall say this is not possible
japod@53
   146
            c.convert("SKK", CurrencyValue.getCurrencyValue(5d, "USD"));
japod@53
   147
            fail("Should not convert");
japod@19
   148
        } catch (Exception e) {
japod@19
   149
        }
japod@19
   150
        try {
japod@19
   151
            // convert 500 CZK to USD, the API shall say this is not possible
japod@53
   152
            c.convert("USD", CurrencyValue.getCurrencyValue(500d, "CZK"));
japod@53
   153
            fail("Should not convert");
japod@19
   154
        } catch (Exception e) {
japod@19
   155
        }
japod@19
   156
    }
japod@6
   157
}