currency/test/org/apidesign/apifest08/test/Task4Test.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 25 Oct 2008 20:53:00 +0200
changeset 84 2ae6e4aa7aef
permissions -rw-r--r--
Solutions by Petr Smid
jaroslav@60
     1
package org.apidesign.apifest08.test;
jaroslav@60
     2
jaroslav@60
     3
import java.util.Date;
jaroslav@60
     4
import junit.framework.TestCase;
jaroslav@60
     5
import org.apidesign.apifest08.currency.Convertor;
jaroslav@60
     6
jaroslav@60
     7
/** The exchange rates are not always the same. They are changing. However
jaroslav@60
     8
 * as in order to predict the future, one needs to understand own past. That is
jaroslav@60
     9
 * why it is important to know the exchange rate as it was at any time during
jaroslav@60
    10
 * the past.
jaroslav@60
    11
 * <p>
jaroslav@60
    12
 * Today's quest is to enhance the convertor API to deal with dates.
jaroslav@60
    13
 * One shall be able to convert a currency at any date. Each currencies rate shall
jaroslav@60
    14
 * be associated with a range between two Date objects. In order
jaroslav@60
    15
 * to keep compatibility with old API that knew nothing about dates, the
jaroslav@60
    16
 * rates associated then are applicable "for eternity". Any use of existing
jaroslav@60
    17
 * convert methods that do not accept a Date argument, uses the current
jaroslav@60
    18
 * System.currentTimeMillis() as default date.
jaroslav@60
    19
 */
jaroslav@60
    20
