task4/solution06/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/solution06/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 static org.apidesign.apifest08.test.Currencies.CZK;
     4 import static org.apidesign.apifest08.test.Currencies.SKK;
     5 import static org.apidesign.apifest08.test.Currencies.USD;
     6 
     7 import java.math.BigDecimal;
     8 
     9 import junit.framework.TestCase;
    10 
    11 import org.apidesign.apifest08.currency.Amount;
    12 import org.apidesign.apifest08.currency.ConversionException;
    13 import org.apidesign.apifest08.currency.Convertor;
    14 import org.apidesign.apifest08.currency.UnsupportedConversionException;
    15 
    16 /** Finish the Convertor API, and then write bodies of methods inside
    17  * of this class to match the given tasks. To fullfil your task, use the
    18  * API define in the <code>org.apidesign.apifest08.currency</code> package.
    19  * Do not you reflection, or other hacks as your code
    20  * shall run without any runtime permissions.
    21  */
    22 public class Task1Test extends TestCase {
    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     /** Create convertor that understands two currencies, CZK and
    36      *  USD. Make 1 USD == 17 CZK.
    37      *
    38      * Creation of the convertor shall not require subclassing of any class
    39      * or interface on the client side.
    40      *
    41      * @return prepared convertor ready for converting USD to CZK and CZK to USD
    42      */
    43     public static Convertor createCZKtoUSD() {
    44         return new Convertor(new BigDecimal(17), USD, CZK);
    45     }
    46 
    47     /** Create convertor that understands two currencies, CZK and
    48      *  SKK. Make 100 SKK == 80 CZK.
    49      *
    50      * Creation of the convertor shall not require subclassing of any class
    51      * or interface on the client side.
    52      * 
    53      * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    54      */
    55     public static Convertor createSKKtoCZK() {
    56     	return new Convertor(new BigDecimal("0.8"), SKK, CZK);
    57     }
    58     
    59     /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    60      * with it.
    61      */
    62     public void testCurrencyCZKUSD() throws Exception {
    63         Convertor c = createCZKtoUSD();
    64         // convert $5 to CZK using c:
    65         Amount result =  c.convert(new BigDecimal(5), USD, CZK);
    66         assertEquals("Result is 85 CZK", 85, result.getValue().intValue());
    67 
    68         // convert $8 to CZK
    69         result =  c.convert(new BigDecimal(8), USD, CZK);        
    70         assertEquals("Result is 136 CZK", 136, result.getValue().intValue());
    71 
    72         // convert 1003CZK to USD
    73         result =  c.convert(new BigDecimal(1003), CZK, USD);
    74         assertEquals("Result is 59 USD", 59, result.getValue().intValue());
    75     }
    76 
    77     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    78      * with it.
    79      */
    80     public void testCurrencySKKCZK() throws Exception {
    81         Convertor c = createSKKtoCZK();
    82         // convert 16CZK using c:
    83         Amount result =  c.convert(new BigDecimal(16), CZK, SKK);
    84         assertEquals("Result is 20 SKK", 20, result.getValue().intValue());
    85 
    86         // convert 500SKK to CZK
    87         result =  c.convert(new BigDecimal(500), SKK, CZK);
    88         assertEquals("Result is 400 CZK", 400, result.getValue().intValue());
    89     }
    90     
    91     
    92     /**
    93      *  Verify that the CZK to USD convertor knows nothing about SKK.
    94      */
    95      public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
    96     	 Convertor c = createCZKtoUSD();
    97     	 // convert $5 to SKK, the API shall say this is not possible
    98     	 try {
    99     		 c.convert(new BigDecimal(5), USD, SKK);
   100     		 fail("convert $5 to SKK, the API shall say this is not possible");
   101     	 } catch (ConversionException e) {
   102     		 //expected
   103     	 }
   104     	 
   105     	 // convert 500 SKK to CZK, the API shall say this is not possible
   106     	 
   107     	 try {
   108     		 c.convert(new BigDecimal("500"), SKK, CZK);
   109     		 fail("convert 500 SKK to CZK, the API shall say this is not possible");
   110     	 } catch (ConversionException e) {
   111     		 //expected
   112     	 }
   113      }
   114     
   115     /** 
   116      * Verify that the CZK to SKK convertor knows nothing about USD.
   117      */
   118     public void testCannotConvertToSKKwithCZKSKKConvertor() throws Exception {
   119     	Convertor c = createSKKtoCZK();
   120     	// convert $5 to SKK, the API shall say this is not possible
   121     	try {
   122     		c.convert(new BigDecimal(5), USD, SKK);
   123     		fail("convert $5 to SKK, the API shall say this is not possible");
   124     	} catch(ConversionException e) {
   125     		//expected
   126     	}
   127     	
   128     	try {
   129     		c.convert(new BigDecimal(500), CZK, USD);	
   130     		fail("convert 500 CZK to USD, the API shall say this is not possible");
   131     	} catch(ConversionException e) {
   132     		//expected
   133     	}
   134     } 
   135 }
   136