diff -r c1d43bc1e9c0 -r 58ec6da75f6f task4/solution12/test/org/apidesign/apifest08/test/Task3Test.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task4/solution12/test/org/apidesign/apifest08/test/Task3Test.java Sat Oct 11 23:38:46 2008 +0200 @@ -0,0 +1,145 @@ +package org.apidesign.apifest08.test; + +import java.util.Currency; + +import junit.framework.TestCase; +import org.apidesign.apifest08.currency.Convertor; +import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException; + +/** 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 { + + private static double actualRate; + private static boolean increasing; + + private static final double EXCHANGE_RATE_MAX = 16.0; + private static final double EXCHANGE_RATE_MIN = 15.0; + + 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() { + actualRate = 16.01; + increasing = false; + + // sets actual exchange rates + setRates(); + + // create new instance + Convertor convertor = null; + try { + convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("CZK")); + } catch (UnknownConvertorException e) { + e.printStackTrace(); + } + + return convertor; + } + + public void testFewQueriesForOnlineConvertor() throws Exception { + Convertor c = createOnlineCZKUSDConvertor(); + doFewQueriesForOnlineConvertor(c); + } + + static void doFewQueriesForOnlineConvertor(Convertor c) throws Exception { + // convert $5 to CZK using c: + double result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("CZK")); + double expectedResult = actualRate * 5; + assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result); + + // change exchange rates + setRates(); + + // convert $8 to CZK using c: + result = c.convert(8d, Currency.getInstance("USD"), Currency.getInstance("CZK")); + expectedResult = actualRate * 8; + assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result); + + // change exchange rates + setRates(); + + // convert $1 to CZK using c: + result = c.convert(1d, Currency.getInstance("USD"), Currency.getInstance("CZK")); + expectedResult = actualRate * 1; + assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result); + + // change exchange rates + setRates(); + + // convert 15.97CZK to USD using c: + result = c.convert(15.97d, Currency.getInstance("CZK"), Currency.getInstance("USD")); + expectedResult = 15.97 / actualRate ; + assertEquals("Result is not " + expectedResult + " USD", expectedResult, result); + } + + /** 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: + double result = c.convert(16d, Currency.getInstance("CZK"), Currency.getInstance("SKK")); + assertEquals("Result is not 20 SKK", 20d, result); + + // convert 500SKK to CZK using c: + result = c.convert(500d, Currency.getInstance("SKK"), Currency.getInstance("CZK")); + assertEquals("Result is not 400 CZK", 400d, result); + + doFewQueriesForOnlineConvertor(c); + } + + private static void setRates() { + // logic for change of actual exchange rate + if(increasing) { + actualRate += 0.01; + if(actualRate == EXCHANGE_RATE_MAX){ + increasing = false; + } + } else { + actualRate -= 0.01; + if(actualRate == EXCHANGE_RATE_MIN){ + increasing = true; + } + } + + // set exchange rates + Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), actualRate, 1d); + } +}