diff -r c1d43bc1e9c0 -r 58ec6da75f6f task4/solution07/test/org/apidesign/apifest08/test/Task3Test.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task4/solution07/test/org/apidesign/apifest08/test/Task3Test.java Sat Oct 11 23:38:46 2008 +0200 @@ -0,0 +1,135 @@ +package org.apidesign.apifest08.test; + +import java.math.BigDecimal; +import java.util.Currency; +import junit.framework.TestCase; +import org.apidesign.apifest08.currency.Convertor; +import org.apidesign.apifest08.currency.MonetaryAmount; +import org.apidesign.apifest08.currency.anothervendor.ZigZaggingBidirectionalConvertor; + +/** 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 { + public Task3Test(String testName) { + super(testName); + } + + @Override + protected void setUp() throws Exception { + } + + @Override + protected void tearDown() throws Exception { + } + + protected static final Currency CZK = Currency.getInstance( "CZK" ); + protected static final Currency SKK = Currency.getInstance( "SKK" ); + protected static final Currency USD = Currency.getInstance( "USD" ); + + // 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() { + // initial rate: 1USD = 16CZK + // 2nd query 1USD = 15.99CZK + // 3rd query 1USD = 15.98CZK + // until 1USD = 15.00CZK + // then 1USD = 15.01CZK + // then 1USD = 15.02CZK + // and so on and on up to 1USD = 16CZK + // and then another round to 15, etc. + final MonetaryAmount oneUSD = new MonetaryAmount( 1, USD ); + final BigDecimal startAmount = new BigDecimal( 16 ); + final BigDecimal endAmount = new BigDecimal( 15 ); + final BigDecimal stepAmount = BigDecimal.ONE.movePointLeft( 2 ).negate(); + return new ContractImposingDelegatingConvertor( new ZigZaggingBidirectionalConvertor( oneUSD, CZK, startAmount, endAmount, stepAmount ) ); + } + + public void testFewQueriesForOnlineConvertor() { + Convertor c = createOnlineCZKUSDConvertor(); + doFewQueriesForOnlineConvertor(c); + } + + static void doFewQueriesForOnlineConvertor( final Convertor c ) { + // convert $5 to CZK using c: + final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) ); + final MonetaryAmount a1 = r1.getNetAmount(); + //assertEquals("Result is 80 CZK"); + assertNotNull( a1 ); + assertEquals( 80.0, a1.getAmount().doubleValue() ); + assertEquals( CZK, a1.getCurrency() ); + + // convert $8 to CZK using c: + final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) ); + final MonetaryAmount a2 = r2.getNetAmount(); + //assertEquals("Result is 127.92 CZK"); + assertNotNull( a2 ); + assertEquals( 12792.0, a2.getAmount().movePointRight( 2 ).doubleValue() ); + assertEquals( CZK, a2.getCurrency() ); + + // convert $1 to CZK using c: + final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1, USD ), CZK ) ); + final MonetaryAmount a3 = r3.getNetAmount(); + //assertEquals("Result is 15.98 CZK"); + assertNotNull( a3 ); + assertEquals( 1598.0, a3.getAmount().movePointRight( 2 ).doubleValue() ); + assertEquals( CZK, a3.getCurrency() ); + + // convert 15.97CZK to USD using c: + final BigDecimal s4 = new BigDecimal( 1597 ).movePointLeft( 2 ); + final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( s4, CZK ), USD ) ); + final MonetaryAmount a4 = r4.getNetAmount(); + //assertEquals("Result is 1$"); + assertNotNull( a4 ); + assertEquals( 1.0, a4.getAmount().doubleValue() ); + assertEquals( USD, a4.getCurrency() ); + } + + /** Join the convertors and show they behave sane. + */ + public void testOnlineConvertorComposition() throws Exception { + final Convertor c = Task2Test.merge( + createOnlineCZKUSDConvertor(), + Task1Test.createSKKtoCZK() + ); + + // convert 16CZK using c: + final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) ); + final MonetaryAmount a4 = r4.getNetAmount(); + // assertEquals("Result is 20 SKK"); + assertNotNull( a4 ); + assertEquals( 20.0, a4.getAmount().doubleValue() ); + assertEquals( SKK, a4.getCurrency() ); + + // convert 500SKK to CZK using c: + final Convertor.ConversionResult r5 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) ); + final MonetaryAmount a5 = r5.getNetAmount(); + // assertEquals("Result is 400 CZK"); + assertNotNull( a5 ); + assertEquals( 400.0, a5.getAmount().doubleValue() ); + assertEquals( CZK, a5.getCurrency() ); + + doFewQueriesForOnlineConvertor(c); + } +}