task4/solution02/test/org/apidesign/apifest08/test/Task1Test.java
changeset 61 58ec6da75f6f
parent 45 251d0ed461fb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution02/test/org/apidesign/apifest08/test/Task1Test.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,160 @@
     1.4 +package org.apidesign.apifest08.test;
     1.5 +
     1.6 +import java.util.Currency;
     1.7 +
     1.8 +import junit.framework.TestCase;
     1.9 +
    1.10 +import org.apidesign.apifest08.currency.Convertor;
    1.11 +import org.apidesign.apifest08.currency.ConvertorFactory;
    1.12 +import org.apidesign.apifest08.currency.MoneyImpl;
    1.13 +
    1.14 +/** Finish the Convertor API, and then write bodies of methods inside
    1.15 + * of this class to match the given tasks. To fullfil your task, use the
    1.16 + * API define in the <code>org.apidesign.apifest08.currency</code> package.
    1.17 + * Do not you reflection, or other hacks as your code
    1.18 + * shall run without any runtime permissions.
    1.19 + */
    1.20 +public class Task1Test extends TestCase {
    1.21 +	
    1.22 +	public static final Currency USD = Currency.getInstance("USD");
    1.23 +	public static final Currency CZK = Currency.getInstance("CZK");
    1.24 +	public static final Currency SKK = Currency.getInstance("SKK");
    1.25 +	
    1.26 +    public Task1Test(String testName) {
    1.27 +        super(testName);
    1.28 +    }
    1.29 +
    1.30 +    @Override
    1.31 +    protected void setUp() throws Exception {
    1.32 +    }
    1.33 +
    1.34 +    @Override
    1.35 +    protected void tearDown() throws Exception {
    1.36 +    }
    1.37 +
    1.38 +    //
    1.39 +    // Imagine that there are three parts of the whole system:
    1.40 +    // 1. there is someone who knows the current exchange rate
    1.41 +    // 2. there is someone who wants to do the conversion
    1.42 +    // 3. there is the API between 1. and 2. which allows them to communicate
    1.43 +    // Please design such API
    1.44 +    //
    1.45 +
    1.46 +    /** Create convertor that understands two currencies, CZK and
    1.47 +     *  USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
    1.48 +     *  e.g. those that know the exchange rate. They somehow need to create
    1.49 +     *  the objects from the API and tell them the exchange rate. The API itself
    1.50 +     *  knows nothing about any rates, before the createCZKtoUSD method is called.
    1.51 +     *
    1.52 +     * Creation of the convertor shall not require subclassing of any class
    1.53 +     * or interface on the client side.
    1.54 +     *
    1.55 +     * @return prepared convertor ready for converting USD to CZK and CZK to USD
    1.56 +     */
    1.57 +    public static Convertor createCZKtoUSD() {
    1.58 +    	return ConvertorFactory.createConvertor(new MoneyImpl(17,CZK), new MoneyImpl(1,USD));
    1.59 +    }
    1.60 +
    1.61 +    /** Create convertor that understands two currencies, CZK and
    1.62 +     *  SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
    1.63 +     *  it knows the exchange rate, and needs to use the API to create objects
    1.64 +     *  with the exchange rate. Anyone shall be ready to call this method without
    1.65 +     *  any other method being called previously. The API itself shall know
    1.66 +     *  nothing about any rates, before this method is called.
    1.67 +     *
    1.68 +     * Creation of the convertor shall not require subclassing of any class
    1.69 +     * or interface on the client side.
    1.70 +     * 
    1.71 +     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    1.72 +     */
    1.73 +    public static Convertor createSKKtoCZK() {
    1.74 +    	return ConvertorFactory.createConvertor(new MoneyImpl(100,SKK), new MoneyImpl(80,CZK));
    1.75 +    }
    1.76 +
    1.77 +    //
    1.78 +    // now the methods for group #2 follow:
    1.79 +    // this group knows nothing about exchange rates, but knows how to use
    1.80 +    // the API to do conversions. It somehow (by calling one of the factory
    1.81 +    // methods) gets objects from the API and uses them to do the conversions.
    1.82 +    //
    1.83 +    
    1.84 +    /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    1.85 +     * with it.
    1.86 +     */
    1.87 +    public void testCurrencyCZKUSD() throws Exception {
    1.88 +    	Convertor c = createCZKtoUSD();
    1.89 +        // convert $5 to CZK using c:
    1.90 +        assertEquals("Result is 85 CZK",new MoneyImpl(85,CZK), c.convert(new MoneyImpl(5,USD),CZK));
    1.91 +
    1.92 +        // convert $8 to CZK
    1.93 +        assertEquals("Result is 136 CZK",new MoneyImpl(136,CZK), c.convert(new MoneyImpl(8,USD),CZK));
    1.94 +
    1.95 +        // convert 1003CZK to USD
    1.96 +        assertEquals("Result is 59 USD", new MoneyImpl(59,USD), c.convert(new MoneyImpl(1003,CZK),USD));
    1.97 +        
    1.98 +    }
    1.99 +
   1.100 +    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
   1.101 +     * with it.
   1.102 +     */
   1.103 +    public void testCurrencySKKCZK() throws Exception {
   1.104 +        Convertor c = createSKKtoCZK();
   1.105 +        // convert 16CZK using c:
   1.106 +        assertEquals("Result is 20 SKK", new MoneyImpl(20,SKK), c.convert(new MoneyImpl(16,CZK),SKK));
   1.107 +
   1.108 +        // convert 500SKK to CZK
   1.109 +        assertEquals("Result is 400 CZK", new MoneyImpl(400,CZK), c.convert(new MoneyImpl(500,SKK),CZK));
   1.110 +    }
   1.111 +
   1.112 +    /** Verify that the CZK to USD convertor knows nothing about SKK.
   1.113 +     */
   1.114 +    public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
   1.115 +        Convertor c = createCZKtoUSD();
   1.116 +        // convert $5 to SKK, the API shall say this is not possible
   1.117 +        try
   1.118 +        {
   1.119 +        	c.convert(new MoneyImpl(5, USD), SKK);
   1.120 +        	fail("Exception expected");
   1.121 +        }
   1.122 +        catch(IllegalArgumentException e)
   1.123 +        {
   1.124 +        	assertTrue("Ok",true);
   1.125 +        }
   1.126 +        // convert 500 SKK to CZK, the API shall say this is not possible
   1.127 +        try
   1.128 +        {
   1.129 +        	c.convert(new MoneyImpl(500, SKK), CZK);
   1.130 +        	fail("Exception expected");
   1.131 +        }
   1.132 +        catch(IllegalArgumentException e)
   1.133 +        {
   1.134 +        	assertTrue("Ok",true);
   1.135 +        }
   1.136 +    }
   1.137 +
   1.138 +    /** Verify that the CZK to SKK convertor knows nothing about USD.
   1.139 +     */
   1.140 +    public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception {
   1.141 +        Convertor c = createSKKtoCZK();
   1.142 +        // convert $5 to SKK, the API shall say this is not possible
   1.143 +        try
   1.144 +        {
   1.145 +        	c.convert(new MoneyImpl(5, USD), SKK);
   1.146 +        	fail("Exception expected");
   1.147 +        }
   1.148 +        catch(IllegalArgumentException e)
   1.149 +        {
   1.150 +        	assertTrue("Ok",true);
   1.151 +        }
   1.152 +        // convert 500 CZK to USD, the API shall say this is not possible
   1.153 +        try
   1.154 +        {
   1.155 +        	c.convert(new MoneyImpl(500, CZK), USD);
   1.156 +        	fail("Exception expected");
   1.157 +        }
   1.158 +        catch(IllegalArgumentException e)
   1.159 +        {
   1.160 +        	assertTrue("Ok",true);
   1.161 +        }
   1.162 +    }
   1.163 +}