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@20: import org.apidesign.apifest08.currency.exceptions.InvalidCurrencyException; japod@20: import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException; japod@6: japod@6: /** japod@6: * Finish the Convertor API, and then write bodies of methods inside of this class to match the given tasks. To fullfil japod@6: * your task, use the API define in the org.apidesign.apifest08.currency package. Do not you reflection, or japod@6: * other hacks as your code 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@20: // japod@20: // Imagine that there are three parts of the whole system: japod@20: // 1. there is someone who knows the current exchange rate japod@20: // 2. there is someone who wants to do the conversion japod@20: // 3. there is the API between 1. and 2. which allows them to communicate japod@20: // Please design such API japod@20: // japod@20: japod@6: /** japod@20: * Create convertor that understands two currencies, CZK and USD. Make 1 USD == 17 CZK. This is a method provided for japod@20: * #1 group - e.g. those that know the exchange rate. They somehow need to create the objects from the API and tell japod@20: * them the exchange rate. The API itself knows nothing about any rates, before the createCZKtoUSD method is called. japod@20: * Creation of the convertor shall not require subclassing of any class or interface on the client side. japod@6: * @return prepared convertor ready for converting USD to CZK and CZK to USD japod@6: */ japod@6: public static Convertor createCZKtoUSD() { japod@20: // set exchange rates japod@20: Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), 17d, 1d); japod@20: japod@20: // create new instance japod@20: Convertor convertor = null; japod@20: try { japod@20: convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("CZK")); japod@20: } catch (UnknownConvertorException e) { japod@20: e.printStackTrace(); japod@20: } japod@20: japod@20: return convertor; japod@6: } japod@6: japod@6: /** japod@20: * Create convertor that understands two currencies, CZK and SKK. Make 100 SKK == 80 CZK. Again this is method for the japod@20: * #1 group - it knows the exchange rate, and needs to use the API to create objects with the exchange rate. Anyone japod@20: * shall be ready to call this method without any other method being called previously. The API itself shall know japod@20: * nothing about any rates, before this method is called. Creation of the convertor shall not require subclassing of japod@20: * any class or interface on the client side. japod@6: * @return prepared convertor ready for converting SKK to CZK and CZK to SKK japod@6: */ japod@6: public static Convertor createSKKtoCZK() { japod@20: // set exchange rates japod@20: Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 80d, 100d); japod@20: japod@20: // create new instance japod@20: Convertor convertor = null; japod@20: try { japod@20: convertor = Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK")); japod@20: } catch (UnknownConvertorException e) { japod@20: e.printStackTrace(); japod@20: } japod@20: japod@20: return convertor; japod@6: } japod@6: japod@20: // japod@20: // now the methods for group #2 follow: japod@20: // this group knows nothing about exchange rates, but knows how to use japod@20: // the API to do conversions. It somehow (by calling one of the factory japod@20: // methods) gets objects from the API and uses them to do the conversions. japod@20: // japod@20: japod@6: /** japod@6: * Use the convertor from createCZKtoUSD method and do few conversions with it. japod@6: */ japod@6: public void testCurrencyCZKUSD() throws Exception { japod@6: Convertor c = createCZKtoUSD(); japod@6: // convert $5 to CZK using c: japod@20: double result = c.convert(5, Currency.getInstance("USD"), Currency.getInstance("CZK")); japod@6: assertEquals("Result is not 85 CZK", 85.0, result); japod@6: japod@6: // convert $8 to CZK japod@20: result = c.convert(8, Currency.getInstance("USD"), Currency.getInstance("CZK")); japod@6: assertEquals("Result is not 136 CZK", 136.0, result); japod@6: japod@6: // convert 1003CZK to USD japod@20: result = c.convert(1003, Currency.getInstance("CZK"), Currency.getInstance("USD")); japod@6: assertEquals("Result is not 59 USD", 59.0, result); japod@6: } japod@6: japod@6: /** japod@6: * Use the convertor from createSKKtoCZK method and do few conversions with it. japod@6: */ japod@6: public void testCurrencySKKCZK() throws Exception { japod@6: Convertor c = createSKKtoCZK(); japod@6: japod@6: // convert 16CZK using c: japod@20: double result = c.convert(16, Currency.getInstance("CZK"), Currency.getInstance("SKK")); japod@6: assertEquals("Result is not 20 SKK", 20.0, result); japod@6: japod@6: // convert 500SKK to CZK japod@20: result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK")); japod@6: assertEquals("Result is not 400 CZK", 400.0, result); japod@6: } japod@20: japod@20: /** japod@20: * Verify that the CZK to USD convertor knows nothing about SKK. japod@20: */ japod@20: public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception { japod@20: Convertor c = createCZKtoUSD(); japod@20: boolean exceptionThrown = false; japod@20: japod@20: // convert $5 to SKK, the API shall say this is not possible japod@20: try { japod@20: c.convert(5, Currency.getInstance("USD"), Currency.getInstance("SKK")); japod@20: exceptionThrown = false; japod@20: } catch (InvalidCurrencyException e) { japod@20: exceptionThrown = true; japod@20: } japod@20: assertEquals("It should be impossible to convert to SKK with USD->CZK convertor", true, exceptionThrown); japod@20: japod@20: // convert 500 SKK to CZK, the API shall say this is not possible japod@20: try { japod@20: c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK")); japod@20: exceptionThrown = false; japod@20: } catch (InvalidCurrencyException e) { japod@20: exceptionThrown = true; japod@20: } japod@20: assertEquals("It should be impossible to convert from SKK with USD->CZK convertor", true, exceptionThrown); japod@20: japod@20: } japod@20: japod@20: /** japod@20: * Verify that the CZK to SKK convertor knows nothing about USD. japod@20: */ japod@20: public void testCannotConvertToSKKwithSKKCZKConvertor() throws Exception { japod@20: Convertor c = createSKKtoCZK(); japod@20: boolean exceptionThrown = false; japod@20: japod@20: // convert $5 to SKK, the API shall say this is not possible japod@20: try { japod@20: c.convert(5, Currency.getInstance("USD"), Currency.getInstance("SKK")); japod@20: exceptionThrown = false; japod@20: } catch (InvalidCurrencyException e) { japod@20: exceptionThrown = true; japod@20: } japod@20: assertEquals("It should be impossible to convert form USD with SKK->CZK convertor", true, exceptionThrown); japod@20: japod@20: // convert 500 CZK to USD, the API shall say this is not possible japod@20: try { japod@20: c.convert(500, Currency.getInstance("CZK"), Currency.getInstance("USD")); japod@20: exceptionThrown = false; japod@20: } catch (InvalidCurrencyException e) { japod@20: exceptionThrown = true; japod@20: } japod@20: assertEquals("It should be impossible to convert to USD with SKK->CZK convertor", true, exceptionThrown); japod@20: } japod@6: }