task4/solution14/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 50 task3/solution14/test/org/apidesign/apifest08/test/Task1Test.java@03c5c5dc94e7
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.ConvertorFactory;
     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 createCZKtoUSD() {
    46         return ConvertorFactory.newInstance().createConvertor("CZK", "USD", 17, 1);
    47     }
    48 
    49     /** Create convertor that understands two currencies, CZK and
    50      *  SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
    51      *  it knows the exchange rate, and needs to use the API to create objects
    52      *  with the exchange rate. Anyone shall be ready to call this method without
    53      *  any other method being called previously. The API itself shall know
    54      *  nothing about any rates, before this method is called.
    55      *
    56      * Creation of the convertor shall not require subclassing of any class
    57      * or interface on the client side.
    58      * 
    59      * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    60      */
    61     public static Convertor createSKKtoCZK() {
    62         return ConvertorFactory.newInstance().createConvertor("SKK", "CZK", 100, 80);
    63     }
    64 
    65     //
    66     // now the methods for group #2 follow:
    67     // this group knows nothing about exchange rates, but knows how to use
    68     // the API to do conversions. It somehow (by calling one of the factory
    69     // methods) gets objects from the API and uses them to do the conversions.
    70     //
    71     
    72     /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    73      * with it.
    74      */
    75     public void testCurrencyCZKUSD() throws Exception {
    76         Convertor c = createCZKtoUSD();        
    77         // convert $5 to CZK using c:        
    78         assertEquals("Result is 85 CZK", 85.0, c.convert("USD", "CZK", 5));
    79 
    80         // convert $8 to CZK
    81         assertEquals("Result is 136 CZK", 136.0, c.convert("USD", "CZK", 8));
    82 
    83         // convert 1003CZK to USD
    84         assertEquals("Result is 59 CZK", 59.0, c.convert("CZK", "USD", 1003));
    85     }
    86 
    87     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    88      * with it.
    89      */
    90     public void testCurrencySKKCZK() throws Exception {
    91         Convertor c = createSKKtoCZK();
    92         // convert 16CZK using c:
    93         // assertEquals("Result is 20 SKK");
    94         assertEquals("Result is 20 SKK", 20.0, c.convert("CZK", "SKK", 16));
    95 
    96         // convert 500SKK to CZK
    97         // assertEquals("Result is 400 CZK");
    98         assertEquals("Result is 400 SKK", 400.0, c.convert("SKK", "CZK", 500));
    99     }
   100 
   101     /** Verify that the CZK to USD convertor knows nothing about SKK.
   102      */
   103     public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
   104         Convertor c = createCZKtoUSD();
   105         // convert $5 to SKK, the API shall say this is not possible
   106         try {
   107             c.convert("USD", "SKK", 5);
   108             fail("Converting SKK with CZKUSD convertor is impossible");
   109         } catch (IllegalArgumentException e){
   110             //ok
   111         }
   112         
   113         // convert 500 SKK to CZK, the API shall say this is not possible
   114         try {
   115             c.convert("SKK", "CZK", 500);
   116             fail("Converting SKK with CZKUSD convertor is impossible");
   117         } catch (IllegalArgumentException e){
   118             //ok
   119         }
   120         
   121     }
   122 
   123     /** Verify that the CZK to SKK convertor knows nothing about USD.
   124      */
   125     public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception {
   126         Convertor c = createSKKtoCZK();
   127         // convert $5 to SKK, the API shall say this is not possible
   128         try {
   129             c.convert("USD", "SKK", 5);
   130             fail("Converting SKK with SKKCZK convertor is impossible");
   131         } catch (IllegalArgumentException e){
   132             //ok
   133         }
   134         
   135         // convert 500 CZK to USD, the API shall say this is not possible
   136         try {
   137             c.convert("CZK", "USD", 500);
   138             fail("Converting USD with SKKCZK convertor is impossible");
   139         } catch (IllegalArgumentException e){
   140             //ok
   141         }
   142         
   143     }
   144 }