japod@23
|
1 |
package org.apidesign.apifest08.test;
|
japod@23
|
2 |
|
japod@23
|
3 |
import java.math.BigDecimal;
|
japod@23
|
4 |
import junit.framework.TestCase;
|
japod@23
|
5 |
import org.apidesign.apifest08.currency.ConversionResult;
|
japod@23
|
6 |
import org.apidesign.apifest08.currency.Convertor;
|
japod@23
|
7 |
import org.apidesign.apifest08.currency.ConvertorCurrency;
|
japod@23
|
8 |
import org.apidesign.apifest08.currency.ExchangeRateProvider;
|
japod@41
|
9 |
import org.apidesign.apifest08.currency.ConversionNotSupportedException;
|
japod@23
|
10 |
|
japod@23
|
11 |
/** Finish the Convertor API, and then write bodies of methods inside
|
japod@23
|
12 |
* of this class to match the given tasks. To fullfil your task, use the
|
japod@23
|
13 |
* API define in the <code>org.apidesign.apifest08.currency</code> package.
|
japod@23
|
14 |
* Do not you reflection, or other hacks as your code
|
japod@23
|
15 |
* shall run without any runtime permissions.
|
japod@23
|
16 |
*/
|
japod@23
|
17 |
public class Task1Test extends TestCase {
|
japod@23
|
18 |
public Task1Test(String testName) {
|
japod@23
|
19 |
super(testName);
|
japod@23
|
20 |
}
|
japod@23
|
21 |
|
japod@23
|
22 |
@Override
|
japod@23
|
23 |
protected void setUp() throws Exception {
|
japod@23
|
24 |
}
|
japod@23
|
25 |
|
japod@23
|
26 |
@Override
|
japod@23
|
27 |
protected void tearDown() throws Exception {
|
japod@23
|
28 |
}
|
japod@23
|
29 |
|
japod@23
|
30 |
/** Create convertor that understands two currencies, CZK and
|
japod@23
|
31 |
* USD. Make 1 USD == 17 CZK.
|
japod@23
|
32 |
*
|
japod@23
|
33 |
* Creation of the convertor shall not require subclassing of any class
|
japod@23
|
34 |
* or interface on the client side.
|
japod@23
|
35 |
*
|
japod@23
|
36 |
* @return prepared convertor ready for converting USD to CZK and CZK to USD
|
japod@23
|
37 |
*/
|
japod@41
|
38 |
public static Convertor createCZKtoUSD() {
|
japod@23
|
39 |
ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK");
|
japod@23
|
40 |
ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("USD");
|
japod@23
|
41 |
ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(17), fromCurrency, new BigDecimal(1), toCurrency);
|
japod@23
|
42 |
|
japod@23
|
43 |
return Convertor.createConvertor(exchangeRateProvider);
|
japod@23
|
44 |
}
|
japod@23
|
45 |
|
japod@23
|
46 |
/** Create convertor that understands two currencies, CZK and
|
japod@23
|
47 |
* SKK. Make 100 SKK == 80 CZK.
|
japod@23
|
48 |
*
|
japod@23
|
49 |
* Creation of the convertor shall not require subclassing of any class
|
japod@23
|
50 |
* or interface on the client side.
|
japod@23
|
51 |
*
|
japod@23
|
52 |
* @return prepared convertor ready for converting SKK to CZK and CZK to SKK
|
japod@23
|
53 |
*/
|
japod@41
|
54 |
public static Convertor createSKKtoCZK() {
|
japod@23
|
55 |
ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("SKK");
|
japod@23
|
56 |
ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("CZK");
|
japod@23
|
57 |
ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(100), fromCurrency, new BigDecimal(80), toCurrency);
|
japod@23
|
58 |
|
japod@23
|
59 |
return Convertor.createConvertor(exchangeRateProvider);
|
japod@23
|
60 |
}
|
japod@23
|
61 |
|
japod@23
|
62 |
|
japod@41
|
63 |
public static Convertor createCZKtoYEN() {
|
japod@23
|
64 |
ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK");
|
japod@23
|
65 |
ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("JPY");
|
japod@23
|
66 |
ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(1), fromCurrency, new BigDecimal(1), toCurrency);
|
japod@23
|
67 |
|
japod@23
|
68 |
return Convertor.createConvertor(exchangeRateProvider);
|
japod@23
|
69 |
}
|
japod@23
|
70 |
|
japod@23
|
71 |
/** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
|
japod@23
|
72 |
* with it.
|
japod@23
|
73 |
*/
|
japod@23
|
74 |
public void testCurrencyCZKUSD() throws Exception {
|
japod@23
|
75 |
Convertor convertCzkUsd = createCZKtoUSD();
|
japod@23
|
76 |
|
japod@23
|
77 |
{
|
japod@23
|
78 |
// convert $1 to CZK using c:
|
japod@23
|
79 |
ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(1));
|
japod@23
|
80 |
assertEquals("Result is 17 CZK", new BigDecimal("17.00"), result.getConverted());
|
japod@23
|
81 |
assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
|
japod@23
|
82 |
}
|
japod@23
|
83 |
|
japod@23
|
84 |
{
|
japod@23
|
85 |
// convert 17CKZ to $ using c:
|
japod@23
|
86 |
ConversionResult result = convertCzkUsd.convert(new BigDecimal(17));
|
japod@23
|
87 |
assertEquals("Result is 1 $", new BigDecimal("1.00"), result.getConverted());
|
japod@23
|
88 |
assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
|
japod@23
|
89 |
}
|
japod@23
|
90 |
|
japod@23
|
91 |
{
|
japod@23
|
92 |
// convert $5 to CZK using c:
|
japod@23
|
93 |
ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(5));
|
japod@23
|
94 |
assertEquals("Result is 85 CZK", new BigDecimal("85.00"), result.getConverted());
|
japod@23
|
95 |
assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
|
japod@23
|
96 |
}
|
japod@23
|
97 |
|
japod@23
|
98 |
{
|
japod@23
|
99 |
// convert $8 to CZK
|
japod@23
|
100 |
ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(8));
|
japod@23
|
101 |
assertEquals("Result is 136 CZK", new BigDecimal("136.00"), result.getConverted());
|
japod@23
|
102 |
assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
|
japod@23
|
103 |
}
|
japod@23
|
104 |
|
japod@23
|
105 |
{
|
japod@23
|
106 |
// convert 1003CZK to USD
|
japod@23
|
107 |
ConversionResult result = convertCzkUsd.convert(new BigDecimal(1003));
|
japod@23
|
108 |
assertEquals("Result is 59 USD", new BigDecimal("59.00"), result.getConverted());
|
japod@23
|
109 |
assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
|
japod@23
|
110 |
}
|
japod@23
|
111 |
}
|
japod@23
|
112 |
|
japod@23
|
113 |
/** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
|
japod@23
|
114 |
* with it.
|
japod@23
|
115 |
*/
|
japod@23
|
116 |
public void testCurrencySKKCZK() throws Exception {
|
japod@23
|
117 |
Convertor convertSkkCzk = createSKKtoCZK();
|
japod@23
|
118 |
{
|
japod@23
|
119 |
// convert 100SKK using c:
|
japod@23
|
120 |
ConversionResult result = convertSkkCzk.convert(new BigDecimal(100));
|
japod@23
|
121 |
assertEquals("Result is 80 CZK", new BigDecimal("80.00"), result.getConverted());
|
japod@23
|
122 |
}
|
japod@23
|
123 |
{
|
japod@23
|
124 |
// convert 80CZK using c:
|
japod@23
|
125 |
ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(80));
|
japod@23
|
126 |
assertEquals("Result is 100 SKK", new BigDecimal("100.00"), result.getConverted());
|
japod@23
|
127 |
}
|
japod@23
|
128 |
|
japod@23
|
129 |
{
|
japod@23
|
130 |
// convert 16CZK using c:
|
japod@23
|
131 |
ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(16));
|
japod@23
|
132 |
assertEquals("Result is 20 SKK", new BigDecimal("20.00"), result.getConverted());
|
japod@23
|
133 |
}
|
japod@23
|
134 |
|
japod@23
|
135 |
{
|
japod@23
|
136 |
// convert 500SKK to CZK
|
japod@23
|
137 |
ConversionResult result = convertSkkCzk.convert(new BigDecimal(500));
|
japod@23
|
138 |
assertEquals("Result is 400 CZK", new BigDecimal("400.00"), result.getConverted());
|
japod@23
|
139 |
assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
|
japod@23
|
140 |
}
|
japod@23
|
141 |
|
japod@23
|
142 |
{
|
japod@23
|
143 |
// convert 501SKK to CZK
|
japod@23
|
144 |
ConversionResult result = convertSkkCzk.convert(new BigDecimal(501));
|
japod@23
|
145 |
assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted());
|
japod@23
|
146 |
assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
|
japod@23
|
147 |
|
japod@23
|
148 |
}
|
japod@23
|
149 |
}
|
japod@23
|
150 |
|
japod@23
|
151 |
/**
|
japod@23
|
152 |
* Convert SKK to CZK. Convertor can't convert whole amout (can't convert one SKK cent to CZK). Remaining
|
japod@23
|
153 |
* amount is stored in remainder result.
|
japod@23
|
154 |
*
|
japod@23
|
155 |
* Test is currently failing, because implementation can't handle this case.
|
japod@23
|
156 |
*/
|
japod@23
|
157 |
// public void testConvertSmallUnits_failing() {
|
japod@23
|
158 |
// Convertor convertSkkCzk = createSKKtoCZK();
|
japod@23
|
159 |
// {
|
japod@23
|
160 |
// // convert 501SKK to CZK
|
japod@23
|
161 |
// ConversionResult result = convertSkkCzk.convert(new BigDecimal("501.01"));
|
japod@23
|
162 |
// assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted());
|
japod@23
|
163 |
// assertEquals("No Remainer", new BigDecimal("0.01"), result.getRemainder());
|
japod@23
|
164 |
//
|
japod@23
|
165 |
// }
|
japod@23
|
166 |
//
|
japod@23
|
167 |
// }
|
japod@23
|
168 |
|
japod@23
|
169 |
/**
|
japod@23
|
170 |
* Test converting from CZK to JPY. Remained has scale of CZK.
|
japod@23
|
171 |
*
|
japod@23
|
172 |
* This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency.
|
japod@23
|
173 |
*/
|
japod@23
|
174 |
// public void testConvertCzkToJpy_failing() {
|
japod@23
|
175 |
// Convertor convertSkkCzk = createCZKtoYEN();
|
japod@23
|
176 |
// {
|
japod@23
|
177 |
// // convert 501SKK to CZK
|
japod@23
|
178 |
// ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00"));
|
japod@23
|
179 |
// assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted());
|
japod@23
|
180 |
// assertEquals("No Remainer", new BigDecimal("0.00"), result.getRemainder());
|
japod@23
|
181 |
//
|
japod@23
|
182 |
// }
|
japod@23
|
183 |
// }
|
japod@23
|
184 |
|
japod@23
|
185 |
/**
|
japod@23
|
186 |
* Test converting from JPY to CZK. Remained has scale of JPY.
|
japod@23
|
187 |
*
|
japod@23
|
188 |
* This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency.
|
japod@23
|
189 |
*/
|
japod@23
|
190 |
// public void testConvertJpyToCzk_failing() {
|
japod@23
|
191 |
// Convertor convertSkkCzk = createCZKtoYEN();
|
japod@23
|
192 |
// {
|
japod@23
|
193 |
// // convert 501SKK to CZK
|
japod@23
|
194 |
// ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00"));
|
japod@23
|
195 |
// assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted());
|
japod@23
|
196 |
// assertEquals("No Remainer", new BigDecimal("0"), result.getRemainder());
|
japod@23
|
197 |
//
|
japod@23
|
198 |
// }
|
japod@23
|
199 |
// }
|
japod@23
|
200 |
|
japod@23
|
201 |
public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
|
japod@23
|
202 |
Convertor c = createCZKtoUSD();
|
japod@23
|
203 |
// convert $5 to SKK, the API shall say this is not possible
|
japod@41
|
204 |
try {
|
japod@41
|
205 |
c.convert(ConvertorCurrency.getInstance("USD"), ConvertorCurrency.getInstance("SKK"), new BigDecimal(5));
|
japod@41
|
206 |
fail();
|
japod@41
|
207 |
} catch (ConversionNotSupportedException e) {
|
japod@41
|
208 |
//expected error;
|
japod@41
|
209 |
assertEquals("Exception From USD", "USD",e.getFromCurrecyCode());
|
japod@41
|
210 |
assertEquals("Exception To SKK", "SKK",e.getToCurrecyCode());
|
japod@41
|
211 |
}
|
japod@23
|
212 |
// convert 500 SKK to CZK, the API shall say this is not possible
|
japod@41
|
213 |
try {
|
japod@41
|
214 |
c.convert(ConvertorCurrency.getInstance("SKK"), ConvertorCurrency.getInstance("CZK"), new BigDecimal(500));
|
japod@41
|
215 |
fail();
|
japod@41
|
216 |
} catch (ConversionNotSupportedException e) {
|
japod@41
|
217 |
assertEquals("Exception From USD", "SKK",e.getFromCurrecyCode());
|
japod@41
|
218 |
assertEquals("Exception To SKK", "CZK",e.getToCurrecyCode()); }
|
japod@23
|
219 |
}
|
japod@23
|
220 |
|
japod@23
|
221 |
|
japod@23
|
222 |
}
|
japod@23
|
223 |
|