task3/solution07/test/org/apidesign/apifest08/test/Task3Test.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 11 Oct 2008 10:49:25 +0200
changeset 59 c1d43bc1e9c0
parent 57 be49855c7fa2
permissions -rw-r--r--
Removing all 'implement me' messages
     1 package org.apidesign.apifest08.test;
     2 
     3 import java.math.BigDecimal;
     4 import java.util.Currency;
     5 import junit.framework.TestCase;
     6 import org.apidesign.apifest08.currency.Convertor;
     7 import org.apidesign.apifest08.currency.MonetaryAmount;
     8 import org.apidesign.apifest08.currency.anothervendor.ZigZaggingBidirectionalConvertor;
     9 
    10 /** The exchange rates are not always the same. They are changing. Day by day,
    11  * hour by hour, minute by minute. For every bank it is important to always
    12  * have the actual exchange rate available in the system. That is why let's
    13  * create a pluggable convertor that will always have up to date value of its
    14  * exchange rate.
    15  * <p>
    16  * The quest for today is to allow 3rd party developer to write a convertor
    17  * that adjusts its exchange rate everytime it is queried. This convertor is
    18  * written by independent vendor, the vendor knows only your Convertor API,
    19  * he does not know how the whole system looks and how the convertor is supposed
    20  * to be used.
    21  */
    22 public class Task3Test extends TestCase {
    23     public Task3Test(String testName) {
    24         super(testName);
    25     }
    26 
    27     @Override
    28     protected void setUp() throws Exception {
    29     }
    30 
    31     @Override
    32     protected void tearDown() throws Exception {
    33     }
    34 
    35     protected static final Currency CZK = Currency.getInstance( "CZK" );
    36     protected static final Currency SKK = Currency.getInstance( "SKK" );
    37     protected static final Currency USD = Currency.getInstance( "USD" );
    38 
    39     // Backward compatibly enhance your existing API to support following
    40     // usecases:
    41     //
    42 
    43 
    44     /** Without knowing anything about the surrounding system, write an
    45      * implementation of convertor that will return different rates everytime
    46      * it is queried. Convert USD to CZK and vice versa. Start with the rate of
    47      * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
    48      * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
    49      * until you reach 1USD = 16CZK
    50      *
    51      * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
    52      */
    53     public static Convertor createOnlineCZKUSDConvertor() {
    54         // initial rate: 1USD = 16CZK
    55         // 2nd query 1USD = 15.99CZK
    56         // 3rd query 1USD = 15.98CZK
    57         // until 1USD = 15.00CZK
    58         // then 1USD = 15.01CZK
    59         // then 1USD = 15.02CZK
    60         // and so on and on up to 1USD = 16CZK
    61         // and then another round to 15, etc.
    62         final MonetaryAmount oneUSD = new MonetaryAmount( 1, USD );
    63         final BigDecimal startAmount = new BigDecimal( 16 );
    64         final BigDecimal endAmount = new BigDecimal( 15 );
    65         final BigDecimal stepAmount = BigDecimal.ONE.movePointLeft( 2 ).negate();
    66         return new ContractImposingDelegatingConvertor( new ZigZaggingBidirectionalConvertor( oneUSD, CZK, startAmount, endAmount, stepAmount ) );
    67     }
    68 
    69     public void testFewQueriesForOnlineConvertor() {
    70         Convertor c = createOnlineCZKUSDConvertor();
    71         doFewQueriesForOnlineConvertor(c);
    72     }
    73 
    74     static void doFewQueriesForOnlineConvertor( final Convertor c ) {
    75         // convert $5 to CZK using c:
    76         final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) );
    77         final MonetaryAmount a1 = r1.getNetAmount();
    78         //assertEquals("Result is 80 CZK");
    79         assertNotNull( a1 );
    80         assertEquals( 80.0, a1.getAmount().doubleValue() );
    81         assertEquals( CZK, a1.getCurrency() );
    82 
    83         // convert $8 to CZK using c:
    84         final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) );
    85         final MonetaryAmount a2 = r2.getNetAmount();
    86         //assertEquals("Result is 127.92 CZK");
    87         assertNotNull( a2 );
    88         assertEquals( 12792.0, a2.getAmount().movePointRight( 2 ).doubleValue() );
    89         assertEquals( CZK, a2.getCurrency() );
    90 
    91         // convert $1 to CZK using c:
    92         final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1, USD ), CZK ) );
    93         final MonetaryAmount a3 = r3.getNetAmount();
    94         //assertEquals("Result is 15.98 CZK");
    95         assertNotNull( a3 );
    96         assertEquals( 1598.0, a3.getAmount().movePointRight( 2 ).doubleValue() );
    97         assertEquals( CZK, a3.getCurrency() );
    98 
    99         // convert 15.97CZK to USD using c:
   100         final BigDecimal s4 = new BigDecimal( 1597 ).movePointLeft( 2 );
   101         final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( s4, CZK ), USD ) );
   102         final MonetaryAmount a4 = r4.getNetAmount();
   103         //assertEquals("Result is 1$");
   104         assertNotNull( a4 );
   105         assertEquals( 1.0, a4.getAmount().doubleValue() );
   106         assertEquals( USD, a4.getCurrency() );
   107     }
   108 
   109     /** Join the convertors and show they behave sane.
   110      */
   111     public void testOnlineConvertorComposition() throws Exception {
   112         final Convertor c = Task2Test.merge(
   113             createOnlineCZKUSDConvertor(),
   114             Task1Test.createSKKtoCZK()
   115         );
   116 
   117         // convert 16CZK using c:
   118         final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) );
   119         final MonetaryAmount a4 = r4.getNetAmount();
   120         // assertEquals("Result is 20 SKK");
   121         assertNotNull( a4 );
   122         assertEquals( 20.0, a4.getAmount().doubleValue() );
   123         assertEquals( SKK, a4.getCurrency() );
   124 
   125         // convert 500SKK to CZK using c:
   126         final Convertor.ConversionResult r5 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) );
   127         final MonetaryAmount a5 = r5.getNetAmount();
   128         // assertEquals("Result is 400 CZK");
   129         assertNotNull( a5 );
   130         assertEquals( 400.0, a5.getAmount().doubleValue() );
   131         assertEquals( CZK, a5.getCurrency() );
   132 
   133         doFewQueriesForOnlineConvertor(c);
   134     }
   135 }