1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task4/solution13/test/org/apidesign/apifest08/test/Task1Test.java Sat Oct 11 23:38:46 2008 +0200
1.3 @@ -0,0 +1,223 @@
1.4 +package org.apidesign.apifest08.test;
1.5 +
1.6 +import java.math.BigDecimal;
1.7 +import junit.framework.TestCase;
1.8 +import org.apidesign.apifest08.currency.ConversionResult;
1.9 +import org.apidesign.apifest08.currency.Convertor;
1.10 +import org.apidesign.apifest08.currency.ConvertorCurrency;
1.11 +import org.apidesign.apifest08.currency.ExchangeRateProvider;
1.12 +import org.apidesign.apifest08.currency.ConversionNotSupportedException;
1.13 +
1.14 +/** Finish the Convertor API, and then write bodies of methods inside
1.15 + * of this class to match the given tasks. To fullfil your task, use the
1.16 + * API define in the <code>org.apidesign.apifest08.currency</code> package.
1.17 + * Do not you reflection, or other hacks as your code
1.18 + * shall run without any runtime permissions.
1.19 + */
1.20 +public class Task1Test extends TestCase {
1.21 + public Task1Test(String testName) {
1.22 + super(testName);
1.23 + }
1.24 +
1.25 + @Override
1.26 + protected void setUp() throws Exception {
1.27 + }
1.28 +
1.29 + @Override
1.30 + protected void tearDown() throws Exception {
1.31 + }
1.32 +
1.33 + /** Create convertor that understands two currencies, CZK and
1.34 + * USD. Make 1 USD == 17 CZK.
1.35 + *
1.36 + * Creation of the convertor shall not require subclassing of any class
1.37 + * or interface on the client side.
1.38 + *
1.39 + * @return prepared convertor ready for converting USD to CZK and CZK to USD
1.40 + */
1.41 + public static Convertor createCZKtoUSD() {
1.42 + ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK");
1.43 + ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("USD");
1.44 + ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(17), fromCurrency, new BigDecimal(1), toCurrency);
1.45 +
1.46 + return Convertor.createConvertor(exchangeRateProvider);
1.47 + }
1.48 +
1.49 + /** Create convertor that understands two currencies, CZK and
1.50 + * SKK. Make 100 SKK == 80 CZK.
1.51 + *
1.52 + * Creation of the convertor shall not require subclassing of any class
1.53 + * or interface on the client side.
1.54 + *
1.55 + * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
1.56 + */
1.57 + public static Convertor createSKKtoCZK() {
1.58 + ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("SKK");
1.59 + ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("CZK");
1.60 + ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(100), fromCurrency, new BigDecimal(80), toCurrency);
1.61 +
1.62 + return Convertor.createConvertor(exchangeRateProvider);
1.63 + }
1.64 +
1.65 +
1.66 + public static Convertor createCZKtoYEN() {
1.67 + ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK");
1.68 + ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("JPY");
1.69 + ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(1), fromCurrency, new BigDecimal(1), toCurrency);
1.70 +
1.71 + return Convertor.createConvertor(exchangeRateProvider);
1.72 + }
1.73 +
1.74 + /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
1.75 + * with it.
1.76 + */
1.77 + public void testCurrencyCZKUSD() throws Exception {
1.78 + Convertor convertCzkUsd = createCZKtoUSD();
1.79 +
1.80 + {
1.81 + // convert $1 to CZK using c:
1.82 + ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(1));
1.83 + assertEquals("Result is 17 CZK", new BigDecimal("17.00"), result.getConverted());
1.84 + assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
1.85 + }
1.86 +
1.87 + {
1.88 + // convert 17CKZ to $ using c:
1.89 + ConversionResult result = convertCzkUsd.convert(new BigDecimal(17));
1.90 + assertEquals("Result is 1 $", new BigDecimal("1.00"), result.getConverted());
1.91 + assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
1.92 + }
1.93 +
1.94 + {
1.95 + // convert $5 to CZK using c:
1.96 + ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(5));
1.97 + assertEquals("Result is 85 CZK", new BigDecimal("85.00"), result.getConverted());
1.98 + assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
1.99 + }
1.100 +
1.101 + {
1.102 + // convert $8 to CZK
1.103 + ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(8));
1.104 + assertEquals("Result is 136 CZK", new BigDecimal("136.00"), result.getConverted());
1.105 + assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
1.106 + }
1.107 +
1.108 + {
1.109 + // convert 1003CZK to USD
1.110 + ConversionResult result = convertCzkUsd.convert(new BigDecimal(1003));
1.111 + assertEquals("Result is 59 USD", new BigDecimal("59.00"), result.getConverted());
1.112 + assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
1.113 + }
1.114 + }
1.115 +
1.116 + /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
1.117 + * with it.
1.118 + */
1.119 + public void testCurrencySKKCZK() throws Exception {
1.120 + Convertor convertSkkCzk = createSKKtoCZK();
1.121 + {
1.122 + // convert 100SKK using c:
1.123 + ConversionResult result = convertSkkCzk.convert(new BigDecimal(100));
1.124 + assertEquals("Result is 80 CZK", new BigDecimal("80.00"), result.getConverted());
1.125 + }
1.126 + {
1.127 + // convert 80CZK using c:
1.128 + ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(80));
1.129 + assertEquals("Result is 100 SKK", new BigDecimal("100.00"), result.getConverted());
1.130 + }
1.131 +
1.132 + {
1.133 + // convert 16CZK using c:
1.134 + ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(16));
1.135 + assertEquals("Result is 20 SKK", new BigDecimal("20.00"), result.getConverted());
1.136 + }
1.137 +
1.138 + {
1.139 + // convert 500SKK to CZK
1.140 + ConversionResult result = convertSkkCzk.convert(new BigDecimal(500));
1.141 + assertEquals("Result is 400 CZK", new BigDecimal("400.00"), result.getConverted());
1.142 + assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
1.143 + }
1.144 +
1.145 + {
1.146 + // convert 501SKK to CZK
1.147 + ConversionResult result = convertSkkCzk.convert(new BigDecimal(501));
1.148 + assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted());
1.149 + assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
1.150 +
1.151 + }
1.152 + }
1.153 +
1.154 + /**
1.155 + * Convert SKK to CZK. Convertor can't convert whole amout (can't convert one SKK cent to CZK). Remaining
1.156 + * amount is stored in remainder result.
1.157 + *
1.158 + * Test is currently failing, because implementation can't handle this case.
1.159 + */
1.160 +// public void testConvertSmallUnits_failing() {
1.161 +// Convertor convertSkkCzk = createSKKtoCZK();
1.162 +// {
1.163 +// // convert 501SKK to CZK
1.164 +// ConversionResult result = convertSkkCzk.convert(new BigDecimal("501.01"));
1.165 +// assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted());
1.166 +// assertEquals("No Remainer", new BigDecimal("0.01"), result.getRemainder());
1.167 +//
1.168 +// }
1.169 +//
1.170 +// }
1.171 +
1.172 + /**
1.173 + * Test converting from CZK to JPY. Remained has scale of CZK.
1.174 + *
1.175 + * This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency.
1.176 + */
1.177 +// public void testConvertCzkToJpy_failing() {
1.178 +// Convertor convertSkkCzk = createCZKtoYEN();
1.179 +// {
1.180 +// // convert 501SKK to CZK
1.181 +// ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00"));
1.182 +// assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted());
1.183 +// assertEquals("No Remainer", new BigDecimal("0.00"), result.getRemainder());
1.184 +//
1.185 +// }
1.186 +// }
1.187 +
1.188 + /**
1.189 + * Test converting from JPY to CZK. Remained has scale of JPY.
1.190 + *
1.191 + * This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency.
1.192 + */
1.193 +// public void testConvertJpyToCzk_failing() {
1.194 +// Convertor convertSkkCzk = createCZKtoYEN();
1.195 +// {
1.196 +// // convert 501SKK to CZK
1.197 +// ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00"));
1.198 +// assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted());
1.199 +// assertEquals("No Remainer", new BigDecimal("0"), result.getRemainder());
1.200 +//
1.201 +// }
1.202 +// }
1.203 +
1.204 + public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
1.205 + Convertor c = createCZKtoUSD();
1.206 + // convert $5 to SKK, the API shall say this is not possible
1.207 + try {
1.208 + c.convert(ConvertorCurrency.getInstance("USD"), ConvertorCurrency.getInstance("SKK"), new BigDecimal(5));
1.209 + fail();
1.210 + } catch (ConversionNotSupportedException e) {
1.211 + //expected error;
1.212 + assertEquals("Exception From USD", "USD",e.getFromCurrecyCode());
1.213 + assertEquals("Exception To SKK", "SKK",e.getToCurrecyCode());
1.214 + }
1.215 + // convert 500 SKK to CZK, the API shall say this is not possible
1.216 + try {
1.217 + c.convert(ConvertorCurrency.getInstance("SKK"), ConvertorCurrency.getInstance("CZK"), new BigDecimal(500));
1.218 + fail();
1.219 + } catch (ConversionNotSupportedException e) {
1.220 + assertEquals("Exception From USD", "SKK",e.getFromCurrecyCode());
1.221 + assertEquals("Exception To SKK", "CZK",e.getToCurrecyCode()); }
1.222 + }
1.223 +
1.224 +
1.225 +}
1.226 +