1.1 --- a/task4/solution14/test/org/apidesign/apifest08/test/Task4Test.java Sat Oct 11 23:46:05 2008 +0200
1.2 +++ b/task4/solution14/test/org/apidesign/apifest08/test/Task4Test.java Fri Oct 17 17:35:52 2008 +0200
1.3 @@ -1,8 +1,10 @@
1.4 package org.apidesign.apifest08.test;
1.5
1.6 import java.util.Date;
1.7 +import java.util.GregorianCalendar;
1.8 import junit.framework.TestCase;
1.9 import org.apidesign.apifest08.currency.Convertor;
1.10 +import org.apidesign.apifest08.currency.ConvertorFactory;
1.11
1.12 /** The exchange rates are not always the same. They are changing. However
1.13 * as in order to predict the future, one needs to understand own past. That is
1.14 @@ -45,19 +47,14 @@
1.15 * @return new convertor
1.16 */
1.17 public static Convertor limitTo(Convertor old, Date from, Date till) {
1.18 - return null;
1.19 + return ConvertorFactory.newInstance().limitConvertor(old, from, till);
1.20 }
1.21
1.22
1.23 public void testCompositionOfLimitedConvertors() throws Exception {
1.24 - if (Boolean.getBoolean("ignore.failing")) {
1.25 - // implement me! then delete this if statement
1.26 - return;
1.27 - }
1.28 -
1.29 - Date d1 = null; // 2008-10-01 0:00 GMT
1.30 - Date d2 = null; // 2008-10-02 0:00 GMT
1.31 - Date d3 = null; // 2008-10-03 0:00 GMT
1.32 + Date d1 = (new GregorianCalendar(2008, 9, 1, 0, 0)).getTime(); // 2008-10-01 0:00 GMT
1.33 + Date d2 = (new GregorianCalendar(2008, 9, 2, 0, 0)).getTime(); // 2008-10-02 0:00 GMT
1.34 + Date d3 = (new GregorianCalendar(2008, 9, 3, 0, 0)).getTime(); // 2008-10-03 0:00 GMT
1.35
1.36 Convertor c = Task2Test.merge(
1.37 limitTo(Task1Test.createCZKtoUSD(), d1, d2),
1.38 @@ -66,36 +63,77 @@
1.39
1.40 // convert $5 to CZK using c:
1.41 // cannot convert as no rate is applicable to current date
1.42 + try {
1.43 + c.convert("USD", "CZK", 5);
1.44 + fail();
1.45 + } catch (IllegalArgumentException e) {
1.46 + //ok
1.47 + }
1.48
1.49 // convert $8 to CZK using c:
1.50 // cannot convert as no rate is applicable to current date
1.51 + try {
1.52 + c.convert("USD", "CZK", 8);
1.53 + fail();
1.54 + } catch (IllegalArgumentException e) {
1.55 + //ok
1.56 + }
1.57
1.58 // convert 1003CZK to USD using c:
1.59 // cannot convert as no rate is applicable to current date
1.60 + try {
1.61 + c.convert("CZK", "USD", 1003);
1.62 + fail();
1.63 + } catch (IllegalArgumentException e) {
1.64 + //ok
1.65 + }
1.66
1.67 // convert 16CZK using c:
1.68 // cannot convert as no rate is applicable to current date
1.69 + try {
1.70 + c.convert("CZK", "USD", 16);
1.71 + fail();
1.72 + } catch (IllegalArgumentException e) {
1.73 + //ok
1.74 + }
1.75
1.76 // convert 500SKK to CZK using c:
1.77 // cannot convert as no rate is applicable to current date
1.78 + try {
1.79 + c.convert("SKK", "CZK", 500);
1.80 + fail();
1.81 + } catch (IllegalArgumentException e) {
1.82 + //ok
1.83 + }
1.84
1.85 // convert $5 to CZK using c at 2008-10-01 6:00 GMT:
1.86 // assertEquals("Result is 85 CZK");
1.87 + assertEquals("Result is 85 CZK", 85.0, c.convert("USD", "CZK", 5, (new GregorianCalendar(2008,9,1,6,0)).getTime()));
1.88
1.89 // convert $8 to CZK using c at 2008-10-01 6:00 GMT:
1.90 // assertEquals("Result is 136 CZK");
1.91 + assertEquals("Result is 136 CZK", 136.0, c.convert("USD", "CZK", 8, (new GregorianCalendar(2008,9,1,6,0)).getTime()));
1.92
1.93 // convert 1003CZK to USD using c at 2008-10-01 6:00 GMT:
1.94 // assertEquals("Result is 59 USD");
1.95 + assertEquals("Result is 59 USD", 59.0, c.convert("CZK", "USD", 1003.0, (new GregorianCalendar(2008, 9, 1, 6, 0)).getTime()));
1.96
1.97 // convert 16CZK using c at 2008-10-02 9:00 GMT:
1.98 // assertEquals("Result is 20 SKK");
1.99 + assertEquals("Result is 20 SKK", 20.0, c.convert("CZK", "SKK", 16.0, (new GregorianCalendar(2008, 9, 2, 9, 0)).getTime()));
1.100
1.101 // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
1.102 // assertEquals("Result is 400 CZK");
1.103 + assertEquals("Result is 400 CZK", 400.0, c.convert("SKK", "CZK", 500.0, (new GregorianCalendar(2008, 9, 2, 9, 0)).getTime()));
1.104
1.105 // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
1.106 // cannot convert as no rate is applicable to current date
1.107 + try {
1.108 + c.convert("SKK", "CZK", 500.0, (new GregorianCalendar(2008, 9, 1, 6, 0)).getTime());
1.109 + fail();
1.110 + } catch (IllegalArgumentException e) {
1.111 + //ok
1.112 + }
1.113 }
1.114
1.115 /** Create convertor that understands two currencies, CZK and
1.116 @@ -104,18 +142,13 @@
1.117 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
1.118 */
1.119 public static Convertor createSKKtoCZK2() {
1.120 - return null;
1.121 + return ConvertorFactory.newInstance().createConvertor("SKK", "CZK", 100, 90);
1.122 }
1.123
1.124 public void testDateConvetorWithTwoDifferentRates() throws Exception {
1.125 - if (Boolean.getBoolean("ignore.failing")) {
1.126 - // implement me! then delete this if statement
1.127 - return;
1.128 - }
1.129 -
1.130 - Date d1 = null; // 2008-10-01 0:00 GMT
1.131 - Date d2 = null; // 2008-10-02 0:00 GMT
1.132 - Date d3 = null; // 2008-10-03 0:00 GMT
1.133 + Date d1 = (new GregorianCalendar(2008,9,1,0,0)).getTime(); // 2008-10-01 0:00 GMT
1.134 + Date d2 = (new GregorianCalendar(2008,9,2,0,0)).getTime(); // 2008-10-02 0:00 GMT
1.135 + Date d3 = (new GregorianCalendar(2008,9,6,0,0)).getTime(); // 2008-10-03 0:00 GMT
1.136
1.137 Convertor c = Task2Test.merge(
1.138 limitTo(createSKKtoCZK2(), d1, d2),
1.139 @@ -124,9 +157,11 @@
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 CZK", 400.0, c.convert("SKK", "CZK", 500.0, (new GregorianCalendar(2008, 9, 2, 9, 0)).getTime()));
1.144
1.145 // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
1.146 // assertEquals("Result is 450 CZK");
1.147 + assertEquals("Result is 450 CZK", 450.0, c.convert("SKK", "CZK", 500.0, (new GregorianCalendar(2008, 9, 1, 6, 0)).getTime()));
1.148 }
1.149
1.150 }