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