task4/solution11/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 53 task3/solution11/test/org/apidesign/apifest08/test/Task1Test.java@09d690bb97f6
permissions -rw-r--r--
Copying structure for task4
     1 package org.apidesign.apifest08.test;
     2 
     3 import junit.framework.TestCase;
     4 import org.apidesign.apifest08.currency.Convertor;
     5 import org.apidesign.apifest08.currency.CurrencyValue;
     6 
     7 /** Finish the Convertor API, and then write bodies of methods inside
     8  * of this class to match the given tasks. To fullfil your task, use the
     9  * API define in the <code>org.apidesign.apifest08.currency</code> package.
    10  * Do not you reflection, or other hacks as your code
    11  * shall run without any runtime permissions.
    12  */
    13 public class Task1Test extends TestCase {
    14     public Task1Test(String testName) {
    15         super(testName);
    16     }
    17 
    18     @Override
    19     protected void setUp() throws Exception {
    20     }
    21 
    22     @Override
    23     protected void tearDown() throws Exception {
    24     }
    25 
    26     //
    27     // Imagine that there are three parts of the whole system:
    28     // 1. there is someone who knows the current exchange rate
    29     // 2. there is someone who wants to do the conversion
    30     // 3. there is the API between 1. and 2. which allows them to communicate
    31     // Please design such API
    32     //
    33 
    34     /** Create convertor that understands two currencies, CZK and
    35      *  USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
    36      *  e.g. those that know the exchange rate. They somehow need to create
    37      *  the objects from the API and tell them the exchange rate. The API itself
    38      *  knows nothing about any rates, before the createCZKtoUSD method is called.
    39      *
    40      * Creation of the convertor shall not require subclassing of any class
    41      * or interface on the client side.
    42      *
    43      * @return prepared convertor ready for converting USD to CZK and CZK to USD
    44      */
    45     public static Convertor<Double, String> createCZKtoUSD() {
    46         return Convertor.getConvertorDoubleString(
    47                 CurrencyValue.getCurrencyValue(1d, "USD"),
    48                 CurrencyValue.getCurrencyValue(17d, "CZK")
    49         );
    50     }
    51 
    52     /** Create convertor that understands two currencies, CZK and
    53      *  SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
    54      *  it knows the exchange rate, and needs to use the API to create objects
    55      *  with the exchange rate. Anyone shall be ready to call this method without
    56      *  any other method being called previously. The API itself shall know
    57      *  nothing about any rates, before this method is called.
    58      *
    59      * Creation of the convertor shall not require subclassing of any class
    60      * or interface on the client side.
    61      * 
    62      * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    63      */
    64     public static Convertor<Double, String> createSKKtoCZK() {
    65         return Convertor.getConvertorDoubleString(
    66                 CurrencyValue.getCurrencyValue(100d, "SKK"),
    67                 CurrencyValue.getCurrencyValue(80d, "CZK")
    68         );
    69     }
    70 
    71     //
    72     // now the methods for group #2 follow:
    73     // this group knows nothing about exchange rates, but knows how to use
    74     // the API to do conversions. It somehow (by calling one of the factory
    75     // methods) gets objects from the API and uses them to do the conversions.
    76     //
    77     
    78     /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    79      * with it.
    80      */
    81     public void testCurrencyCZKUSD() throws Exception {
    82         Convertor<Double, String> c = createCZKtoUSD();
    83         
    84         CurrencyValue<Double, String> result;
    85         
    86         // convert $5 to CZK using c:
    87         // assertEquals("Result is 85 CZK");
    88         result = c.convert("CZK", CurrencyValue.getCurrencyValue(5d, "USD"));
    89         assertEquals(CurrencyValue.getCurrencyValue(85d, "CZK"), result);
    90 
    91         // convert $8 to CZK
    92         // assertEquals("Result is 136 CZK");
    93         result = c.convert("CZK", CurrencyValue.getCurrencyValue(8d, "USD"));
    94         assertEquals(CurrencyValue.getCurrencyValue(136d, "CZK"), result);
    95 
    96         // convert 1003CZK to USD
    97         // assertEquals("Result is 59 USD");
    98         result = c.convert("USD", CurrencyValue.getCurrencyValue(1003d, "CZK"));
    99         assertEquals(CurrencyValue.getCurrencyValue(59d, "USD"), result);
   100     }
   101 
   102     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
   103      * with it.
   104      */
   105     public void testCurrencySKKCZK() throws Exception {
   106         Convertor<Double, String> c = createSKKtoCZK();
   107         
   108         CurrencyValue<Double, String> result;
   109         
   110         // convert 16CZK using c:
   111         // assertEquals("Result is 20 SKK");
   112         result = c.convert("SKK", CurrencyValue.getCurrencyValue(16d, "CZK"));
   113         assertEquals(CurrencyValue.getCurrencyValue(20d, "SKK"), result);
   114 
   115         // convert 500SKK to CZK
   116         // assertEquals("Result is 400 CZK");
   117         result = c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"));
   118         assertEquals(CurrencyValue.getCurrencyValue(400d, "CZK"), result);
   119     }
   120 
   121     /** Verify that the CZK to USD convertor knows nothing about SKK.
   122      */
   123     public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
   124         Convertor<Double, String> c = createCZKtoUSD();
   125         try {
   126             // convert $5 to SKK, the API shall say this is not possible
   127             c.convert("SKK", CurrencyValue.getCurrencyValue(16d, "CZK"));
   128             fail("Should not convert");
   129         } catch (Exception e) {
   130         }
   131         try {
   132             // convert 500 SKK to CZK, the API shall say this is not possible
   133             c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"));
   134             fail("Should not convert");
   135         } catch (Exception e) {
   136         }
   137         
   138     }
   139 
   140     /** Verify that the CZK to SKK convertor knows nothing about USD.
   141      */
   142     public void testCannotConvertToUSDwithSKKCZKConvertor() throws Exception {
   143         Convertor<Double, String> c = createSKKtoCZK();
   144         try {
   145             // convert $5 to SKK, the API shall say this is not possible
   146             c.convert("SKK", CurrencyValue.getCurrencyValue(5d, "USD"));
   147             fail("Should not convert");
   148         } catch (Exception e) {
   149         }
   150         try {
   151             // convert 500 CZK to USD, the API shall say this is not possible
   152             c.convert("USD", CurrencyValue.getCurrencyValue(500d, "CZK"));
   153             fail("Should not convert");
   154         } catch (Exception e) {
   155         }
   156     }
   157 }