1.1 --- a/task4/solution13/test/org/apidesign/apifest08/test/Task4Test.java Sat Oct 11 23:46:05 2008 +0200
1.2 +++ b/task4/solution13/test/org/apidesign/apifest08/test/Task4Test.java Fri Oct 17 17:31:48 2008 +0200
1.3 @@ -1,8 +1,13 @@
1.4 package org.apidesign.apifest08.test;
1.5
1.6 +import java.math.BigDecimal;
1.7 +import java.text.SimpleDateFormat;
1.8 import java.util.Date;
1.9 import junit.framework.TestCase;
1.10 +import org.apidesign.apifest08.currency.ConversionNotSupportedException;
1.11 import org.apidesign.apifest08.currency.Convertor;
1.12 +import org.apidesign.apifest08.currency.ConvertorCurrency;
1.13 +import org.apidesign.apifest08.currency.ExchangeRateProvider;
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 @@ -18,16 +23,26 @@
1.18 * System.currentTimeMillis() as default date.
1.19 */
1.20 public class Task4Test extends TestCase {
1.21 + private static ConvertorCurrency CZK = ConvertorCurrency.getInstance("CZK");
1.22 + private static ConvertorCurrency SKK = ConvertorCurrency.getInstance("SKK");
1.23 + private static ConvertorCurrency USD = ConvertorCurrency.getInstance("USD");
1.24 + private SimpleDateFormat df;
1.25 +
1.26 +
1.27 public Task4Test(String testName) {
1.28 super(testName);
1.29 }
1.30
1.31 @Override
1.32 protected void setUp() throws Exception {
1.33 + super.setUp();
1.34 + df = new SimpleDateFormat("yyyy-MM-dd HH:mm zzzz");
1.35 }
1.36
1.37 @Override
1.38 protected void tearDown() throws Exception {
1.39 + super.tearDown();
1.40 + df = null;
1.41 }
1.42
1.43 // Backward compatibly enhance your existing API to support following
1.44 @@ -45,19 +60,22 @@
1.45 * @return new convertor
1.46 */
1.47 public static Convertor limitTo(Convertor old, Date from, Date till) {
1.48 - return null;
1.49 + Convertor c = Convertor.createConvertorAsMerge(new Convertor[]{old});
1.50 + c.limitAllowedDates(from, till);
1.51 + return c;
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 +// if (Boolean.getBoolean("ignore.failing")) {
1.61 +// // implement me! then delete this if statement
1.62 +// return;
1.63 +// }
1.64 +
1.65
1.66 - Date d1 = null; // 2008-10-01 0:00 GMT
1.67 - Date d2 = null; // 2008-10-02 0:00 GMT
1.68 - Date d3 = null; // 2008-10-03 0:00 GMT
1.69 + Date d1 = df.parse("2008-10-01 0:00 GMT");
1.70 + Date d2 = df.parse("2008-10-02 0:00 GMT");
1.71 + Date d3 = df.parse("2008-10-03 0:00 GMT");
1.72
1.73 Convertor c = Task2Test.merge(
1.74 limitTo(Task1Test.createCZKtoUSD(), d1, d2),
1.75 @@ -66,36 +84,81 @@
1.76
1.77 // convert $5 to CZK using c:
1.78 // cannot convert as no rate is applicable to current date
1.79 + try {
1.80 + c.convert(USD, CZK, new BigDecimal(5));
1.81 + fail();
1.82 + } catch (ConversionNotSupportedException e) {
1.83 + //exception expected
1.84 + }
1.85
1.86 // convert $8 to CZK using c:
1.87 // cannot convert as no rate is applicable to current date
1.88 + try {
1.89 + c.convert(USD, CZK, new BigDecimal(8));
1.90 + fail();
1.91 + } catch (ConversionNotSupportedException e) {
1.92 + //exception expected
1.93 + }
1.94 +
1.95
1.96 // convert 1003CZK to USD using c:
1.97 // cannot convert as no rate is applicable to current date
1.98 + try {
1.99 + c.convert(CZK, USD, new BigDecimal(1003));
1.100 + fail();
1.101 + } catch (ConversionNotSupportedException e) {
1.102 + //exception expected
1.103 + }
1.104
1.105 // convert 16CZK using c:
1.106 // cannot convert as no rate is applicable to current date
1.107 + // ???
1.108 + try {
1.109 + c.convert(CZK, USD, new BigDecimal(16));
1.110 + fail();
1.111 + } catch (ConversionNotSupportedException e) {
1.112 + //exception expected
1.113 + }
1.114
1.115 // convert 500SKK to CZK using c:
1.116 // cannot convert as no rate is applicable to current date
1.117 + try {
1.118 + c.convert(SKK, CZK, new BigDecimal(500));
1.119 + fail();
1.120 + } catch (ConversionNotSupportedException e) {
1.121 + //exception expected
1.122 + }
1.123 +
1.124
1.125 // convert $5 to CZK using c at 2008-10-01 6:00 GMT:
1.126 // assertEquals("Result is 85 CZK");
1.127 + assertEquals("Result is 85 CZK", new BigDecimal("85.00"), c.convertWithReversibleRates(USD, CZK , new BigDecimal("5"),df.parse("2008-10-01 6:00 GMT")).getConverted());
1.128
1.129 // convert $8 to CZK using c at 2008-10-01 6:00 GMT:
1.130 // assertEquals("Result is 136 CZK");
1.131 + assertEquals("Result is 136 CZK", new BigDecimal("136.00"), c.convertWithReversibleRates(USD, CZK , new BigDecimal("8"),df.parse("2008-10-01 6:00 GMT")).getConverted());
1.132
1.133 // convert 1003CZK to USD using c at 2008-10-01 6:00 GMT:
1.134 // assertEquals("Result is 59 USD");
1.135 + assertEquals("Result is 59 USD", new BigDecimal("59.00"), c.convertWithReversibleRates(CZK, USD , new BigDecimal("1003"),df.parse("2008-10-01 6:00 GMT")).getConverted());
1.136
1.137 // convert 16CZK using c at 2008-10-02 9:00 GMT:
1.138 // assertEquals("Result is 20 SKK");
1.139 + assertEquals("Result is 20 SKK", new BigDecimal("20.00"), c.convertWithReversibleRates(CZK, SKK , new BigDecimal("16"),df.parse("2008-10-02 9:00 GMT")).getConverted());
1.140
1.141 // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
1.142 // assertEquals("Result is 400 CZK");
1.143 + assertEquals("Result is 400 SKK", new BigDecimal("400.00"), c.convertWithReversibleRates(SKK, CZK , new BigDecimal("500"),df.parse("2008-10-02 9:00 GMT")).getConverted());
1.144
1.145 // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
1.146 // cannot convert as no rate is applicable to current date
1.147 + try {
1.148 + c.convertWithReversibleRates(SKK, CZK , new BigDecimal("500"),df.parse("2008-10-01 6:00 GMT")).getConverted();
1.149 + fail();
1.150 + } catch (ConversionNotSupportedException e) {
1.151 + //exception expected
1.152 + }
1.153 +
1.154 }
1.155
1.156 /** Create convertor that understands two currencies, CZK and
1.157 @@ -104,18 +167,17 @@
1.158 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
1.159 */
1.160 public static Convertor createSKKtoCZK2() {
1.161 - return null;
1.162 + ExchangeRateProvider exchangeRateProvider = ExchangeRateProvider.createExchangeRateProvider();
1.163 + exchangeRateProvider.addFixedCurencyRate(SKK, new BigDecimal("100.00"), CZK, new BigDecimal("90.00"));
1.164 + Convertor c = Convertor.createConvertor(exchangeRateProvider);
1.165 + return c;
1.166 }
1.167
1.168 public void testDateConvetorWithTwoDifferentRates() throws Exception {
1.169 - if (Boolean.getBoolean("ignore.failing")) {
1.170 - // implement me! then delete this if statement
1.171 - return;
1.172 - }
1.173
1.174 - Date d1 = null; // 2008-10-01 0:00 GMT
1.175 - Date d2 = null; // 2008-10-02 0:00 GMT
1.176 - Date d3 = null; // 2008-10-03 0:00 GMT
1.177 + Date d1 = df.parse("2008-10-01 0:00 GMT");
1.178 + Date d2 = df.parse("2008-10-02 0:00 GMT");
1.179 + Date d3 = df.parse("2008-10-03 0:00 GMT");
1.180
1.181 Convertor c = Task2Test.merge(
1.182 limitTo(createSKKtoCZK2(), d1, d2),
1.183 @@ -123,10 +185,10 @@
1.184 );
1.185
1.186 // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
1.187 - // assertEquals("Result is 400 CZK");
1.188 + assertEquals("Result is 400 CZK", new BigDecimal("400.00"), c.convertWithReversibleRates(SKK, CZK , new BigDecimal("500"),df.parse("2008-10-02 9:00 GMT")).getConverted());
1.189
1.190 // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
1.191 - // assertEquals("Result is 450 CZK");
1.192 + assertEquals("Result is 450 CZK", new BigDecimal("450.00"), c.convertWithReversibleRates(SKK, CZK , new BigDecimal("500"),df.parse("2008-10-01 9:00 GMT")).getConverted());
1.193 }
1.194
1.195 }