1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task2/solution04/test/org/apidesign/apifest08/test/Task1Test.java Wed Oct 01 10:43:05 2008 +0200
1.3 @@ -0,0 +1,184 @@
1.4 +package org.apidesign.apifest08.test;
1.5 +
1.6 +
1.7 +import java.math.BigDecimal;
1.8 +import java.util.Currency;
1.9 +import junit.framework.TestCase;
1.10 +import org.apidesign.apifest08.currency.Convertor;
1.11 +import org.apidesign.apifest08.currency.ConvertorFactory;
1.12 +import org.apidesign.apifest08.currency.InvalidConversionException;
1.13 +
1.14 +
1.15 +/** Finish the Convertor API, and then write bodies of methods inside
1.16 + * of this class to match the given tasks. To fullfil your task, use the
1.17 + * API define in the <code>org.apidesign.apifest08.currency</code> package.
1.18 + * Do not you reflection, or other hacks as your code
1.19 + * shall run without any runtime permissions.
1.20 + */
1.21 +public class Task1Test extends TestCase {
1.22 +
1.23 + private final static Currency CZK;
1.24 + private final static Currency SKK;
1.25 + private final static Currency USD;
1.26 +
1.27 + static
1.28 + {
1.29 + CZK = Currency.getInstance("CZK");
1.30 + SKK = Currency.getInstance("SKK");
1.31 + USD = Currency.getInstance("USD");
1.32 + }
1.33 +
1.34 + public Task1Test(String testName) {
1.35 + super(testName);
1.36 + }
1.37 +
1.38 + @Override
1.39 + protected void setUp() throws Exception {
1.40 + }
1.41 +
1.42 + @Override
1.43 + protected void tearDown() throws Exception {
1.44 + }
1.45 +
1.46 + /** Create convertor that understands two currencies, CZK and
1.47 + * USD. Make 1 USD == 17 CZK.
1.48 + *
1.49 + * Creation of the convertor shall not require subclassing of any class
1.50 + * or interface on the client side.
1.51 + *
1.52 + * @return prepared convertor ready for converting USD to CZK and CZK to USD
1.53 + */
1.54 + public static Convertor createCZKtoUSD()
1.55 + {
1.56 + return (ConvertorFactory.getConvertor("CZK", BigDecimal.valueOf(17.0), "USD", BigDecimal.valueOf(1)));
1.57 + }
1.58 +
1.59 + /** Create convertor that understands two currencies, CZK and
1.60 + * SKK. Make 100 SKK == 80 CZK.
1.61 + *
1.62 + * Creation of the convertor shall not require subclassing of any class
1.63 + * or interface on the client side.
1.64 + *
1.65 + * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
1.66 + */
1.67 + public static Convertor createSKKtoCZK()
1.68 + {
1.69 + return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), BigDecimal.valueOf(100), Currency.getInstance("CZK"), BigDecimal.valueOf(80)));
1.70 + }
1.71 +
1.72 + /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
1.73 + * with it.
1.74 + */
1.75 + public void testCurrencyCZKUSD() throws Exception {
1.76 + Convertor c = createCZKtoUSD();
1.77 + BigDecimal result;
1.78 +
1.79 + // convert $5 to CZK using c:
1.80 + // assertEquals("Result is 85 CZK");
1.81 + result = c.convert(USD, CZK, BigDecimal.valueOf(5));
1.82 + assertEquals(new BigDecimal("85.00"), result);
1.83 +
1.84 + // convert $8 to CZK
1.85 + // assertEquals("Result is 136 CZK");
1.86 + result = c.convert(USD, CZK, BigDecimal.valueOf(8));
1.87 + assertEquals(new BigDecimal("136.00"), result);
1.88 +
1.89 + // convert 1003CZK to USD
1.90 + // assertEquals("Result is 59 USD");
1.91 + result = c.convert(CZK, USD, BigDecimal.valueOf(1003));
1.92 + assertEquals(new BigDecimal("59.00"), result);
1.93 + }
1.94 +
1.95 + /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
1.96 + * with it.
1.97 + */
1.98 + public void testCurrencySKKCZK() throws Exception {
1.99 + Convertor c = createSKKtoCZK();
1.100 + BigDecimal result;
1.101 +
1.102 + // convert 16CZK using c:
1.103 + // assertEquals("Result is 20 SKK");
1.104 + result = c.convert(CZK, SKK, BigDecimal.valueOf(16));
1.105 + assertEquals(new BigDecimal("20.00"), result);
1.106 +
1.107 + // convert 500SKK to CZK
1.108 + // assertEquals("Result is 400 CZK");
1.109 + result = c.convert(SKK, CZK, BigDecimal.valueOf(500));
1.110 + assertEquals(new BigDecimal("400.00"), result);
1.111 + }
1.112 +
1.113 + /**
1.114 + * Verify that the CZK to USD convertor knows nothing about SKK.
1.115 + */
1.116 + public void testCannotConvertToSKKwithCZKUSDConvertor()
1.117 + throws Exception
1.118 + {
1.119 + Convertor c = createCZKtoUSD();
1.120 +
1.121 + try
1.122 + {
1.123 + // convert $5 to SKK, the API shall say this is not possible
1.124 + c.convert(USD, SKK, BigDecimal.valueOf(5));
1.125 + fail("cannot use the CZKtoUSD converter to convert to SKK");
1.126 + }
1.127 + catch(InvalidConversionException ex)
1.128 + {
1.129 + assertEquals("cannot convert to: SKK", ex.getMessage());
1.130 + assertEquals(SKK, ex.getBadCurrency());
1.131 + assertEquals(CZK, ex.getCurrencyA());
1.132 + assertEquals(USD, ex.getCurrencyB());
1.133 + }
1.134 +
1.135 + try
1.136 + {
1.137 + // convert 500 SKK to CZK, the API shall say this is not possible
1.138 + c.convert(SKK, CZK, BigDecimal.valueOf(5));
1.139 + fail("cannot use the CZKtoUSD converter to convert from SKK");
1.140 + }
1.141 + catch(InvalidConversionException ex)
1.142 + {
1.143 + assertEquals("cannot convert from: SKK", ex.getMessage());
1.144 + assertEquals(SKK, ex.getBadCurrency());
1.145 + assertEquals(CZK, ex.getCurrencyA());
1.146 + assertEquals(USD, ex.getCurrencyB());
1.147 + }
1.148 + }
1.149 +
1.150 + /**
1.151 + * Verify that the CZK to SKK convertor knows nothing about USD.
1.152 + */
1.153 + public void testCannotConvertToUSDwithSKKCZKConvertor()
1.154 + throws Exception
1.155 + {
1.156 + Convertor c = createSKKtoCZK();
1.157 +
1.158 + try
1.159 + {
1.160 + // convert $5 to SKK, the API shall say this is not possible
1.161 + c.convert(USD, SKK, BigDecimal.valueOf(5));
1.162 + fail("cannot use the CZKtoUSD converter to convert to SKK");
1.163 + }
1.164 + catch(InvalidConversionException ex)
1.165 + {
1.166 + assertEquals("cannot convert from: USD", ex.getMessage());
1.167 + assertEquals(USD, ex.getBadCurrency());
1.168 + assertEquals(SKK, ex.getCurrencyA());
1.169 + assertEquals(CZK, ex.getCurrencyB());
1.170 + }
1.171 +
1.172 + try
1.173 + {
1.174 + // convert 500 CZK to USD, the API shall say this is not possible
1.175 + c.convert(CZK, USD, BigDecimal.valueOf(500));
1.176 + fail("cannot use the CZKtoUSD converter to convert from SKK");
1.177 + }
1.178 + catch(InvalidConversionException ex)
1.179 + {
1.180 + assertEquals("cannot convert to: USD", ex.getMessage());
1.181 + assertEquals(USD, ex.getBadCurrency());
1.182 + assertEquals(SKK, ex.getCurrencyA());
1.183 + assertEquals(CZK, ex.getCurrencyB());
1.184 + }
1.185 + }
1.186 +}
1.187 +