japod@6: package org.apidesign.apifest08.test;
japod@6:
japod@17:
japod@6: import java.math.BigDecimal;
japod@6: import java.util.Currency;
japod@35: import java.util.Set;
japod@6: import junit.framework.TestCase;
japod@6: import org.apidesign.apifest08.currency.Convertor;
japod@6: import org.apidesign.apifest08.currency.ConvertorFactory;
japod@17: import org.apidesign.apifest08.currency.InvalidConversionException;
japod@17:
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@17:
japod@17: private final static Currency CZK;
japod@17: private final static Currency SKK;
japod@17: private final static Currency USD;
japod@17:
japod@17: static
japod@17: {
japod@17: CZK = Currency.getInstance("CZK");
japod@17: SKK = Currency.getInstance("SKK");
japod@17: USD = Currency.getInstance("USD");
japod@17: }
japod@17:
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@6: {
japod@35: return (ConvertorFactory.getConvertor("CZK", BigDecimal.valueOf(17.0),
japod@35: "USD", BigDecimal.valueOf(1)));
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@6: {
japod@35: return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), BigDecimal.valueOf(100),
japod@35: Currency.getInstance("CZK"), BigDecimal.valueOf(80)));
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: BigDecimal result;
japod@6:
japod@6: // convert $5 to CZK using c:
japod@6: // assertEquals("Result is 85 CZK");
japod@17: result = c.convert(USD, CZK, BigDecimal.valueOf(5));
japod@6: assertEquals(new BigDecimal("85.00"), result);
japod@6:
japod@6: // convert $8 to CZK
japod@6: // assertEquals("Result is 136 CZK");
japod@17: result = c.convert(USD, CZK, BigDecimal.valueOf(8));
japod@6: assertEquals(new BigDecimal("136.00"), result);
japod@6:
japod@6: // convert 1003CZK to USD
japod@6: // assertEquals("Result is 59 USD");
japod@17: result = c.convert(CZK, USD, BigDecimal.valueOf(1003));
japod@6: assertEquals(new BigDecimal("59.00"), result);
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: BigDecimal result;
japod@6:
japod@6: // convert 16CZK using c:
japod@6: // assertEquals("Result is 20 SKK");
japod@17: result = c.convert(CZK, SKK, BigDecimal.valueOf(16));
japod@6: assertEquals(new BigDecimal("20.00"), result);
japod@6:
japod@6: // convert 500SKK to CZK
japod@6: // assertEquals("Result is 400 CZK");
japod@17: result = c.convert(SKK, CZK, BigDecimal.valueOf(500));
japod@6: assertEquals(new BigDecimal("400.00"), result);
japod@6: }
japod@6:
japod@17: /**
japod@17: * Verify that the CZK to USD convertor knows nothing about SKK.
japod@6: */
japod@17: public void testCannotConvertToSKKwithCZKUSDConvertor()
japod@17: throws Exception
japod@17: {
japod@17: Convertor c = createCZKtoUSD();
japod@6:
japod@17: try
japod@17: {
japod@17: // convert $5 to SKK, the API shall say this is not possible
japod@17: c.convert(USD, SKK, BigDecimal.valueOf(5));
japod@17: fail("cannot use the CZKtoUSD converter to convert to SKK");
japod@17: }
japod@17: catch(InvalidConversionException ex)
japod@17: {
japod@17: assertEquals("cannot convert to: SKK", ex.getMessage());
japod@17: assertEquals(SKK, ex.getBadCurrency());
japod@17: assertEquals(CZK, ex.getCurrencyA());
japod@17: assertEquals(USD, ex.getCurrencyB());
japod@17: }
japod@17:
japod@17: try
japod@17: {
japod@17: // convert 500 SKK to CZK, the API shall say this is not possible
japod@17: c.convert(SKK, CZK, BigDecimal.valueOf(5));
japod@17: fail("cannot use the CZKtoUSD converter to convert from SKK");
japod@17: }
japod@17: catch(InvalidConversionException ex)
japod@17: {
japod@17: assertEquals("cannot convert from: SKK", ex.getMessage());
japod@17: assertEquals(SKK, ex.getBadCurrency());
japod@17: assertEquals(CZK, ex.getCurrencyA());
japod@17: assertEquals(USD, ex.getCurrencyB());
japod@17: }
japod@17: }
japod@17:
japod@17: /**
japod@17: * Verify that the CZK to SKK convertor knows nothing about USD.
japod@17: */
japod@17: public void testCannotConvertToUSDwithSKKCZKConvertor()
japod@17: throws Exception
japod@17: {
japod@17: Convertor c = createSKKtoCZK();
japod@17:
japod@17: try
japod@17: {
japod@17: // convert $5 to SKK, the API shall say this is not possible
japod@17: c.convert(USD, SKK, BigDecimal.valueOf(5));
japod@17: fail("cannot use the CZKtoUSD converter to convert to SKK");
japod@17: }
japod@17: catch(InvalidConversionException ex)
japod@17: {
japod@17: assertEquals("cannot convert from: USD", ex.getMessage());
japod@17: assertEquals(USD, ex.getBadCurrency());
japod@17: assertEquals(SKK, ex.getCurrencyA());
japod@17: assertEquals(CZK, ex.getCurrencyB());
japod@17: }
japod@17:
japod@17: try
japod@17: {
japod@17: // convert 500 CZK to USD, the API shall say this is not possible
japod@17: c.convert(CZK, USD, BigDecimal.valueOf(500));
japod@17: fail("cannot use the CZKtoUSD converter to convert from SKK");
japod@17: }
japod@17: catch(InvalidConversionException ex)
japod@17: {
japod@17: assertEquals("cannot convert to: USD", ex.getMessage());
japod@17: assertEquals(USD, ex.getBadCurrency());
japod@17: assertEquals(SKK, ex.getCurrencyA());
japod@17: assertEquals(CZK, ex.getCurrencyB());
japod@17: }
japod@17: }
japod@35:
japod@35: public void testGetCurrencies()
japod@35: {
japod@35: Convertor c;
japod@35: Set currencies;
japod@35:
japod@35: c = createSKKtoCZK();
japod@35: currencies = c.getCurrencies();
japod@35: assertEquals(2, currencies.size());
japod@35: assertTrue(currencies.contains(Currency.getInstance("SKK")));
japod@35: assertTrue(currencies.contains(Currency.getInstance("CZK")));
japod@35:
japod@35: c = createCZKtoUSD();
japod@35: currencies = c.getCurrencies();
japod@35: assertEquals(2, currencies.size());
japod@35: assertTrue(currencies.contains(Currency.getInstance("USD")));
japod@35: assertTrue(currencies.contains(Currency.getInstance("CZK")));
japod@35: }
japod@35:
japod@35: public void testGetConverstionRate()
japod@35: throws InvalidConversionException
japod@35: {
japod@35: Convertor c;
japod@35:
japod@35: c = createSKKtoCZK();
japod@35: assertEquals(1.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("CZK")).doubleValue());
japod@35: assertEquals(1.0, c.getConversionRate(Currency.getInstance("SKK"), Currency.getInstance("SKK")).doubleValue());
japod@35: assertEquals(0.80, c.getConversionRate(Currency.getInstance("SKK"), Currency.getInstance("CZK")).doubleValue());
japod@35: assertEquals(1.25, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("SKK")).doubleValue());
japod@35:
japod@35: c = createCZKtoUSD();
japod@35: assertEquals(1.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("CZK")).doubleValue());
japod@35: assertEquals(1.0, c.getConversionRate(Currency.getInstance("USD"), Currency.getInstance("USD")).doubleValue());
japod@35: assertEquals(1.0/17.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("USD")).doubleValue(), 0.00000000000000001);
japod@35: assertEquals(17.0, c.getConversionRate(Currency.getInstance("USD"), Currency.getInstance("CZK")).doubleValue(), 0.00000000000000001);
japod@35: }
japod@6: }
japod@6: