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