task1/solution07/test/org/apidesign/apifest08/test/Task1Test.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
child 18 83e731257bdc
permissions -rw-r--r--
Adding solutions received for task1
japod@6
     1
package org.apidesign.apifest08.test;
japod@6
     2
japod@6
     3
import java.util.Currency;
japod@6
     4
import junit.framework.TestCase;
japod@6
     5
import org.apidesign.apifest08.currency.ConversionRate;
japod@6
     6
import org.apidesign.apifest08.currency.Convertor;
japod@6
     7
import org.apidesign.apifest08.currency.MonetaryAmount;
japod@6
     8
import org.apidesign.apifest08.currency.TableConvertor;
japod@6
     9
japod@6
    10
/** Finish the Convertor API, and then write bodies of methods inside
japod@6
    11
 * of this class to match the given tasks. To fullfil your task, use the
japod@6
    12
 * API define in the <code>org.apidesign.apifest08.currency</code> package.
japod@6
    13
 * Do not you reflection, or other hacks as your code
japod@6
    14
 * shall run without any runtime permissions.
japod@6
    15
 */
japod@6
    16
public class Task1Test extends TestCase {
japod@6
    17
    public Task1Test(String testName) {
japod@6
    18
        super(testName);
japod@6
    19
    }
japod@6
    20
japod@6
    21
    @Override
japod@6
    22
    protected void setUp() throws Exception {
japod@6
    23
    }
japod@6
    24
japod@6
    25
    @Override
japod@6
    26
    protected void tearDown() throws Exception {
japod@6
    27
    }
japod@6
    28
japod@6
    29
    protected static final Currency CZK = Currency.getInstance( "CZK" );
japod@6
    30
    protected static final Currency SKK = Currency.getInstance( "SKK" );
japod@6
    31
    protected static final Currency USD = Currency.getInstance( "USD" );
japod@6
    32
    
japod@6
    33
    /** Create convertor that understands two currencies, CZK and
japod@6
    34
     *  USD. Make 1 USD == 17 CZK.
japod@6
    35
     *
japod@6
    36
     * Creation of the convertor shall not require subclassing of any class
japod@6
    37
     * or interface on the client side.
japod@6
    38
     *
japod@6
    39
     * @return prepared convertor ready for converting USD to CZK and CZK to USD
japod@6
    40
     */
japod@6
    41
    public static Convertor createCZKtoUSD() {
japod@6
    42
        final TableConvertor convertor = new TableConvertor();
japod@6
    43
        final MonetaryAmount amountInCZK = new MonetaryAmount( 17, CZK );
japod@6
    44
        final MonetaryAmount amountInUSD = new MonetaryAmount( 1, USD );
japod@6
    45
        convertor.putIntoTable( new ConversionRate( amountInCZK, amountInUSD ) );
japod@6
    46
        convertor.putIntoTable( new ConversionRate( amountInUSD, amountInCZK ) );
japod@6
    47
        return new ContractImposingDelegatingConvertor( convertor ).test();
japod@6
    48
    }
japod@6
    49
japod@6
    50
    /** Create convertor that understands two currencies, CZK and
japod@6
    51
     *  SKK. Make 100 SKK == 80 CZK.
japod@6
    52
     *
japod@6
    53
     * Creation of the convertor shall not require subclassing of any class
japod@6
    54
     * or interface on the client side.
japod@6
    55
     * 
japod@6
    56
     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
japod@6
    57
     */
japod@6
    58
    public static Convertor createSKKtoCZK() {
japod@6
    59
        final TableConvertor convertor = new TableConvertor();
japod@6
    60
        final MonetaryAmount amountInSKK = new MonetaryAmount( 100, SKK );
japod@6
    61
        final MonetaryAmount amountInCZK = new MonetaryAmount( 80, CZK );
japod@6
    62
        convertor.putIntoTable( new ConversionRate( amountInSKK, amountInCZK ) );
japod@6
    63
        convertor.putIntoTable( new ConversionRate( amountInCZK, amountInSKK ) );
japod@6
    64
        return new ContractImposingDelegatingConvertor( convertor ).test();
japod@6
    65
    }
japod@6
    66
japod@6
    67
    /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
japod@6
    68
     * with it.
japod@6
    69
     */
japod@6
    70
    public void testCurrencyCZKUSD() throws Exception {
japod@6
    71
        final Convertor c = createCZKtoUSD();
japod@6
    72
        
japod@6
    73
        // convert $5 to CZK using c:
japod@6
    74
        final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) );
japod@6
    75
        final MonetaryAmount a1 = r1.getNetAmount();
japod@6
    76
        // assertEquals("Result is 85 CZK");
japod@6
    77
        assertNotNull( a1 );
japod@6
    78
        assertEquals( 85.0, a1.getAmount().doubleValue() );
japod@6
    79
        assertEquals( CZK, a1.getCurrency() );
japod@6
    80
japod@6
    81
        // convert $8 to CZK
japod@6
    82
        final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) );
japod@6
    83
        final MonetaryAmount a2 = r2.getNetAmount();
japod@6
    84
        // assertEquals("Result is 136 CZK");
japod@6
    85
        assertNotNull( a2 );
japod@6
    86
        assertEquals( 136.0, a2.getAmount().doubleValue() );
japod@6
    87
        assertEquals( CZK, a2.getCurrency() );
japod@6
    88
japod@6
    89
        // convert 1003CZK to USD
japod@6
    90
        final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1003, CZK ), USD ) );
japod@6
    91
        final MonetaryAmount a3 = r3.getNetAmount();
japod@6
    92
        // assertEquals("Result is 59 USD");
japod@6
    93
        assertNotNull( a3 );
japod@6
    94
        assertEquals( 59.0, a3.getAmount().doubleValue() );
japod@6
    95
        assertEquals( USD, a3.getCurrency() );
japod@6
    96
    }
japod@6
    97
japod@6
    98
    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
japod@6
    99
     * with it.
japod@6
   100
     */
japod@6
   101
    public void testCurrencySKKCZK() throws Exception {
japod@6
   102
        final Convertor c = createSKKtoCZK();
japod@6
   103
        
japod@6
   104
        // convert 16CZK using c:
japod@6
   105
        final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) );
japod@6
   106
        final MonetaryAmount a1 = r1.getNetAmount();
japod@6
   107
        // assertEquals("Result is 20 SKK");
japod@6
   108
        assertNotNull( a1 );
japod@6
   109
        assertEquals( 20.0, a1.getAmount().doubleValue() );
japod@6
   110
        assertEquals( SKK, a1.getCurrency() );
japod@6
   111
        
japod@6
   112
        // convert 500SKK to CZK
japod@6
   113
        final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) );
japod@6
   114
        final MonetaryAmount a2 = r2.getNetAmount();
japod@6
   115
        // assertEquals("Result is 400 CZK");
japod@6
   116
        assertNotNull( a2 );
japod@6
   117
        assertEquals( 400.0, a2.getAmount().doubleValue() );
japod@6
   118
        assertEquals( CZK, a2.getCurrency() );
japod@6
   119
    }
japod@6
   120
japod@6
   121
}
japod@6
   122