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