1 package org.apidesign.apifest08.test;
4 import junit.framework.TestCase;
5 import org.apidesign.apifest08.currency.Convertor;
7 /** The exchange rates are not always the same. They are changing. However
8 * as in order to predict the future, one needs to understand own past. That is
9 * why it is important to know the exchange rate as it was at any time during
12 * Today's quest is to enhance the convertor API to deal with dates.
13 * One shall be able to convert a currency at any date. Each currencies rate shall
14 * be associated with a range between two Date objects. In order
15 * to keep compatibility with old API that knew nothing about dates, the
16 * rates associated then are applicable "for eternity". Any use of existing
17 * convert methods that do not accept a Date argument, uses the current
18 * System.currentTimeMillis() as default date.
20 public class Task4Test extends TestCase {
21 public Task4Test(String testName) {
26 protected void setUp() throws Exception {
30 protected void tearDown() throws Exception {
33 // Backward compatibly enhance your existing API to support following
37 /** Takes a convertor with any rates associated and creates new convertor
38 * that returns the same values as the old one for time between from to till.
39 * Otherwise it returns no results. This is just a helper method that
40 * shall call some real one in the API.
42 * @param old existing convertor
43 * @param from initial date (inclusive)
44 * @param till final date (exclusive)
45 * @return new convertor
47 public static Convertor limitTo(Convertor old, Date from, Date till) {
52 public void testCompositionOfLimitedConvertors() throws Exception {
53 if (Boolean.getBoolean("ignore.failing")) {
54 // implement me! then delete this if statement
58 Date d1 = null; // 2008-10-01 0:00 GMT
59 Date d2 = null; // 2008-10-02 0:00 GMT
60 Date d3 = null; // 2008-10-03 0:00 GMT
62 Convertor c = Task2Test.merge(
63 limitTo(Task1Test.createCZKtoUSD(), d1, d2),
64 limitTo(Task1Test.createSKKtoCZK(), d2, d3)
67 // convert $5 to CZK using c:
68 // cannot convert as no rate is applicable to current date
70 // convert $8 to CZK using c:
71 // cannot convert as no rate is applicable to current date
73 // convert 1003CZK to USD using c:
74 // cannot convert as no rate is applicable to current date
76 // convert 16CZK using c:
77 // cannot convert as no rate is applicable to current date
79 // convert 500SKK to CZK using c:
80 // cannot convert as no rate is applicable to current date
82 // convert $5 to CZK using c at 2008-10-01 6:00 GMT:
83 // assertEquals("Result is 85 CZK");
85 // convert $8 to CZK using c at 2008-10-01 6:00 GMT:
86 // assertEquals("Result is 136 CZK");
88 // convert 1003CZK to USD using c at 2008-10-01 6:00 GMT:
89 // assertEquals("Result is 59 USD");
91 // convert 16CZK using c at 2008-10-02 9:00 GMT:
92 // assertEquals("Result is 20 SKK");
94 // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
95 // assertEquals("Result is 400 CZK");
97 // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
98 // cannot convert as no rate is applicable to current date
101 /** Create convertor that understands two currencies, CZK and
102 * SKK. Make 100 SKK == 90 CZK.
104 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
106 public static Convertor createSKKtoCZK2() {
110 public void testDateConvetorWithTwoDifferentRates() throws Exception {
111 if (Boolean.getBoolean("ignore.failing")) {
112 // implement me! then delete this if statement
116 Date d1 = null; // 2008-10-01 0:00 GMT
117 Date d2 = null; // 2008-10-02 0:00 GMT
118 Date d3 = null; // 2008-10-03 0:00 GMT
120 Convertor c = Task2Test.merge(
121 limitTo(createSKKtoCZK2(), d1, d2),
122 limitTo(Task1Test.createSKKtoCZK(), d2, d3)
125 // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
126 // assertEquals("Result is 400 CZK");
128 // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
129 // assertEquals("Result is 450 CZK");