diff -r c1d43bc1e9c0 -r 58ec6da75f6f task4/solution06/test/org/apidesign/apifest08/test/Task3Test.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task4/solution06/test/org/apidesign/apifest08/test/Task3Test.java Sat Oct 11 23:38:46 2008 +0200 @@ -0,0 +1,111 @@ +package org.apidesign.apifest08.test; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +import junit.framework.TestCase; +import org.apidesign.apifest08.currency.Convertor; +import static org.apidesign.apifest08.test.Currencies.*; + +/** 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() { + Convertor.RateProvider rateProvider = new Convertor.RateProvider() { + private BigDecimal seed = new BigDecimal("16.01").setScale(2); + + @Override + public BigDecimal getRate() { + if(seed.equals(new BigDecimal("15").setScale(2))) { + seed = seed.add(new BigDecimal("0.01")).setScale(2); + } else { + seed = seed.subtract(new BigDecimal("0.01")).setScale(2); + } + return seed; + } + + }; + + // 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 Convertor(rateProvider, USD, CZK); + } + + public void testFewQueriesForOnlineConvertor() { + Convertor c = createOnlineCZKUSDConvertor(); + doFewQueriesForOnlineConvertor(c); + } + + static void doFewQueriesForOnlineConvertor(Convertor c) { + // convert $5 to CZK using c: + assertEquals("Result is 80 CZK",80 ,c.convert(new BigDecimal(5), USD, CZK).getValue().intValue()); + + // convert $8 to CZK using c: + assertEquals("Result is 127.92 CZK", 127.92d , c.convert(new BigDecimal(8), USD, CZK).getValue().doubleValue()); + + // convert $1 to CZK using c: + assertEquals("Result is 15.98 CZK", 15.98d, c.convert(new BigDecimal(1), USD, CZK).getValue().doubleValue()); + + // convert 15.97CZK to USD using c: + assertEquals("Result is 1$", 1, c.convert(new BigDecimal("15.97").setScale(2), CZK, USD).getValue().intValue()); + } + + /** 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", 20, c.convert(new BigDecimal(16), CZK, SKK).getValue().intValue()); + + // convert 500SKK to CZK using c: + assertEquals("Result is 400 CZK", 400, c.convert(new BigDecimal(500), SKK, CZK).getValue().intValue()); + + doFewQueriesForOnlineConvertor(c); + } +}