task1/solution07/test/org/apidesign/apifest08/test/Task1Test.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
child 18 83e731257bdc
permissions -rw-r--r--
Adding solutions received for task1
     1 package org.apidesign.apifest08.test;
     2 
     3 import java.util.Currency;
     4 import junit.framework.TestCase;
     5 import org.apidesign.apifest08.currency.ConversionRate;
     6 import org.apidesign.apifest08.currency.Convertor;
     7 import org.apidesign.apifest08.currency.MonetaryAmount;
     8 import org.apidesign.apifest08.currency.TableConvertor;
     9 
    10 /** Finish the Convertor API, and then write bodies of methods inside
    11  * of this class to match the given tasks. To fullfil your task, use the
    12  * API define in the <code>org.apidesign.apifest08.currency</code> package.
    13  * Do not you reflection, or other hacks as your code
    14  * shall run without any runtime permissions.
    15  */
    16 public class Task1Test extends TestCase {
    17     public Task1Test(String testName) {
    18         super(testName);
    19     }
    20 
    21     @Override
    22     protected void setUp() throws Exception {
    23     }
    24 
    25     @Override
    26     protected void tearDown() throws Exception {
    27     }
    28 
    29     protected static final Currency CZK = Currency.getInstance( "CZK" );
    30     protected static final Currency SKK = Currency.getInstance( "SKK" );
    31     protected static final Currency USD = Currency.getInstance( "USD" );
    32     
    33     /** Create convertor that understands two currencies, CZK and
    34      *  USD. Make 1 USD == 17 CZK.
    35      *
    36      * Creation of the convertor shall not require subclassing of any class
    37      * or interface on the client side.
    38      *
    39      * @return prepared convertor ready for converting USD to CZK and CZK to USD
    40      */
    41     public static Convertor createCZKtoUSD() {
    42         final TableConvertor convertor = new TableConvertor();
    43         final MonetaryAmount amountInCZK = new MonetaryAmount( 17, CZK );
    44         final MonetaryAmount amountInUSD = new MonetaryAmount( 1, USD );
    45         convertor.putIntoTable( new ConversionRate( amountInCZK, amountInUSD ) );
    46         convertor.putIntoTable( new ConversionRate( amountInUSD, amountInCZK ) );
    47         return new ContractImposingDelegatingConvertor( convertor ).test();
    48     }
    49 
    50     /** Create convertor that understands two currencies, CZK and
    51      *  SKK. Make 100 SKK == 80 CZK.
    52      *
    53      * Creation of the convertor shall not require subclassing of any class
    54      * or interface on the client side.
    55      * 
    56      * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    57      */
    58     public static Convertor createSKKtoCZK() {
    59         final TableConvertor convertor = new TableConvertor();
    60         final MonetaryAmount amountInSKK = new MonetaryAmount( 100, SKK );
    61         final MonetaryAmount amountInCZK = new MonetaryAmount( 80, CZK );
    62         convertor.putIntoTable( new ConversionRate( amountInSKK, amountInCZK ) );
    63         convertor.putIntoTable( new ConversionRate( amountInCZK, amountInSKK ) );
    64         return new ContractImposingDelegatingConvertor( convertor ).test();
    65     }
    66 
    67     /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    68      * with it.
    69      */
    70     public void testCurrencyCZKUSD() throws Exception {
    71         final Convertor c = createCZKtoUSD();
    72         
    73         // convert $5 to CZK using c:
    74         final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) );
    75         final MonetaryAmount a1 = r1.getNetAmount();
    76         // assertEquals("Result is 85 CZK");
    77         assertNotNull( a1 );
    78         assertEquals( 85.0, a1.getAmount().doubleValue() );
    79         assertEquals( CZK, a1.getCurrency() );
    80 
    81         // convert $8 to CZK
    82         final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) );
    83         final MonetaryAmount a2 = r2.getNetAmount();
    84         // assertEquals("Result is 136 CZK");
    85         assertNotNull( a2 );
    86         assertEquals( 136.0, a2.getAmount().doubleValue() );
    87         assertEquals( CZK, a2.getCurrency() );
    88 
    89         // convert 1003CZK to USD
    90         final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1003, CZK ), USD ) );
    91         final MonetaryAmount a3 = r3.getNetAmount();
    92         // assertEquals("Result is 59 USD");
    93         assertNotNull( a3 );
    94         assertEquals( 59.0, a3.getAmount().doubleValue() );
    95         assertEquals( USD, a3.getCurrency() );
    96     }
    97 
    98     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    99      * with it.
   100      */
   101     public void testCurrencySKKCZK() throws Exception {
   102         final Convertor c = createSKKtoCZK();
   103         
   104         // convert 16CZK using c:
   105         final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) );
   106         final MonetaryAmount a1 = r1.getNetAmount();
   107         // assertEquals("Result is 20 SKK");
   108         assertNotNull( a1 );
   109         assertEquals( 20.0, a1.getAmount().doubleValue() );
   110         assertEquals( SKK, a1.getCurrency() );
   111         
   112         // convert 500SKK to CZK
   113         final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) );
   114         final MonetaryAmount a2 = r2.getNetAmount();
   115         // assertEquals("Result is 400 CZK");
   116         assertNotNull( a2 );
   117         assertEquals( 400.0, a2.getAmount().doubleValue() );
   118         assertEquals( CZK, a2.getCurrency() );
   119     }
   120 
   121 }
   122