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