task4/solution06/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/solution06/test/org/apidesign/apifest08/test/Task1Test.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,136 @@
     1.4 +package org.apidesign.apifest08.test;
     1.5 +
     1.6 +import static org.apidesign.apifest08.test.Currencies.CZK;
     1.7 +import static org.apidesign.apifest08.test.Currencies.SKK;
     1.8 +import static org.apidesign.apifest08.test.Currencies.USD;
     1.9 +
    1.10 +import java.math.BigDecimal;
    1.11 +
    1.12 +import junit.framework.TestCase;
    1.13 +
    1.14 +import org.apidesign.apifest08.currency.Amount;
    1.15 +import org.apidesign.apifest08.currency.ConversionException;
    1.16 +import org.apidesign.apifest08.currency.Convertor;
    1.17 +import org.apidesign.apifest08.currency.UnsupportedConversionException;
    1.18 +
    1.19 +/** Finish the Convertor API, and then write bodies of methods inside
    1.20 + * of this class to match the given tasks. To fullfil your task, use the
    1.21 + * API define in the <code>org.apidesign.apifest08.currency</code> package.
    1.22 + * Do not you reflection, or other hacks as your code
    1.23 + * shall run without any runtime permissions.
    1.24 + */
    1.25 +public class Task1Test extends TestCase {
    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 +    /** Create convertor that understands two currencies, CZK and
    1.39 +     *  USD. Make 1 USD == 17 CZK.
    1.40 +     *
    1.41 +     * Creation of the convertor shall not require subclassing of any class
    1.42 +     * or interface on the client side.
    1.43 +     *
    1.44 +     * @return prepared convertor ready for converting USD to CZK and CZK to USD
    1.45 +     */
    1.46 +    public static Convertor createCZKtoUSD() {
    1.47 +        return new Convertor(new BigDecimal(17), USD, CZK);
    1.48 +    }
    1.49 +
    1.50 +    /** Create convertor that understands two currencies, CZK and
    1.51 +     *  SKK. Make 100 SKK == 80 CZK.
    1.52 +     *
    1.53 +     * Creation of the convertor shall not require subclassing of any class
    1.54 +     * or interface on the client side.
    1.55 +     * 
    1.56 +     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    1.57 +     */
    1.58 +    public static Convertor createSKKtoCZK() {
    1.59 +    	return new Convertor(new BigDecimal("0.8"), SKK, CZK);
    1.60 +    }
    1.61 +    
    1.62 +    /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    1.63 +     * with it.
    1.64 +     */
    1.65 +    public void testCurrencyCZKUSD() throws Exception {
    1.66 +        Convertor c = createCZKtoUSD();
    1.67 +        // convert $5 to CZK using c:
    1.68 +        Amount result =  c.convert(new BigDecimal(5), USD, CZK);
    1.69 +        assertEquals("Result is 85 CZK", 85, result.getValue().intValue());
    1.70 +
    1.71 +        // convert $8 to CZK
    1.72 +        result =  c.convert(new BigDecimal(8), USD, CZK);        
    1.73 +        assertEquals("Result is 136 CZK", 136, result.getValue().intValue());
    1.74 +
    1.75 +        // convert 1003CZK to USD
    1.76 +        result =  c.convert(new BigDecimal(1003), CZK, USD);
    1.77 +        assertEquals("Result is 59 USD", 59, result.getValue().intValue());
    1.78 +    }
    1.79 +
    1.80 +    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    1.81 +     * with it.
    1.82 +     */
    1.83 +    public void testCurrencySKKCZK() throws Exception {
    1.84 +        Convertor c = createSKKtoCZK();
    1.85 +        // convert 16CZK using c:
    1.86 +        Amount result =  c.convert(new BigDecimal(16), CZK, SKK);
    1.87 +        assertEquals("Result is 20 SKK", 20, result.getValue().intValue());
    1.88 +
    1.89 +        // convert 500SKK to CZK
    1.90 +        result =  c.convert(new BigDecimal(500), SKK, CZK);
    1.91 +        assertEquals("Result is 400 CZK", 400, result.getValue().intValue());
    1.92 +    }
    1.93 +    
    1.94 +    
    1.95 +    /**
    1.96 +     *  Verify that the CZK to USD convertor knows nothing about SKK.
    1.97 +     */
    1.98 +     public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
    1.99 +    	 Convertor c = createCZKtoUSD();
   1.100 +    	 // convert $5 to SKK, the API shall say this is not possible
   1.101 +    	 try {
   1.102 +    		 c.convert(new BigDecimal(5), USD, SKK);
   1.103 +    		 fail("convert $5 to SKK, the API shall say this is not possible");
   1.104 +    	 } catch (ConversionException e) {
   1.105 +    		 //expected
   1.106 +    	 }
   1.107 +    	 
   1.108 +    	 // convert 500 SKK to CZK, the API shall say this is not possible
   1.109 +    	 
   1.110 +    	 try {
   1.111 +    		 c.convert(new BigDecimal("500"), SKK, CZK);
   1.112 +    		 fail("convert 500 SKK to CZK, the API shall say this is not possible");
   1.113 +    	 } catch (ConversionException e) {
   1.114 +    		 //expected
   1.115 +    	 }
   1.116 +     }
   1.117 +    
   1.118 +    /** 
   1.119 +     * Verify that the CZK to SKK convertor knows nothing about USD.
   1.120 +     */
   1.121 +    public void testCannotConvertToSKKwithCZKSKKConvertor() throws Exception {
   1.122 +    	Convertor c = createSKKtoCZK();
   1.123 +    	// convert $5 to SKK, the API shall say this is not possible
   1.124 +    	try {
   1.125 +    		c.convert(new BigDecimal(5), USD, SKK);
   1.126 +    		fail("convert $5 to SKK, the API shall say this is not possible");
   1.127 +    	} catch(ConversionException e) {
   1.128 +    		//expected
   1.129 +    	}
   1.130 +    	
   1.131 +    	try {
   1.132 +    		c.convert(new BigDecimal(500), CZK, USD);	
   1.133 +    		fail("convert 500 CZK to USD, the API shall say this is not possible");
   1.134 +    	} catch(ConversionException e) {
   1.135 +    		//expected
   1.136 +    	}
   1.137 +    } 
   1.138 +}
   1.139 +