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