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