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