japod@6: package org.apidesign.apifest08.test; japod@6: japod@6: import java.util.Currency; japod@6: japod@6: import junit.framework.TestCase; japod@6: japod@6: import org.apidesign.apifest08.currency.Convertor; japod@6: import org.apidesign.apifest08.currency.ConvertorFactory; japod@6: import org.apidesign.apifest08.currency.MoneyImpl; 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@16: japod@6: public static final Currency USD = Currency.getInstance("USD"); japod@6: public static final Currency CZK = Currency.getInstance("CZK"); japod@6: public static final Currency SKK = Currency.getInstance("SKK"); japod@16: 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@16: // japod@16: // Imagine that there are three parts of the whole system: japod@16: // 1. there is someone who knows the current exchange rate japod@16: // 2. there is someone who wants to do the conversion japod@16: // 3. there is the API between 1. and 2. which allows them to communicate japod@16: // Please design such API japod@16: // japod@16: japod@6: /** Create convertor that understands two currencies, CZK and japod@16: * USD. Make 1 USD == 17 CZK. This is a method provided for #1 group - japod@16: * e.g. those that know the exchange rate. They somehow need to create japod@16: * the objects from the API and tell them the exchange rate. The API itself japod@16: * 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@16: return ConvertorFactory.createConvertor(new MoneyImpl(17,CZK), new MoneyImpl(1,USD)); japod@6: } japod@6: japod@6: /** Create convertor that understands two currencies, CZK and japod@16: * SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group - japod@16: * it knows the exchange rate, and needs to use the API to create objects japod@16: * with the exchange rate. Anyone shall be ready to call this method without japod@16: * any other method being called previously. The API itself shall know japod@16: * 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@16: return ConvertorFactory.createConvertor(new MoneyImpl(100,SKK), new MoneyImpl(80,CZK)); japod@6: } japod@16: japod@16: // japod@16: // now the methods for group #2 follow: japod@16: // this group knows nothing about exchange rates, but knows how to use japod@16: // the API to do conversions. It somehow (by calling one of the factory japod@16: // methods) gets objects from the API and uses them to do the conversions. japod@16: // japod@6: 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@16: Convertor c = createCZKtoUSD(); japod@6: // convert $5 to CZK using c: japod@16: assertEquals("Result is 85 CZK",new MoneyImpl(85,CZK), c.convert(new MoneyImpl(5,USD),CZK)); japod@6: japod@6: // convert $8 to CZK japod@16: assertEquals("Result is 136 CZK",new MoneyImpl(136,CZK), c.convert(new MoneyImpl(8,USD),CZK)); japod@6: japod@6: // convert 1003CZK to USD japod@16: assertEquals("Result is 59 USD", new MoneyImpl(59,USD), c.convert(new MoneyImpl(1003,CZK),USD)); japod@16: 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@16: Convertor c = createSKKtoCZK(); japod@6: // convert 16CZK using c: japod@16: assertEquals("Result is 20 SKK", new MoneyImpl(20,SKK), c.convert(new MoneyImpl(16,CZK),SKK)); japod@6: japod@6: // convert 500SKK to CZK japod@16: assertEquals("Result is 400 CZK", new MoneyImpl(400,CZK), c.convert(new MoneyImpl(500,SKK),CZK)); japod@16: } japod@16: japod@16: /** Verify that the CZK to USD convertor knows nothing about SKK. japod@16: */ japod@16: public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception { japod@16: Convertor c = createCZKtoUSD(); japod@16: // convert $5 to SKK, the API shall say this is not possible japod@16: try japod@16: { japod@16: c.convert(new MoneyImpl(5, USD), SKK); japod@16: fail("Exception expected"); japod@16: } japod@16: catch(IllegalArgumentException e) japod@16: { japod@16: assertTrue("Ok",true); japod@16: } japod@16: // convert 500 SKK to CZK, the API shall say this is not possible japod@16: try japod@16: { japod@16: c.convert(new MoneyImpl(500, SKK), CZK); japod@16: fail("Exception expected"); japod@16: } japod@16: catch(IllegalArgumentException e) japod@16: { japod@16: assertTrue("Ok",true); japod@16: } japod@16: } japod@16: japod@16: /** Verify that the CZK to SKK convertor knows nothing about USD. japod@16: */ japod@16: public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception { japod@16: Convertor c = createSKKtoCZK(); japod@16: // convert $5 to SKK, the API shall say this is not possible japod@16: try japod@16: { japod@16: c.convert(new MoneyImpl(5, USD), SKK); japod@16: fail("Exception expected"); japod@16: } japod@16: catch(IllegalArgumentException e) japod@16: { japod@16: assertTrue("Ok",true); japod@16: } japod@16: // convert 500 CZK to USD, the API shall say this is not possible japod@16: try japod@16: { japod@16: c.convert(new MoneyImpl(500, CZK), USD); japod@16: fail("Exception expected"); japod@16: } japod@16: catch(IllegalArgumentException e) japod@16: { japod@16: assertTrue("Ok",true); japod@16: } japod@6: } japod@6: }