task4/solution07/test/org/apidesign/apifest08/test/Task1Test.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 45 task3/solution07/test/org/apidesign/apifest08/test/Task1Test.java@251d0ed461fb
permissions -rw-r--r--
Copying structure for task4
     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     //
    30     // Imagine that there are three parts of the whole system:
    31     // 1. there is someone who knows the current exchange rate
    32     // 2. there is someone who wants to do the conversion
    33     // 3. there is the API between 1. and 2. which allows them to communicate
    34     // Please design such API
    35     //
    36 
    37     protected static final Currency CZK = Currency.getInstance( "CZK" );
    38     protected static final Currency SKK = Currency.getInstance( "SKK" );
    39     protected static final Currency USD = Currency.getInstance( "USD" );
    40     
    41     /** Create convertor that understands two currencies, CZK and
    42      *  USD. Make 1 USD == 17 CZK.
    43      *  USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
    44      *  e.g. those that know the exchange rate. They somehow need to create
    45      *  the objects from the API and tell them the exchange rate. The API itself
    46      *  knows nothing about any rates, before the createCZKtoUSD method is called.
    47      *
    48      * Creation of the convertor shall not require subclassing of any class
    49      * or interface on the client side.
    50      *
    51      * @return prepared convertor ready for converting USD to CZK and CZK to USD
    52      */
    53     public static Convertor createCZKtoUSD() {
    54         final TableConvertor convertor = new TableConvertor();
    55         final MonetaryAmount amountInCZK = new MonetaryAmount( 17, CZK );
    56         final MonetaryAmount amountInUSD = new MonetaryAmount( 1, USD );
    57         convertor.putIntoTable( new ConversionRate( amountInCZK, amountInUSD ) );
    58         convertor.putIntoTable( new ConversionRate( amountInUSD, amountInCZK ) );
    59         return new ContractImposingDelegatingConvertor( convertor ).test();
    60     }
    61 
    62     /** Create convertor that understands two currencies, CZK and
    63      *  SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
    64      *  it knows the exchange rate, and needs to use the API to create objects
    65      *  with the exchange rate. Anyone shall be ready to call this method without
    66      *  any other method being called previously. The API itself shall know
    67      *  nothing about any rates, before this method is called.
    68      *
    69      * Creation of the convertor shall not require subclassing of any class
    70      * or interface on the client side.
    71      * 
    72      * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    73      */
    74     public static Convertor createSKKtoCZK() {
    75         final TableConvertor convertor = new TableConvertor();
    76         final MonetaryAmount amountInSKK = new MonetaryAmount( 100, SKK );
    77         final MonetaryAmount amountInCZK = new MonetaryAmount( 80, CZK );
    78         convertor.putIntoTable( new ConversionRate( amountInSKK, amountInCZK ) );
    79         convertor.putIntoTable( new ConversionRate( amountInCZK, amountInSKK ) );
    80         return new ContractImposingDelegatingConvertor( convertor ).test();
    81     }
    82 
    83     //
    84     // now the methods for group #2 follow:
    85     // this group knows nothing about exchange rates, but knows how to use
    86     // the API to do conversions. It somehow (by calling one of the factory
    87     // methods) gets objects from the API and uses them to do the conversions.
    88     //
    89 
    90     /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    91      * with it.
    92      */
    93     public void testCurrencyCZKUSD() throws Exception {
    94         final Convertor c = createCZKtoUSD();
    95         
    96         // convert $5 to CZK using c:
    97         final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) );
    98         final MonetaryAmount a1 = r1.getNetAmount();
    99         // assertEquals("Result is 85 CZK");
   100         assertNotNull( a1 );
   101         assertEquals( 85.0, a1.getAmount().doubleValue() );
   102         assertEquals( CZK, a1.getCurrency() );
   103 
   104         // convert $8 to CZK
   105         final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) );
   106         final MonetaryAmount a2 = r2.getNetAmount();
   107         // assertEquals("Result is 136 CZK");
   108         assertNotNull( a2 );
   109         assertEquals( 136.0, a2.getAmount().doubleValue() );
   110         assertEquals( CZK, a2.getCurrency() );
   111 
   112         // convert 1003CZK to USD
   113         final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1003, CZK ), USD ) );
   114         final MonetaryAmount a3 = r3.getNetAmount();
   115         // assertEquals("Result is 59 USD");
   116         assertNotNull( a3 );
   117         assertEquals( 59.0, a3.getAmount().doubleValue() );
   118         assertEquals( USD, a3.getCurrency() );
   119     }
   120 
   121     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
   122      * with it.
   123      */
   124     public void testCurrencySKKCZK() throws Exception {
   125         final Convertor c = createSKKtoCZK();
   126         
   127         // convert 16CZK using c:
   128         final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) );
   129         final MonetaryAmount a1 = r1.getNetAmount();
   130         // assertEquals("Result is 20 SKK");
   131         assertNotNull( a1 );
   132         assertEquals( 20.0, a1.getAmount().doubleValue() );
   133         assertEquals( SKK, a1.getCurrency() );
   134         
   135         // convert 500SKK to CZK
   136         final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) );
   137         final MonetaryAmount a2 = r2.getNetAmount();
   138         // assertEquals("Result is 400 CZK");
   139         assertNotNull( a2 );
   140         assertEquals( 400.0, a2.getAmount().doubleValue() );
   141         assertEquals( CZK, a2.getCurrency() );
   142     }
   143 
   144     /** Verify that the CZK to USD convertor knows nothing about SKK.
   145     */
   146     public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
   147         final Convertor c = createCZKtoUSD();
   148         
   149         // convert $5 to SKK, the API shall say this is not possible
   150         final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), SKK ) );
   151         final MonetaryAmount a1 = r1.getNetAmount();
   152         assertNull( a1 );
   153 
   154         // convert 500 SKK to CZK, the API shall say this is not possible
   155         final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, SKK ), CZK ) );
   156         final MonetaryAmount a2 = r2.getNetAmount();
   157         assertNull( a2 );
   158     }
   159 
   160     /** Verify that the CZK to SKK convertor knows nothing about USD.
   161     */
   162     public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception {
   163         final Convertor c = createSKKtoCZK();
   164         
   165         // convert $5 to SKK, the API shall say this is not possible
   166         final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), SKK ) );
   167         final MonetaryAmount a1 = r1.getNetAmount();
   168         assertNull( a1 );
   169         
   170         // convert 500 CZK to USD, the API shall say this is not possible
   171         final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, CZK ), USD ) );
   172         final MonetaryAmount a2 = r2.getNetAmount();
   173         assertNull( a2 );
   174     }
   175 
   176 }
   177