1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task2/solution11/test/org/apidesign/apifest08/test/Task1Test.java Wed Oct 01 10:43:05 2008 +0200
1.3 @@ -0,0 +1,161 @@
1.4 +package org.apidesign.apifest08.test;
1.5 +
1.6 +import junit.framework.TestCase;
1.7 +import org.apidesign.apifest08.currency.Convertor;
1.8 +import org.apidesign.apifest08.currency.CurrencyValue;
1.9 +
1.10 +/** Finish the Convertor API, and then write bodies of methods inside
1.11 + * of this class to match the given tasks. To fullfil your task, use the
1.12 + * API define in the <code>org.apidesign.apifest08.currency</code> package.
1.13 + * Do not you reflection, or other hacks as your code
1.14 + * shall run without any runtime permissions.
1.15 + */
1.16 +public class Task1Test extends TestCase {
1.17 + public Task1Test(String testName) {
1.18 + super(testName);
1.19 + }
1.20 +
1.21 + @Override
1.22 + protected void setUp() throws Exception {
1.23 + }
1.24 +
1.25 + @Override
1.26 + protected void tearDown() throws Exception {
1.27 + }
1.28 +
1.29 + //
1.30 + // Imagine that there are three parts of the whole system:
1.31 + // 1. there is someone who knows the current exchange rate
1.32 + // 2. there is someone who wants to do the conversion
1.33 + // 3. there is the API between 1. and 2. which allows them to communicate
1.34 + // Please design such API
1.35 + //
1.36 +
1.37 + /** Create convertor that understands two currencies, CZK and
1.38 + * USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
1.39 + * e.g. those that know the exchange rate. They somehow need to create
1.40 + * the objects from the API and tell them the exchange rate. The API itself
1.41 + * knows nothing about any rates, before the createCZKtoUSD method is called.
1.42 + *
1.43 + * Creation of the convertor shall not require subclassing of any class
1.44 + * or interface on the client side.
1.45 + *
1.46 + * @return prepared convertor ready for converting USD to CZK and CZK to USD
1.47 + */
1.48 + public static Convertor<Integer, String> createCZKtoUSD() {
1.49 + return Convertor.getConvertorIntegerString(
1.50 + CurrencyValue.getCurrencyValue(1, "USD"),
1.51 + CurrencyValue.getCurrencyValue(17, "CZK")
1.52 + );
1.53 + }
1.54 +
1.55 + /** Create convertor that understands two currencies, CZK and
1.56 + * SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
1.57 + * it knows the exchange rate, and needs to use the API to create objects
1.58 + * with the exchange rate. Anyone shall be ready to call this method without
1.59 + * any other method being called previously. The API itself shall know
1.60 + * nothing about any rates, before this method is called.
1.61 + *
1.62 + * Creation of the convertor shall not require subclassing of any class
1.63 + * or interface on the client side.
1.64 + *
1.65 + * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
1.66 + */
1.67 + public static Convertor<Integer, String> createSKKtoCZK() {
1.68 + return Convertor.getConvertorIntegerString(
1.69 + CurrencyValue.getCurrencyValue(100, "SKK"),
1.70 + CurrencyValue.getCurrencyValue(80, "CZK")
1.71 + );
1.72 + }
1.73 +
1.74 + //
1.75 + // now the methods for group #2 follow:
1.76 + // this group knows nothing about exchange rates, but knows how to use
1.77 + // the API to do conversions. It somehow (by calling one of the factory
1.78 + // methods) gets objects from the API and uses them to do the conversions.
1.79 + //
1.80 +
1.81 + /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
1.82 + * with it.
1.83 + */
1.84 + public void testCurrencyCZKUSD() throws Exception {
1.85 + Convertor<Integer, String> c = createCZKtoUSD();
1.86 +
1.87 + CurrencyValue<Integer, String> result;
1.88 +
1.89 + // convert $5 to CZK using c:
1.90 + // assertEquals("Result is 85 CZK");
1.91 + result = c.convert("CZK", CurrencyValue.getCurrencyValue(5, "USD"));
1.92 + assertEquals(CurrencyValue.getCurrencyValue(85, "CZK"), result);
1.93 +
1.94 + // convert $8 to CZK
1.95 + // assertEquals("Result is 136 CZK");
1.96 + result = c.convert("CZK", CurrencyValue.getCurrencyValue(8, "USD"));
1.97 + assertEquals(CurrencyValue.getCurrencyValue(136, "CZK"), result);
1.98 +
1.99 + // convert 1003CZK to USD
1.100 + // assertEquals("Result is 59 USD");
1.101 + result = c.convert("USD", CurrencyValue.getCurrencyValue(1003, "CZK"));
1.102 + assertEquals(CurrencyValue.getCurrencyValue(59, "USD"), result);
1.103 + }
1.104 +
1.105 + /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
1.106 + * with it.
1.107 + */
1.108 + public void testCurrencySKKCZK() throws Exception {
1.109 + Convertor<Integer, String> c = createSKKtoCZK();
1.110 +
1.111 + CurrencyValue<Integer, String> result;
1.112 +
1.113 + // convert 16CZK using c:
1.114 + // assertEquals("Result is 20 SKK");
1.115 + result = c.convert("SKK", CurrencyValue.getCurrencyValue(16, "CZK"));
1.116 + assertEquals(CurrencyValue.getCurrencyValue(20, "SKK"), result);
1.117 +
1.118 + // convert 500SKK to CZK
1.119 + // assertEquals("Result is 400 CZK");
1.120 + result = c.convert("CZK", CurrencyValue.getCurrencyValue(500, "SKK"));
1.121 + assertEquals(CurrencyValue.getCurrencyValue(400, "CZK"), result);
1.122 + }
1.123 +
1.124 + /** Verify that the CZK to USD convertor knows nothing about SKK.
1.125 + */
1.126 + public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
1.127 + Convertor<Integer, String> c = createCZKtoUSD();
1.128 + try {
1.129 + // convert $5 to SKK, the API shall say this is not possible
1.130 + c.convert("SKK", CurrencyValue.getCurrencyValue(16, "CZK"));
1.131 + assertTrue("Should not convert", false);
1.132 + } catch (Exception e) {
1.133 + assertTrue(true);
1.134 + }
1.135 + try {
1.136 + // convert 500 SKK to CZK, the API shall say this is not possible
1.137 + c.convert("CZK", CurrencyValue.getCurrencyValue(500, "SKK"));
1.138 + assertTrue("Should not convert", false);
1.139 + } catch (Exception e) {
1.140 + assertTrue(true);
1.141 + }
1.142 +
1.143 + }
1.144 +
1.145 + /** Verify that the CZK to SKK convertor knows nothing about USD.
1.146 + */
1.147 + public void testCannotConvertToUSDwithSKKCZKConvertor() throws Exception {
1.148 + Convertor<Integer, String> c = createSKKtoCZK();
1.149 + try {
1.150 + // convert $5 to SKK, the API shall say this is not possible
1.151 + c.convert("SKK", CurrencyValue.getCurrencyValue(5, "USD"));
1.152 + assertTrue("Should not convert", false);
1.153 + } catch (Exception e) {
1.154 + assertTrue(true);
1.155 + }
1.156 + try {
1.157 + // convert 500 CZK to USD, the API shall say this is not possible
1.158 + c.convert("USD", CurrencyValue.getCurrencyValue(500, "CZK"));
1.159 + assertTrue("Should not convert", false);
1.160 + } catch (Exception e) {
1.161 + assertTrue(true);
1.162 + }
1.163 + }
1.164 +}