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