japod@6: package org.apidesign.apifest08.test; japod@6: japod@6: import java.util.Currency; japod@6: import junit.framework.TestCase; japod@6: import org.apidesign.apifest08.currency.ConversionRate; japod@6: import org.apidesign.apifest08.currency.Convertor; japod@6: import org.apidesign.apifest08.currency.MonetaryAmount; japod@6: import org.apidesign.apifest08.currency.TableConvertor; japod@6: japod@6: /** Finish the Convertor API, and then write bodies of methods inside japod@6: * of this class to match the given tasks. To fullfil your task, use the japod@6: * API define in the org.apidesign.apifest08.currency package. japod@6: * Do not you reflection, or other hacks as your code japod@6: * shall run without any runtime permissions. japod@6: */ japod@6: public class Task1Test extends TestCase { japod@6: public Task1Test(String testName) { japod@6: super(testName); japod@6: } japod@6: japod@6: @Override japod@6: protected void setUp() throws Exception { japod@6: } japod@6: japod@6: @Override japod@6: protected void tearDown() throws Exception { japod@6: } japod@6: japod@18: // japod@18: // Imagine that there are three parts of the whole system: japod@18: // 1. there is someone who knows the current exchange rate japod@18: // 2. there is someone who wants to do the conversion japod@18: // 3. there is the API between 1. and 2. which allows them to communicate japod@18: // Please design such API japod@18: // japod@18: japod@6: protected static final Currency CZK = Currency.getInstance( "CZK" ); japod@6: protected static final Currency SKK = Currency.getInstance( "SKK" ); japod@6: protected static final Currency USD = Currency.getInstance( "USD" ); japod@6: japod@6: /** Create convertor that understands two currencies, CZK and japod@6: * USD. Make 1 USD == 17 CZK. japod@18: * USD. Make 1 USD == 17 CZK. This is a method provided for #1 group - japod@18: * e.g. those that know the exchange rate. They somehow need to create japod@18: * the objects from the API and tell them the exchange rate. The API itself japod@18: * knows nothing about any rates, before the createCZKtoUSD method is called. japod@6: * japod@6: * Creation of the convertor shall not require subclassing of any class japod@6: * or interface on the client side. japod@6: * japod@6: * @return prepared convertor ready for converting USD to CZK and CZK to USD japod@6: */ japod@6: public static Convertor createCZKtoUSD() { japod@6: final TableConvertor convertor = new TableConvertor(); japod@6: final MonetaryAmount amountInCZK = new MonetaryAmount( 17, CZK ); japod@6: final MonetaryAmount amountInUSD = new MonetaryAmount( 1, USD ); japod@6: convertor.putIntoTable( new ConversionRate( amountInCZK, amountInUSD ) ); japod@6: convertor.putIntoTable( new ConversionRate( amountInUSD, amountInCZK ) ); japod@6: return new ContractImposingDelegatingConvertor( convertor ).test(); japod@6: } japod@6: japod@6: /** Create convertor that understands two currencies, CZK and japod@18: * SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group - japod@18: * it knows the exchange rate, and needs to use the API to create objects japod@18: * with the exchange rate. Anyone shall be ready to call this method without japod@18: * any other method being called previously. The API itself shall know japod@18: * nothing about any rates, before this method is called. japod@6: * japod@6: * Creation of the convertor shall not require subclassing of any class japod@6: * or interface on the client side. japod@6: * japod@6: * @return prepared convertor ready for converting SKK to CZK and CZK to SKK japod@6: */ japod@6: public static Convertor createSKKtoCZK() { japod@6: final TableConvertor convertor = new TableConvertor(); japod@6: final MonetaryAmount amountInSKK = new MonetaryAmount( 100, SKK ); japod@6: final MonetaryAmount amountInCZK = new MonetaryAmount( 80, CZK ); japod@6: convertor.putIntoTable( new ConversionRate( amountInSKK, amountInCZK ) ); japod@6: convertor.putIntoTable( new ConversionRate( amountInCZK, amountInSKK ) ); japod@6: return new ContractImposingDelegatingConvertor( convertor ).test(); japod@6: } japod@6: japod@18: // japod@18: // now the methods for group #2 follow: japod@18: // this group knows nothing about exchange rates, but knows how to use japod@18: // the API to do conversions. It somehow (by calling one of the factory japod@18: // methods) gets objects from the API and uses them to do the conversions. japod@18: // japod@18: japod@6: /** Use the convertor from createCZKtoUSD method and do few conversions japod@6: * with it. japod@6: */ japod@6: public void testCurrencyCZKUSD() throws Exception { japod@6: final Convertor c = createCZKtoUSD(); japod@6: japod@6: // convert $5 to CZK using c: japod@6: final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) ); japod@6: final MonetaryAmount a1 = r1.getNetAmount(); japod@6: // assertEquals("Result is 85 CZK"); japod@6: assertNotNull( a1 ); japod@6: assertEquals( 85.0, a1.getAmount().doubleValue() ); japod@6: assertEquals( CZK, a1.getCurrency() ); japod@6: japod@6: // convert $8 to CZK japod@6: final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) ); japod@6: final MonetaryAmount a2 = r2.getNetAmount(); japod@6: // assertEquals("Result is 136 CZK"); japod@6: assertNotNull( a2 ); japod@6: assertEquals( 136.0, a2.getAmount().doubleValue() ); japod@6: assertEquals( CZK, a2.getCurrency() ); japod@6: japod@6: // convert 1003CZK to USD japod@6: final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1003, CZK ), USD ) ); japod@6: final MonetaryAmount a3 = r3.getNetAmount(); japod@6: // assertEquals("Result is 59 USD"); japod@6: assertNotNull( a3 ); japod@6: assertEquals( 59.0, a3.getAmount().doubleValue() ); japod@6: assertEquals( USD, a3.getCurrency() ); japod@6: } japod@6: japod@6: /** Use the convertor from createSKKtoCZK method and do few conversions japod@6: * with it. japod@6: */ japod@6: public void testCurrencySKKCZK() throws Exception { japod@6: final Convertor c = createSKKtoCZK(); japod@6: japod@6: // convert 16CZK using c: japod@6: final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) ); japod@6: final MonetaryAmount a1 = r1.getNetAmount(); japod@6: // assertEquals("Result is 20 SKK"); japod@6: assertNotNull( a1 ); japod@6: assertEquals( 20.0, a1.getAmount().doubleValue() ); japod@6: assertEquals( SKK, a1.getCurrency() ); japod@6: japod@6: // convert 500SKK to CZK japod@6: final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) ); japod@6: final MonetaryAmount a2 = r2.getNetAmount(); japod@6: // assertEquals("Result is 400 CZK"); japod@6: assertNotNull( a2 ); japod@6: assertEquals( 400.0, a2.getAmount().doubleValue() ); japod@6: assertEquals( CZK, a2.getCurrency() ); japod@6: } japod@6: japod@18: /** Verify that the CZK to USD convertor knows nothing about SKK. japod@18: */ japod@18: public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception { japod@18: final Convertor c = createCZKtoUSD(); japod@18: japod@18: // convert $5 to SKK, the API shall say this is not possible japod@18: final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), SKK ) ); japod@18: final MonetaryAmount a1 = r1.getNetAmount(); japod@18: assertNull( a1 ); japod@18: japod@18: // convert 500 SKK to CZK, the API shall say this is not possible japod@18: final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, SKK ), CZK ) ); japod@18: final MonetaryAmount a2 = r2.getNetAmount(); japod@18: assertNull( a2 ); japod@18: } japod@18: japod@18: /** Verify that the CZK to SKK convertor knows nothing about USD. japod@18: */ japod@18: public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception { japod@18: final Convertor c = createSKKtoCZK(); japod@18: japod@18: // convert $5 to SKK, the API shall say this is not possible japod@18: final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), SKK ) ); japod@18: final MonetaryAmount a1 = r1.getNetAmount(); japod@18: assertNull( a1 ); japod@18: japod@18: // convert 500 CZK to USD, the API shall say this is not possible japod@18: final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, CZK ), USD ) ); japod@18: final MonetaryAmount a2 = r2.getNetAmount(); japod@18: assertNull( a2 ); japod@18: } japod@18: japod@6: } japod@6: