1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task1/solution01/test/org/apidesign/apifest08/test/Task1Test.java Sun Sep 28 14:12:38 2008 +0200
1.3 @@ -0,0 +1,104 @@
1.4 +package org.apidesign.apifest08.test;
1.5 +
1.6 +import junit.framework.TestCase;
1.7 +import org.apidesign.apifest08.currency.*;
1.8 +
1.9 +import java.math.BigDecimal;
1.10 +import java.math.RoundingMode;
1.11 +import java.util.Currency;
1.12 +
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 + private static CurrencyConvertorFactory currencyConvertorFactoryInstance;
1.22 +
1.23 + public Task1Test(String testName) {
1.24 + super(testName);
1.25 + }
1.26 +
1.27 + @Override
1.28 + protected void setUp() throws Exception {
1.29 + currencyConvertorFactoryInstance = ConvertorsFactory.getCurrencyConvertorFactoryInstance();
1.30 + }
1.31 +
1.32 + @Override
1.33 + protected void tearDown() throws Exception {
1.34 + currencyConvertorFactoryInstance = null;
1.35 + }
1.36 +
1.37 + /**
1.38 + * Create convertor that understands two currencies, CZK and
1.39 + * USD. Make 1 USD == 17 CZK.
1.40 + * <p/>
1.41 + * Creation of the convertor shall not require subclassing of any class
1.42 + * or interface on the client side.
1.43 + *
1.44 + * @return prepared convertor ready for converting USD to CZK and CZK to USD
1.45 + */
1.46 + public static Convertor createCZKtoUSD() throws ConvertorNotAvailableException {
1.47 + final Currency czk = Currency.getInstance("CZK");
1.48 + final Currency usd = Currency.getInstance("USD");
1.49 + final BigDecimal constant = BigDecimal.ONE.divide(BigDecimal.valueOf(17), 10, RoundingMode.HALF_EVEN);
1.50 + return currencyConvertorFactoryInstance.createConvertor(czk, usd, ConversionProperties.create(constant, RoundingMode.HALF_EVEN));
1.51 + }
1.52 +
1.53 + /**
1.54 + * Create convertor that understands two currencies, CZK and
1.55 + * SKK. Make 100 SKK == 80 CZK.
1.56 + * <p/>
1.57 + * Creation of the convertor shall not require subclassing of any class
1.58 + * or interface on the client side.
1.59 + *
1.60 + * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
1.61 + */
1.62 + public static Convertor createSKKtoCZK() throws ConvertorNotAvailableException {
1.63 + final Currency skk = Currency.getInstance("SKK");
1.64 + final Currency czk = Currency.getInstance("CZK");
1.65 + final BigDecimal constant = new BigDecimal("0.8");
1.66 + return currencyConvertorFactoryInstance.createConvertor(skk, czk, ConversionProperties.create(constant, RoundingMode.HALF_EVEN));
1.67 + }
1.68 +
1.69 + /**
1.70 + * Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
1.71 + * with it.
1.72 + */
1.73 + public void testCurrencyCZKUSD() throws Exception {
1.74 + Convertor c = createCZKtoUSD();
1.75 + // convert $5 to CZK using c:
1.76 +
1.77 + final int czkDigitsScale = c.getCurrency1().getDefaultFractionDigits();
1.78 + final int usdDigitsScale = c.getCurrency2().getDefaultFractionDigits();
1.79 +
1.80 + assertEquals("Result is 85 CZK", BigDecimal.valueOf(85).setScale(czkDigitsScale), c.convertCurrency2ToCurrency1(BigDecimal.valueOf(5)));
1.81 +
1.82 + // convert $8 to CZK
1.83 + assertEquals("Result is 136 CZK", BigDecimal.valueOf(136).setScale(czkDigitsScale), c.convertCurrency2ToCurrency1(BigDecimal.valueOf(8)));
1.84 +
1.85 + // convert 1003CZK to USD
1.86 + assertEquals("Result is 59 USD", BigDecimal.valueOf(59).setScale(usdDigitsScale), c.convertCurrency1ToCurrency2(BigDecimal.valueOf(1003)));
1.87 + }
1.88 +
1.89 + /**
1.90 + * Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
1.91 + * with it.
1.92 + */
1.93 + public void testCurrencySKKCZK() throws Exception {
1.94 + Convertor c = createSKKtoCZK();
1.95 + // convert 16CZK using c:
1.96 +
1.97 + final int skkDigitsScale = c.getCurrency1().getDefaultFractionDigits();
1.98 + final int czkDigitsScale = c.getCurrency2().getDefaultFractionDigits();
1.99 +
1.100 + assertEquals("Result is 20 SKK", BigDecimal.valueOf(20).setScale(skkDigitsScale), c.convertCurrency2ToCurrency1(BigDecimal.valueOf(16)));
1.101 +
1.102 + // convert 500SKK to CZK
1.103 + assertEquals("Result is 400 CZK", BigDecimal.valueOf(400).setScale(czkDigitsScale), c.convertCurrency1ToCurrency2(BigDecimal.valueOf(500)));
1.104 + }
1.105 +
1.106 +}
1.107 +