diff -r 251d0ed461fb -r 58ec6da75f6f task4/solution12/test/org/apidesign/apifest08/test/Task1Test.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/task4/solution12/test/org/apidesign/apifest08/test/Task1Test.java Sat Oct 11 23:38:46 2008 +0200
@@ -0,0 +1,174 @@
+package org.apidesign.apifest08.test;
+
+import java.util.Currency;
+
+import junit.framework.TestCase;
+
+import org.apidesign.apifest08.currency.Convertor;
+import org.apidesign.apifest08.currency.exceptions.InvalidCurrencyException;
+import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
+
+/**
+ * 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 {
+ public Task1Test(String testName) {
+ super(testName);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ }
+
+ //
+ // Imagine that there are three parts of the whole system:
+ // 1. there is someone who knows the current exchange rate
+ // 2. there is someone who wants to do the conversion
+ // 3. there is the API between 1. and 2. which allows them to communicate
+ // Please design such API
+ //
+
+ /**
+ * Create convertor that understands two currencies, CZK and USD. Make 1 USD == 17 CZK. This is a method provided for
+ * #1 group - e.g. those that know the exchange rate. They somehow need to create the objects from the API and tell
+ * them the exchange rate. The API itself knows nothing about any rates, before the createCZKtoUSD method is called.
+ * 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() {
+ // set exchange rates
+ Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), 17d, 1d);
+
+ // create new instance
+ Convertor convertor = null;
+ try {
+ convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("CZK"));
+ } catch (UnknownConvertorException e) {
+ e.printStackTrace();
+ }
+
+ return convertor;
+ }
+
+ /**
+ * Create convertor that understands two currencies, CZK and SKK. Make 100 SKK == 80 CZK. Again this is method for the
+ * #1 group - it knows the exchange rate, and needs to use the API to create objects with the exchange rate. Anyone
+ * shall be ready to call this method without any other method being called previously. The API itself shall know
+ * nothing about any rates, before this method is called. 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() {
+ // set exchange rates
+ Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 80d, 100d);
+
+ // create new instance
+ Convertor convertor = null;
+ try {
+ convertor = Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
+ } catch (UnknownConvertorException e) {
+ e.printStackTrace();
+ }
+
+ return convertor;
+ }
+
+ //
+ // now the methods for group #2 follow:
+ // this group knows nothing about exchange rates, but knows how to use
+ // the API to do conversions. It somehow (by calling one of the factory
+ // methods) gets objects from the API and uses them to do the conversions.
+ //
+
+ /**
+ * Use the convertor from createCZKtoUSD
method and do few conversions with it.
+ */
+ public void testCurrencyCZKUSD() throws Exception {
+ Convertor c = createCZKtoUSD();
+ // convert $5 to CZK using c:
+ double result = c.convert(5, Currency.getInstance("USD"), Currency.getInstance("CZK"));
+ assertEquals("Result is not 85 CZK", 85.0, result);
+
+ // convert $8 to CZK
+ result = c.convert(8, Currency.getInstance("USD"), Currency.getInstance("CZK"));
+ assertEquals("Result is not 136 CZK", 136.0, result);
+
+ // convert 1003CZK to USD
+ result = c.convert(1003, Currency.getInstance("CZK"), Currency.getInstance("USD"));
+ assertEquals("Result is not 59 USD", 59.0, result);
+ }
+
+ /**
+ * Use the convertor from createSKKtoCZK
method and do few conversions with it.
+ */
+ public void testCurrencySKKCZK() throws Exception {
+ Convertor c = createSKKtoCZK();
+
+ // convert 16CZK using c:
+ double result = c.convert(16, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
+ assertEquals("Result is not 20 SKK", 20.0, result);
+
+ // convert 500SKK to CZK
+ result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
+ assertEquals("Result is not 400 CZK", 400.0, result);
+ }
+
+ /**
+ * Verify that the CZK to USD convertor knows nothing about SKK.
+ */
+ public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
+ Convertor c = createCZKtoUSD();
+ boolean exceptionThrown = false;
+
+ // convert $5 to SKK, the API shall say this is not possible
+ try {
+ c.convert(5, Currency.getInstance("USD"), Currency.getInstance("SKK"));
+ exceptionThrown = false;
+ } catch (InvalidCurrencyException e) {
+ exceptionThrown = true;
+ }
+ assertEquals("It should be impossible to convert to SKK with USD->CZK convertor", true, exceptionThrown);
+
+ // convert 500 SKK to CZK, the API shall say this is not possible
+ try {
+ c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
+ exceptionThrown = false;
+ } catch (InvalidCurrencyException e) {
+ exceptionThrown = true;
+ }
+ assertEquals("It should be impossible to convert from SKK with USD->CZK convertor", true, exceptionThrown);
+
+ }
+
+ /**
+ * Verify that the CZK to SKK convertor knows nothing about USD.
+ */
+ public void testCannotConvertToSKKwithSKKCZKConvertor() throws Exception {
+ Convertor c = createSKKtoCZK();
+ boolean exceptionThrown = false;
+
+ // convert $5 to SKK, the API shall say this is not possible
+ try {
+ c.convert(5, Currency.getInstance("USD"), Currency.getInstance("SKK"));
+ exceptionThrown = false;
+ } catch (InvalidCurrencyException e) {
+ exceptionThrown = true;
+ }
+ assertEquals("It should be impossible to convert form USD with SKK->CZK convertor", true, exceptionThrown);
+
+ // convert 500 CZK to USD, the API shall say this is not possible
+ try {
+ c.convert(500, Currency.getInstance("CZK"), Currency.getInstance("USD"));
+ exceptionThrown = false;
+ } catch (InvalidCurrencyException e) {
+ exceptionThrown = true;
+ }
+ assertEquals("It should be impossible to convert to USD with SKK->CZK convertor", true, exceptionThrown);
+ }
+}