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