1 package org.apidesign.apifest08.test;
3 import java.text.SimpleDateFormat;
4 import java.util.Currency;
6 import junit.framework.TestCase;
7 import org.apidesign.apifest08.currency.Convertor;
8 import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
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
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.
23 public class Task4Test extends TestCase {
24 public Task4Test(String testName) {
29 protected void setUp() throws Exception {
33 protected void tearDown() throws Exception {
36 // Backward compatibly enhance your existing API to support following
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.
45 * @param old existing convertor
46 * @param from initial date (inclusive)
47 * @param till final date (exclusive)
48 * @return new convertor
50 public static Convertor limitTo(Convertor old, Date from, Date till) {
51 return Convertor.limitExchangeRatesValidity(old, from, till);
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
61 Convertor c = Task2Test.merge(
62 limitTo(Task1Test.createCZKtoUSD(), d1, d2),
63 limitTo(Task1Test.createSKKtoCZK(), d2, d3)
66 Date now = new Date();
68 // convert $5 to CZK using c:
70 boolean exceptionThrown = false;
72 result = c.convert(5, Currency.getInstance("USD"), Currency.getInstance("CZK"));
73 } catch (Exception e) {
74 exceptionThrown = true;
77 if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
78 System.out.println("Result.");
79 assertEquals("Result is not 85 CZK.", 85d, result);
81 System.out.println("Exception.");
82 assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
85 // convert $8 to CZK using c:
86 exceptionThrown = false;
88 result = c.convert(8, Currency.getInstance("USD"), Currency.getInstance("CZK"));
89 } catch (Exception e) {
90 exceptionThrown = true;
93 if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
94 System.out.println("Result.");
95 assertEquals("Result is not 136 CZK.", 136d, result);
97 System.out.println("Exception.");
98 assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
101 // convert 1003CZK to USD using c:
102 exceptionThrown = false;
104 result = c.convert(1003, Currency.getInstance("CZK"), Currency.getInstance("USD"));
105 } catch (Exception e) {
106 exceptionThrown = true;
109 if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
110 System.out.println("Result.");
111 assertEquals("Result is not 59 USD.", 59d, result);
113 System.out.println("Exception.");
114 assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
117 // convert 16CZK using c:
118 exceptionThrown = false;
120 result = c.convert(16, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
121 } catch (Exception e) {
122 exceptionThrown = true;
125 if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
126 System.out.println("Result.");
127 assertEquals("Result is not 20 SKK.", 20d, result);
129 System.out.println("Exception.");
130 assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
133 // convert 500SKK to CZK using c:
134 exceptionThrown = false;
136 result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
137 } catch (Exception e) {
138 exceptionThrown = true;
141 if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
142 System.out.println("Result.");
143 assertEquals("Result is not 400 CZK.", 400d, result);
145 System.out.println("Exception.");
146 assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
151 /** Create convertor that understands two currencies, CZK and
152 * SKK. Make 100 SKK == 90 CZK.
154 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
156 public static Convertor createSKKtoCZK2() {
157 // set exchange rates
158 Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 90d, 100d);
160 // create new instance
161 Convertor convertor = null;
163 convertor = Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
164 } catch (UnknownConvertorException e) {
171 public void testDateConvetorWithTwoDifferentRates() throws Exception {
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
178 Convertor c = Task2Test.merge(
179 limitTo(createSKKtoCZK2(), d1, d2),
180 limitTo(Task1Test.createSKKtoCZK(), d2, d3)
183 // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
184 // assertEquals("Result is 400 CZK");
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();
190 // convert 500SKK to CZK using c:
192 boolean exceptionThrown = false;
194 result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
195 } catch (Exception e) {
196 exceptionThrown = true;
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);
206 System.out.println("\nException");
207 assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);