jaroslav@43: package org.apidesign.apifest08.test; jaroslav@43: jaroslav@43: import junit.framework.TestCase; jaroslav@43: import org.apidesign.apifest08.currency.Convertor; 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. jaroslav@43: return null; 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"); jaroslav@43: jaroslav@43: // convert $8 to CZK using c: jaroslav@43: //assertEquals("Result is 127.92 CZK"); jaroslav@43: jaroslav@43: // convert $1 to CZK using c: jaroslav@43: //assertEquals("Result is 15.98 CZK"); jaroslav@43: jaroslav@43: // convert 15.97CZK to USD using c: jaroslav@43: //assertEquals("Result is 1$"); jaroslav@43: jaroslav@43: fail("Implement me!"); 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"); jaroslav@43: jaroslav@43: // convert 500SKK to CZK using c: jaroslav@43: // assertEquals("Result is 400 CZK"); jaroslav@43: jaroslav@43: doFewQueriesForOnlineConvertor(c); jaroslav@43: } jaroslav@43: }