diff -r a3144e7f9c90 -r 58ec6da75f6f task4/solution02/test/org/apidesign/apifest08/test/Task3Test.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task4/solution02/test/org/apidesign/apifest08/test/Task3Test.java Sat Oct 11 23:38:46 2008 +0200 @@ -0,0 +1,100 @@ +package org.apidesign.apifest08.test; + +import static org.apidesign.apifest08.currency.MoneyImpl.money; +import static org.apidesign.apifest08.test.Task1Test.CZK; +import static org.apidesign.apifest08.test.Task1Test.SKK; +import static org.apidesign.apifest08.test.Task1Test.USD; +import junit.framework.TestCase; + +import org.apidesign.apifest08.currency.Convertor; + +/** The exchange rates are not always the same. They are changing. Day by day, + * hour by hour, minute by minute. For every bank it is important to always + * have the actual exchange rate available in the system. That is why let's + * create a pluggable convertor that will always have up to date value of its + * exchange rate. + *

+ * The quest for today is to allow 3rd party developer to write a convertor + * that adjusts its exchange rate everytime it is queried. This convertor is + * written by independent vendor, the vendor knows only your Convertor API, + * he does not know how the whole system looks and how the convertor is supposed + * to be used. + */ +public class Task3Test extends TestCase { + public Task3Test(String testName) { + super(testName); + } + + @Override + protected void setUp() throws Exception { + } + + @Override + protected void tearDown() throws Exception { + } + + // Backward compatibly enhance your existing API to support following + // usecases: + // + + + /** Without knowing anything about the surrounding system, write an + * implementation of convertor that will return different rates everytime + * it is queried. Convert USD to CZK and vice versa. Start with the rate of + * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query. + * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD + * until you reach 1USD = 16CZK + * + * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK + */ + public static Convertor createOnlineCZKUSDConvertor() { + // initial rate: 1USD = 16CZK + // 2nd query 1USD = 15.99CZK + // 3rd query 1USD = 15.98CZK + // until 1USD = 15.00CZK + // then 1USD = 15.01CZK + // then 1USD = 15.02CZK + // and so on and on up to 1USD = 16CZK + // and then another round to 15, etc. + return new OnlineConvertor(); + } + + public void testFewQueriesForOnlineConvertor() { + Convertor c = createOnlineCZKUSDConvertor(); + doFewQueriesForOnlineConvertor(c); + } + + static void doFewQueriesForOnlineConvertor(Convertor c) { + // convert $5 to CZK using c: + assertEquals("Result is 80 CZK", money(80,CZK), c.convert(money(5,USD), CZK)); + + // convert $8 to CZK using c: + assertEquals("Result is 127.92 CZK", money(127.92,CZK), c.convert(money(8,USD), CZK)); + + // convert $1 to CZK using c: + assertEquals("Result is 15.98 CZK", money(15.98,CZK), c.convert(money(1,USD), CZK)); + + // convert 15.97CZK to USD using c: + assertEquals("Result is 1 USD", money(1,USD), c.convert(money(15.97,CZK), USD)); + //assertEquals("Result is 1$"); + + } + + /** Join the convertors and show they behave sane. + */ + public void testOnlineConvertorComposition() throws Exception { + + Convertor c = Task2Test.merge( + createOnlineCZKUSDConvertor(), + Task1Test.createSKKtoCZK() + ); + + // convert 16CZK to SKK using c: + assertEquals("Result is 20 SKK", money(20,SKK), c.convert(money(16,CZK), SKK)); + + // convert 500SKK to CZK using c: + assertEquals("Result is 400 CZK", money(400,CZK), c.convert(money(500,SKK), CZK)); + + doFewQueriesForOnlineConvertor(c); + } +}