jaroslav@43: package org.apidesign.apifest08.test; jaroslav@43: japod@51: import java.util.Currency; japod@51: jaroslav@43: import junit.framework.TestCase; jaroslav@43: import org.apidesign.apifest08.currency.Convertor; japod@51: import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException; 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 { japod@51: japod@51: private static double actualRate; japod@51: private static boolean increasing; japod@51: japod@51: private static final double EXCHANGE_RATE_MAX = 16.0; japod@51: private static final double EXCHANGE_RATE_MIN = 15.0; japod@51: 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() { japod@51: actualRate = 16.01; japod@51: increasing = false; japod@51: japod@51: // sets actual exchange rates japod@51: setRates(); japod@51: japod@51: // create new instance japod@51: Convertor convertor = null; japod@51: try { japod@51: convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("CZK")); japod@51: } catch (UnknownConvertorException e) { japod@51: e.printStackTrace(); japod@51: } japod@51: japod@51: return convertor; jaroslav@43: } jaroslav@43: japod@51: public void testFewQueriesForOnlineConvertor() throws Exception { jaroslav@43: Convertor c = createOnlineCZKUSDConvertor(); jaroslav@43: doFewQueriesForOnlineConvertor(c); jaroslav@43: } jaroslav@43: japod@51: static void doFewQueriesForOnlineConvertor(Convertor c) throws Exception { jaroslav@43: // convert $5 to CZK using c: japod@51: double result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("CZK")); japod@51: double expectedResult = actualRate * 5; japod@51: assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result); jaroslav@43: japod@51: // change exchange rates japod@51: setRates(); japod@51: jaroslav@43: // convert $8 to CZK using c: japod@51: result = c.convert(8d, Currency.getInstance("USD"), Currency.getInstance("CZK")); japod@51: expectedResult = actualRate * 8; japod@51: assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result); japod@51: japod@51: // change exchange rates japod@51: setRates(); jaroslav@43: jaroslav@43: // convert $1 to CZK using c: japod@51: result = c.convert(1d, Currency.getInstance("USD"), Currency.getInstance("CZK")); japod@51: expectedResult = actualRate * 1; japod@51: assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result); japod@51: japod@51: // change exchange rates japod@51: setRates(); jaroslav@43: jaroslav@43: // convert 15.97CZK to USD using c: japod@51: result = c.convert(15.97d, Currency.getInstance("CZK"), Currency.getInstance("USD")); japod@51: expectedResult = 15.97 / actualRate ; japod@51: assertEquals("Result is not " + expectedResult + " USD", expectedResult, result); 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: japod@51: double result = c.convert(16d, Currency.getInstance("CZK"), Currency.getInstance("SKK")); japod@51: assertEquals("Result is not 20 SKK", 20d, result); jaroslav@43: jaroslav@43: // convert 500SKK to CZK using c: japod@51: result = c.convert(500d, Currency.getInstance("SKK"), Currency.getInstance("CZK")); japod@51: assertEquals("Result is not 400 CZK", 400d, result); jaroslav@43: jaroslav@43: doFewQueriesForOnlineConvertor(c); jaroslav@43: } japod@51: japod@51: private static void setRates() { japod@51: // logic for change of actual exchange rate japod@51: if(increasing) { japod@51: actualRate += 0.01; japod@51: if(actualRate == EXCHANGE_RATE_MAX){ japod@51: increasing = false; japod@51: } japod@51: } else { japod@51: actualRate -= 0.01; japod@51: if(actualRate == EXCHANGE_RATE_MIN){ japod@51: increasing = true; japod@51: } japod@51: } japod@51: japod@51: // set exchange rates japod@51: Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), actualRate, 1d); japod@51: } jaroslav@43: }