1.1 --- a/task1/solution02/test/org/apidesign/apifest08/test/Task1Test.java Sun Sep 28 14:12:38 2008 +0200
1.2 +++ b/task1/solution02/test/org/apidesign/apifest08/test/Task1Test.java Tue Sep 30 11:47:02 2008 +0200
1.3 @@ -15,9 +15,11 @@
1.4 * shall run without any runtime permissions.
1.5 */
1.6 public class Task1Test extends TestCase {
1.7 +
1.8 public static final Currency USD = Currency.getInstance("USD");
1.9 public static final Currency CZK = Currency.getInstance("CZK");
1.10 public static final Currency SKK = Currency.getInstance("SKK");
1.11 +
1.12 public Task1Test(String testName) {
1.13 super(testName);
1.14 }
1.15 @@ -30,8 +32,19 @@
1.16 protected void tearDown() throws Exception {
1.17 }
1.18
1.19 + //
1.20 + // Imagine that there are three parts of the whole system:
1.21 + // 1. there is someone who knows the current exchange rate
1.22 + // 2. there is someone who wants to do the conversion
1.23 + // 3. there is the API between 1. and 2. which allows them to communicate
1.24 + // Please design such API
1.25 + //
1.26 +
1.27 /** Create convertor that understands two currencies, CZK and
1.28 - * USD. Make 1 USD == 17 CZK.
1.29 + * USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
1.30 + * e.g. those that know the exchange rate. They somehow need to create
1.31 + * the objects from the API and tell them the exchange rate. The API itself
1.32 + * knows nothing about any rates, before the createCZKtoUSD method is called.
1.33 *
1.34 * Creation of the convertor shall not require subclassing of any class
1.35 * or interface on the client side.
1.36 @@ -39,14 +52,15 @@
1.37 * @return prepared convertor ready for converting USD to CZK and CZK to USD
1.38 */
1.39 public static Convertor createCZKtoUSD() {
1.40 - //You can use both variants. I wrote the first one but than I realized that maybe
1.41 - //you want me to write the second one. So I have written both.
1.42 - return ConvertorFactory.createConvertor(CZK, USD);
1.43 - //return ConvertorFactory.createConvertor(new MoneyImpl(17,CZK), new MoneyImpl(1,USD));
1.44 + return ConvertorFactory.createConvertor(new MoneyImpl(17,CZK), new MoneyImpl(1,USD));
1.45 }
1.46
1.47 /** Create convertor that understands two currencies, CZK and
1.48 - * SKK. Make 100 SKK == 80 CZK.
1.49 + * SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
1.50 + * it knows the exchange rate, and needs to use the API to create objects
1.51 + * with the exchange rate. Anyone shall be ready to call this method without
1.52 + * any other method being called previously. The API itself shall know
1.53 + * nothing about any rates, before this method is called.
1.54 *
1.55 * Creation of the convertor shall not require subclassing of any class
1.56 * or interface on the client side.
1.57 @@ -54,38 +68,93 @@
1.58 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
1.59 */
1.60 public static Convertor createSKKtoCZK() {
1.61 - //You can use both variants. I wrote the first one but than I realized that maybe
1.62 - //you want me to write the second one. So I have written both.
1.63 - //return ConvertorFactory.createConvertor(SKK, CZK);
1.64 - return ConvertorFactory.createConvertor(new MoneyImpl(100,SKK), new MoneyImpl(80,CZK));
1.65 + return ConvertorFactory.createConvertor(new MoneyImpl(100,SKK), new MoneyImpl(80,CZK));
1.66 }
1.67 +
1.68 + //
1.69 + // now the methods for group #2 follow:
1.70 + // this group knows nothing about exchange rates, but knows how to use
1.71 + // the API to do conversions. It somehow (by calling one of the factory
1.72 + // methods) gets objects from the API and uses them to do the conversions.
1.73 + //
1.74
1.75 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
1.76 * with it.
1.77 */
1.78 public void testCurrencyCZKUSD() throws Exception {
1.79 - Convertor czkToUsdConvertor = createCZKtoUSD();
1.80 + Convertor c = createCZKtoUSD();
1.81 // convert $5 to CZK using c:
1.82 - Convertor usdToCzkConvertor = czkToUsdConvertor.revert();
1.83 - assertEquals("Result is 85 CZK",new MoneyImpl(85,CZK), usdToCzkConvertor.convert(new MoneyImpl(5,USD)));
1.84 + assertEquals("Result is 85 CZK",new MoneyImpl(85,CZK), c.convert(new MoneyImpl(5,USD),CZK));
1.85
1.86 // convert $8 to CZK
1.87 - assertEquals("Result is 136 CZK",new MoneyImpl(136,CZK), usdToCzkConvertor.convert(new MoneyImpl(8,USD)));
1.88 + assertEquals("Result is 136 CZK",new MoneyImpl(136,CZK), c.convert(new MoneyImpl(8,USD),CZK));
1.89
1.90 // convert 1003CZK to USD
1.91 - assertEquals("Result is 59 USD", new MoneyImpl(59,USD), czkToUsdConvertor.convert(new MoneyImpl(1003,CZK)));
1.92 + assertEquals("Result is 59 USD", new MoneyImpl(59,USD), c.convert(new MoneyImpl(1003,CZK),USD));
1.93 +
1.94 }
1.95
1.96 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
1.97 * with it.
1.98 */
1.99 public void testCurrencySKKCZK() throws Exception {
1.100 - Convertor skkToCzkConvertor = createSKKtoCZK();
1.101 + Convertor c = createSKKtoCZK();
1.102 // convert 16CZK using c:
1.103 - assertEquals("Result is 20 SKK", new MoneyImpl(20,SKK), skkToCzkConvertor.revert().convert(new MoneyImpl(16,CZK)));
1.104 + assertEquals("Result is 20 SKK", new MoneyImpl(20,SKK), c.convert(new MoneyImpl(16,CZK),SKK));
1.105
1.106 // convert 500SKK to CZK
1.107 - assertEquals("Result is 400 CZK", new MoneyImpl(400,CZK), skkToCzkConvertor.convert(new MoneyImpl(500,SKK)));
1.108 + assertEquals("Result is 400 CZK", new MoneyImpl(400,CZK), c.convert(new MoneyImpl(500,SKK),CZK));
1.109 + }
1.110 +
1.111 + /** Verify that the CZK to USD convertor knows nothing about SKK.
1.112 + */
1.113 + public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
1.114 + Convertor c = createCZKtoUSD();
1.115 + // convert $5 to SKK, the API shall say this is not possible
1.116 + try
1.117 + {
1.118 + c.convert(new MoneyImpl(5, USD), SKK);
1.119 + fail("Exception expected");
1.120 + }
1.121 + catch(IllegalArgumentException e)
1.122 + {
1.123 + assertTrue("Ok",true);
1.124 + }
1.125 + // convert 500 SKK to CZK, the API shall say this is not possible
1.126 + try
1.127 + {
1.128 + c.convert(new MoneyImpl(500, SKK), CZK);
1.129 + fail("Exception expected");
1.130 + }
1.131 + catch(IllegalArgumentException e)
1.132 + {
1.133 + assertTrue("Ok",true);
1.134 + }
1.135 + }
1.136 +
1.137 + /** Verify that the CZK to SKK convertor knows nothing about USD.
1.138 + */
1.139 + public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception {
1.140 + Convertor c = createSKKtoCZK();
1.141 + // convert $5 to SKK, the API shall say this is not possible
1.142 + try
1.143 + {
1.144 + c.convert(new MoneyImpl(5, USD), SKK);
1.145 + fail("Exception expected");
1.146 + }
1.147 + catch(IllegalArgumentException e)
1.148 + {
1.149 + assertTrue("Ok",true);
1.150 + }
1.151 + // convert 500 CZK to USD, the API shall say this is not possible
1.152 + try
1.153 + {
1.154 + c.convert(new MoneyImpl(500, CZK), USD);
1.155 + fail("Exception expected");
1.156 + }
1.157 + catch(IllegalArgumentException e)
1.158 + {
1.159 + assertTrue("Ok",true);
1.160 + }
1.161 }
1.162 }
1.163 -