jaroslav@43: package org.apidesign.apifest08.test; jaroslav@43: japod@55: import java.math.BigDecimal; japod@55: import java.util.Currency; jaroslav@43: import junit.framework.TestCase; jaroslav@43: import org.apidesign.apifest08.currency.Convertor; japod@55: import org.apidesign.apifest08.currency.InvalidConversionException; japod@55: import org.apidesign.apifest08.currency.OnlineConvertor; 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: */ japod@55: public class Task3Test japod@55: extends TestCase japod@55: { japod@55: private final static Currency CZK; japod@55: private final static Currency SKK; japod@55: private final static Currency USD; japod@55: japod@55: static japod@55: { japod@55: CZK = Currency.getInstance("CZK"); japod@55: SKK = Currency.getInstance("SKK"); japod@55: USD = Currency.getInstance("USD"); japod@55: } japod@55: japod@55: public Task3Test(String testName) japod@55: { jaroslav@43: super(testName); jaroslav@43: } jaroslav@43: jaroslav@43: @Override japod@55: protected void setUp() throws Exception japod@55: { jaroslav@43: } jaroslav@43: jaroslav@43: @Override japod@55: protected void tearDown() throws Exception japod@55: { 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 japod@55: * @throws InvalidConversionException jaroslav@43: */ japod@55: public static Convertor createOnlineCZKUSDConvertor() japod@55: throws InvalidConversionException japod@55: { japod@55: final Convertor convertor; japod@55: japod@55: convertor = new OnlineConvertor(USD, japod@55: CZK, japod@55: new TestExchangeRateFinder(new BigDecimal("15.00"), japod@55: new BigDecimal("16.00"), japod@55: new BigDecimal("16.00"), japod@55: new BigDecimal("0.01"), japod@55: new BigDecimal("-0.01"))); japod@55: 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@55: return convertor; jaroslav@43: } jaroslav@43: japod@55: public void testFewQueriesForOnlineConvertor() japod@55: throws InvalidConversionException japod@55: { jaroslav@43: Convertor c = createOnlineCZKUSDConvertor(); jaroslav@43: doFewQueriesForOnlineConvertor(c); jaroslav@43: } jaroslav@43: japod@55: static void doFewQueriesForOnlineConvertor(Convertor c) japod@55: throws InvalidConversionException japod@55: { japod@55: BigDecimal amount; japod@55: jaroslav@43: // convert $5 to CZK using c: jaroslav@43: //assertEquals("Result is 80 CZK"); japod@55: amount = c.convert(USD, CZK, new BigDecimal("5.00")); japod@55: assertEquals(new BigDecimal("80.00"), amount); jaroslav@43: jaroslav@43: // convert $8 to CZK using c: jaroslav@43: //assertEquals("Result is 127.92 CZK"); japod@55: amount = c.convert(USD, CZK, new BigDecimal("8.00")); japod@55: assertEquals(new BigDecimal("127.92"), amount); jaroslav@43: jaroslav@43: // convert $1 to CZK using c: jaroslav@43: //assertEquals("Result is 15.98 CZK"); japod@55: amount = c.convert(USD, CZK, new BigDecimal("1.00")); japod@55: assertEquals(new BigDecimal("15.98"), amount); jaroslav@43: jaroslav@43: // convert 15.97CZK to USD using c: jaroslav@43: //assertEquals("Result is 1$"); japod@55: amount = c.convert(CZK, USD, new BigDecimal("15.97")); japod@55: assertEquals(new BigDecimal("1.00"), amount); jaroslav@43: } jaroslav@43: jaroslav@43: /** Join the convertors and show they behave sane. jaroslav@43: */ jaroslav@43: public void testOnlineConvertorComposition() throws Exception { japod@55: BigDecimal amount; japod@55: 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@55: amount = c.convert(CZK, SKK, new BigDecimal("16.00")); japod@55: assertEquals(new BigDecimal("20.00"), amount); japod@55: jaroslav@43: // convert 500SKK to CZK using c: jaroslav@43: // assertEquals("Result is 400 CZK"); japod@55: amount = c.convert(SKK, CZK, new BigDecimal("500.00")); japod@55: assertEquals(new BigDecimal("400.00"), amount); jaroslav@43: jaroslav@43: doFewQueriesForOnlineConvertor(c); jaroslav@43: } jaroslav@43: }