1.1 --- a/task4/solution04/test/org/apidesign/apifest08/test/Task4Test.java Sat Oct 11 23:46:05 2008 +0200
1.2 +++ b/task4/solution04/test/org/apidesign/apifest08/test/Task4Test.java Fri Oct 17 17:40:14 2008 +0200
1.3 @@ -1,8 +1,15 @@
1.4 package org.apidesign.apifest08.test;
1.5
1.6 +import java.math.BigDecimal;
1.7 +import java.util.Calendar;
1.8 import java.util.Date;
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.ConvertorFactory;
1.13 +import org.apidesign.apifest08.currency.ExchangeRate;
1.14 +import org.apidesign.apifest08.currency.InvalidConversionException;
1.15 +import org.apidesign.apifest08.currency.TimedConvertor;
1.16
1.17 /** The exchange rates are not always the same. They are changing. However
1.18 * as in order to predict the future, one needs to understand own past. That is
1.19 @@ -22,8 +29,11 @@
1.20 super(testName);
1.21 }
1.22
1.23 + private Calendar gmtCalendar;
1.24 +
1.25 @Override
1.26 protected void setUp() throws Exception {
1.27 + gmtCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
1.28 }
1.29
1.30 @Override
1.31 @@ -45,57 +55,133 @@
1.32 * @return new convertor
1.33 */
1.34 public static Convertor limitTo(Convertor old, Date from, Date till) {
1.35 - return null;
1.36 + final Convertor convertor;
1.37 +
1.38 + convertor = ConvertorFactory.getConvertor(from, till, old);
1.39 +
1.40 + return convertor;
1.41 }
1.42
1.43
1.44 public void testCompositionOfLimitedConvertors() throws Exception {
1.45 - if (Boolean.getBoolean("ignore.failing")) {
1.46 - // implement me! then delete this if statement
1.47 - return;
1.48 - }
1.49
1.50 - Date d1 = null; // 2008-10-01 0:00 GMT
1.51 - Date d2 = null; // 2008-10-02 0:00 GMT
1.52 - Date d3 = null; // 2008-10-03 0:00 GMT
1.53 + gmtCalendar.set(2008, Calendar.OCTOBER, 1, 0, 0, 0);
1.54 + Date d1 = gmtCalendar.getTime(); // 2008-10-01 0:00 GMT
1.55 + gmtCalendar.set(2008, Calendar.OCTOBER, 2, 0, 0, 0);
1.56 + Date d2 = gmtCalendar.getTime(); // 2008-10-02 0:00 GMT
1.57 + gmtCalendar.set(2008, Calendar.OCTOBER, 3, 0, 0, 0);
1.58 + Date d3 = gmtCalendar.getTime(); // 2008-10-03 0:00 GMT
1.59
1.60 Convertor c = Task2Test.merge(
1.61 limitTo(Task1Test.createCZKtoUSD(), d1, d2),
1.62 limitTo(Task1Test.createSKKtoCZK(), d2, d3)
1.63 );
1.64
1.65 + Date date;
1.66 + BigDecimal amount;
1.67 +
1.68 // convert $5 to CZK using c:
1.69 // cannot convert as no rate is applicable to current date
1.70 + try
1.71 + {
1.72 + c.convert(Task1Test.USD, Task1Test.CZK, new BigDecimal("5.00"));
1.73 + fail("test A");
1.74 + }
1.75 + catch(final InvalidConversionException ex)
1.76 + {
1.77 + }
1.78
1.79 // convert $8 to CZK using c:
1.80 // cannot convert as no rate is applicable to current date
1.81 + try
1.82 + {
1.83 + c.convert(Task1Test.USD, Task1Test.CZK, new BigDecimal("8.00"));
1.84 + fail("test B");
1.85 + }
1.86 + catch(final InvalidConversionException ex)
1.87 + {
1.88 + }
1.89
1.90 // convert 1003CZK to USD using c:
1.91 // cannot convert as no rate is applicable to current date
1.92 + try
1.93 + {
1.94 + c.convert(Task1Test.CZK, Task1Test.USD, new BigDecimal("1003.00"));
1.95 + fail("test C");
1.96 + }
1.97 + catch(final InvalidConversionException ex)
1.98 + {
1.99 + }
1.100
1.101 // convert 16CZK using c:
1.102 // cannot convert as no rate is applicable to current date
1.103 + try
1.104 + {
1.105 + c.convert(Task1Test.CZK, Task1Test.USD, new BigDecimal("16.00"));
1.106 + fail("test D");
1.107 + }
1.108 + catch(final InvalidConversionException ex)
1.109 + {
1.110 + }
1.111
1.112 // convert 500SKK to CZK using c:
1.113 // cannot convert as no rate is applicable to current date
1.114 + try
1.115 + {
1.116 + c.convert(Task1Test.SKK, Task1Test.CZK, new BigDecimal("500.00"));
1.117 + fail("test C");
1.118 + }
1.119 + catch(final InvalidConversionException ex)
1.120 + {
1.121 + }
1.122
1.123 // convert $5 to CZK using c at 2008-10-01 6:00 GMT:
1.124 // assertEquals("Result is 85 CZK");
1.125 + gmtCalendar.set(2008, Calendar.OCTOBER, 1, 6, 0, 0);
1.126 + date = gmtCalendar.getTime();
1.127 + amount = ((TimedConvertor)c).convert(Task1Test.USD, Task1Test.CZK, new BigDecimal("5.00"), date);
1.128 + assertEquals(new BigDecimal("85.00"), amount);
1.129
1.130 // convert $8 to CZK using c at 2008-10-01 6:00 GMT:
1.131 // assertEquals("Result is 136 CZK");
1.132 + gmtCalendar.set(2008, Calendar.OCTOBER, 1, 6, 0, 0);
1.133 + date = gmtCalendar.getTime();
1.134 + amount = ((TimedConvertor)c).convert(Task1Test.USD, Task1Test.CZK, new BigDecimal("8.00"), date);
1.135 + assertEquals(new BigDecimal("136.00"), amount);
1.136
1.137 // convert 1003CZK to USD using c at 2008-10-01 6:00 GMT:
1.138 // assertEquals("Result is 59 USD");
1.139 + gmtCalendar.set(2008, Calendar.OCTOBER, 1, 6, 0, 0);
1.140 + date = gmtCalendar.getTime();
1.141 + amount = ((TimedConvertor)c).convert(Task1Test.CZK, Task1Test.USD, new BigDecimal("1003.00"), date);
1.142 + assertEquals(new BigDecimal("59.00"), amount);
1.143
1.144 // convert 16CZK using c at 2008-10-02 9:00 GMT:
1.145 // assertEquals("Result is 20 SKK");
1.146 + gmtCalendar.set(2008, Calendar.OCTOBER, 2, 9, 0, 0);
1.147 + date = gmtCalendar.getTime();
1.148 + amount = ((TimedConvertor)c).convert(Task1Test.CZK, Task1Test.SKK, new BigDecimal("16.00"), date);
1.149 + assertEquals(new BigDecimal("20.00"), amount);
1.150
1.151 // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
1.152 // assertEquals("Result is 400 CZK");
1.153 + gmtCalendar.set(2008, Calendar.OCTOBER, 2, 9, 0, 0);
1.154 + date = gmtCalendar.getTime();
1.155 + amount = ((TimedConvertor)c).convert(Task1Test.SKK, Task1Test.CZK, new BigDecimal("500.00"), date);
1.156 + assertEquals(new BigDecimal("400.00"), amount);
1.157
1.158 // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
1.159 // cannot convert as no rate is applicable to current date
1.160 + try
1.161 + {
1.162 + gmtCalendar.set(2008, Calendar.OCTOBER, 1, 6, 0, 0);
1.163 + date = gmtCalendar.getTime();
1.164 + ((TimedConvertor)c).convert(Task1Test.SKK, Task1Test.CZK, new BigDecimal("500.00"), date);
1.165 + fail("test D");
1.166 + }
1.167 + catch(final InvalidConversionException ex)
1.168 + {
1.169 + }
1.170 }
1.171
1.172 /** Create convertor that understands two currencies, CZK and
1.173 @@ -103,30 +189,49 @@
1.174 *
1.175 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
1.176 */
1.177 - public static Convertor createSKKtoCZK2() {
1.178 - return null;
1.179 + public static Convertor createSKKtoCZK2()
1.180 + {
1.181 + final ExchangeRate rate;
1.182 + final Convertor convertor;
1.183 +
1.184 + rate = ExchangeRate.getExchangeRate(Task1Test.SKK,
1.185 + Task1Test.CZK,
1.186 + new BigDecimal("100.00"),
1.187 + new BigDecimal("90.00"));
1.188 + convertor = ConvertorFactory.getConvertor(rate);
1.189 +
1.190 + return (convertor);
1.191 }
1.192
1.193 public void testDateConvetorWithTwoDifferentRates() throws Exception {
1.194 - if (Boolean.getBoolean("ignore.failing")) {
1.195 - // implement me! then delete this if statement
1.196 - return;
1.197 - }
1.198 -
1.199 - Date d1 = null; // 2008-10-01 0:00 GMT
1.200 - Date d2 = null; // 2008-10-02 0:00 GMT
1.201 - Date d3 = null; // 2008-10-03 0:00 GMT
1.202 + gmtCalendar.set(2008, Calendar.OCTOBER, 1, 0, 0, 0);
1.203 + Date d1 = gmtCalendar.getTime(); // 2008-10-01 0:00 GMT
1.204 + gmtCalendar.set(2008, Calendar.OCTOBER, 2, 0, 0, 0);
1.205 + Date d2 = gmtCalendar.getTime(); // 2008-10-02 0:00 GMT
1.206 + gmtCalendar.set(2008, Calendar.OCTOBER, 3, 0, 0, 0);
1.207 + Date d3 = gmtCalendar.getTime(); // 2008-10-03 0:00 GMT
1.208
1.209 Convertor c = Task2Test.merge(
1.210 limitTo(createSKKtoCZK2(), d1, d2),
1.211 limitTo(Task1Test.createSKKtoCZK(), d2, d3)
1.212 );
1.213
1.214 + Date date;
1.215 + BigDecimal amount;
1.216 +
1.217 // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
1.218 // assertEquals("Result is 400 CZK");
1.219 + gmtCalendar.set(2008, Calendar.OCTOBER, 2, 6, 0, 0);
1.220 + date = gmtCalendar.getTime();
1.221 + amount = ((TimedConvertor)c).convert(Task1Test.SKK, Task1Test.CZK, new BigDecimal("500.00"), date);
1.222 + assertEquals(new BigDecimal("400.00"), amount);
1.223
1.224 // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
1.225 // assertEquals("Result is 450 CZK");
1.226 + gmtCalendar.set(2008, Calendar.OCTOBER, 1, 6, 0, 0);
1.227 + date = gmtCalendar.getTime();
1.228 + amount = ((TimedConvertor)c).convert(Task1Test.SKK, Task1Test.CZK, new BigDecimal("500.00"), date);
1.229 + assertEquals(new BigDecimal("450.00"), amount);
1.230 }
1.231
1.232 }