task4/solution07/test/org/apidesign/apifest08/test/Task3Test.java
changeset 61 58ec6da75f6f
parent 59 c1d43bc1e9c0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution07/test/org/apidesign/apifest08/test/Task3Test.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,135 @@
     1.4 +package org.apidesign.apifest08.test;
     1.5 +
     1.6 +import java.math.BigDecimal;
     1.7 +import java.util.Currency;
     1.8 +import junit.framework.TestCase;
     1.9 +import org.apidesign.apifest08.currency.Convertor;
    1.10 +import org.apidesign.apifest08.currency.MonetaryAmount;
    1.11 +import org.apidesign.apifest08.currency.anothervendor.ZigZaggingBidirectionalConvertor;
    1.12 +
    1.13 +/** The exchange rates are not always the same. They are changing. Day by day,
    1.14 + * hour by hour, minute by minute. For every bank it is important to always
    1.15 + * have the actual exchange rate available in the system. That is why let's
    1.16 + * create a pluggable convertor that will always have up to date value of its
    1.17 + * exchange rate.
    1.18 + * <p>
    1.19 + * The quest for today is to allow 3rd party developer to write a convertor
    1.20 + * that adjusts its exchange rate everytime it is queried. This convertor is
    1.21 + * written by independent vendor, the vendor knows only your Convertor API,
    1.22 + * he does not know how the whole system looks and how the convertor is supposed
    1.23 + * to be used.
    1.24 + */
    1.25 +public class Task3Test extends TestCase {
    1.26 +    public Task3Test(String testName) {
    1.27 +        super(testName);
    1.28 +    }
    1.29 +
    1.30 +    @Override
    1.31 +    protected void setUp() throws Exception {
    1.32 +    }
    1.33 +
    1.34 +    @Override
    1.35 +    protected void tearDown() throws Exception {
    1.36 +    }
    1.37 +
    1.38 +    protected static final Currency CZK = Currency.getInstance( "CZK" );
    1.39 +    protected static final Currency SKK = Currency.getInstance( "SKK" );
    1.40 +    protected static final Currency USD = Currency.getInstance( "USD" );
    1.41 +
    1.42 +    // Backward compatibly enhance your existing API to support following
    1.43 +    // usecases:
    1.44 +    //
    1.45 +
    1.46 +
    1.47 +    /** Without knowing anything about the surrounding system, write an
    1.48 +     * implementation of convertor that will return different rates everytime
    1.49 +     * it is queried. Convert USD to CZK and vice versa. Start with the rate of
    1.50 +     * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
    1.51 +     * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
    1.52 +     * until you reach 1USD = 16CZK
    1.53 +     *
    1.54 +     * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
    1.55 +     */
    1.56 +    public static Convertor createOnlineCZKUSDConvertor() {
    1.57 +        // initial rate: 1USD = 16CZK
    1.58 +        // 2nd query 1USD = 15.99CZK
    1.59 +        // 3rd query 1USD = 15.98CZK
    1.60 +        // until 1USD = 15.00CZK
    1.61 +        // then 1USD = 15.01CZK
    1.62 +        // then 1USD = 15.02CZK
    1.63 +        // and so on and on up to 1USD = 16CZK
    1.64 +        // and then another round to 15, etc.
    1.65 +        final MonetaryAmount oneUSD = new MonetaryAmount( 1, USD );
    1.66 +        final BigDecimal startAmount = new BigDecimal( 16 );
    1.67 +        final BigDecimal endAmount = new BigDecimal( 15 );
    1.68 +        final BigDecimal stepAmount = BigDecimal.ONE.movePointLeft( 2 ).negate();
    1.69 +        return new ContractImposingDelegatingConvertor( new ZigZaggingBidirectionalConvertor( oneUSD, CZK, startAmount, endAmount, stepAmount ) );
    1.70 +    }
    1.71 +
    1.72 +    public void testFewQueriesForOnlineConvertor() {
    1.73 +        Convertor c = createOnlineCZKUSDConvertor();
    1.74 +        doFewQueriesForOnlineConvertor(c);
    1.75 +    }
    1.76 +
    1.77 +    static void doFewQueriesForOnlineConvertor( final Convertor c ) {
    1.78 +        // convert $5 to CZK using c:
    1.79 +        final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) );
    1.80 +        final MonetaryAmount a1 = r1.getNetAmount();
    1.81 +        //assertEquals("Result is 80 CZK");
    1.82 +        assertNotNull( a1 );
    1.83 +        assertEquals( 80.0, a1.getAmount().doubleValue() );
    1.84 +        assertEquals( CZK, a1.getCurrency() );
    1.85 +
    1.86 +        // convert $8 to CZK using c:
    1.87 +        final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) );
    1.88 +        final MonetaryAmount a2 = r2.getNetAmount();
    1.89 +        //assertEquals("Result is 127.92 CZK");
    1.90 +        assertNotNull( a2 );
    1.91 +        assertEquals( 12792.0, a2.getAmount().movePointRight( 2 ).doubleValue() );
    1.92 +        assertEquals( CZK, a2.getCurrency() );
    1.93 +
    1.94 +        // convert $1 to CZK using c:
    1.95 +        final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1, USD ), CZK ) );
    1.96 +        final MonetaryAmount a3 = r3.getNetAmount();
    1.97 +        //assertEquals("Result is 15.98 CZK");
    1.98 +        assertNotNull( a3 );
    1.99 +        assertEquals( 1598.0, a3.getAmount().movePointRight( 2 ).doubleValue() );
   1.100 +        assertEquals( CZK, a3.getCurrency() );
   1.101 +
   1.102 +        // convert 15.97CZK to USD using c:
   1.103 +        final BigDecimal s4 = new BigDecimal( 1597 ).movePointLeft( 2 );
   1.104 +        final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( s4, CZK ), USD ) );
   1.105 +        final MonetaryAmount a4 = r4.getNetAmount();
   1.106 +        //assertEquals("Result is 1$");
   1.107 +        assertNotNull( a4 );
   1.108 +        assertEquals( 1.0, a4.getAmount().doubleValue() );
   1.109 +        assertEquals( USD, a4.getCurrency() );
   1.110 +    }
   1.111 +
   1.112 +    /** Join the convertors and show they behave sane.
   1.113 +     */
   1.114 +    public void testOnlineConvertorComposition() throws Exception {
   1.115 +        final Convertor c = Task2Test.merge(
   1.116 +            createOnlineCZKUSDConvertor(),
   1.117 +            Task1Test.createSKKtoCZK()
   1.118 +        );
   1.119 +
   1.120 +        // convert 16CZK using c:
   1.121 +        final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) );
   1.122 +        final MonetaryAmount a4 = r4.getNetAmount();
   1.123 +        // assertEquals("Result is 20 SKK");
   1.124 +        assertNotNull( a4 );
   1.125 +        assertEquals( 20.0, a4.getAmount().doubleValue() );
   1.126 +        assertEquals( SKK, a4.getCurrency() );
   1.127 +
   1.128 +        // convert 500SKK to CZK using c:
   1.129 +        final Convertor.ConversionResult r5 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) );
   1.130 +        final MonetaryAmount a5 = r5.getNetAmount();
   1.131 +        // assertEquals("Result is 400 CZK");
   1.132 +        assertNotNull( a5 );
   1.133 +        assertEquals( 400.0, a5.getAmount().doubleValue() );
   1.134 +        assertEquals( CZK, a5.getCurrency() );
   1.135 +
   1.136 +        doFewQueriesForOnlineConvertor(c);
   1.137 +    }
   1.138 +}