japod@6: package org.apidesign.apifest08.test;
japod@6:
japod@6: import junit.framework.TestCase;
japod@6: import org.apidesign.apifest08.currency.Convertor;
japod@6: import org.apidesign.apifest08.currency.CurrencyValue;
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@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@19: //
japod@19: // Imagine that there are three parts of the whole system:
japod@19: // 1. there is someone who knows the current exchange rate
japod@19: // 2. there is someone who wants to do the conversion
japod@19: // 3. there is the API between 1. and 2. which allows them to communicate
japod@19: // Please design such API
japod@19: //
japod@19:
japod@6: /** Create convertor that understands two currencies, CZK and
japod@19: * USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
japod@19: * e.g. those that know the exchange rate. They somehow need to create
japod@19: * the objects from the API and tell them the exchange rate. The API itself
japod@19: * knows nothing about any rates, before the createCZKtoUSD method is called.
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@53: public static Convertor createCZKtoUSD() {
japod@53: return Convertor.getConvertorDoubleString(
japod@53: CurrencyValue.getCurrencyValue(1d, "USD"),
japod@53: CurrencyValue.getCurrencyValue(17d, "CZK")
japod@6: );
japod@6: }
japod@6:
japod@6: /** Create convertor that understands two currencies, CZK and
japod@19: * SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
japod@19: * it knows the exchange rate, and needs to use the API to create objects
japod@19: * with the exchange rate. Anyone shall be ready to call this method without
japod@19: * any other method being called previously. The API itself shall know
japod@19: * nothing about any rates, before this method is called.
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@53: public static Convertor createSKKtoCZK() {
japod@53: return Convertor.getConvertorDoubleString(
japod@53: CurrencyValue.getCurrencyValue(100d, "SKK"),
japod@53: CurrencyValue.getCurrencyValue(80d, "CZK")
japod@6: );
japod@6: }
japod@19:
japod@19: //
japod@19: // now the methods for group #2 follow:
japod@19: // this group knows nothing about exchange rates, but knows how to use
japod@19: // the API to do conversions. It somehow (by calling one of the factory
japod@19: // methods) gets objects from the API and uses them to do the conversions.
japod@19: //
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@53: Convertor c = createCZKtoUSD();
japod@6:
japod@53: CurrencyValue result;
japod@6:
japod@6: // convert $5 to CZK using c:
japod@6: // assertEquals("Result is 85 CZK");
japod@53: result = c.convert("CZK", CurrencyValue.getCurrencyValue(5d, "USD"));
japod@53: assertEquals(CurrencyValue.getCurrencyValue(85d, "CZK"), result);
japod@6:
japod@6: // convert $8 to CZK
japod@6: // assertEquals("Result is 136 CZK");
japod@53: result = c.convert("CZK", CurrencyValue.getCurrencyValue(8d, "USD"));
japod@53: assertEquals(CurrencyValue.getCurrencyValue(136d, "CZK"), result);
japod@6:
japod@6: // convert 1003CZK to USD
japod@6: // assertEquals("Result is 59 USD");
japod@53: result = c.convert("USD", CurrencyValue.getCurrencyValue(1003d, "CZK"));
japod@53: assertEquals(CurrencyValue.getCurrencyValue(59d, "USD"), 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@53: Convertor c = createSKKtoCZK();
japod@6:
japod@53: CurrencyValue result;
japod@6:
japod@6: // convert 16CZK using c:
japod@6: // assertEquals("Result is 20 SKK");
japod@53: result = c.convert("SKK", CurrencyValue.getCurrencyValue(16d, "CZK"));
japod@53: assertEquals(CurrencyValue.getCurrencyValue(20d, "SKK"), result);
japod@6:
japod@6: // convert 500SKK to CZK
japod@6: // assertEquals("Result is 400 CZK");
japod@53: result = c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"));
japod@53: assertEquals(CurrencyValue.getCurrencyValue(400d, "CZK"), result);
japod@6: }
japod@19:
japod@19: /** Verify that the CZK to USD convertor knows nothing about SKK.
japod@19: */
japod@19: public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
japod@53: Convertor c = createCZKtoUSD();
japod@19: try {
japod@19: // convert $5 to SKK, the API shall say this is not possible
japod@53: c.convert("SKK", CurrencyValue.getCurrencyValue(16d, "CZK"));
japod@53: fail("Should not convert");
japod@19: } catch (Exception e) {
japod@19: }
japod@19: try {
japod@19: // convert 500 SKK to CZK, the API shall say this is not possible
japod@53: c.convert("CZK", CurrencyValue.getCurrencyValue(500d, "SKK"));
japod@53: fail("Should not convert");
japod@19: } catch (Exception e) {
japod@19: }
japod@19:
japod@19: }
japod@19:
japod@19: /** Verify that the CZK to SKK convertor knows nothing about USD.
japod@19: */
japod@19: public void testCannotConvertToUSDwithSKKCZKConvertor() throws Exception {
japod@53: Convertor c = createSKKtoCZK();
japod@19: try {
japod@19: // convert $5 to SKK, the API shall say this is not possible
japod@53: c.convert("SKK", CurrencyValue.getCurrencyValue(5d, "USD"));
japod@53: fail("Should not convert");
japod@19: } catch (Exception e) {
japod@19: }
japod@19: try {
japod@19: // convert 500 CZK to USD, the API shall say this is not possible
japod@53: c.convert("USD", CurrencyValue.getCurrencyValue(500d, "CZK"));
japod@53: fail("Should not convert");
japod@19: } catch (Exception e) {
japod@19: }
japod@19: }
japod@6: }