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