task2/solution13/test/org/apidesign/apifest08/test/Task1Test.java
author japod@localhost
Tue, 07 Oct 2008 01:18:23 +0200
changeset 41 a7e6f84fb078
parent 29 f6073056b9fe
permissions -rw-r--r--
adding solution13 for task2
     1 package org.apidesign.apifest08.test;
     2 
     3 import java.math.BigDecimal;
     4 import junit.framework.TestCase;
     5 import org.apidesign.apifest08.currency.ConversionResult;
     6 import org.apidesign.apifest08.currency.Convertor;
     7 import org.apidesign.apifest08.currency.ConvertorCurrency;
     8 import org.apidesign.apifest08.currency.ExchangeRateProvider;
     9 import org.apidesign.apifest08.currency.ConversionNotSupportedException;
    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     public Task1Test(String testName) {
    19         super(testName);
    20     }
    21 
    22     @Override
    23     protected void setUp() throws Exception {
    24     }
    25 
    26     @Override
    27     protected void tearDown() throws Exception {
    28     }
    29 
    30     /** Create convertor that understands two currencies, CZK and
    31      *  USD. Make 1 USD == 17 CZK.
    32      *
    33      * Creation of the convertor shall not require subclassing of any class
    34      * or interface on the client side.
    35      *
    36      * @return prepared convertor ready for converting USD to CZK and CZK to USD
    37      */
    38     public static Convertor createCZKtoUSD() {
    39         ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK");
    40         ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("USD");
    41         ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(17), fromCurrency, new BigDecimal(1), toCurrency);
    42         
    43         return Convertor.createConvertor(exchangeRateProvider);
    44     }
    45 
    46     /** Create convertor that understands two currencies, CZK and
    47      *  SKK. Make 100 SKK == 80 CZK.
    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 SKK to CZK and CZK to SKK
    53      */
    54     public static Convertor createSKKtoCZK() {
    55         ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("SKK");
    56         ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("CZK");
    57         ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(100), fromCurrency, new BigDecimal(80), toCurrency);
    58         
    59         return Convertor.createConvertor(exchangeRateProvider);
    60     }
    61     
    62     
    63     public static Convertor createCZKtoYEN() {
    64         ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK");
    65         ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("JPY");
    66         ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(1), fromCurrency, new BigDecimal(1), toCurrency);
    67         
    68         return Convertor.createConvertor(exchangeRateProvider);
    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 convertCzkUsd = createCZKtoUSD();
    76 
    77         {
    78             // convert $1 to CZK using c:
    79             ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(1));
    80             assertEquals("Result is 17 CZK", new BigDecimal("17.00"), result.getConverted());
    81             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
    82         }
    83 
    84         {
    85             // convert 17CKZ to $ using c:
    86             ConversionResult result = convertCzkUsd.convert(new BigDecimal(17));
    87             assertEquals("Result is 1 $", new BigDecimal("1.00"), result.getConverted());
    88             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
    89         }
    90 
    91         {
    92             // convert $5 to CZK using c:
    93             ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(5));
    94             assertEquals("Result is 85 CZK", new BigDecimal("85.00"), result.getConverted());
    95             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
    96         }
    97         
    98         {
    99             // convert $8 to CZK
   100             ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(8));
   101             assertEquals("Result is 136 CZK", new BigDecimal("136.00"), result.getConverted());
   102             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
   103         }
   104 
   105         {
   106             // convert 1003CZK to USD
   107             ConversionResult result = convertCzkUsd.convert(new BigDecimal(1003));
   108             assertEquals("Result is 59 USD", new BigDecimal("59.00"), result.getConverted());
   109             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
   110         }
   111     }
   112 
   113     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
   114      * with it.
   115      */
   116     public void testCurrencySKKCZK() throws Exception {
   117         Convertor convertSkkCzk = createSKKtoCZK();
   118         {
   119             // convert 100SKK using c:
   120             ConversionResult result = convertSkkCzk.convert(new BigDecimal(100));
   121             assertEquals("Result is 80 CZK", new BigDecimal("80.00"), result.getConverted());
   122         }
   123         {
   124             // convert 80CZK using c:
   125             ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(80));
   126             assertEquals("Result is 100 SKK", new BigDecimal("100.00"), result.getConverted());
   127         }
   128         
   129         {
   130             // convert 16CZK using c:
   131             ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(16));
   132             assertEquals("Result is 20 SKK", new BigDecimal("20.00"), result.getConverted());
   133         }
   134 
   135         {
   136             // convert 500SKK to CZK
   137             ConversionResult result = convertSkkCzk.convert(new BigDecimal(500));
   138             assertEquals("Result is 400 CZK", new BigDecimal("400.00"), result.getConverted());
   139             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());            
   140         }
   141         
   142         {
   143             // convert 501SKK to CZK
   144             ConversionResult result = convertSkkCzk.convert(new BigDecimal(501));
   145             assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted());
   146             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
   147             
   148         }
   149     }
   150     
   151     /**
   152      * Convert SKK to CZK. Convertor can't convert whole amout (can't convert one SKK cent to CZK). Remaining
   153      * amount is stored in remainder result.
   154      * 
   155      * Test is currently failing, because implementation can't handle this case.
   156      */
   157 //    public void testConvertSmallUnits_failing() {
   158 //        Convertor convertSkkCzk = createSKKtoCZK();
   159 //        {
   160 //            // convert 501SKK to CZK
   161 //            ConversionResult result = convertSkkCzk.convert(new BigDecimal("501.01"));
   162 //            assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted());
   163 //            assertEquals("No Remainer", new BigDecimal("0.01"), result.getRemainder());
   164 //            
   165 //        }
   166 //        
   167 //    }
   168     
   169     /**
   170      * Test converting from CZK to JPY. Remained has scale of CZK.
   171      * 
   172      * This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency.
   173      */
   174 //    public void testConvertCzkToJpy_failing() {
   175 //        Convertor convertSkkCzk = createCZKtoYEN();
   176 //        {
   177 //            // convert 501SKK to CZK
   178 //            ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00"));
   179 //            assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted());
   180 //            assertEquals("No Remainer", new BigDecimal("0.00"), result.getRemainder());
   181 //            
   182 //        }
   183 //    }
   184     
   185     /**
   186      * Test converting from JPY to CZK. Remained has scale of JPY.
   187      * 
   188      * This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency.
   189      */
   190 //    public void testConvertJpyToCzk_failing() {
   191 //        Convertor convertSkkCzk = createCZKtoYEN();
   192 //        {
   193 //            // convert 501SKK to CZK
   194 //            ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00"));
   195 //            assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted());
   196 //            assertEquals("No Remainer", new BigDecimal("0"), result.getRemainder());
   197 //            
   198 //        }
   199 //    }
   200     
   201     public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
   202             Convertor c = createCZKtoUSD();
   203             // convert $5 to SKK, the API shall say this is not possible
   204             try {
   205                 c.convert(ConvertorCurrency.getInstance("USD"), ConvertorCurrency.getInstance("SKK"), new BigDecimal(5));
   206                 fail();
   207             } catch (ConversionNotSupportedException e) {
   208                 //expected error;
   209                 assertEquals("Exception From USD", "USD",e.getFromCurrecyCode());
   210                 assertEquals("Exception To SKK", "SKK",e.getToCurrecyCode());
   211             }
   212             // convert 500 SKK to CZK, the API shall say this is not possible
   213             try {
   214                 c.convert(ConvertorCurrency.getInstance("SKK"), ConvertorCurrency.getInstance("CZK"), new BigDecimal(500));
   215                 fail();
   216             } catch (ConversionNotSupportedException e) {
   217                 assertEquals("Exception From USD", "SKK",e.getFromCurrecyCode());
   218                 assertEquals("Exception To SKK", "CZK",e.getToCurrecyCode());            }
   219     }
   220     
   221  
   222 }
   223