1 package org.apidesign.apifest08.test;
3 import java.math.BigDecimal;
4 import junit.framework.TestCase;
5 import org.apidesign.apifest08.currency.ConversionResult;
6 import org.apidesign.apifest08.currency.Convertor;
7 import org.apidesign.apifest08.currency.ConvertorCurrency;
8 import org.apidesign.apifest08.currency.ExchangeRateProvider;
9 import org.apidesign.apifest08.currency.ConversionNotSupportedException;
11 /** Finish the Convertor API, and then write bodies of methods inside
12 * of this class to match the given tasks. To fullfil your task, use the
13 * API define in the <code>org.apidesign.apifest08.currency</code> package.
14 * Do not you reflection, or other hacks as your code
15 * shall run without any runtime permissions.
17 public class Task1Test extends TestCase {
18 public Task1Test(String testName) {
23 protected void setUp() throws Exception {
27 protected void tearDown() throws Exception {
30 /** Create convertor that understands two currencies, CZK and
31 * USD. Make 1 USD == 17 CZK.
33 * Creation of the convertor shall not require subclassing of any class
34 * or interface on the client side.
36 * @return prepared convertor ready for converting USD to CZK and CZK to USD
38 public static Convertor createCZKtoUSD() {
39 ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK");
40 ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("USD");
41 ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(17), fromCurrency, new BigDecimal(1), toCurrency);
43 return Convertor.createConvertor(exchangeRateProvider);
46 /** Create convertor that understands two currencies, CZK and
47 * SKK. Make 100 SKK == 80 CZK.
49 * Creation of the convertor shall not require subclassing of any class
50 * or interface on the client side.
52 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
54 public static Convertor createSKKtoCZK() {
55 ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("SKK");
56 ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("CZK");
57 ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(100), fromCurrency, new BigDecimal(80), toCurrency);
59 return Convertor.createConvertor(exchangeRateProvider);
63 public static Convertor createCZKtoYEN() {
64 ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK");
65 ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("JPY");
66 ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(1), fromCurrency, new BigDecimal(1), toCurrency);
68 return Convertor.createConvertor(exchangeRateProvider);
71 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
74 public void testCurrencyCZKUSD() throws Exception {
75 Convertor convertCzkUsd = createCZKtoUSD();
78 // convert $1 to CZK using c:
79 ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(1));
80 assertEquals("Result is 17 CZK", new BigDecimal("17.00"), result.getConverted());
81 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
85 // convert 17CKZ to $ using c:
86 ConversionResult result = convertCzkUsd.convert(new BigDecimal(17));
87 assertEquals("Result is 1 $", new BigDecimal("1.00"), result.getConverted());
88 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
92 // convert $5 to CZK using c:
93 ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(5));
94 assertEquals("Result is 85 CZK", new BigDecimal("85.00"), result.getConverted());
95 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
100 ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(8));
101 assertEquals("Result is 136 CZK", new BigDecimal("136.00"), result.getConverted());
102 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
106 // convert 1003CZK to USD
107 ConversionResult result = convertCzkUsd.convert(new BigDecimal(1003));
108 assertEquals("Result is 59 USD", new BigDecimal("59.00"), result.getConverted());
109 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
113 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
116 public void testCurrencySKKCZK() throws Exception {
117 Convertor convertSkkCzk = createSKKtoCZK();
119 // convert 100SKK using c:
120 ConversionResult result = convertSkkCzk.convert(new BigDecimal(100));
121 assertEquals("Result is 80 CZK", new BigDecimal("80.00"), result.getConverted());
124 // convert 80CZK using c:
125 ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(80));
126 assertEquals("Result is 100 SKK", new BigDecimal("100.00"), result.getConverted());
130 // convert 16CZK using c:
131 ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(16));
132 assertEquals("Result is 20 SKK", new BigDecimal("20.00"), result.getConverted());
136 // convert 500SKK to CZK
137 ConversionResult result = convertSkkCzk.convert(new BigDecimal(500));
138 assertEquals("Result is 400 CZK", new BigDecimal("400.00"), result.getConverted());
139 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
143 // convert 501SKK to CZK
144 ConversionResult result = convertSkkCzk.convert(new BigDecimal(501));
145 assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted());
146 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
152 * Convert SKK to CZK. Convertor can't convert whole amout (can't convert one SKK cent to CZK). Remaining
153 * amount is stored in remainder result.
155 * Test is currently failing, because implementation can't handle this case.
157 // public void testConvertSmallUnits_failing() {
158 // Convertor convertSkkCzk = createSKKtoCZK();
160 // // convert 501SKK to CZK
161 // ConversionResult result = convertSkkCzk.convert(new BigDecimal("501.01"));
162 // assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted());
163 // assertEquals("No Remainer", new BigDecimal("0.01"), result.getRemainder());
170 * Test converting from CZK to JPY. Remained has scale of CZK.
172 * This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency.
174 // public void testConvertCzkToJpy_failing() {
175 // Convertor convertSkkCzk = createCZKtoYEN();
177 // // convert 501SKK to CZK
178 // ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00"));
179 // assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted());
180 // assertEquals("No Remainer", new BigDecimal("0.00"), result.getRemainder());
186 * Test converting from JPY to CZK. Remained has scale of JPY.
188 * This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency.
190 // public void testConvertJpyToCzk_failing() {
191 // Convertor convertSkkCzk = createCZKtoYEN();
193 // // convert 501SKK to CZK
194 // ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00"));
195 // assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted());
196 // assertEquals("No Remainer", new BigDecimal("0"), result.getRemainder());
201 public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
202 Convertor c = createCZKtoUSD();
203 // convert $5 to SKK, the API shall say this is not possible
205 c.convert(ConvertorCurrency.getInstance("USD"), ConvertorCurrency.getInstance("SKK"), new BigDecimal(5));
207 } catch (ConversionNotSupportedException e) {
209 assertEquals("Exception From USD", "USD",e.getFromCurrecyCode());
210 assertEquals("Exception To SKK", "SKK",e.getToCurrecyCode());
212 // convert 500 SKK to CZK, the API shall say this is not possible
214 c.convert(ConvertorCurrency.getInstance("SKK"), ConvertorCurrency.getInstance("CZK"), new BigDecimal(500));
216 } catch (ConversionNotSupportedException e) {
217 assertEquals("Exception From USD", "SKK",e.getFromCurrecyCode());
218 assertEquals("Exception To SKK", "CZK",e.getToCurrecyCode()); }