task4/solution06/test/org/apidesign/apifest08/test/Task4Test.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 17 Oct 2008 17:54:38 +0200
changeset 71 978f6a78c22e
parent 64 51f7b894eba6
permissions -rw-r--r--
removing 'ignore.failing' messages
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@64
    58
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm zzzz");
jaroslav@64
    59
        
jaroslav@64
    60
        Date d1 = df.parse("2008-10-01 0:00 GMT"); 
jaroslav@64
    61
        Date d2 = df.parse("2008-10-02 0:00 GMT"); 
jaroslav@64
    62
        Date d3 = df.parse("2008-10-03 0:00 GMT");
jaroslav@60
    63
        
jaroslav@60
    64
        Convertor c = Task2Test.merge(
jaroslav@60
    65
            limitTo(Task1Test.createCZKtoUSD(), d1, d2),
jaroslav@60
    66
            limitTo(Task1Test.createSKKtoCZK(), d2, d3)
jaroslav@60
    67
        );
jaroslav@60
    68
jaroslav@60
    69
        // convert $5 to CZK using c:
jaroslav@64
    70
        try {
jaroslav@64
    71
        	c.convert(new BigDecimal(5), USD , CZK);
jaroslav@64
    72
        	fail("cannot convert as no rate is applicable to current date");
jaroslav@64
    73
        } catch(UnsupportedConversionException e) {
jaroslav@64
    74
        	//expected
jaroslav@64
    75
        }
jaroslav@60
    76
jaroslav@60
    77
        // convert $8 to CZK using c:
jaroslav@64
    78
        try {
jaroslav@64
    79
        	c.convert(new BigDecimal(8), USD , CZK);
jaroslav@64
    80
        	fail("cannot convert as no rate is applicable to current date");
jaroslav@64
    81
        } catch(UnsupportedConversionException e) {
jaroslav@64
    82
        	//expected
jaroslav@64
    83
        }
jaroslav@60
    84
jaroslav@60
    85
        // convert 1003CZK to USD using c:
jaroslav@64
    86
        try {
jaroslav@64
    87
        	c.convert(new BigDecimal(1003), CZK, USD);
jaroslav@64
    88
        	fail("cannot convert as no rate is applicable to current date");
jaroslav@64
    89
        } catch(UnsupportedConversionException e) {
jaroslav@64
    90
        	//expected
jaroslav@64
    91
        }
jaroslav@60
    92
jaroslav@60
    93
        // convert 16CZK using c:
jaroslav@64
    94
        try {
jaroslav@64
    95
        	c.convert(new BigDecimal(16), CZK, USD);
jaroslav@64
    96
        	fail("cannot convert as no rate is applicable to current date");
jaroslav@64
    97
        } catch(UnsupportedConversionException e) {
jaroslav@64
    98
        	//expected
jaroslav@64
    99
        }
jaroslav@60
   100
jaroslav@60
   101
        // convert 500SKK to CZK using c:
jaroslav@64
   102
        try {
jaroslav@64
   103
        	c.convert(new BigDecimal(500), SKK, CZK);
jaroslav@64
   104
        	fail("cannot convert as no rate is applicable to current date");
jaroslav@64
   105
        } catch(UnsupportedConversionException e) {
jaroslav@64
   106
        	//expected
jaroslav@64
   107
        }
jaroslav@60
   108
jaroslav@64
   109
        // convert $5 to CZK using c at 2008-10-01 6:00 GMT:         
jaroslav@64
   110
        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
   111
jaroslav@60
   112
        // convert $8 to CZK using c at 2008-10-01 6:00 GMT:
jaroslav@64
   113
        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
   114
     
jaroslav@60
   115
        // convert 1003CZK to USD using c at 2008-10-01 6:00 GMT:
jaroslav@64
   116
        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
   117
jaroslav@60
   118
        // convert 16CZK using c at 2008-10-02 9:00 GMT:
jaroslav@64
   119
        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
   120
jaroslav@60
   121
        // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
jaroslav@64
   122
        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
   123
jaroslav@60
   124
        // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
jaroslav@64
   125
        try {
jaroslav@64
   126
        	c.convert(new BigDecimal(500), SKK, CZK, df.parse("2008-10-01 6:00 GMT"));
jaroslav@64
   127
        	fail("cannot convert as no rate is applicable to current date");
jaroslav@64
   128
        } catch(UnsupportedConversionException e) {
jaroslav@64
   129
        	//expected
jaroslav@64
   130
        }
jaroslav@60
   131
    }
jaroslav@60
   132
jaroslav@60
   133
    /** Create convertor that understands two currencies, CZK and
jaroslav@60
   134
     *  SKK. Make 100 SKK == 90 CZK.
jaroslav@60
   135
     *
jaroslav@60
   136
     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
jaroslav@60
   137
     */
jaroslav@60
   138
    public static Convertor createSKKtoCZK2() {
jaroslav@64
   139
    	return new Convertor(new BigDecimal("0.9"), SKK, CZK);
jaroslav@60
   140
    }
jaroslav@60
   141
jaroslav@60
   142
    public void testDateConvetorWithTwoDifferentRates() throws Exception {
jaroslav@64
   143
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm zzzz");
jaroslav@64
   144
        
jaroslav@64
   145
        Date d1 = df.parse("2008-10-01 0:00 GMT"); 
jaroslav@64
   146
        Date d2 = df.parse("2008-10-02 0:00 GMT"); 
jaroslav@64
   147
        Date d3 = df.parse("2008-10-03 0:00 GMT");
jaroslav@60
   148
jaroslav@60
   149
        Convertor c = Task2Test.merge(
jaroslav@60
   150
            limitTo(createSKKtoCZK2(), d1, d2),
jaroslav@60
   151
            limitTo(Task1Test.createSKKtoCZK(), d2, d3)
jaroslav@60
   152
        );
jaroslav@60
   153
jaroslav@60
   154
        // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
jaroslav@64
   155
        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
   156
jaroslav@60
   157
        // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
jaroslav@64
   158
        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
   159
    }
jaroslav@60
   160
jaroslav@60
   161
}