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 {
30 // Imagine that there are three parts of the whole system:
31 // 1. there is someone who knows the current exchange rate
32 // 2. there is someone who wants to do the conversion
33 // 3. there is the API between 1. and 2. which allows them to communicate
34 // Please design such API
37 protected static final Currency CZK = Currency.getInstance( "CZK" );
38 protected static final Currency SKK = Currency.getInstance( "SKK" );
39 protected static final Currency USD = Currency.getInstance( "USD" );
41 /** Create convertor that understands two currencies, CZK and
42 * USD. Make 1 USD == 17 CZK.
43 * USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
44 * e.g. those that know the exchange rate. They somehow need to create
45 * the objects from the API and tell them the exchange rate. The API itself
46 * knows nothing about any rates, before the createCZKtoUSD method is called.
48 * Creation of the convertor shall not require subclassing of any class
49 * or interface on the client side.
51 * @return prepared convertor ready for converting USD to CZK and CZK to USD
53 public static Convertor createCZKtoUSD() {
54 final TableConvertor convertor = new TableConvertor();
55 final MonetaryAmount amountInCZK = new MonetaryAmount( 17, CZK );
56 final MonetaryAmount amountInUSD = new MonetaryAmount( 1, USD );
57 convertor.putIntoTable( new ConversionRate( amountInCZK, amountInUSD ) );
58 convertor.putIntoTable( new ConversionRate( amountInUSD, amountInCZK ) );
59 return new ContractImposingDelegatingConvertor( convertor ).test();
62 /** Create convertor that understands two currencies, CZK and
63 * SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
64 * it knows the exchange rate, and needs to use the API to create objects
65 * with the exchange rate. Anyone shall be ready to call this method without
66 * any other method being called previously. The API itself shall know
67 * nothing about any rates, before this method is called.
69 * Creation of the convertor shall not require subclassing of any class
70 * or interface on the client side.
72 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
74 public static Convertor createSKKtoCZK() {
75 final TableConvertor convertor = new TableConvertor();
76 final MonetaryAmount amountInSKK = new MonetaryAmount( 100, SKK );
77 final MonetaryAmount amountInCZK = new MonetaryAmount( 80, CZK );
78 convertor.putIntoTable( new ConversionRate( amountInSKK, amountInCZK ) );
79 convertor.putIntoTable( new ConversionRate( amountInCZK, amountInSKK ) );
80 return new ContractImposingDelegatingConvertor( convertor ).test();
84 // now the methods for group #2 follow:
85 // this group knows nothing about exchange rates, but knows how to use
86 // the API to do conversions. It somehow (by calling one of the factory
87 // methods) gets objects from the API and uses them to do the conversions.
90 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
93 public void testCurrencyCZKUSD() throws Exception {
94 final Convertor c = createCZKtoUSD();
96 // convert $5 to CZK using c:
97 final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) );
98 final MonetaryAmount a1 = r1.getNetAmount();
99 // assertEquals("Result is 85 CZK");
101 assertEquals( 85.0, a1.getAmount().doubleValue() );
102 assertEquals( CZK, a1.getCurrency() );
105 final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) );
106 final MonetaryAmount a2 = r2.getNetAmount();
107 // assertEquals("Result is 136 CZK");
109 assertEquals( 136.0, a2.getAmount().doubleValue() );
110 assertEquals( CZK, a2.getCurrency() );
112 // convert 1003CZK to USD
113 final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1003, CZK ), USD ) );
114 final MonetaryAmount a3 = r3.getNetAmount();
115 // assertEquals("Result is 59 USD");
117 assertEquals( 59.0, a3.getAmount().doubleValue() );
118 assertEquals( USD, a3.getCurrency() );
121 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
124 public void testCurrencySKKCZK() throws Exception {
125 final Convertor c = createSKKtoCZK();
127 // convert 16CZK using c:
128 final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) );
129 final MonetaryAmount a1 = r1.getNetAmount();
130 // assertEquals("Result is 20 SKK");
132 assertEquals( 20.0, a1.getAmount().doubleValue() );
133 assertEquals( SKK, a1.getCurrency() );
135 // convert 500SKK to CZK
136 final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) );
137 final MonetaryAmount a2 = r2.getNetAmount();
138 // assertEquals("Result is 400 CZK");
140 assertEquals( 400.0, a2.getAmount().doubleValue() );
141 assertEquals( CZK, a2.getCurrency() );
144 /** Verify that the CZK to USD convertor knows nothing about SKK.
146 public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
147 final Convertor c = createCZKtoUSD();
149 // convert $5 to SKK, the API shall say this is not possible
150 final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), SKK ) );
151 final MonetaryAmount a1 = r1.getNetAmount();
154 // convert 500 SKK to CZK, the API shall say this is not possible
155 final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, SKK ), CZK ) );
156 final MonetaryAmount a2 = r2.getNetAmount();
160 /** Verify that the CZK to SKK convertor knows nothing about USD.
162 public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception {
163 final Convertor c = createSKKtoCZK();
165 // convert $5 to SKK, the API shall say this is not possible
166 final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), SKK ) );
167 final MonetaryAmount a1 = r1.getNetAmount();
170 // convert 500 CZK to USD, the API shall say this is not possible
171 final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, CZK ), USD ) );
172 final MonetaryAmount a2 = r2.getNetAmount();