task4/solution12/test/org/apidesign/apifest08/test/Task4Test.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 17 Oct 2008 17:39:18 +0200
changeset 68 4de3a4b5445a
parent 62 f711ecd374f3
permissions -rw-r--r--
solution 12, task4
     1 package org.apidesign.apifest08.test;
     2 
     3 import java.text.SimpleDateFormat;
     4 import java.util.Currency;
     5 import java.util.Date;
     6 import junit.framework.TestCase;
     7 import org.apidesign.apifest08.currency.Convertor;
     8 import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
     9 
    10 /** The exchange rates are not always the same. They are changing. However
    11  * as in order to predict the future, one needs to understand own past. That is
    12  * why it is important to know the exchange rate as it was at any time during
    13  * the past.
    14  * <p>
    15  * Today's quest is to enhance the convertor API to deal with dates.
    16  * One shall be able to convert a currency at any date. Each currencies rate shall
    17  * be associated with a range between two Date objects. In order
    18  * to keep compatibility with old API that knew nothing about dates, the
    19  * rates associated then are applicable "for eternity". Any use of existing
    20  * convert methods that do not accept a Date argument, uses the current
    21  * System.currentTimeMillis() as default date.
    22  */
    23 public class Task4Test extends TestCase {
    24     public Task4Test(String testName) {
    25         super(testName);
    26     }
    27 
    28     @Override
    29     protected void setUp() throws Exception {
    30     }
    31 
    32     @Override
    33     protected void tearDown() throws Exception {
    34     }
    35 
    36     // Backward compatibly enhance your existing API to support following
    37     // usecases:
    38     //
    39 
    40     /** Takes a convertor with any rates associated and creates new convertor
    41      * that returns the same values as the old one for time between from to till.
    42      * Otherwise it returns no results. This is just a helper method that
    43      * shall call some real one in the API.
    44      * 
    45      * @param old existing convertor
    46      * @param from initial date (inclusive)
    47      * @param till final date (exclusive)
    48      * @return new convertor
    49      */
    50     public static Convertor limitTo(Convertor old, Date from, Date till) {
    51         return Convertor.limitExchangeRatesValidity(old, from, till);
    52     }
    53 
    54 
    55     public void testCompositionOfLimitedConvertors() throws Exception {
    56         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz");
    57         Date d1 = format.parse("2008-10-01 00:00 GMT"); // 2008-10-01 0:00 GMT
    58         Date d2 = format.parse("2008-10-02 00:00 GMT"); // 2008-10-02 0:00 GMT
    59         Date d3 = format.parse("2008-10-03 00:00 GMT"); // 2008-10-03 0:00 GMT
    60         
    61         Convertor c = Task2Test.merge(
    62             limitTo(Task1Test.createCZKtoUSD(), d1, d2),
    63             limitTo(Task1Test.createSKKtoCZK(), d2, d3)
    64         );
    65         
    66         Date now = new Date();
    67 
    68         // convert $5 to CZK using c:
    69         double result = 0;
    70         boolean exceptionThrown = false;
    71         try {
    72           result = c.convert(5, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    73         } catch (Exception e) {
    74           exceptionThrown = true;
    75         }
    76         
    77         if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
    78           System.out.println("Result.");
    79           assertEquals("Result is not 85 CZK.", 85d, result);          
    80         } else {
    81           System.out.println("Exception.");
    82           assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
    83         }
    84 
    85         // convert $8 to CZK using c:
    86         exceptionThrown = false;
    87         try {
    88           result = c.convert(8, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    89         } catch (Exception e) {
    90           exceptionThrown = true;
    91         }
    92         
    93         if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
    94           System.out.println("Result.");
    95           assertEquals("Result is not 136 CZK.", 136d, result);          
    96         } else {
    97           System.out.println("Exception.");
    98           assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
    99         }
   100 
   101         // convert 1003CZK to USD using c:
   102         exceptionThrown = false;
   103         try {
   104           result = c.convert(1003, Currency.getInstance("CZK"), Currency.getInstance("USD"));
   105         } catch (Exception e) {
   106           exceptionThrown = true;
   107         }
   108         
   109         if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
   110           System.out.println("Result.");
   111           assertEquals("Result is not 59 USD.", 59d, result);
   112         } else {
   113           System.out.println("Exception.");
   114           assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   115         }
   116 
   117         // convert 16CZK using c:
   118         exceptionThrown = false;
   119         try {
   120           result = c.convert(16, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
   121         } catch (Exception e) {
   122           exceptionThrown = true;
   123         }
   124         
   125         if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
   126           System.out.println("Result.");
   127           assertEquals("Result is not 20 SKK.", 20d, result);          
   128         } else {
   129           System.out.println("Exception.");
   130           assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   131         }
   132 
   133         // convert 500SKK to CZK using c:
   134         exceptionThrown = false;
   135         try {
   136           result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   137         } catch (Exception e) {
   138           exceptionThrown = true;
   139         }
   140         
   141         if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
   142           System.out.println("Result.");
   143           assertEquals("Result is not 400 CZK.", 400d, result);          
   144         } else {
   145           System.out.println("Exception.");
   146           assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   147         }
   148 
   149     }
   150 
   151     /** Create convertor that understands two currencies, CZK and
   152      *  SKK. Make 100 SKK == 90 CZK.
   153      *
   154      * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
   155      */
   156     public static Convertor createSKKtoCZK2() {
   157       // set exchange rates
   158       Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 90d, 100d);
   159 
   160       // create new instance
   161       Convertor convertor = null;
   162       try {
   163         convertor = Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   164       } catch (UnknownConvertorException e) {
   165         e.printStackTrace();
   166       }
   167 
   168       return convertor;
   169     }
   170 
   171     public void testDateConvetorWithTwoDifferentRates() throws Exception {
   172 
   173         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz");
   174         Date d1 = format.parse("2008-10-01 00:00 GMT"); // 2008-10-01 0:00 GMT
   175         Date d2 = format.parse("2008-10-02 00:00 GMT"); // 2008-10-02 0:00 GMT
   176         Date d3 = format.parse("2008-10-03 00:00 GMT"); // 2008-10-03 0:00 GMT
   177 
   178         Convertor c = Task2Test.merge(
   179             limitTo(createSKKtoCZK2(), d1, d2),
   180             limitTo(Task1Test.createSKKtoCZK(), d2, d3)
   181         );
   182 
   183         // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
   184         // assertEquals("Result is 400 CZK");
   185 
   186         // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   187         // assertEquals("Result is 450 CZK");
   188         Date now = new Date();
   189         
   190         // convert 500SKK to CZK using c:
   191         double result = 0;
   192         boolean exceptionThrown = false;
   193         try {
   194           result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   195         } catch (Exception e) {
   196           exceptionThrown = true;
   197         }
   198         
   199         if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
   200           System.out.println("\nResult");
   201           assertEquals("Result is not 450 CZK.", 450d, result);
   202         } else if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
   203           System.out.println("\nResult");
   204           assertEquals("Result is not 400 CZK.", 400d, result);
   205         } else {
   206           System.out.println("\nException");
   207           assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   208         }
   209     }
   210 
   211 }