japod@6: package org.apidesign.apifest08.test; japod@6: japod@21: import static org.apidesign.apifest08.test.Currencies.CZK; japod@21: import static org.apidesign.apifest08.test.Currencies.SKK; japod@21: import static org.apidesign.apifest08.test.Currencies.USD; japod@6: japod@6: import java.math.BigDecimal; japod@6: japod@6: import junit.framework.TestCase; japod@6: japod@6: import org.apidesign.apifest08.currency.Amount; japod@21: import org.apidesign.apifest08.currency.ConversionException; japod@6: import org.apidesign.apifest08.currency.Convertor; japod@6: import org.apidesign.apifest08.currency.UnsupportedConversionException; 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@6: /** Create convertor that understands two currencies, CZK and japod@6: * USD. Make 1 USD == 17 CZK. 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@21: return new Convertor(new BigDecimal(17), USD, CZK); japod@6: } japod@6: japod@6: /** Create convertor that understands two currencies, CZK and japod@6: * SKK. Make 100 SKK == 80 CZK. 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@21: return new Convertor(new BigDecimal("0.8"), SKK, CZK); japod@6: } 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@6: Convertor c = createCZKtoUSD(); japod@6: // convert $5 to CZK using c: japod@6: Amount result = c.convert(new BigDecimal(5), USD, CZK); japod@6: assertEquals("Result is 85 CZK", 85, result.getValue().intValue()); japod@6: japod@6: // convert $8 to CZK japod@6: result = c.convert(new BigDecimal(8), USD, CZK); japod@6: assertEquals("Result is 136 CZK", 136, result.getValue().intValue()); japod@6: japod@6: // convert 1003CZK to USD japod@6: result = c.convert(new BigDecimal(1003), CZK, USD); japod@6: assertEquals("Result is 59 USD", 59, result.getValue().intValue()); 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: Convertor c = createSKKtoCZK(); japod@6: // convert 16CZK using c: japod@6: Amount result = c.convert(new BigDecimal(16), CZK, SKK); japod@6: assertEquals("Result is 20 SKK", 20, result.getValue().intValue()); japod@6: japod@6: // convert 500SKK to CZK japod@6: result = c.convert(new BigDecimal(500), SKK, CZK); japod@6: assertEquals("Result is 400 CZK", 400, result.getValue().intValue()); japod@6: } japod@6: japod@21: japod@21: /** japod@21: * Verify that the CZK to USD convertor knows nothing about SKK. japod@21: */ japod@21: public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception { japod@21: Convertor c = createCZKtoUSD(); japod@21: // convert $5 to SKK, the API shall say this is not possible japod@21: try { japod@21: c.convert(new BigDecimal(5), USD, SKK); japod@21: fail("convert $5 to SKK, the API shall say this is not possible"); japod@21: } catch (ConversionException e) { japod@21: //expected japod@21: } japod@21: japod@21: // convert 500 SKK to CZK, the API shall say this is not possible japod@21: japod@21: try { japod@21: c.convert(new BigDecimal("500"), SKK, CZK); japod@21: fail("convert 500 SKK to CZK, the API shall say this is not possible"); japod@21: } catch (ConversionException e) { japod@21: //expected japod@21: } japod@21: } japod@21: japod@21: /** japod@21: * Verify that the CZK to SKK convertor knows nothing about USD. japod@21: */ japod@21: public void testCannotConvertToSKKwithCZKSKKConvertor() throws Exception { japod@21: Convertor c = createSKKtoCZK(); japod@21: // convert $5 to SKK, the API shall say this is not possible japod@6: try { japod@6: c.convert(new BigDecimal(5), USD, SKK); japod@21: fail("convert $5 to SKK, the API shall say this is not possible"); japod@21: } catch(ConversionException e) { japod@21: //expected japod@21: } japod@21: japod@21: try { japod@21: c.convert(new BigDecimal(500), CZK, USD); japod@21: fail("convert 500 CZK to USD, the API shall say this is not possible"); japod@21: } catch(ConversionException e) { japod@21: //expected japod@21: } japod@21: } japod@6: } japod@6: