jaroslav@43: package org.apidesign.apifest08.test; jaroslav@43: jaroslav@43: import junit.framework.TestCase; jaroslav@43: import org.apidesign.apifest08.currency.Convertor; japod@54: import org.apidesign.apifest08.currency.ConvertorFactory; japod@54: import org.apidesign.apifest08.currency.CurrencyRate; japod@54: import org.apidesign.apifest08.currency.Rate; jaroslav@43: jaroslav@43: /** The exchange rates are not always the same. They are changing. Day by day, jaroslav@43: * hour by hour, minute by minute. For every bank it is important to always jaroslav@43: * have the actual exchange rate available in the system. That is why let's jaroslav@43: * create a pluggable convertor that will always have up to date value of its jaroslav@43: * exchange rate. jaroslav@43: *

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