1.1 --- a/task1/solution11/test/org/apidesign/apifest08/test/Task1Test.java Sun Sep 28 14:12:38 2008 +0200
1.2 +++ b/task1/solution11/test/org/apidesign/apifest08/test/Task1Test.java Tue Sep 30 12:01:18 2008 +0200
1.3 @@ -23,8 +23,19 @@
1.4 protected void tearDown() throws Exception {
1.5 }
1.6
1.7 + //
1.8 + // Imagine that there are three parts of the whole system:
1.9 + // 1. there is someone who knows the current exchange rate
1.10 + // 2. there is someone who wants to do the conversion
1.11 + // 3. there is the API between 1. and 2. which allows them to communicate
1.12 + // Please design such API
1.13 + //
1.14 +
1.15 /** Create convertor that understands two currencies, CZK and
1.16 - * USD. Make 1 USD == 17 CZK.
1.17 + * USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
1.18 + * e.g. those that know the exchange rate. They somehow need to create
1.19 + * the objects from the API and tell them the exchange rate. The API itself
1.20 + * knows nothing about any rates, before the createCZKtoUSD method is called.
1.21 *
1.22 * Creation of the convertor shall not require subclassing of any class
1.23 * or interface on the client side.
1.24 @@ -39,7 +50,11 @@
1.25 }
1.26
1.27 /** Create convertor that understands two currencies, CZK and
1.28 - * SKK. Make 100 SKK == 80 CZK.
1.29 + * SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
1.30 + * it knows the exchange rate, and needs to use the API to create objects
1.31 + * with the exchange rate. Anyone shall be ready to call this method without
1.32 + * any other method being called previously. The API itself shall know
1.33 + * nothing about any rates, before this method is called.
1.34 *
1.35 * Creation of the convertor shall not require subclassing of any class
1.36 * or interface on the client side.
1.37 @@ -52,6 +67,13 @@
1.38 CurrencyValue.getCurrencyValue(80, "CZK")
1.39 );
1.40 }
1.41 +
1.42 + //
1.43 + // now the methods for group #2 follow:
1.44 + // this group knows nothing about exchange rates, but knows how to use
1.45 + // the API to do conversions. It somehow (by calling one of the factory
1.46 + // methods) gets objects from the API and uses them to do the conversions.
1.47 + //
1.48
1.49 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
1.50 * with it.
1.51 @@ -63,17 +85,17 @@
1.52
1.53 // convert $5 to CZK using c:
1.54 // assertEquals("Result is 85 CZK");
1.55 - result = c.convert(CurrencyValue.getCurrencyValue(5, "USD"));
1.56 + result = c.convert("CZK", CurrencyValue.getCurrencyValue(5, "USD"));
1.57 assertEquals(CurrencyValue.getCurrencyValue(85, "CZK"), result);
1.58
1.59 // convert $8 to CZK
1.60 // assertEquals("Result is 136 CZK");
1.61 - result = c.convert(CurrencyValue.getCurrencyValue(8, "USD"));
1.62 + result = c.convert("CZK", CurrencyValue.getCurrencyValue(8, "USD"));
1.63 assertEquals(CurrencyValue.getCurrencyValue(136, "CZK"), result);
1.64
1.65 // convert 1003CZK to USD
1.66 // assertEquals("Result is 59 USD");
1.67 - result = c.convert(CurrencyValue.getCurrencyValue(1003, "CZK"));
1.68 + result = c.convert("USD", CurrencyValue.getCurrencyValue(1003, "CZK"));
1.69 assertEquals(CurrencyValue.getCurrencyValue(59, "USD"), result);
1.70 }
1.71
1.72 @@ -87,13 +109,53 @@
1.73
1.74 // convert 16CZK using c:
1.75 // assertEquals("Result is 20 SKK");
1.76 - result = c.convert(CurrencyValue.getCurrencyValue(16, "CZK"));
1.77 + result = c.convert("SKK", CurrencyValue.getCurrencyValue(16, "CZK"));
1.78 assertEquals(CurrencyValue.getCurrencyValue(20, "SKK"), result);
1.79
1.80 // convert 500SKK to CZK
1.81 // assertEquals("Result is 400 CZK");
1.82 - result = c.convert(CurrencyValue.getCurrencyValue(500, "SKK"));
1.83 + result = c.convert("CZK", CurrencyValue.getCurrencyValue(500, "SKK"));
1.84 assertEquals(CurrencyValue.getCurrencyValue(400, "CZK"), result);
1.85 }
1.86 +
1.87 + /** Verify that the CZK to USD convertor knows nothing about SKK.
1.88 + */
1.89 + public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
1.90 + Convertor<Integer, String> c = createCZKtoUSD();
1.91 + try {
1.92 + // convert $5 to SKK, the API shall say this is not possible
1.93 + c.convert("SKK", CurrencyValue.getCurrencyValue(16, "CZK"));
1.94 + assertTrue("Should not convert", false);
1.95 + } catch (Exception e) {
1.96 + assertTrue(true);
1.97 + }
1.98 + try {
1.99 + // convert 500 SKK to CZK, the API shall say this is not possible
1.100 + c.convert("CZK", CurrencyValue.getCurrencyValue(500, "SKK"));
1.101 + assertTrue("Should not convert", false);
1.102 + } catch (Exception e) {
1.103 + assertTrue(true);
1.104 + }
1.105 +
1.106 + }
1.107 +
1.108 + /** Verify that the CZK to SKK convertor knows nothing about USD.
1.109 + */
1.110 + public void testCannotConvertToUSDwithSKKCZKConvertor() throws Exception {
1.111 + Convertor<Integer, String> c = createSKKtoCZK();
1.112 + try {
1.113 + // convert $5 to SKK, the API shall say this is not possible
1.114 + c.convert("SKK", CurrencyValue.getCurrencyValue(5, "USD"));
1.115 + assertTrue("Should not convert", false);
1.116 + } catch (Exception e) {
1.117 + assertTrue(true);
1.118 + }
1.119 + try {
1.120 + // convert 500 CZK to USD, the API shall say this is not possible
1.121 + c.convert("USD", CurrencyValue.getCurrencyValue(500, "CZK"));
1.122 + assertTrue("Should not convert", false);
1.123 + } catch (Exception e) {
1.124 + assertTrue(true);
1.125 + }
1.126 + }
1.127 }
1.128 -