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