task1/solution07/test/org/apidesign/apifest08/test/Task1Test.java
changeset 6 97662396c0fd
child 18 83e731257bdc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task1/solution07/test/org/apidesign/apifest08/test/Task1Test.java	Sun Sep 28 14:12:38 2008 +0200
     1.3 @@ -0,0 +1,122 @@
     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 +    protected static final Currency CZK = Currency.getInstance( "CZK" );
    1.33 +    protected static final Currency SKK = Currency.getInstance( "SKK" );
    1.34 +    protected static final Currency USD = Currency.getInstance( "USD" );
    1.35 +    
    1.36 +    /** Create convertor that understands two currencies, CZK and
    1.37 +     *  USD. Make 1 USD == 17 CZK.
    1.38 +     *
    1.39 +     * Creation of the convertor shall not require subclassing of any class
    1.40 +     * or interface on the client side.
    1.41 +     *
    1.42 +     * @return prepared convertor ready for converting USD to CZK and CZK to USD
    1.43 +     */
    1.44 +    public static Convertor createCZKtoUSD() {
    1.45 +        final TableConvertor convertor = new TableConvertor();
    1.46 +        final MonetaryAmount amountInCZK = new MonetaryAmount( 17, CZK );
    1.47 +        final MonetaryAmount amountInUSD = new MonetaryAmount( 1, USD );
    1.48 +        convertor.putIntoTable( new ConversionRate( amountInCZK, amountInUSD ) );
    1.49 +        convertor.putIntoTable( new ConversionRate( amountInUSD, amountInCZK ) );
    1.50 +        return new ContractImposingDelegatingConvertor( convertor ).test();
    1.51 +    }
    1.52 +
    1.53 +    /** Create convertor that understands two currencies, CZK and
    1.54 +     *  SKK. Make 100 SKK == 80 CZK.
    1.55 +     *
    1.56 +     * Creation of the convertor shall not require subclassing of any class
    1.57 +     * or interface on the client side.
    1.58 +     * 
    1.59 +     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    1.60 +     */
    1.61 +    public static Convertor createSKKtoCZK() {
    1.62 +        final TableConvertor convertor = new TableConvertor();
    1.63 +        final MonetaryAmount amountInSKK = new MonetaryAmount( 100, SKK );
    1.64 +        final MonetaryAmount amountInCZK = new MonetaryAmount( 80, CZK );
    1.65 +        convertor.putIntoTable( new ConversionRate( amountInSKK, amountInCZK ) );
    1.66 +        convertor.putIntoTable( new ConversionRate( amountInCZK, amountInSKK ) );
    1.67 +        return new ContractImposingDelegatingConvertor( convertor ).test();
    1.68 +    }
    1.69 +
    1.70 +    /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    1.71 +     * with it.
    1.72 +     */
    1.73 +    public void testCurrencyCZKUSD() throws Exception {
    1.74 +        final Convertor c = createCZKtoUSD();
    1.75 +        
    1.76 +        // convert $5 to CZK using c:
    1.77 +        final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) );
    1.78 +        final MonetaryAmount a1 = r1.getNetAmount();
    1.79 +        // assertEquals("Result is 85 CZK");
    1.80 +        assertNotNull( a1 );
    1.81 +        assertEquals( 85.0, a1.getAmount().doubleValue() );
    1.82 +        assertEquals( CZK, a1.getCurrency() );
    1.83 +
    1.84 +        // convert $8 to CZK
    1.85 +        final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) );
    1.86 +        final MonetaryAmount a2 = r2.getNetAmount();
    1.87 +        // assertEquals("Result is 136 CZK");
    1.88 +        assertNotNull( a2 );
    1.89 +        assertEquals( 136.0, a2.getAmount().doubleValue() );
    1.90 +        assertEquals( CZK, a2.getCurrency() );
    1.91 +
    1.92 +        // convert 1003CZK to USD
    1.93 +        final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1003, CZK ), USD ) );
    1.94 +        final MonetaryAmount a3 = r3.getNetAmount();
    1.95 +        // assertEquals("Result is 59 USD");
    1.96 +        assertNotNull( a3 );
    1.97 +        assertEquals( 59.0, a3.getAmount().doubleValue() );
    1.98 +        assertEquals( USD, a3.getCurrency() );
    1.99 +    }
   1.100 +
   1.101 +    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
   1.102 +     * with it.
   1.103 +     */
   1.104 +    public void testCurrencySKKCZK() throws Exception {
   1.105 +        final Convertor c = createSKKtoCZK();
   1.106 +        
   1.107 +        // convert 16CZK using c:
   1.108 +        final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) );
   1.109 +        final MonetaryAmount a1 = r1.getNetAmount();
   1.110 +        // assertEquals("Result is 20 SKK");
   1.111 +        assertNotNull( a1 );
   1.112 +        assertEquals( 20.0, a1.getAmount().doubleValue() );
   1.113 +        assertEquals( SKK, a1.getCurrency() );
   1.114 +        
   1.115 +        // convert 500SKK to CZK
   1.116 +        final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) );
   1.117 +        final MonetaryAmount a2 = r2.getNetAmount();
   1.118 +        // assertEquals("Result is 400 CZK");
   1.119 +        assertNotNull( a2 );
   1.120 +        assertEquals( 400.0, a2.getAmount().doubleValue() );
   1.121 +        assertEquals( CZK, a2.getCurrency() );
   1.122 +    }
   1.123 +
   1.124 +}
   1.125 +