1 package org.apidesign.apifest08.test;
3 import java.util.Currency;
4 import junit.framework.TestCase;
5 import org.apidesign.apifest08.currency.ConversionRate;
6 import org.apidesign.apifest08.currency.Convertor;
7 import org.apidesign.apifest08.currency.MonetaryAmount;
8 import org.apidesign.apifest08.currency.TableConvertor;
10 /** Finish the Convertor API, and then write bodies of methods inside
11 * of this class to match the given tasks. To fullfil your task, use the
12 * API define in the <code>org.apidesign.apifest08.currency</code> package.
13 * Do not you reflection, or other hacks as your code
14 * shall run without any runtime permissions.
16 public class Task1Test extends TestCase {
17 public Task1Test(String testName) {
22 protected void setUp() throws Exception {
26 protected void tearDown() throws Exception {
29 protected static final Currency CZK = Currency.getInstance( "CZK" );
30 protected static final Currency SKK = Currency.getInstance( "SKK" );
31 protected static final Currency USD = Currency.getInstance( "USD" );
33 /** Create convertor that understands two currencies, CZK and
34 * USD. Make 1 USD == 17 CZK.
36 * Creation of the convertor shall not require subclassing of any class
37 * or interface on the client side.
39 * @return prepared convertor ready for converting USD to CZK and CZK to USD
41 public static Convertor createCZKtoUSD() {
42 final TableConvertor convertor = new TableConvertor();
43 final MonetaryAmount amountInCZK = new MonetaryAmount( 17, CZK );
44 final MonetaryAmount amountInUSD = new MonetaryAmount( 1, USD );
45 convertor.putIntoTable( new ConversionRate( amountInCZK, amountInUSD ) );
46 convertor.putIntoTable( new ConversionRate( amountInUSD, amountInCZK ) );
47 return new ContractImposingDelegatingConvertor( convertor ).test();
50 /** Create convertor that understands two currencies, CZK and
51 * SKK. Make 100 SKK == 80 CZK.
53 * Creation of the convertor shall not require subclassing of any class
54 * or interface on the client side.
56 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
58 public static Convertor createSKKtoCZK() {
59 final TableConvertor convertor = new TableConvertor();
60 final MonetaryAmount amountInSKK = new MonetaryAmount( 100, SKK );
61 final MonetaryAmount amountInCZK = new MonetaryAmount( 80, CZK );
62 convertor.putIntoTable( new ConversionRate( amountInSKK, amountInCZK ) );
63 convertor.putIntoTable( new ConversionRate( amountInCZK, amountInSKK ) );
64 return new ContractImposingDelegatingConvertor( convertor ).test();
67 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
70 public void testCurrencyCZKUSD() throws Exception {
71 final Convertor c = createCZKtoUSD();
73 // convert $5 to CZK using c:
74 final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) );
75 final MonetaryAmount a1 = r1.getNetAmount();
76 // assertEquals("Result is 85 CZK");
78 assertEquals( 85.0, a1.getAmount().doubleValue() );
79 assertEquals( CZK, a1.getCurrency() );
82 final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) );
83 final MonetaryAmount a2 = r2.getNetAmount();
84 // assertEquals("Result is 136 CZK");
86 assertEquals( 136.0, a2.getAmount().doubleValue() );
87 assertEquals( CZK, a2.getCurrency() );
89 // convert 1003CZK to USD
90 final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1003, CZK ), USD ) );
91 final MonetaryAmount a3 = r3.getNetAmount();
92 // assertEquals("Result is 59 USD");
94 assertEquals( 59.0, a3.getAmount().doubleValue() );
95 assertEquals( USD, a3.getCurrency() );
98 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
101 public void testCurrencySKKCZK() throws Exception {
102 final Convertor c = createSKKtoCZK();
104 // convert 16CZK using c:
105 final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) );
106 final MonetaryAmount a1 = r1.getNetAmount();
107 // assertEquals("Result is 20 SKK");
109 assertEquals( 20.0, a1.getAmount().doubleValue() );
110 assertEquals( SKK, a1.getCurrency() );
112 // convert 500SKK to CZK
113 final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) );
114 final MonetaryAmount a2 = r2.getNetAmount();
115 // assertEquals("Result is 400 CZK");
117 assertEquals( 400.0, a2.getAmount().doubleValue() );
118 assertEquals( CZK, a2.getCurrency() );