currency/test/org/apidesign/apifest08/test/Task4Test.java
changeset 60 a92eff6b426f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/currency/test/org/apidesign/apifest08/test/Task4Test.java	Sat Oct 11 23:36:46 2008 +0200
     1.3 @@ -0,0 +1,132 @@
     1.4 +package org.apidesign.apifest08.test;
     1.5 +
     1.6 +import java.util.Date;
     1.7 +import junit.framework.TestCase;
     1.8 +import org.apidesign.apifest08.currency.Convertor;
     1.9 +
    1.10 +/** The exchange rates are not always the same. They are changing. However
    1.11 + * as in order to predict the future, one needs to understand own past. That is
    1.12 + * why it is important to know the exchange rate as it was at any time during
    1.13 + * the past.
    1.14 + * <p>
    1.15 + * Today's quest is to enhance the convertor API to deal with dates.
    1.16 + * One shall be able to convert a currency at any date. Each currencies rate shall
    1.17 + * be associated with a range between two Date objects. In order
    1.18 + * to keep compatibility with old API that knew nothing about dates, the
    1.19 + * rates associated then are applicable "for eternity". Any use of existing
    1.20 + * convert methods that do not accept a Date argument, uses the current
    1.21 + * System.currentTimeMillis() as default date.
    1.22 + */
    1.23 +public class Task4Test extends TestCase {
    1.24 +    public Task4Test(String testName) {
    1.25 +        super(testName);
    1.26 +    }
    1.27 +
    1.28 +    @Override
    1.29 +    protected void setUp() throws Exception {
    1.30 +    }
    1.31 +
    1.32 +    @Override
    1.33 +    protected void tearDown() throws Exception {
    1.34 +    }
    1.35 +
    1.36 +    // Backward compatibly enhance your existing API to support following
    1.37 +    // usecases:
    1.38 +    //
    1.39 +
    1.40 +    /** Takes a convertor with any rates associated and creates new convertor
    1.41 +     * that returns the same values as the old one for time between from to till.
    1.42 +     * Otherwise it returns no results. This is just a helper method that
    1.43 +     * shall call some real one in the API.
    1.44 +     * 
    1.45 +     * @param old existing convertor
    1.46 +     * @param from initial date (inclusive)
    1.47 +     * @param till final date (exclusive)
    1.48 +     * @return new convertor
    1.49 +     */
    1.50 +    public static Convertor limitTo(Convertor old, Date from, Date till) {
    1.51 +        return null;
    1.52 +    }
    1.53 +
    1.54 +
    1.55 +    public void testCompositionOfLimitedConvertors() throws Exception {
    1.56 +        if (Boolean.getBoolean("ignore.failing")) {
    1.57 +            // implement me! then delete this if statement
    1.58 +            return;
    1.59 +        }
    1.60 +
    1.61 +        Date d1 = null; // 2008-10-01 0:00 GMT
    1.62 +        Date d2 = null; // 2008-10-02 0:00 GMT
    1.63 +        Date d3 = null; // 2008-10-03 0:00 GMT
    1.64 +        
    1.65 +        Convertor c = Task2Test.merge(
    1.66 +            limitTo(Task1Test.createCZKtoUSD(), d1, d2),
    1.67 +            limitTo(Task1Test.createSKKtoCZK(), d2, d3)
    1.68 +        );
    1.69 +
    1.70 +        // convert $5 to CZK using c:
    1.71 +        // cannot convert as no rate is applicable to current date
    1.72 +
    1.73 +        // convert $8 to CZK using c:
    1.74 +        // cannot convert as no rate is applicable to current date
    1.75 +
    1.76 +        // convert 1003CZK to USD using c:
    1.77 +        // cannot convert as no rate is applicable to current date
    1.78 +
    1.79 +        // convert 16CZK using c:
    1.80 +        // cannot convert as no rate is applicable to current date
    1.81 +
    1.82 +        // convert 500SKK to CZK using c:
    1.83 +        // cannot convert as no rate is applicable to current date
    1.84 +
    1.85 +        // convert $5 to CZK using c at 2008-10-01 6:00 GMT:
    1.86 +        // assertEquals("Result is 85 CZK");
    1.87 +
    1.88 +        // convert $8 to CZK using c at 2008-10-01 6:00 GMT:
    1.89 +        // assertEquals("Result is 136 CZK");
    1.90 +
    1.91 +        // convert 1003CZK to USD using c at 2008-10-01 6:00 GMT:
    1.92 +        // assertEquals("Result is 59 USD");
    1.93 +
    1.94 +        // convert 16CZK using c at 2008-10-02 9:00 GMT:
    1.95 +        // assertEquals("Result is 20 SKK");
    1.96 +
    1.97 +        // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
    1.98 +        // assertEquals("Result is 400 CZK");
    1.99 +
   1.100 +        // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   1.101 +        // cannot convert as no rate is applicable to current date
   1.102 +    }
   1.103 +
   1.104 +    /** Create convertor that understands two currencies, CZK and
   1.105 +     *  SKK. Make 100 SKK == 90 CZK.
   1.106 +     *
   1.107 +     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
   1.108 +     */
   1.109 +    public static Convertor createSKKtoCZK2() {
   1.110 +        return null;
   1.111 +    }
   1.112 +
   1.113 +    public void testDateConvetorWithTwoDifferentRates() throws Exception {
   1.114 +        if (Boolean.getBoolean("ignore.failing")) {
   1.115 +            // implement me! then delete this if statement
   1.116 +            return;
   1.117 +        }
   1.118 +
   1.119 +        Date d1 = null; // 2008-10-01 0:00 GMT
   1.120 +        Date d2 = null; // 2008-10-02 0:00 GMT
   1.121 +        Date d3 = null; // 2008-10-03 0:00 GMT
   1.122 +
   1.123 +        Convertor c = Task2Test.merge(
   1.124 +            limitTo(createSKKtoCZK2(), d1, d2),
   1.125 +            limitTo(Task1Test.createSKKtoCZK(), d2, d3)
   1.126 +        );
   1.127 +
   1.128 +        // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
   1.129 +        // assertEquals("Result is 400 CZK");
   1.130 +
   1.131 +        // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   1.132 +        // assertEquals("Result is 450 CZK");
   1.133 +    }
   1.134 +
   1.135 +}