task2/solution13/test/org/apidesign/apifest08/test/Task2Test.java
changeset 41 a7e6f84fb078
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task2/solution13/test/org/apidesign/apifest08/test/Task2Test.java	Tue Oct 07 01:18:23 2008 +0200
     1.3 @@ -0,0 +1,136 @@
     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.Convertor;
     1.9 +import org.apidesign.apifest08.currency.ConvertorCurrency;
    1.10 +import org.apidesign.apifest08.currency.ExchangeRateProvider;
    1.11 +
    1.12 +/** There are many currencies around the world and many banks manipulate
    1.13 + * with more than one or two at the same time. As banks are usually the
    1.14 + * best paying clients, which is true even in case of your Convertor API,
    1.15 + * it is reasonable to listen to their requests.
    1.16 + * <p>
    1.17 + * The quest for today is to enhance your existing convertor API to hold
    1.18 + * information about many currencies and allow conversions between any of them.
    1.19 + * Also, as conversion rates for diferent currencies usually arise from various
    1.20 + * bank departments, there is another important need. There is a need to
    1.21 + * compose two convertors into one by merging all the information about
    1.22 + * currencies they know about.
    1.23 + */
    1.24 +public class Task2Test extends TestCase {
    1.25 +    private static ConvertorCurrency currencyCZK = ConvertorCurrency.getInstance("CZK");
    1.26 +    private static ConvertorCurrency currencySKK = ConvertorCurrency.getInstance("SKK");
    1.27 +    private static ConvertorCurrency currencyUSD = ConvertorCurrency.getInstance("USD");
    1.28 +  
    1.29 +    public Task2Test(String testName) {
    1.30 +        super(testName);
    1.31 +    }
    1.32 +
    1.33 +    @Override
    1.34 +    protected void setUp() throws Exception {
    1.35 +
    1.36 +    }
    1.37 +
    1.38 +    @Override
    1.39 +    protected void tearDown() throws Exception {
    1.40 +    }
    1.41 +    
    1.42 +    public static Convertor createUsdToSkkConvertor() {
    1.43 +        ConvertorCurrency fromCurrency = currencyUSD;
    1.44 +        ConvertorCurrency toCurrency = currencyUSD;
    1.45 +        ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(1), fromCurrency, new BigDecimal(20), toCurrency);
    1.46 +        
    1.47 +        return Convertor.createConvertor(exchangeRateProvider);
    1.48 +        
    1.49 +    }
    1.50 +
    1.51 +    // As in Task1Test, keep in mind, that there are three parts
    1.52 +    // of the whole system:
    1.53 +    // 1. there is someone who knows the current exchange rate
    1.54 +    // 2. there is someone who wants to do the conversion
    1.55 +    // 3. there is the API between 1. and 2. which allows them to communicate
    1.56 +    // 
    1.57 +    // Please backward compatibly enhance your existing API to support following
    1.58 +    // usecases:
    1.59 +    //
    1.60 +    
    1.61 +    /** Create convertor that understands two currencies, CZK and
    1.62 +     *  SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
    1.63 +     *  knows the exchange rate, and needs to use the API to create objects
    1.64 +     *  with the exchange rate. Anyone shall be ready to call this method without
    1.65 +     *  any other method being called previously. The API itself shall know
    1.66 +     *  nothing about any rates, before this method is called.
    1.67 +     */
    1.68 +    public static Convertor createTripleConvertor() {
    1.69 +        ExchangeRateProvider exRateProvider = ExchangeRateProvider.createExchangeRateProvider();
    1.70 +        
    1.71 +        // Rates: 1USD = 15CZK
    1.72 +        exRateProvider.addFixedCurencyRate(currencyUSD, new BigDecimal(1),currencyCZK,  new BigDecimal(15));
    1.73 +        
    1.74 +        // Rates: 1USD = 20SKK
    1.75 +        exRateProvider.addFixedCurencyRate(currencyUSD, new BigDecimal(1), currencySKK,  new BigDecimal(20));
    1.76 +        
    1.77 +        // Rates: 75CZK = 100SKK
    1.78 +        exRateProvider.addFixedCurencyRate(currencyCZK, new BigDecimal(75), currencySKK,  new BigDecimal(100));
    1.79 +        
    1.80 +        Convertor c = Convertor.createConvertor(exRateProvider);
    1.81 +        
    1.82 +        return c;
    1.83 +    }
    1.84 +
    1.85 +    /** Define convertor that understands three currencies. Use it.
    1.86 +     */
    1.87 +    public void testConvertorForUSDandCZKandSKK() throws Exception {
    1.88 +        Convertor c = createTripleConvertor();
    1.89 +
    1.90 +        // convert $5 to CZK using c:
    1.91 +        assertEquals("Result is 75 CZK",new BigDecimal("75.00"),c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(5)).getConverted());
    1.92 +
    1.93 +
    1.94 +        // convert $5 to SKK using c:
    1.95 +        assertEquals("Result is 100 SKK",new BigDecimal("100.00"),c.convertWithReversibleRates(currencyUSD, currencySKK, new BigDecimal(5)).getConverted());
    1.96 +
    1.97 +        // convert 200SKK to CZK using c:
    1.98 +        assertEquals("Result is 150 CZK",new BigDecimal("150.00"),c.convertWithReversibleRates(currencySKK, currencyCZK, new BigDecimal(200)).getConverted());
    1.99 +
   1.100 +        // convert 200SKK to USK using c:
   1.101 +        // assertEquals("Result is 10 USD");
   1.102 +    }
   1.103 +
   1.104 +    /** Merge all currency rates of convertor 1 with convertor 2.
   1.105 +     * Implement this using your API, preferably this method just delegates
   1.106 +     * into some API method which does the actual work, without requiring
   1.107 +     * API clients to code anything complex.
   1.108 +     */
   1.109 +    public static Convertor merge(Convertor one, Convertor two) {
   1.110 +        return Convertor.createConvertorAsMerge(new Convertor[]{one, two});
   1.111 +    }
   1.112 +
   1.113 +    /** Join the convertors from previous task, Task1Test and show that it
   1.114 +     * can be used to do reasonable conversions.
   1.115 +     */
   1.116 +    public void testConvertorComposition() throws Exception {
   1.117 +        Convertor c = merge(
   1.118 +            Task1Test.createCZKtoUSD(),
   1.119 +            Task1Test.createSKKtoCZK()
   1.120 +        );
   1.121 +
   1.122 +        // convert $5 to CZK using c:
   1.123 +        assertEquals("Result is 85 CZK",new BigDecimal("85.00"),c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(5)).getConverted());
   1.124 +
   1.125 +        // convert $8 to CZK using c:
   1.126 +        // assertEquals("Result is 136 CZK");
   1.127 +        assertEquals("Result is 136 CZK",new BigDecimal("136.00"),c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(8)).getConverted());
   1.128 +
   1.129 +        // convert 1003CZK to USD using c:
   1.130 +        assertEquals("Result is 59 USD",new BigDecimal("59.00"),c.convertWithReversibleRates(currencyCZK, currencyUSD, new BigDecimal(1003)).getConverted());
   1.131 +
   1.132 +        // convert 16CZK using c:
   1.133 +        assertEquals("Result is 20 SKK",new BigDecimal("20.00"),c.convertWithReversibleRates(currencyCZK, currencySKK, new BigDecimal(16)).getConverted());
   1.134 +
   1.135 +        // convert 500SKK to CZK using c:
   1.136 +        assertEquals("Result is 400 CZK",new BigDecimal("400.00"),c.convertWithReversibleRates(currencySKK, currencyCZK, new BigDecimal(500)).getConverted());
   1.137 +
   1.138 +    }
   1.139 +}