task3/solution07/test/org/apidesign/apifest08/test/Task3Test.java
author japod@localhost
Fri, 10 Oct 2008 22:10:25 +0200
changeset 57 be49855c7fa2
parent 48 79a576394dd7
child 59 c1d43bc1e9c0
permissions -rw-r--r--
solution07 task3
     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         if (Boolean.getBoolean("ignore.failing")) {
    71             // implement me!
    72             return;
    73         }
    74 
    75         Convertor c = createOnlineCZKUSDConvertor();
    76         doFewQueriesForOnlineConvertor(c);
    77     }
    78 
    79     static void doFewQueriesForOnlineConvertor( final Convertor c ) {
    80         // convert $5 to CZK using c:
    81         final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) );
    82         final MonetaryAmount a1 = r1.getNetAmount();
    83         //assertEquals("Result is 80 CZK");
    84         assertNotNull( a1 );
    85         assertEquals( 80.0, a1.getAmount().doubleValue() );
    86         assertEquals( CZK, a1.getCurrency() );
    87 
    88         // convert $8 to CZK using c:
    89         final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) );
    90         final MonetaryAmount a2 = r2.getNetAmount();
    91         //assertEquals("Result is 127.92 CZK");
    92         assertNotNull( a2 );
    93         assertEquals( 12792.0, a2.getAmount().movePointRight( 2 ).doubleValue() );
    94         assertEquals( CZK, a2.getCurrency() );
    95 
    96         // convert $1 to CZK using c:
    97         final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1, USD ), CZK ) );
    98         final MonetaryAmount a3 = r3.getNetAmount();
    99         //assertEquals("Result is 15.98 CZK");
   100         assertNotNull( a3 );
   101         assertEquals( 1598.0, a3.getAmount().movePointRight( 2 ).doubleValue() );
   102         assertEquals( CZK, a3.getCurrency() );
   103 
   104         // convert 15.97CZK to USD using c:
   105         final BigDecimal s4 = new BigDecimal( 1597 ).movePointLeft( 2 );
   106         final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( s4, CZK ), USD ) );
   107         final MonetaryAmount a4 = r4.getNetAmount();
   108         //assertEquals("Result is 1$");
   109         assertNotNull( a4 );
   110         assertEquals( 1.0, a4.getAmount().doubleValue() );
   111         assertEquals( USD, a4.getCurrency() );
   112     }
   113 
   114     /** Join the convertors and show they behave sane.
   115      */
   116     public void testOnlineConvertorComposition() throws Exception {
   117         if (Boolean.getBoolean("ignore.failing")) {
   118             // implement me!
   119             return;
   120         }
   121         
   122         final Convertor c = Task2Test.merge(
   123             createOnlineCZKUSDConvertor(),
   124             Task1Test.createSKKtoCZK()
   125         );
   126 
   127         // convert 16CZK using c:
   128         final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) );
   129         final MonetaryAmount a4 = r4.getNetAmount();
   130         // assertEquals("Result is 20 SKK");
   131         assertNotNull( a4 );
   132         assertEquals( 20.0, a4.getAmount().doubleValue() );
   133         assertEquals( SKK, a4.getCurrency() );
   134 
   135         // convert 500SKK to CZK using c:
   136         final Convertor.ConversionResult r5 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) );
   137         final MonetaryAmount a5 = r5.getNetAmount();
   138         // assertEquals("Result is 400 CZK");
   139         assertNotNull( a5 );
   140         assertEquals( 400.0, a5.getAmount().doubleValue() );
   141         assertEquals( CZK, a5.getCurrency() );
   142 
   143         doFewQueriesForOnlineConvertor(c);
   144     }
   145 }