japod@23: package org.apidesign.apifest08.test; japod@23: japod@23: import java.math.BigDecimal; japod@23: import junit.framework.TestCase; japod@23: import org.apidesign.apifest08.currency.ConversionResult; japod@23: import org.apidesign.apifest08.currency.Convertor; japod@23: import org.apidesign.apifest08.currency.ConvertorCurrency; japod@23: import org.apidesign.apifest08.currency.ExchangeRateProvider; japod@41: import org.apidesign.apifest08.currency.ConversionNotSupportedException; japod@23: japod@23: /** Finish the Convertor API, and then write bodies of methods inside japod@23: * of this class to match the given tasks. To fullfil your task, use the japod@23: * API define in the org.apidesign.apifest08.currency package. japod@23: * Do not you reflection, or other hacks as your code japod@23: * shall run without any runtime permissions. japod@23: */ japod@23: public class Task1Test extends TestCase { japod@23: public Task1Test(String testName) { japod@23: super(testName); japod@23: } japod@23: japod@23: @Override japod@23: protected void setUp() throws Exception { japod@23: } japod@23: japod@23: @Override japod@23: protected void tearDown() throws Exception { japod@23: } japod@23: japod@23: /** Create convertor that understands two currencies, CZK and japod@23: * USD. Make 1 USD == 17 CZK. japod@23: * japod@23: * Creation of the convertor shall not require subclassing of any class japod@23: * or interface on the client side. japod@23: * japod@23: * @return prepared convertor ready for converting USD to CZK and CZK to USD japod@23: */ japod@41: public static Convertor createCZKtoUSD() { japod@23: ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK"); japod@23: ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("USD"); japod@23: ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(17), fromCurrency, new BigDecimal(1), toCurrency); japod@23: japod@23: return Convertor.createConvertor(exchangeRateProvider); japod@23: } japod@23: japod@23: /** Create convertor that understands two currencies, CZK and japod@23: * SKK. Make 100 SKK == 80 CZK. japod@23: * japod@23: * Creation of the convertor shall not require subclassing of any class japod@23: * or interface on the client side. japod@23: * japod@23: * @return prepared convertor ready for converting SKK to CZK and CZK to SKK japod@23: */ japod@41: public static Convertor createSKKtoCZK() { japod@23: ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("SKK"); japod@23: ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("CZK"); japod@23: ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(100), fromCurrency, new BigDecimal(80), toCurrency); japod@23: japod@23: return Convertor.createConvertor(exchangeRateProvider); japod@23: } japod@23: japod@23: japod@41: public static Convertor createCZKtoYEN() { japod@23: ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK"); japod@23: ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("JPY"); japod@23: ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(1), fromCurrency, new BigDecimal(1), toCurrency); japod@23: japod@23: return Convertor.createConvertor(exchangeRateProvider); japod@23: } japod@23: japod@23: /** Use the convertor from createCZKtoUSD method and do few conversions japod@23: * with it. japod@23: */ japod@23: public void testCurrencyCZKUSD() throws Exception { japod@23: Convertor convertCzkUsd = createCZKtoUSD(); japod@23: japod@23: { japod@23: // convert $1 to CZK using c: japod@23: ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(1)); japod@23: assertEquals("Result is 17 CZK", new BigDecimal("17.00"), result.getConverted()); japod@23: assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder()); japod@23: } japod@23: japod@23: { japod@23: // convert 17CKZ to $ using c: japod@23: ConversionResult result = convertCzkUsd.convert(new BigDecimal(17)); japod@23: assertEquals("Result is 1 $", new BigDecimal("1.00"), result.getConverted()); japod@23: assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder()); japod@23: } japod@23: japod@23: { japod@23: // convert $5 to CZK using c: japod@23: ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(5)); japod@23: assertEquals("Result is 85 CZK", new BigDecimal("85.00"), result.getConverted()); japod@23: assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder()); japod@23: } japod@23: japod@23: { japod@23: // convert $8 to CZK japod@23: ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(8)); japod@23: assertEquals("Result is 136 CZK", new BigDecimal("136.00"), result.getConverted()); japod@23: assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder()); japod@23: } japod@23: japod@23: { japod@23: // convert 1003CZK to USD japod@23: ConversionResult result = convertCzkUsd.convert(new BigDecimal(1003)); japod@23: assertEquals("Result is 59 USD", new BigDecimal("59.00"), result.getConverted()); japod@23: assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder()); japod@23: } japod@23: } japod@23: japod@23: /** Use the convertor from createSKKtoCZK method and do few conversions japod@23: * with it. japod@23: */ japod@23: public void testCurrencySKKCZK() throws Exception { japod@23: Convertor convertSkkCzk = createSKKtoCZK(); japod@23: { japod@23: // convert 100SKK using c: japod@23: ConversionResult result = convertSkkCzk.convert(new BigDecimal(100)); japod@23: assertEquals("Result is 80 CZK", new BigDecimal("80.00"), result.getConverted()); japod@23: } japod@23: { japod@23: // convert 80CZK using c: japod@23: ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(80)); japod@23: assertEquals("Result is 100 SKK", new BigDecimal("100.00"), result.getConverted()); japod@23: } japod@23: japod@23: { japod@23: // convert 16CZK using c: japod@23: ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(16)); japod@23: assertEquals("Result is 20 SKK", new BigDecimal("20.00"), result.getConverted()); japod@23: } japod@23: japod@23: { japod@23: // convert 500SKK to CZK japod@23: ConversionResult result = convertSkkCzk.convert(new BigDecimal(500)); japod@23: assertEquals("Result is 400 CZK", new BigDecimal("400.00"), result.getConverted()); japod@23: assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder()); japod@23: } japod@23: japod@23: { japod@23: // convert 501SKK to CZK japod@23: ConversionResult result = convertSkkCzk.convert(new BigDecimal(501)); japod@23: assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted()); japod@23: assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder()); japod@23: japod@23: } japod@23: } japod@23: japod@23: /** japod@23: * Convert SKK to CZK. Convertor can't convert whole amout (can't convert one SKK cent to CZK). Remaining japod@23: * amount is stored in remainder result. japod@23: * japod@23: * Test is currently failing, because implementation can't handle this case. japod@23: */ japod@23: // public void testConvertSmallUnits_failing() { japod@23: // Convertor convertSkkCzk = createSKKtoCZK(); japod@23: // { japod@23: // // convert 501SKK to CZK japod@23: // ConversionResult result = convertSkkCzk.convert(new BigDecimal("501.01")); japod@23: // assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted()); japod@23: // assertEquals("No Remainer", new BigDecimal("0.01"), result.getRemainder()); japod@23: // japod@23: // } japod@23: // japod@23: // } japod@23: japod@23: /** japod@23: * Test converting from CZK to JPY. Remained has scale of CZK. japod@23: * japod@23: * This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency. japod@23: */ japod@23: // public void testConvertCzkToJpy_failing() { japod@23: // Convertor convertSkkCzk = createCZKtoYEN(); japod@23: // { japod@23: // // convert 501SKK to CZK japod@23: // ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00")); japod@23: // assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted()); japod@23: // assertEquals("No Remainer", new BigDecimal("0.00"), result.getRemainder()); japod@23: // japod@23: // } japod@23: // } japod@23: japod@23: /** japod@23: * Test converting from JPY to CZK. Remained has scale of JPY. japod@23: * japod@23: * This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency. japod@23: */ japod@23: // public void testConvertJpyToCzk_failing() { japod@23: // Convertor convertSkkCzk = createCZKtoYEN(); japod@23: // { japod@23: // // convert 501SKK to CZK japod@23: // ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00")); japod@23: // assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted()); japod@23: // assertEquals("No Remainer", new BigDecimal("0"), result.getRemainder()); japod@23: // japod@23: // } japod@23: // } japod@23: japod@23: public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception { japod@23: Convertor c = createCZKtoUSD(); japod@23: // convert $5 to SKK, the API shall say this is not possible japod@41: try { japod@41: c.convert(ConvertorCurrency.getInstance("USD"), ConvertorCurrency.getInstance("SKK"), new BigDecimal(5)); japod@41: fail(); japod@41: } catch (ConversionNotSupportedException e) { japod@41: //expected error; japod@41: assertEquals("Exception From USD", "USD",e.getFromCurrecyCode()); japod@41: assertEquals("Exception To SKK", "SKK",e.getToCurrecyCode()); japod@41: } japod@23: // convert 500 SKK to CZK, the API shall say this is not possible japod@41: try { japod@41: c.convert(ConvertorCurrency.getInstance("SKK"), ConvertorCurrency.getInstance("CZK"), new BigDecimal(500)); japod@41: fail(); japod@41: } catch (ConversionNotSupportedException e) { japod@41: assertEquals("Exception From USD", "SKK",e.getFromCurrecyCode()); japod@41: assertEquals("Exception To SKK", "CZK",e.getToCurrecyCode()); } japod@23: } japod@23: japod@23: japod@23: } japod@23: