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