jaroslav@60
|
1 |
package org.apidesign.apifest08.test;
|
jaroslav@60
|
2 |
|
jaroslav@68
|
3 |
import java.text.SimpleDateFormat;
|
jaroslav@68
|
4 |
import java.util.Currency;
|
jaroslav@60
|
5 |
import java.util.Date;
|
jaroslav@60
|
6 |
import junit.framework.TestCase;
|
jaroslav@60
|
7 |
import org.apidesign.apifest08.currency.Convertor;
|
jaroslav@68
|
8 |
import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
|
jaroslav@60
|
9 |
|
jaroslav@60
|
10 |
/** The exchange rates are not always the same. They are changing. However
|
jaroslav@60
|
11 |
* as in order to predict the future, one needs to understand own past. That is
|
jaroslav@60
|
12 |
* why it is important to know the exchange rate as it was at any time during
|
jaroslav@60
|
13 |
* the past.
|
jaroslav@60
|
14 |
* <p>
|
jaroslav@60
|
15 |
* Today's quest is to enhance the convertor API to deal with dates.
|
jaroslav@60
|
16 |
* One shall be able to convert a currency at any date. Each currencies rate shall
|
jaroslav@60
|
17 |
* be associated with a range between two Date objects. In order
|
jaroslav@60
|
18 |
* to keep compatibility with old API that knew nothing about dates, the
|
jaroslav@60
|
19 |
* rates associated then are applicable "for eternity". Any use of existing
|
jaroslav@60
|
20 |
* convert methods that do not accept a Date argument, uses the current
|
jaroslav@60
|
21 |
* System.currentTimeMillis() as default date.
|
jaroslav@60
|
22 |
*/
|
jaroslav@60
|
23 |
public class Task4Test extends TestCase {
|
jaroslav@60
|
24 |
public Task4Test(String testName) {
|
jaroslav@60
|
25 |
super(testName);
|
jaroslav@60
|
26 |
}
|
jaroslav@60
|
27 |
|
jaroslav@60
|
28 |
@Override
|
jaroslav@60
|
29 |
protected void setUp() throws Exception {
|
jaroslav@60
|
30 |
}
|
jaroslav@60
|
31 |
|
jaroslav@60
|
32 |
@Override
|
jaroslav@60
|
33 |
protected void tearDown() throws Exception {
|
jaroslav@60
|
34 |
}
|
jaroslav@60
|
35 |
|
jaroslav@60
|
36 |
// Backward compatibly enhance your existing API to support following
|
jaroslav@60
|
37 |
// usecases:
|
jaroslav@60
|
38 |
//
|
jaroslav@60
|
39 |
|
jaroslav@60
|
40 |
/** Takes a convertor with any rates associated and creates new convertor
|
jaroslav@60
|
41 |
* that returns the same values as the old one for time between from to till.
|
jaroslav@60
|
42 |
* Otherwise it returns no results. This is just a helper method that
|
jaroslav@60
|
43 |
* shall call some real one in the API.
|
jaroslav@60
|
44 |
*
|
jaroslav@60
|
45 |
* @param old existing convertor
|
jaroslav@60
|
46 |
* @param from initial date (inclusive)
|
jaroslav@60
|
47 |
* @param till final date (exclusive)
|
jaroslav@60
|
48 |
* @return new convertor
|
jaroslav@60
|
49 |
*/
|
jaroslav@60
|
50 |
public static Convertor limitTo(Convertor old, Date from, Date till) {
|
jaroslav@68
|
51 |
return Convertor.limitExchangeRatesValidity(old, from, till);
|
jaroslav@60
|
52 |
}
|
jaroslav@60
|
53 |
|
jaroslav@60
|
54 |
|
jaroslav@60
|
55 |
public void testCompositionOfLimitedConvertors() throws Exception {
|
jaroslav@68
|
56 |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz");
|
jaroslav@68
|
57 |
Date d1 = format.parse("2008-10-01 00:00 GMT"); // 2008-10-01 0:00 GMT
|
jaroslav@68
|
58 |
Date d2 = format.parse("2008-10-02 00:00 GMT"); // 2008-10-02 0:00 GMT
|
jaroslav@68
|
59 |
Date d3 = format.parse("2008-10-03 00:00 GMT"); // 2008-10-03 0:00 GMT
|
jaroslav@60
|
60 |
|
jaroslav@60
|
61 |
Convertor c = Task2Test.merge(
|
jaroslav@60
|
62 |
limitTo(Task1Test.createCZKtoUSD(), d1, d2),
|
jaroslav@60
|
63 |
limitTo(Task1Test.createSKKtoCZK(), d2, d3)
|
jaroslav@60
|
64 |
);
|
jaroslav@68
|
65 |
|
jaroslav@68
|
66 |
Date now = new Date();
|
jaroslav@60
|
67 |
|
jaroslav@60
|
68 |
// convert $5 to CZK using c:
|
jaroslav@68
|
69 |
double result = 0;
|
jaroslav@68
|
70 |
boolean exceptionThrown = false;
|
jaroslav@68
|
71 |
try {
|
jaroslav@68
|
72 |
result = c.convert(5, Currency.getInstance("USD"), Currency.getInstance("CZK"));
|
jaroslav@68
|
73 |
} catch (Exception e) {
|
jaroslav@68
|
74 |
exceptionThrown = true;
|
jaroslav@68
|
75 |
}
|
jaroslav@68
|
76 |
|
jaroslav@68
|
77 |
if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
|
jaroslav@68
|
78 |
System.out.println("Result.");
|
jaroslav@68
|
79 |
assertEquals("Result is not 85 CZK.", 85d, result);
|
jaroslav@68
|
80 |
} else {
|
jaroslav@68
|
81 |
System.out.println("Exception.");
|
jaroslav@68
|
82 |
assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
|
jaroslav@68
|
83 |
}
|
jaroslav@60
|
84 |
|
jaroslav@60
|
85 |
// convert $8 to CZK using c:
|
jaroslav@68
|
86 |
exceptionThrown = false;
|
jaroslav@68
|
87 |
try {
|
jaroslav@68
|
88 |
result = c.convert(8, Currency.getInstance("USD"), Currency.getInstance("CZK"));
|
jaroslav@68
|
89 |
} catch (Exception e) {
|
jaroslav@68
|
90 |
exceptionThrown = true;
|
jaroslav@68
|
91 |
}
|
jaroslav@68
|
92 |
|
jaroslav@68
|
93 |
if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
|
jaroslav@68
|
94 |
System.out.println("Result.");
|
jaroslav@68
|
95 |
assertEquals("Result is not 136 CZK.", 136d, result);
|
jaroslav@68
|
96 |
} else {
|
jaroslav@68
|
97 |
System.out.println("Exception.");
|
jaroslav@68
|
98 |
assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
|
jaroslav@68
|
99 |
}
|
jaroslav@60
|
100 |
|
jaroslav@60
|
101 |
// convert 1003CZK to USD using c:
|
jaroslav@68
|
102 |
exceptionThrown = false;
|
jaroslav@68
|
103 |
try {
|
jaroslav@68
|
104 |
result = c.convert(1003, Currency.getInstance("CZK"), Currency.getInstance("USD"));
|
jaroslav@68
|
105 |
} catch (Exception e) {
|
jaroslav@68
|
106 |
exceptionThrown = true;
|
jaroslav@68
|
107 |
}
|
jaroslav@68
|
108 |
|
jaroslav@68
|
109 |
if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
|
jaroslav@68
|
110 |
System.out.println("Result.");
|
jaroslav@68
|
111 |
assertEquals("Result is not 59 USD.", 59d, result);
|
jaroslav@68
|
112 |
} else {
|
jaroslav@68
|
113 |
System.out.println("Exception.");
|
jaroslav@68
|
114 |
assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
|
jaroslav@68
|
115 |
}
|
jaroslav@60
|
116 |
|
jaroslav@60
|
117 |
// convert 16CZK using c:
|
jaroslav@68
|
118 |
exceptionThrown = false;
|
jaroslav@68
|
119 |
try {
|
jaroslav@68
|
120 |
result = c.convert(16, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
|
jaroslav@68
|
121 |
} catch (Exception e) {
|
jaroslav@68
|
122 |
exceptionThrown = true;
|
jaroslav@68
|
123 |
}
|
jaroslav@68
|
124 |
|
jaroslav@68
|
125 |
if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
|
jaroslav@68
|
126 |
System.out.println("Result.");
|
jaroslav@68
|
127 |
assertEquals("Result is not 20 SKK.", 20d, result);
|
jaroslav@68
|
128 |
} else {
|
jaroslav@68
|
129 |
System.out.println("Exception.");
|
jaroslav@68
|
130 |
assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
|
jaroslav@68
|
131 |
}
|
jaroslav@60
|
132 |
|
jaroslav@60
|
133 |
// convert 500SKK to CZK using c:
|
jaroslav@68
|
134 |
exceptionThrown = false;
|
jaroslav@68
|
135 |
try {
|
jaroslav@68
|
136 |
result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
|
jaroslav@68
|
137 |
} catch (Exception e) {
|
jaroslav@68
|
138 |
exceptionThrown = true;
|
jaroslav@68
|
139 |
}
|
jaroslav@68
|
140 |
|
jaroslav@68
|
141 |
if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
|
jaroslav@68
|
142 |
System.out.println("Result.");
|
jaroslav@68
|
143 |
assertEquals("Result is not 400 CZK.", 400d, result);
|
jaroslav@68
|
144 |
} else {
|
jaroslav@68
|
145 |
System.out.println("Exception.");
|
jaroslav@68
|
146 |
assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
|
jaroslav@68
|
147 |
}
|
jaroslav@60
|
148 |
|
jaroslav@60
|
149 |
}
|
jaroslav@60
|
150 |
|
jaroslav@60
|
151 |
/** Create convertor that understands two currencies, CZK and
|
jaroslav@60
|
152 |
* SKK. Make 100 SKK == 90 CZK.
|
jaroslav@60
|
153 |
*
|
jaroslav@60
|
154 |
* @return prepared convertor ready for converting SKK to CZK and CZK to SKK
|
jaroslav@60
|
155 |
*/
|
jaroslav@60
|
156 |
public static Convertor createSKKtoCZK2() {
|
jaroslav@68
|
157 |
// set exchange rates
|
jaroslav@68
|
158 |
Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 90d, 100d);
|
jaroslav@68
|
159 |
|
jaroslav@68
|
160 |
// create new instance
|
jaroslav@68
|
161 |
Convertor convertor = null;
|
jaroslav@68
|
162 |
try {
|
jaroslav@68
|
163 |
convertor = Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
|
jaroslav@68
|
164 |
} catch (UnknownConvertorException e) {
|
jaroslav@68
|
165 |
e.printStackTrace();
|
jaroslav@68
|
166 |
}
|
jaroslav@68
|
167 |
|
jaroslav@68
|
168 |
return convertor;
|
jaroslav@60
|
169 |
}
|
jaroslav@60
|
170 |
|
jaroslav@60
|
171 |
public void testDateConvetorWithTwoDifferentRates() throws Exception {
|
jaroslav@60
|
172 |
|
jaroslav@68
|
173 |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz");
|
jaroslav@68
|
174 |
Date d1 = format.parse("2008-10-01 00:00 GMT"); // 2008-10-01 0:00 GMT
|
jaroslav@68
|
175 |
Date d2 = format.parse("2008-10-02 00:00 GMT"); // 2008-10-02 0:00 GMT
|
jaroslav@68
|
176 |
Date d3 = format.parse("2008-10-03 00:00 GMT"); // 2008-10-03 0:00 GMT
|
jaroslav@60
|
177 |
|
jaroslav@60
|
178 |
Convertor c = Task2Test.merge(
|
jaroslav@60
|
179 |
limitTo(createSKKtoCZK2(), d1, d2),
|
jaroslav@60
|
180 |
limitTo(Task1Test.createSKKtoCZK(), d2, d3)
|
jaroslav@60
|
181 |
);
|
jaroslav@60
|
182 |
|
jaroslav@60
|
183 |
// convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
|
jaroslav@60
|
184 |
// assertEquals("Result is 400 CZK");
|
jaroslav@60
|
185 |
|
jaroslav@60
|
186 |
// convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
|
jaroslav@60
|
187 |
// assertEquals("Result is 450 CZK");
|
jaroslav@68
|
188 |
Date now = new Date();
|
jaroslav@68
|
189 |
|
jaroslav@68
|
190 |
// convert 500SKK to CZK using c:
|
jaroslav@68
|
191 |
double result = 0;
|
jaroslav@68
|
192 |
boolean exceptionThrown = false;
|
jaroslav@68
|
193 |
try {
|
jaroslav@68
|
194 |
result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
|
jaroslav@68
|
195 |
} catch (Exception e) {
|
jaroslav@68
|
196 |
exceptionThrown = true;
|
jaroslav@68
|
197 |
}
|
jaroslav@68
|
198 |
|
jaroslav@68
|
199 |
if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
|
jaroslav@68
|
200 |
System.out.println("\nResult");
|
jaroslav@68
|
201 |
assertEquals("Result is not 450 CZK.", 450d, result);
|
jaroslav@68
|
202 |
} else if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
|
jaroslav@68
|
203 |
System.out.println("\nResult");
|
jaroslav@68
|
204 |
assertEquals("Result is not 400 CZK.", 400d, result);
|
jaroslav@68
|
205 |
} else {
|
jaroslav@68
|
206 |
System.out.println("\nException");
|
jaroslav@68
|
207 |
assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
|
jaroslav@68
|
208 |
}
|
jaroslav@60
|
209 |
}
|
jaroslav@60
|
210 |
|
jaroslav@60
|
211 |
}
|