task4/solution11/test/org/apidesign/apifest08/test/Task4Test.java
changeset 66 aa3f99f845ef
parent 62 f711ecd374f3
     1.1 --- a/task4/solution11/test/org/apidesign/apifest08/test/Task4Test.java	Sat Oct 11 23:46:05 2008 +0200
     1.2 +++ b/task4/solution11/test/org/apidesign/apifest08/test/Task4Test.java	Fri Oct 17 17:34:40 2008 +0200
     1.3 @@ -1,8 +1,13 @@
     1.4  package org.apidesign.apifest08.test;
     1.5  
     1.6 +import java.util.Calendar;
     1.7  import java.util.Date;
     1.8 +import java.util.GregorianCalendar;
     1.9 +import java.util.TimeZone;
    1.10  import junit.framework.TestCase;
    1.11  import org.apidesign.apifest08.currency.Convertor;
    1.12 +import org.apidesign.apifest08.currency.CurrencyValue;
    1.13 +import org.apidesign.apifest08.currency.ExchangeRateValue;
    1.14  
    1.15  /** The exchange rates are not always the same. They are changing. However
    1.16   * as in order to predict the future, one needs to understand own past. That is
    1.17 @@ -44,58 +49,105 @@
    1.18       * @param till final date (exclusive)
    1.19       * @return new convertor
    1.20       */
    1.21 -    public static Convertor limitTo(Convertor old, Date from, Date till) {
    1.22 -        return null;
    1.23 +    public static <AmountType, IdentifierType> Convertor<AmountType, IdentifierType> limitTo(Convertor<AmountType, IdentifierType> old, Date from, Date till) {
    1.24 +        return old.limitConvertor(from, till);
    1.25      }
    1.26  
    1.27  
    1.28      public void testCompositionOfLimitedConvertors() throws Exception {
    1.29 -        if (Boolean.getBoolean("ignore.failing")) {
    1.30 -            // implement me! then delete this if statement
    1.31 -            return;
    1.32 -        }
    1.33 -
    1.34 -        Date d1 = null; // 2008-10-01 0:00 GMT
    1.35 -        Date d2 = null; // 2008-10-02 0:00 GMT
    1.36 -        Date d3 = null; // 2008-10-03 0:00 GMT
    1.37 +        Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    1.38 +        cal.clear();
    1.39 +        cal.set(2008, 9, 1, 0, 0);
    1.40 +        Date d1 = cal.getTime(); // 2008-10-01 0:00 GMT
    1.41 +        cal.set(2008, 9, 2, 0, 0);
    1.42 +        Date d2 = cal.getTime(); // 2008-10-02 0:00 GMT
    1.43 +        cal.set(2008, 9, 3, 0, 0);
    1.44 +        Date d3 = cal.getTime(); // 2008-10-03 0:00 GMT
    1.45          
    1.46 -        Convertor c = Task2Test.merge(
    1.47 +        Convertor<Double, String> c = Task2Test.merge(
    1.48              limitTo(Task1Test.createCZKtoUSD(), d1, d2),
    1.49              limitTo(Task1Test.createSKKtoCZK(), d2, d3)
    1.50          );
    1.51  
    1.52 +        CurrencyValue<Double, String> result;
    1.53          // convert $5 to CZK using c:
    1.54          // cannot convert as no rate is applicable to current date
    1.55 -
    1.56 +        try {
    1.57 +            c.convert("CZK", CurrencyValue.getCurrencyValue(5d, "USD"));
    1.58 +            fail("Should not convert");
    1.59 +        } catch (Exception e) {
    1.60 +        }
    1.61 +        
    1.62          // convert $8 to CZK using c:
    1.63          // cannot convert as no rate is applicable to current date
    1.64 +        try {
    1.65 +            c.convert("CZK", CurrencyValue.getCurrencyValue(8d, "USD"));
    1.66 +            fail("Should not convert");
    1.67 +        } catch (Exception e) {
    1.68 +        }
    1.69  
    1.70          // convert 1003CZK to USD using c:
    1.71          // cannot convert as no rate is applicable to current date
    1.72 +        try {
    1.73 +            c.convert("USD", CurrencyValue.getCurrencyValue(1003d, "CZK"));
    1.74 +            fail("Should not convert");
    1.75 +        } catch (Exception e) {
    1.76 +        }
    1.77  
    1.78          // convert 16CZK using c:
    1.79          // cannot convert as no rate is applicable to current date
    1.80 +        try {
    1.81 +            c.convert("SKK", CurrencyValue.getCurrencyValue(16d, "CZK"));
    1.82 +            fail("Should not convert");
    1.83 +        } catch (Exception e) {
    1.84 +        }
    1.85  
    1.86          // convert 500SKK to CZK using c:
    1.87          // cannot convert as no rate is applicable to current date
    1.88 +        try {
    1.89 +            c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"));
    1.90 +            fail("Should not convert");
    1.91 +        } catch (Exception e) {
    1.92 +        }
    1.93  
    1.94          // convert $5 to CZK using c at 2008-10-01 6:00 GMT:
    1.95          // assertEquals("Result is 85 CZK");
    1.96 +        cal.set(2008, 9, 1, 6, 0);
    1.97 +        result = c.convert("CZK", CurrencyValue.getCurrencyValue(5d, "USD"), cal.getTime());
    1.98 +        assertEquals(CurrencyValue.getCurrencyValue(85d, "CZK"), result);
    1.99  
   1.100          // convert $8 to CZK using c at 2008-10-01 6:00 GMT:
   1.101          // assertEquals("Result is 136 CZK");
   1.102 +        cal.set(2008, 9, 1, 6, 0);
   1.103 +        result = c.convert("CZK", CurrencyValue.getCurrencyValue(8d, "USD"), cal.getTime());
   1.104 +        assertEquals(CurrencyValue.getCurrencyValue(136d, "CZK"), result);
   1.105  
   1.106          // convert 1003CZK to USD using c at 2008-10-01 6:00 GMT:
   1.107          // assertEquals("Result is 59 USD");
   1.108 +        cal.set(2008, 9, 1, 6, 0);
   1.109 +        result = c.convert("USD", CurrencyValue.getCurrencyValue(1003d, "CZK"), cal.getTime());
   1.110 +        assertEquals(CurrencyValue.getCurrencyValue(59d, "USD"), result);
   1.111  
   1.112          // convert 16CZK using c at 2008-10-02 9:00 GMT:
   1.113          // assertEquals("Result is 20 SKK");
   1.114 +        cal.set(2008, 9, 2, 9, 0);
   1.115 +        result = c.convert("SKK", CurrencyValue.getCurrencyValue(16d, "CZK"), cal.getTime());
   1.116 +        assertEquals(CurrencyValue.getCurrencyValue(20d, "SKK"), result);
   1.117  
   1.118          // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
   1.119          // assertEquals("Result is 400 CZK");
   1.120 +        cal.set(2008, 9, 2, 9, 0);
   1.121 +        result = c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"), cal.getTime());
   1.122 +        assertEquals(CurrencyValue.getCurrencyValue(400d, "CZK"), result);
   1.123  
   1.124          // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   1.125          // cannot convert as no rate is applicable to current date
   1.126 +        cal.set(2008, 9, 1, 6, 0);
   1.127 +        try {
   1.128 +            result = c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"), cal.getTime());
   1.129 +            fail("Should not convert");
   1.130 +        } catch (Exception e) {
   1.131 +        }
   1.132      }
   1.133  
   1.134      /** Create convertor that understands two currencies, CZK and
   1.135 @@ -103,30 +155,40 @@
   1.136       *
   1.137       * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
   1.138       */
   1.139 -    public static Convertor createSKKtoCZK2() {
   1.140 -        return null;
   1.141 +    public static Convertor<Double, String> createSKKtoCZK2() {
   1.142 +        return Convertor.getConvertorDoubleString(
   1.143 +                ExchangeRateValue.getExchangeRate(
   1.144 +                    CurrencyValue.getCurrencyValue(100d, "SKK"),
   1.145 +                    CurrencyValue.getCurrencyValue(90d, "CZK")));
   1.146      }
   1.147  
   1.148      public void testDateConvetorWithTwoDifferentRates() throws Exception {
   1.149 -        if (Boolean.getBoolean("ignore.failing")) {
   1.150 -            // implement me! then delete this if statement
   1.151 -            return;
   1.152 -        }
   1.153 +        Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
   1.154 +        cal.clear();
   1.155 +        cal.set(2008, 9, 1, 0, 0);
   1.156 +        Date d1 = cal.getTime(); // 2008-10-01 0:00 GMT
   1.157 +        cal.set(2008, 9, 2, 0, 0);
   1.158 +        Date d2 = cal.getTime(); // 2008-10-02 0:00 GMT
   1.159 +        cal.set(2008, 9, 3, 0, 0);
   1.160 +        Date d3 = cal.getTime(); // 2008-10-03 0:00 GMT
   1.161  
   1.162 -        Date d1 = null; // 2008-10-01 0:00 GMT
   1.163 -        Date d2 = null; // 2008-10-02 0:00 GMT
   1.164 -        Date d3 = null; // 2008-10-03 0:00 GMT
   1.165 -
   1.166 -        Convertor c = Task2Test.merge(
   1.167 +        Convertor<Double, String> c = Task2Test.merge(
   1.168              limitTo(createSKKtoCZK2(), d1, d2),
   1.169              limitTo(Task1Test.createSKKtoCZK(), d2, d3)
   1.170          );
   1.171  
   1.172 +        CurrencyValue<Double, String> result;
   1.173          // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
   1.174          // assertEquals("Result is 400 CZK");
   1.175 +        cal.set(2008, 9, 2, 9, 0);
   1.176 +        result = c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"), cal.getTime());
   1.177 +        assertEquals(CurrencyValue.getCurrencyValue(400d, "CZK"), result);
   1.178  
   1.179          // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   1.180          // assertEquals("Result is 450 CZK");
   1.181 +        cal.set(2008, 9, 1, 6, 0);
   1.182 +        result = c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"), cal.getTime());
   1.183 +        assertEquals(CurrencyValue.getCurrencyValue(450d, "CZK"), result);
   1.184      }
   1.185  
   1.186  }