task4/solution12/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/solution12/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 
     5 import junit.framework.TestCase;
     6 
     7 import org.apidesign.apifest08.currency.Convertor;
     8 import org.apidesign.apifest08.currency.exceptions.InvalidCurrencyException;
     9 import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
    10 
    11 /**
    12  * Finish the Convertor API, and then write bodies of methods inside of this class to match the given tasks. To fullfil
    13  * your task, use the API define in the <code>org.apidesign.apifest08.currency</code> package. Do not you reflection, or
    14  * other hacks as your code 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   /**
    38    * Create convertor that understands two currencies, CZK and USD. Make 1 USD == 17 CZK. This is a method provided for
    39    * #1 group - e.g. those that know the exchange rate. They somehow need to create the objects from the API and tell
    40    * them the exchange rate. The API itself knows nothing about any rates, before the createCZKtoUSD method is called.
    41    * Creation of the convertor shall not require subclassing of any class or interface on the client side.
    42    * @return prepared convertor ready for converting USD to CZK and CZK to USD
    43    */
    44   public static Convertor createCZKtoUSD() {
    45     // set exchange rates
    46     Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), 17d, 1d);
    47 
    48     // create new instance
    49     Convertor convertor = null;
    50     try {
    51       convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("CZK"));
    52     } catch (UnknownConvertorException e) {
    53       e.printStackTrace();
    54     }
    55 
    56     return convertor;
    57   }
    58 
    59   /**
    60    * Create convertor that understands two currencies, CZK and SKK. Make 100 SKK == 80 CZK. Again this is method for the
    61    * #1 group - it knows the exchange rate, and needs to use the API to create objects with the exchange rate. Anyone
    62    * shall be ready to call this method without any other method being called previously. The API itself shall know
    63    * nothing about any rates, before this method is called. Creation of the convertor shall not require subclassing of
    64    * any class or interface on the client side.
    65    * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    66    */
    67   public static Convertor createSKKtoCZK() {
    68     // set exchange rates
    69     Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 80d, 100d);
    70 
    71     // create new instance
    72     Convertor convertor = null;
    73     try {
    74       convertor = Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
    75     } catch (UnknownConvertorException e) {
    76       e.printStackTrace();
    77     }
    78 
    79     return convertor;
    80   }
    81 
    82   //
    83   // now the methods for group #2 follow:
    84   // this group knows nothing about exchange rates, but knows how to use
    85   // the API to do conversions. It somehow (by calling one of the factory
    86   // methods) gets objects from the API and uses them to do the conversions.
    87   //
    88 
    89   /**
    90    * Use the convertor from <code>createCZKtoUSD</code> method and do few conversions with it.
    91    */
    92   public void testCurrencyCZKUSD() throws Exception {
    93     Convertor c = createCZKtoUSD();
    94     // convert $5 to CZK using c:
    95     double result = c.convert(5, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    96     assertEquals("Result is not 85 CZK", 85.0, result);
    97 
    98     // convert $8 to CZK
    99     result = c.convert(8, Currency.getInstance("USD"), Currency.getInstance("CZK"));
   100     assertEquals("Result is not 136 CZK", 136.0, result);
   101 
   102     // convert 1003CZK to USD
   103     result = c.convert(1003, Currency.getInstance("CZK"), Currency.getInstance("USD"));
   104     assertEquals("Result is not 59 USD", 59.0, result);
   105   }
   106 
   107   /**
   108    * Use the convertor from <code>createSKKtoCZK</code> method and do few conversions with it.
   109    */
   110   public void testCurrencySKKCZK() throws Exception {
   111     Convertor c = createSKKtoCZK();
   112 
   113     // convert 16CZK using c:
   114     double result = c.convert(16, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
   115     assertEquals("Result is not 20 SKK", 20.0, result);
   116 
   117     // convert 500SKK to CZK
   118     result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   119     assertEquals("Result is not 400 CZK", 400.0, result);
   120   }
   121 
   122   /**
   123    * Verify that the CZK to USD convertor knows nothing about SKK.
   124    */
   125   public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
   126     Convertor c = createCZKtoUSD();
   127     boolean exceptionThrown = false;
   128 
   129     // convert $5 to SKK, the API shall say this is not possible
   130     try {
   131       c.convert(5, Currency.getInstance("USD"), Currency.getInstance("SKK"));
   132       exceptionThrown = false;
   133     } catch (InvalidCurrencyException e) {
   134       exceptionThrown = true;
   135     }
   136     assertEquals("It should be impossible to convert to SKK with USD->CZK convertor", true, exceptionThrown);
   137 
   138     // convert 500 SKK to CZK, the API shall say this is not possible
   139     try {
   140       c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   141       exceptionThrown = false;
   142     } catch (InvalidCurrencyException e) {
   143       exceptionThrown = true;
   144     }
   145     assertEquals("It should be impossible to convert from SKK with USD->CZK convertor", true, exceptionThrown);
   146 
   147   }
   148 
   149   /**
   150    * Verify that the CZK to SKK convertor knows nothing about USD.
   151    */
   152   public void testCannotConvertToSKKwithSKKCZKConvertor() throws Exception {
   153     Convertor c = createSKKtoCZK();
   154     boolean exceptionThrown = false;
   155 
   156     // convert $5 to SKK, the API shall say this is not possible
   157     try {
   158       c.convert(5, Currency.getInstance("USD"), Currency.getInstance("SKK"));
   159       exceptionThrown = false;
   160     } catch (InvalidCurrencyException e) {
   161       exceptionThrown = true;
   162     }
   163     assertEquals("It should be impossible to convert form USD with SKK->CZK convertor", true, exceptionThrown);
   164 
   165     // convert 500 CZK to USD, the API shall say this is not possible
   166     try {
   167       c.convert(500, Currency.getInstance("CZK"), Currency.getInstance("USD"));
   168       exceptionThrown = false;
   169     } catch (InvalidCurrencyException e) {
   170       exceptionThrown = true;
   171     }
   172     assertEquals("It should be impossible to convert to USD with SKK->CZK convertor", true, exceptionThrown);
   173   }
   174 }