currency/test/org/apidesign/apifest08/test/Task1Test.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 25 Oct 2008 20:53:00 +0200
changeset 84 2ae6e4aa7aef
parent 12 b6c21003ddc9
permissions -rw-r--r--
Solutions by Petr Smid
     1 package org.apidesign.apifest08.test;
     2 
     3 import junit.framework.TestCase;
     4 import org.apidesign.apifest08.currency.Convertor;
     5 
     6 /** Finish the Convertor API, and then write bodies of methods inside
     7  * of this class to match the given tasks. To fullfil your task, use the
     8  * API define in the <code>org.apidesign.apifest08.currency</code> package.
     9  * Do not you reflection, or other hacks as your code
    10  * shall run without any runtime permissions.
    11  */
    12 public class Task1Test extends TestCase {
    13     public Task1Test(String testName) {
    14         super(testName);
    15     }
    16 
    17     @Override
    18     protected void setUp() throws Exception {
    19     }
    20 
    21     @Override
    22     protected void tearDown() throws Exception {
    23     }
    24 
    25     //
    26     // Imagine that there are three parts of the whole system:
    27     // 1. there is someone who knows the current exchange rate
    28     // 2. there is someone who wants to do the conversion
    29     // 3. there is the API between 1. and 2. which allows them to communicate
    30     // Please design such API
    31     //
    32 
    33     /** Create convertor that understands two currencies, CZK and
    34      *  USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
    35      *  e.g. those that know the exchange rate. They somehow need to create
    36      *  the objects from the API and tell them the exchange rate. The API itself
    37      *  knows nothing about any rates, before the createCZKtoUSD method is called.
    38      *
    39      * Creation of the convertor shall not require subclassing of any class
    40      * or interface on the client side.
    41      *
    42      * @return prepared convertor ready for converting USD to CZK and CZK to USD
    43      */
    44     public static Convertor createCZKtoUSD() {
    45         return null;
    46     }
    47 
    48     /** Create convertor that understands two currencies, CZK and
    49      *  SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
    50      *  it knows the exchange rate, and needs to use the API to create objects
    51      *  with the exchange rate. Anyone shall be ready to call this method without
    52      *  any other method being called previously. The API itself shall know
    53      *  nothing about any rates, before this method is called.
    54      *
    55      * Creation of the convertor shall not require subclassing of any class
    56      * or interface on the client side.
    57      * 
    58      * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    59      */
    60     public static Convertor createSKKtoCZK() {
    61         return null;
    62     }
    63 
    64     //
    65     // now the methods for group #2 follow:
    66     // this group knows nothing about exchange rates, but knows how to use
    67     // the API to do conversions. It somehow (by calling one of the factory
    68     // methods) gets objects from the API and uses them to do the conversions.
    69     //
    70     
    71     /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    72      * with it.
    73      */
    74     public void testCurrencyCZKUSD() throws Exception {
    75         Convertor c = createCZKtoUSD();
    76         // convert $5 to CZK using c:
    77         // assertEquals("Result is 85 CZK");
    78 
    79         // convert $8 to CZK
    80         // assertEquals("Result is 136 CZK");
    81 
    82         // convert 1003CZK to USD
    83         // assertEquals("Result is 59 USD");
    84     }
    85 
    86     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    87      * with it.
    88      */
    89     public void testCurrencySKKCZK() throws Exception {
    90         Convertor c = createSKKtoCZK();
    91         // convert 16CZK using c:
    92         // assertEquals("Result is 20 SKK");
    93 
    94         // convert 500SKK to CZK
    95         // assertEquals("Result is 400 CZK");
    96     }
    97 
    98     /** Verify that the CZK to USD convertor knows nothing about SKK.
    99      */
   100     public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
   101         Convertor c = createCZKtoUSD();
   102         // convert $5 to SKK, the API shall say this is not possible
   103         // convert 500 SKK to CZK, the API shall say this is not possible
   104     }
   105 
   106     /** Verify that the CZK to SKK convertor knows nothing about USD.
   107      */
   108     public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception {
   109         Convertor c = createSKKtoCZK();
   110         // convert $5 to SKK, the API shall say this is not possible
   111         // convert 500 CZK to USD, the API shall say this is not possible
   112     }
   113 }