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;
10 /** Finish the Convertor API, and then write bodies of methods inside
11 * of this class to match the given tasks. To fullfil your task, use the
12 * API define in the <code>org.apidesign.apifest08.currency</code> package.
13 * Do not you reflection, or other hacks as your code
14 * shall run without any runtime permissions.
16 public class Task1Test extends TestCase {
17 public Task1Test(String testName) {
22 protected void setUp() throws Exception {
26 protected void tearDown() throws Exception {
29 /** Create convertor that understands two currencies, CZK and
30 * USD. Make 1 USD == 17 CZK.
32 * Creation of the convertor shall not require subclassing of any class
33 * or interface on the client side.
35 * @return prepared convertor ready for converting USD to CZK and CZK to USD
37 public Convertor createCZKtoUSD() {
38 ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK");
39 ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("USD");
40 ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(17), fromCurrency, new BigDecimal(1), toCurrency);
42 return Convertor.createConvertor(exchangeRateProvider);
45 /** Create convertor that understands two currencies, CZK and
46 * SKK. Make 100 SKK == 80 CZK.
48 * Creation of the convertor shall not require subclassing of any class
49 * or interface on the client side.
51 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
53 public Convertor createSKKtoCZK() {
54 ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("SKK");
55 ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("CZK");
56 ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(100), fromCurrency, new BigDecimal(80), toCurrency);
58 return Convertor.createConvertor(exchangeRateProvider);
62 public Convertor createCZKtoYEN() {
63 ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK");
64 ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("JPY");
65 ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(1), fromCurrency, new BigDecimal(1), toCurrency);
67 return Convertor.createConvertor(exchangeRateProvider);
70 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
73 public void testCurrencyCZKUSD() throws Exception {
74 Convertor convertCzkUsd = createCZKtoUSD();
77 // convert $1 to CZK using c:
78 ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(1));
79 assertEquals("Result is 17 CZK", new BigDecimal("17.00"), result.getConverted());
80 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
84 // convert 17CKZ to $ using c:
85 ConversionResult result = convertCzkUsd.convert(new BigDecimal(17));
86 assertEquals("Result is 1 $", new BigDecimal("1.00"), result.getConverted());
87 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
91 // convert $5 to CZK using c:
92 ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(5));
93 assertEquals("Result is 85 CZK", new BigDecimal("85.00"), result.getConverted());
94 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
99 ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(8));
100 assertEquals("Result is 136 CZK", new BigDecimal("136.00"), result.getConverted());
101 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
105 // convert 1003CZK to USD
106 ConversionResult result = convertCzkUsd.convert(new BigDecimal(1003));
107 assertEquals("Result is 59 USD", new BigDecimal("59.00"), result.getConverted());
108 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
112 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
115 public void testCurrencySKKCZK() throws Exception {
116 Convertor convertSkkCzk = createSKKtoCZK();
118 // convert 100SKK using c:
119 ConversionResult result = convertSkkCzk.convert(new BigDecimal(100));
120 assertEquals("Result is 80 CZK", new BigDecimal("80.00"), result.getConverted());
123 // convert 80CZK using c:
124 ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(80));
125 assertEquals("Result is 100 SKK", new BigDecimal("100.00"), result.getConverted());
129 // convert 16CZK using c:
130 ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(16));
131 assertEquals("Result is 20 SKK", new BigDecimal("20.00"), result.getConverted());
135 // convert 500SKK to CZK
136 ConversionResult result = convertSkkCzk.convert(new BigDecimal(500));
137 assertEquals("Result is 400 CZK", new BigDecimal("400.00"), result.getConverted());
138 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
142 // convert 501SKK to CZK
143 ConversionResult result = convertSkkCzk.convert(new BigDecimal(501));
144 assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted());
145 assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
151 * Convert SKK to CZK. Convertor can't convert whole amout (can't convert one SKK cent to CZK). Remaining
152 * amount is stored in remainder result.
154 * Test is currently failing, because implementation can't handle this case.
156 // public void testConvertSmallUnits_failing() {
157 // Convertor convertSkkCzk = createSKKtoCZK();
159 // // convert 501SKK to CZK
160 // ConversionResult result = convertSkkCzk.convert(new BigDecimal("501.01"));
161 // assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted());
162 // assertEquals("No Remainer", new BigDecimal("0.01"), result.getRemainder());
169 * Test converting from CZK to JPY. Remained has scale of CZK.
171 * This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency.
173 // public void testConvertCzkToJpy_failing() {
174 // Convertor convertSkkCzk = createCZKtoYEN();
176 // // convert 501SKK to CZK
177 // ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00"));
178 // assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted());
179 // assertEquals("No Remainer", new BigDecimal("0.00"), result.getRemainder());
185 * Test converting from JPY to CZK. Remained has scale of JPY.
187 * This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency.
189 // public void testConvertJpyToCzk_failing() {
190 // Convertor convertSkkCzk = createCZKtoYEN();
192 // // convert 501SKK to CZK
193 // ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00"));
194 // assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted());
195 // assertEquals("No Remainer", new BigDecimal("0"), result.getRemainder());
200 public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
201 Convertor c = createCZKtoUSD();
202 // convert $5 to SKK, the API shall say this is not possible
203 // convert 500 SKK to CZK, the API shall say this is not possible
204 // ... no api for required call, should be here?