public class Task4Test extends TestCase {
jaroslav@60
    21
    public Task4Test(String testName) {
jaroslav@60
    22
        super(testName);
jaroslav@60
    23
    }
jaroslav@60
    24
jaroslav@60
    25
    @Override
jaroslav@60
    26
    protected void setUp() throws Exception {
jaroslav@60
    27
    }
jaroslav@60
    28
jaroslav@60
    29
    @Override
jaroslav@60
    30
    protected void tearDown() throws Exception {
jaroslav@60
    31
    }
jaroslav@60
    32
jaroslav@60
    33
    // Backward compatibly enhance your existing API to support following
jaroslav@60
    34
    // usecases:
jaroslav@60
    35
    //
jaroslav@60
    36
jaroslav@60
    37
    /** Takes a convertor with any rates associated and creates new convertor
jaroslav@60
    38
     * that returns the same values as the old one for time between from to till.
jaroslav@60
    39
     * Otherwise it returns no results. This is just a helper method that
jaroslav@60
    40
     * shall call some real one in the API.
jaroslav@60
    41
     * 
jaroslav@60
    42
     * @param old existing convertor
jaroslav@60
    43
     * @param from initial date (inclusive)
jaroslav@60
    44
     * @param till final date (exclusive)
jaroslav@60
    45
     * @return new convertor
jaroslav@60
    46
     */
jaroslav@60
    47
    public static Convertor limitTo(Convertor old, Date from, Date till) {
jaroslav@60
    48
        return null;
jaroslav@60
    49
    }
jaroslav@60
    50
jaroslav@60
    51
jaroslav@60
    52
    public void testCompositionOfLimitedConvertors() throws Exception {
jaroslav@60
    53
        if (Boolean.getBoolean("ignore.failing")) {
jaroslav@60
    54
            // implement me! then delete this if statement
jaroslav@60
    55
            return;
jaroslav@60
    56
        }
jaroslav@60
    57
jaroslav@60
    58
        Date d1 = null; // 2008-10-01 0:00 GMT
jaroslav@60
    59
        Date d2 = null; // 2008-10-02 0:00 GMT
jaroslav@60
    60
        Date d3 = null; // 2008-10-03 0:00 GMT
jaroslav@60
    61
        
jaroslav@60
    62
        Convertor c = Task2Test.merge(
jaroslav@60
    63
            limitTo(Task1Test.createCZKtoUSD(), d1, d2),
jaroslav@60
    64
            limitTo(Task1Test.createSKKtoCZK(), d2, d3)
jaroslav@60
    65
        );
jaroslav@60
    66
jaroslav@60
    67
        // convert $5 to CZK using c:
jaroslav@60
    68
        // cannot convert as no rate is applicable to current date
jaroslav@60
    69
jaroslav@60
    70
        // convert $8 to CZK using c:
jaroslav@60
    71
        // cannot convert as no rate is applicable to current date
jaroslav@60
    72
jaroslav@60
    73
        // convert 1003CZK to USD using c:
jaroslav@60
    74
        // cannot convert as no rate is applicable to current date
jaroslav@60
    75
jaroslav@60
    76
        // convert 16CZK using c:
jaroslav@60
    77
        // cannot convert as no rate is applicable to current date
jaroslav@60
    78
jaroslav@60
    79
        // convert 500SKK to CZK using c:
jaroslav@60
    80
        // cannot convert as no rate is applicable to current date
jaroslav@60
    81
jaroslav@60
    82
        // convert $5 to CZK using c at 2008-10-01 6:00 GMT:
jaroslav@60
    83
        // assertEquals("Result is 85 CZK");
jaroslav@60
    84
jaroslav@60
    85
        // convert $8 to CZK using c at 2008-10-01 6:00 GMT:
jaroslav@60
    86
        // assertEquals("Result is 136 CZK");
jaroslav@60
    87
jaroslav@60
    88
        // convert 1003CZK to USD using c at 2008-10-01 6:00 GMT:
jaroslav@60
    89
        // assertEquals("Result is 59 USD");
jaroslav@60
    90
jaroslav@60
    91
        // convert 16CZK using c at 2008-10-02 9:00 GMT:
jaroslav@60
    92
        // assertEquals("Result is 20 SKK");
jaroslav@60
    93
jaroslav@60
    94
        // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
jaroslav@60
    95
        // assertEquals("Result is 400 CZK");
jaroslav@60
    96
jaroslav@60
    97
        // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
jaroslav@60
    98
        // cannot convert as no rate is applicable to current date
jaroslav@60
    99
    }
jaroslav@60
   100
jaroslav@60
   101
    /** Create convertor that understands two currencies, CZK and
jaroslav@60
   102
     *  SKK. Make 100 SKK == 90 CZK.
jaroslav@60
   103
     *
jaroslav@60
   104
     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
jaroslav@60
   105
     */
jaroslav@60
   106
    public static Convertor createSKKtoCZK2() {
jaroslav@60
   107
        return null;
jaroslav@60
   108
    }
jaroslav@60
   109
jaroslav@60
   110
    public void testDateConvetorWithTwoDifferentRates() throws Exception {
jaroslav@60
   111
        if (Boolean.getBoolean("ignore.failing")) {
jaroslav@60
   112
            // implement me! then delete this if statement
jaroslav@60
   113
            return;
jaroslav@60
   114
        }
jaroslav@60
   115
jaroslav@60
   116
        Date d1 = null; // 2008-10-01 0:00 GMT
jaroslav@60
   117
        Date d2 = null; // 2008-10-02 0:00 GMT
jaroslav@60
   118
        Date d3 = null; // 2008-10-03 0:00 GMT
jaroslav@60
   119
jaroslav@60
   120
        Convertor c = Task2Test.merge(
jaroslav@60
   121
            limitTo(createSKKtoCZK2(), d1, d2),
jaroslav@60
   122
            limitTo(Task1Test.createSKKtoCZK(), d2, d3)
jaroslav@60
   123
        );
jaroslav@60
   124
jaroslav@60
   125
        // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
jaroslav@60
   126
        // assertEquals("Result is 400 CZK");
jaroslav@60
   127
jaroslav@60
   128
        // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
jaroslav@60
   129
        // assertEquals("Result is 450 CZK");
jaroslav@60
   130
    }
jaroslav@60
   131
jaroslav@60
   132
}