task2/solution04/test/org/apidesign/apifest08/test/Task1Test.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 01 Oct 2008 10:43:05 +0200
changeset 29 f6073056b9fe
parent 17 task1/solution04/test/org/apidesign/apifest08/test/Task1Test.java@37c9921c653e
child 35 8898c620fe96
permissions -rw-r--r--
Getting ready for task2: copying all solutions to new locations
     1 package org.apidesign.apifest08.test;
     2 
     3 
     4 import java.math.BigDecimal;
     5 import java.util.Currency;
     6 import junit.framework.TestCase;
     7 import org.apidesign.apifest08.currency.Convertor;
     8 import org.apidesign.apifest08.currency.ConvertorFactory;
     9 import org.apidesign.apifest08.currency.InvalidConversionException;
    10 
    11 
    12 /** Finish the Convertor API, and then write bodies of methods inside
    13  * of this class to match the given tasks. To fullfil your task, use the
    14  * API define in the <code>org.apidesign.apifest08.currency</code> package.
    15  * Do not you reflection, or other hacks as your code
    16  * shall run without any runtime permissions.
    17  */
    18 public class Task1Test extends TestCase {
    19     
    20     private final static Currency CZK;
    21     private final static Currency SKK;
    22     private final static Currency USD;
    23 
    24     static
    25     {
    26         CZK = Currency.getInstance("CZK");
    27         SKK = Currency.getInstance("SKK");
    28         USD = Currency.getInstance("USD");
    29     }
    30     
    31     public Task1Test(String testName) {
    32         super(testName);
    33     }
    34 
    35     @Override
    36     protected void setUp() throws Exception {
    37     }
    38 
    39     @Override
    40     protected void tearDown() throws Exception {
    41     }
    42 
    43     /** Create convertor that understands two currencies, CZK and
    44      *  USD. Make 1 USD == 17 CZK.
    45      *
    46      * Creation of the convertor shall not require subclassing of any class
    47      * or interface on the client side.
    48      *
    49      * @return prepared convertor ready for converting USD to CZK and CZK to USD
    50      */
    51     public static Convertor createCZKtoUSD()
    52     {
    53         return (ConvertorFactory.getConvertor("CZK", BigDecimal.valueOf(17.0), "USD", BigDecimal.valueOf(1)));
    54     }
    55 
    56     /** Create convertor that understands two currencies, CZK and
    57      *  SKK. Make 100 SKK == 80 CZK.
    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 createSKKtoCZK()
    65     {
    66         return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), BigDecimal.valueOf(100), Currency.getInstance("CZK"), BigDecimal.valueOf(80)));
    67     }
    68     
    69     /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    70      * with it.
    71      */
    72     public void testCurrencyCZKUSD() throws Exception {
    73         Convertor  c = createCZKtoUSD();
    74         BigDecimal result;
    75         
    76         // convert $5 to CZK using c:
    77         // assertEquals("Result is 85 CZK");
    78         result = c.convert(USD, CZK, BigDecimal.valueOf(5));
    79         assertEquals(new BigDecimal("85.00"), result);
    80 
    81         // convert $8 to CZK
    82         // assertEquals("Result is 136 CZK");
    83         result = c.convert(USD, CZK, BigDecimal.valueOf(8));
    84         assertEquals(new BigDecimal("136.00"), result);
    85 
    86         // convert 1003CZK to USD
    87         // assertEquals("Result is 59 USD");
    88         result = c.convert(CZK, USD, BigDecimal.valueOf(1003));
    89         assertEquals(new BigDecimal("59.00"), result);
    90     }
    91 
    92     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    93      * with it.
    94      */
    95     public void testCurrencySKKCZK() throws Exception {
    96         Convertor c = createSKKtoCZK();
    97         BigDecimal result;
    98         
    99         // convert 16CZK using c:
   100         // assertEquals("Result is 20 SKK");
   101         result = c.convert(CZK, SKK, BigDecimal.valueOf(16));
   102         assertEquals(new BigDecimal("20.00"), result);
   103                         
   104         // convert 500SKK to CZK
   105         // assertEquals("Result is 400 CZK");
   106         result = c.convert(SKK, CZK, BigDecimal.valueOf(500));
   107         assertEquals(new BigDecimal("400.00"), result);
   108    }
   109 
   110     /** 
   111      * Verify that the CZK to USD convertor knows nothing about SKK.
   112      */
   113     public void testCannotConvertToSKKwithCZKUSDConvertor() 
   114         throws Exception 
   115     {
   116         Convertor c = createCZKtoUSD();
   117         
   118         try
   119         {
   120             // convert $5 to SKK, the API shall say this is not possible
   121             c.convert(USD, SKK, BigDecimal.valueOf(5));
   122             fail("cannot use the CZKtoUSD converter to convert to SKK");
   123         }
   124         catch(InvalidConversionException ex)
   125         {       
   126             assertEquals("cannot convert to: SKK", ex.getMessage());
   127             assertEquals(SKK, ex.getBadCurrency());
   128             assertEquals(CZK, ex.getCurrencyA());
   129             assertEquals(USD, ex.getCurrencyB());
   130         }
   131 
   132         try
   133         {
   134             // convert 500 SKK to CZK, the API shall say this is not possible
   135             c.convert(SKK, CZK, BigDecimal.valueOf(5));
   136             fail("cannot use the CZKtoUSD converter to convert from SKK");
   137         }
   138         catch(InvalidConversionException ex)
   139         {            
   140             assertEquals("cannot convert from: SKK", ex.getMessage());
   141             assertEquals(SKK, ex.getBadCurrency());
   142             assertEquals(CZK, ex.getCurrencyA());
   143             assertEquals(USD, ex.getCurrencyB());
   144         }
   145     }
   146     
   147     /** 
   148      * Verify that the CZK to SKK convertor knows nothing about USD.
   149      */
   150     public void testCannotConvertToUSDwithSKKCZKConvertor() 
   151         throws Exception 
   152     {
   153         Convertor c = createSKKtoCZK();
   154         
   155         try
   156         {
   157             // convert $5 to SKK, the API shall say this is not possible
   158             c.convert(USD, SKK, BigDecimal.valueOf(5));
   159             fail("cannot use the CZKtoUSD converter to convert to SKK");
   160         }
   161         catch(InvalidConversionException ex)
   162         {       
   163             assertEquals("cannot convert from: USD", ex.getMessage());
   164             assertEquals(USD, ex.getBadCurrency());
   165             assertEquals(SKK, ex.getCurrencyA());
   166             assertEquals(CZK, ex.getCurrencyB());
   167         }
   168 
   169         try
   170         {
   171             // convert 500 CZK to USD, the API shall say this is not possible
   172             c.convert(CZK, USD, BigDecimal.valueOf(500));
   173             fail("cannot use the CZKtoUSD converter to convert from SKK");
   174         }
   175         catch(InvalidConversionException ex)
   176         {            
   177             assertEquals("cannot convert to: USD", ex.getMessage());
   178             assertEquals(USD, ex.getBadCurrency());
   179             assertEquals(SKK, ex.getCurrencyA());
   180             assertEquals(CZK, ex.getCurrencyB());
   181         }
   182     }
   183 }
   184