diff -r 251d0ed461fb -r 58ec6da75f6f task4/solution04/test/org/apidesign/apifest08/test/Task1Test.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/task4/solution04/test/org/apidesign/apifest08/test/Task1Test.java Sat Oct 11 23:38:46 2008 +0200
@@ -0,0 +1,223 @@
+package org.apidesign.apifest08.test;
+
+
+import java.math.BigDecimal;
+import java.util.Currency;
+import java.util.Set;
+import junit.framework.TestCase;
+import org.apidesign.apifest08.currency.Convertor;
+import org.apidesign.apifest08.currency.ConvertorFactory;
+import org.apidesign.apifest08.currency.InvalidConversionException;
+
+
+/** Finish the Convertor API, and then write bodies of methods inside
+ * of this class to match the given tasks. To fullfil your task, use the
+ * API define in the org.apidesign.apifest08.currency
package.
+ * Do not you reflection, or other hacks as your code
+ * shall run without any runtime permissions.
+ */
+public class Task1Test extends TestCase {
+
+ private final static Currency CZK;
+ private final static Currency SKK;
+ private final static Currency USD;
+
+ static
+ {
+ CZK = Currency.getInstance("CZK");
+ SKK = Currency.getInstance("SKK");
+ USD = Currency.getInstance("USD");
+ }
+
+ public Task1Test(String testName) {
+ super(testName);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ }
+
+ /** Create convertor that understands two currencies, CZK and
+ * USD. Make 1 USD == 17 CZK.
+ *
+ * Creation of the convertor shall not require subclassing of any class
+ * or interface on the client side.
+ *
+ * @return prepared convertor ready for converting USD to CZK and CZK to USD
+ */
+ public static Convertor createCZKtoUSD()
+ {
+ return (ConvertorFactory.getConvertor("CZK", BigDecimal.valueOf(17.0),
+ "USD", BigDecimal.valueOf(1)));
+ }
+
+ /** Create convertor that understands two currencies, CZK and
+ * SKK. Make 100 SKK == 80 CZK.
+ *
+ * Creation of the convertor shall not require subclassing of any class
+ * or interface on the client side.
+ *
+ * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
+ */
+ public static Convertor createSKKtoCZK()
+ {
+ return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), BigDecimal.valueOf(100),
+ Currency.getInstance("CZK"), BigDecimal.valueOf(80)));
+ }
+
+ /** Use the convertor from createCZKtoUSD
method and do few conversions
+ * with it.
+ */
+ public void testCurrencyCZKUSD() throws Exception {
+ Convertor c = createCZKtoUSD();
+ BigDecimal result;
+
+ // convert $5 to CZK using c:
+ // assertEquals("Result is 85 CZK");
+ result = c.convert(USD, CZK, BigDecimal.valueOf(5));
+ assertEquals(new BigDecimal("85.00"), result);
+
+ // convert $8 to CZK
+ // assertEquals("Result is 136 CZK");
+ result = c.convert(USD, CZK, BigDecimal.valueOf(8));
+ assertEquals(new BigDecimal("136.00"), result);
+
+ // convert 1003CZK to USD
+ // assertEquals("Result is 59 USD");
+ result = c.convert(CZK, USD, BigDecimal.valueOf(1003));
+ assertEquals(new BigDecimal("59.00"), result);
+ }
+
+ /** Use the convertor from createSKKtoCZK
method and do few conversions
+ * with it.
+ */
+ public void testCurrencySKKCZK() throws Exception {
+ Convertor c = createSKKtoCZK();
+ BigDecimal result;
+
+ // convert 16CZK using c:
+ // assertEquals("Result is 20 SKK");
+ result = c.convert(CZK, SKK, BigDecimal.valueOf(16));
+ assertEquals(new BigDecimal("20.00"), result);
+
+ // convert 500SKK to CZK
+ // assertEquals("Result is 400 CZK");
+ result = c.convert(SKK, CZK, BigDecimal.valueOf(500));
+ assertEquals(new BigDecimal("400.00"), result);
+ }
+
+ /**
+ * Verify that the CZK to USD convertor knows nothing about SKK.
+ */
+ public void testCannotConvertToSKKwithCZKUSDConvertor()
+ throws Exception
+ {
+ Convertor c = createCZKtoUSD();
+
+ try
+ {
+ // convert $5 to SKK, the API shall say this is not possible
+ c.convert(USD, SKK, BigDecimal.valueOf(5));
+ fail("cannot use the CZKtoUSD converter to convert to SKK");
+ }
+ catch(InvalidConversionException ex)
+ {
+ assertEquals("cannot convert to: SKK", ex.getMessage());
+ assertEquals(SKK, ex.getBadCurrency());
+ assertEquals(CZK, ex.getCurrencyA());
+ assertEquals(USD, ex.getCurrencyB());
+ }
+
+ try
+ {
+ // convert 500 SKK to CZK, the API shall say this is not possible
+ c.convert(SKK, CZK, BigDecimal.valueOf(5));
+ fail("cannot use the CZKtoUSD converter to convert from SKK");
+ }
+ catch(InvalidConversionException ex)
+ {
+ assertEquals("cannot convert from: SKK", ex.getMessage());
+ assertEquals(SKK, ex.getBadCurrency());
+ assertEquals(CZK, ex.getCurrencyA());
+ assertEquals(USD, ex.getCurrencyB());
+ }
+ }
+
+ /**
+ * Verify that the CZK to SKK convertor knows nothing about USD.
+ */
+ public void testCannotConvertToUSDwithSKKCZKConvertor()
+ throws Exception
+ {
+ Convertor c = createSKKtoCZK();
+
+ try
+ {
+ // convert $5 to SKK, the API shall say this is not possible
+ c.convert(USD, SKK, BigDecimal.valueOf(5));
+ fail("cannot use the CZKtoUSD converter to convert to SKK");
+ }
+ catch(InvalidConversionException ex)
+ {
+ assertEquals("cannot convert from: USD", ex.getMessage());
+ assertEquals(USD, ex.getBadCurrency());
+ assertEquals(SKK, ex.getCurrencyA());
+ assertEquals(CZK, ex.getCurrencyB());
+ }
+
+ try
+ {
+ // convert 500 CZK to USD, the API shall say this is not possible
+ c.convert(CZK, USD, BigDecimal.valueOf(500));
+ fail("cannot use the CZKtoUSD converter to convert from SKK");
+ }
+ catch(InvalidConversionException ex)
+ {
+ assertEquals("cannot convert to: USD", ex.getMessage());
+ assertEquals(USD, ex.getBadCurrency());
+ assertEquals(SKK, ex.getCurrencyA());
+ assertEquals(CZK, ex.getCurrencyB());
+ }
+ }
+
+ public void testGetCurrencies()
+ {
+ Convertor c;
+ Set currencies;
+
+ c = createSKKtoCZK();
+ currencies = c.getCurrencies();
+ assertEquals(2, currencies.size());
+ assertTrue(currencies.contains(Currency.getInstance("SKK")));
+ assertTrue(currencies.contains(Currency.getInstance("CZK")));
+
+ c = createCZKtoUSD();
+ currencies = c.getCurrencies();
+ assertEquals(2, currencies.size());
+ assertTrue(currencies.contains(Currency.getInstance("USD")));
+ assertTrue(currencies.contains(Currency.getInstance("CZK")));
+ }
+
+ public void testGetConverstionRate()
+ throws InvalidConversionException
+ {
+ Convertor c;
+
+ c = createSKKtoCZK();
+ assertEquals(1.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("CZK")).doubleValue());
+ assertEquals(1.0, c.getConversionRate(Currency.getInstance("SKK"), Currency.getInstance("SKK")).doubleValue());
+ assertEquals(0.80, c.getConversionRate(Currency.getInstance("SKK"), Currency.getInstance("CZK")).doubleValue());
+ assertEquals(1.25, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("SKK")).doubleValue());
+
+ c = createCZKtoUSD();
+ assertEquals(1.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("CZK")).doubleValue());
+ assertEquals(1.0, c.getConversionRate(Currency.getInstance("USD"), Currency.getInstance("USD")).doubleValue());
+ assertEquals(1.0/17.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("USD")).doubleValue(), 0.00000000000000001);
+ assertEquals(17.0, c.getConversionRate(Currency.getInstance("USD"), Currency.getInstance("CZK")).doubleValue(), 0.00000000000000001);
+ }
+}
+