japod@34: package org.apidesign.apifest08.test; japod@34: japod@34: import static org.apidesign.apifest08.currency.ConvertorFactory.createConvertor; japod@34: import static org.apidesign.apifest08.currency.ConvertorFactory.mergeConvertors; japod@34: import static org.apidesign.apifest08.currency.MoneyImpl.money; japod@34: import static org.apidesign.apifest08.test.Task1Test.CZK; japod@34: import static org.apidesign.apifest08.test.Task1Test.SKK; japod@34: import static org.apidesign.apifest08.test.Task1Test.USD; japod@34: import junit.framework.TestCase; japod@34: japod@34: import org.apidesign.apifest08.currency.Convertor; japod@34: import org.apidesign.apifest08.currency.ExtendedConvertor; japod@34: japod@34: /** There are many currencies around the world and many banks manipulate japod@34: * with more than one or two at the same time. As banks are usually the japod@34: * best paying clients, which is true even in case of your Convertor API, japod@34: * it is reasonable to listen to their requests. japod@34: *

japod@34: * The quest for today is to enhance your existing convertor API to hold japod@34: * information about many currencies and allow conversions between any of them. japod@34: * Also, as conversion rates for diferent currencies usually arise from various japod@34: * bank departments, there is another important need. There is a need to japod@34: * compose two convertors into one by merging all the information about japod@34: * currencies they know about. japod@34: */ japod@34: public class Task2Test extends TestCase { japod@34: public Task2Test(String testName) { japod@34: super(testName); japod@34: } japod@34: japod@34: @Override japod@34: protected void setUp() throws Exception { japod@34: } japod@34: japod@34: @Override japod@34: protected void tearDown() throws Exception { japod@34: } japod@34: japod@34: // As in Task1Test, keep in mind, that there are three parts japod@34: // of the whole system: japod@34: // 1. there is someone who knows the current exchange rate japod@34: // 2. there is someone who wants to do the conversion japod@34: // 3. there is the API between 1. and 2. which allows them to communicate japod@34: // japod@34: // Please backward compatibly enhance your existing API to support following japod@34: // usecases: japod@34: // japod@34: japod@34: /** Create convertor that understands two currencies, CZK and japod@34: * SKK. Make 100 SKK == 75 CZK. This is method for the group of users that japod@34: * knows the exchange rate, and needs to use the API to create objects japod@34: * with the exchange rate. Anyone shall be ready to call this method without japod@34: * any other method being called previously. The API itself shall know japod@34: * nothing about any rates, before this method is called. japod@34: */ japod@34: public static Convertor createTripleConvertor() { japod@34: // Rates: 1USD = 15CZK japod@34: // Rates: 1USD = 20SKK japod@34: // Rates: 75CZK = 100SKK japod@34: return mergeConvertors( japod@34: createConvertor(money(1, USD), money(15, CZK)), japod@34: createConvertor(money(1, USD), money(20, SKK)), japod@34: createConvertor(money(75, CZK), money(100, SKK)) japod@34: ); japod@34: } japod@34: japod@34: /** Define convertor that understands three currencies. Use it. japod@34: */ japod@34: public void testConvertorForUSDandCZKandSKK() throws Exception { japod@34: Convertor c = createTripleConvertor(); japod@34: japod@34: // convert $5 to CZK using c: japod@34: assertEquals("Result is 75 CZK", money(75, CZK),c.convert(money(5,USD), CZK)); japod@34: japod@34: // convert $5 to SKK using c: japod@34: assertEquals("Result is 100 SKK", money(100, SKK),c.convert(money(5,USD), SKK)); japod@34: japod@34: // convert 200SKK to CZK using c: japod@34: assertEquals("Result is 150 CZK", money(150, CZK),c.convert(money(200,SKK), CZK)); japod@34: japod@34: // convert 200SKK to USK using c: japod@34: assertEquals("Result is 10 USD", money(10, USD),c.convert(money(200,SKK), USD)); japod@34: } japod@34: japod@34: /** Merge all currency rates of convertor 1 with convertor 2. japod@34: * Implement this using your API, preferably this method just delegates japod@34: * into some API method which does the actual work, without requiring japod@34: * API clients to code anything complex. japod@34: */ japod@34: public static Convertor merge(Convertor one, Convertor two) { japod@34: return mergeConvertors((ExtendedConvertor)one, (ExtendedConvertor)two); japod@34: } japod@34: japod@34: /** Join the convertors from previous task, Task1Test and show that it japod@34: * can be used to do reasonable conversions. japod@34: */ japod@34: public void testConvertorComposition() throws Exception { japod@34: Convertor c = merge( japod@34: Task1Test.createCZKtoUSD(), japod@34: Task1Test.createSKKtoCZK() japod@34: ); japod@34: japod@34: // convert $5 to CZK using c: japod@34: assertEquals("Result is 85 CZK", money(85, CZK),c.convert(money(5,USD), CZK)); japod@34: japod@34: // convert $8 to CZK using c: japod@34: assertEquals("Result is 136 CZK", money(136, CZK),c.convert(money(8,USD), CZK)); japod@34: japod@34: // convert 1003CZK to USD using c: japod@34: assertEquals("Result is 59 USD", money(59, USD),c.convert(money(1003,CZK), USD)); japod@34: japod@34: // convert 16CZK using c: japod@34: assertEquals("Result is 20 SKK", money(20, SKK),c.convert(money(16,CZK), SKK)); japod@34: japod@34: // convert 500SKK to CZK using c: japod@34: assertEquals("Result is 400 CZK", money(400, CZK),c.convert(money(500,SKK), CZK)); japod@34: japod@34: } japod@34: }