1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task4/solution14/test/org/apidesign/apifest08/test/Task2Test.java Sat Oct 11 23:38:46 2008 +0200
1.3 @@ -0,0 +1,169 @@
1.4 +package org.apidesign.apifest08.test;
1.5 +
1.6 +import junit.framework.TestCase;
1.7 +import org.apidesign.apifest08.currency.Convertor;
1.8 +import org.apidesign.apifest08.currency.ConvertorFactory;
1.9 +import org.apidesign.apifest08.currency.CurrencyRate;
1.10 +import org.apidesign.apifest08.currency.CurrencyRateFactory;
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 + public Task2Test(String testName) {
1.26 + super(testName);
1.27 + }
1.28 +
1.29 + @Override
1.30 + protected void setUp() throws Exception {
1.31 + }
1.32 +
1.33 + @Override
1.34 + protected void tearDown() throws Exception {
1.35 + }
1.36 +
1.37 + // As in Task1Test, keep in mind, that there are three parts
1.38 + // of the whole system:
1.39 + // 1. there is someone who knows the current exchange rate
1.40 + // 2. there is someone who wants to do the conversion
1.41 + // 3. there is the API between 1. and 2. which allows them to communicate
1.42 + //
1.43 + // Please backward compatibly enhance your existing API to support following
1.44 + // usecases:
1.45 + //
1.46 +
1.47 + /** Create convertor that understands two currencies, CZK and
1.48 + * SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
1.49 + * knows the exchange rate, and needs to use the API to create objects
1.50 + * with the exchange rate. Anyone shall be ready to call this method without
1.51 + * any other method being called previously. The API itself shall know
1.52 + * nothing about any rates, before this method is called.
1.53 + */
1.54 + public static Convertor createTripleConvertor() {
1.55 + // Rates: 1USD = 15CZK
1.56 + // Rates: 1USD = 20SKK
1.57 + // Rates: 75CZK = 100SKK
1.58 + CurrencyRate usdCzk = CurrencyRateFactory.getInstance().createCurrencyRate("USD", "CZK", 1, 15);
1.59 + CurrencyRate usdSkk = CurrencyRateFactory.getInstance().createCurrencyRate("USD", "SKK", 1, 20);
1.60 + CurrencyRate czkSkk = CurrencyRateFactory.getInstance().createCurrencyRate("CZK", "SKK", 75, 100);
1.61 + return ConvertorFactory.newInstance().createConvertor(usdCzk, usdSkk, czkSkk);
1.62 + }
1.63 +
1.64 + /** Define convertor that understands three currencies. Use it.
1.65 + */
1.66 + public void testConvertorForUSDandCZKandSKK() throws Exception {
1.67 + Convertor c = createTripleConvertor();
1.68 +
1.69 + // convert $5 to CZK using c:
1.70 + // assertEquals("Result is 75 CZK");
1.71 + assertEquals("Result is 75 CZK", 75.0, c.convert("USD", "CZK", 5));
1.72 +
1.73 + // convert $5 to SKK using c:
1.74 + // assertEquals("Result is 100 SKK");
1.75 + assertEquals("Result is 100 SKK", 100.0, c.convert("USD", "SKK", 5));
1.76 +
1.77 + // convert 200SKK to CZK using c:
1.78 + // assertEquals("Result is 150 CZK");
1.79 + assertEquals("Result is 150 CZK", 150.0, c.convert("SKK", "CZK", 200));
1.80 +
1.81 + // convert 200SKK to USD using c:
1.82 + // assertEquals("Result is 10 USD");
1.83 + assertEquals("Result is 10 USD", 10.0, c.convert("SKK", "USD", 200));
1.84 + }
1.85 +
1.86 + /** Merge all currency rates of convertor 1 with convertor 2.
1.87 + * Implement this using your API, preferably this method just delegates
1.88 + * into some API method which does the actual work, without requiring
1.89 + * API clients to code anything complex.
1.90 + */
1.91 + public static Convertor merge(Convertor one, Convertor two) {
1.92 + return ConvertorFactory.newInstance().mergeConvertors(one,two);
1.93 + }
1.94 +
1.95 + /** Join the convertors from previous task, Task1Test and show that it
1.96 + * can be used to do reasonable conversions.
1.97 + */
1.98 + public void testConvertorComposition() throws Exception {
1.99 + Convertor c = merge(
1.100 + Task1Test.createCZKtoUSD(),
1.101 + Task1Test.createSKKtoCZK()
1.102 + );
1.103 +
1.104 + // convert $5 to CZK using c:
1.105 + // assertEquals("Result is 85 CZK");
1.106 + assertEquals("Result is 85 CZK", 85.0, c.convert("USD", "CZK", 5));
1.107 +
1.108 + // convert $8 to CZK using c:
1.109 + // assertEquals("Result is 136 CZK");
1.110 + assertEquals("Result is 136 CZK", 136.0, c.convert("USD", "CZK", 8));
1.111 +
1.112 + // convert 1003CZK to USD using c:
1.113 + // assertEquals("Result is 59 USD");
1.114 + assertEquals("Result is 59 USD", 59.0, c.convert("CZK", "USD", 1003));
1.115 +
1.116 + // convert 16CZK using c:
1.117 + // assertEquals("Result is 20 SKK");
1.118 + assertEquals("Result is 20 SKK", 20.0, c.convert("CZK", "SKK", 16));
1.119 +
1.120 + // convert 500SKK to CZK using c:
1.121 + // assertEquals("Result is 400 CZK");
1.122 + assertEquals("Result is 400 CZK", 400.0, c.convert("SKK", "CZK", 500));
1.123 +
1.124 + //test exceptions
1.125 + Convertor one = Task1Test.createCZKtoUSD();
1.126 + Convertor two = Task1Test.createSKKtoCZK();
1.127 + Convertor three = Task1Test.createSKKtoCZK();
1.128 + try {
1.129 + ConvertorFactory.newInstance().mergeConvertors(one,two,three);
1.130 + fail();
1.131 + } catch (IllegalArgumentException e) {
1.132 + //ok
1.133 + }
1.134 +
1.135 + //test exceptions
1.136 + try {
1.137 + ConvertorFactory.newInstance().mergeConvertors(c, two);
1.138 + fail();
1.139 + } catch (IllegalArgumentException e) {
1.140 + //ok
1.141 + }
1.142 +
1.143 + //try convertors from version 1
1.144 + Convertor v1one = ConvertorFactory.newInstance().createConvertor("CZE", "CZE", 1, 2);
1.145 + assertEquals("CZE->CZE 1:2 10 expects 20", 20.0, v1one.convert("CZE", "CZE", 10));
1.146 + try {
1.147 + ConvertorFactory.newInstance().mergeConvertors(v1one, two);
1.148 + fail();
1.149 + } catch (IllegalArgumentException e) {
1.150 + //ok
1.151 + }
1.152 +
1.153 + Convertor v1two = ConvertorFactory.newInstance().createConvertor("EUR", "", 1, 2);
1.154 + assertEquals("EUR->'' 1:2 10 expects 20", 20.0, v1two.convert("EUR", "", 10));
1.155 + try {
1.156 + ConvertorFactory.newInstance().mergeConvertors(v1two, two);
1.157 + fail();
1.158 + } catch (IllegalArgumentException e) {
1.159 + //ok
1.160 + }
1.161 +
1.162 + Convertor v1three = ConvertorFactory.newInstance().createConvertor("EUR", "", 1, 2);
1.163 + assertEquals("''->EUR 1:2 10 expects 5", 5.0, v1three.convert("", "EUR", 10));
1.164 + try {
1.165 + ConvertorFactory.newInstance().mergeConvertors(v1three, two);
1.166 + fail();
1.167 + } catch (IllegalArgumentException e) {
1.168 + //ok
1.169 + }
1.170 +
1.171 + }
1.172 +}