task1/solution12/test/org/apidesign/apifest08/test/Task1Test.java
changeset 20 7ca97f802b5a
parent 6 97662396c0fd
     1.1 --- a/task1/solution12/test/org/apidesign/apifest08/test/Task1Test.java	Sun Sep 28 14:12:38 2008 +0200
     1.2 +++ b/task1/solution12/test/org/apidesign/apifest08/test/Task1Test.java	Tue Sep 30 12:04:26 2008 +0200
     1.3 @@ -5,6 +5,8 @@
     1.4  import junit.framework.TestCase;
     1.5  
     1.6  import org.apidesign.apifest08.currency.Convertor;
     1.7 +import org.apidesign.apifest08.currency.exceptions.InvalidCurrencyException;
     1.8 +import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
     1.9  
    1.10  /**
    1.11   * Finish the Convertor API, and then write bodies of methods inside of this class to match the given tasks. To fullfil
    1.12 @@ -24,40 +26,81 @@
    1.13    protected void tearDown() throws Exception {
    1.14    }
    1.15  
    1.16 +  //
    1.17 +  // Imagine that there are three parts of the whole system:
    1.18 +  // 1. there is someone who knows the current exchange rate
    1.19 +  // 2. there is someone who wants to do the conversion
    1.20 +  // 3. there is the API between 1. and 2. which allows them to communicate
    1.21 +  // Please design such API
    1.22 +  //
    1.23 +
    1.24    /**
    1.25 -   * Create convertor that understands two currencies, CZK and USD. Make 1 USD == 17 CZK. Creation of the convertor
    1.26 -   * shall not require subclassing of any class or interface on the client side.
    1.27 +   * Create convertor that understands two currencies, CZK and USD. Make 1 USD == 17 CZK. This is a method provided for
    1.28 +   * #1 group - e.g. those that know the exchange rate. They somehow need to create the objects from the API and tell
    1.29 +   * them the exchange rate. The API itself knows nothing about any rates, before the createCZKtoUSD method is called.
    1.30 +   * Creation of the convertor shall not require subclassing of any class or interface on the client side.
    1.31     * @return prepared convertor ready for converting USD to CZK and CZK to USD
    1.32     */
    1.33    public static Convertor createCZKtoUSD() {
    1.34 -    return Convertor.getConvertorInstance(Currency.getInstance("CZK"), Currency.getInstance("USD"));
    1.35 +    // set exchange rates
    1.36 +    Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), 17d, 1d);
    1.37 +
    1.38 +    // create new instance
    1.39 +    Convertor convertor = null;
    1.40 +    try {
    1.41 +      convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("CZK"));
    1.42 +    } catch (UnknownConvertorException e) {
    1.43 +      e.printStackTrace();
    1.44 +    }
    1.45 +
    1.46 +    return convertor;
    1.47    }
    1.48  
    1.49    /**
    1.50 -   * Create convertor that understands two currencies, CZK and SKK. Make 100 SKK == 80 CZK. Creation of the convertor
    1.51 -   * shall not require subclassing of any class or interface on the client side.
    1.52 +   * Create convertor that understands two currencies, CZK and SKK. Make 100 SKK == 80 CZK. Again this is method for the
    1.53 +   * #1 group - it knows the exchange rate, and needs to use the API to create objects with the exchange rate. Anyone
    1.54 +   * shall be ready to call this method without any other method being called previously. The API itself shall know
    1.55 +   * nothing about any rates, before this method is called. Creation of the convertor shall not require subclassing of
    1.56 +   * any class or interface on the client side.
    1.57     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    1.58     */
    1.59    public static Convertor createSKKtoCZK() {
    1.60 -    return Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
    1.61 +    // set exchange rates
    1.62 +    Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 80d, 100d);
    1.63 +
    1.64 +    // create new instance
    1.65 +    Convertor convertor = null;
    1.66 +    try {
    1.67 +      convertor = Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
    1.68 +    } catch (UnknownConvertorException e) {
    1.69 +      e.printStackTrace();
    1.70 +    }
    1.71 +
    1.72 +    return convertor;
    1.73    }
    1.74  
    1.75 +  //
    1.76 +  // now the methods for group #2 follow:
    1.77 +  // this group knows nothing about exchange rates, but knows how to use
    1.78 +  // the API to do conversions. It somehow (by calling one of the factory
    1.79 +  // methods) gets objects from the API and uses them to do the conversions.
    1.80 +  //
    1.81 +
    1.82    /**
    1.83     * Use the convertor from <code>createCZKtoUSD</code> method and do few conversions with it.
    1.84     */
    1.85    public void testCurrencyCZKUSD() throws Exception {
    1.86      Convertor c = createCZKtoUSD();
    1.87 -
    1.88      // convert $5 to CZK using c:
    1.89 -    double result = c.convert(5, Currency.getInstance("USD"));
    1.90 +    double result = c.convert(5, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    1.91      assertEquals("Result is not 85 CZK", 85.0, result);
    1.92  
    1.93      // convert $8 to CZK
    1.94 -    result = c.convert(8, Currency.getInstance("USD"));
    1.95 +    result = c.convert(8, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    1.96      assertEquals("Result is not 136 CZK", 136.0, result);
    1.97  
    1.98      // convert 1003CZK to USD
    1.99 -    result = c.convert(1003, Currency.getInstance("CZK"));
   1.100 +    result = c.convert(1003, Currency.getInstance("CZK"), Currency.getInstance("USD"));
   1.101      assertEquals("Result is not 59 USD", 59.0, result);
   1.102    }
   1.103  
   1.104 @@ -68,11 +111,64 @@
   1.105      Convertor c = createSKKtoCZK();
   1.106  
   1.107      // convert 16CZK using c:
   1.108 -    double result = c.convert(16, Currency.getInstance("CZK"));
   1.109 +    double result = c.convert(16, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
   1.110      assertEquals("Result is not 20 SKK", 20.0, result);
   1.111  
   1.112      // convert 500SKK to CZK
   1.113 -    result = c.convert(500, Currency.getInstance("SKK"));
   1.114 +    result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   1.115      assertEquals("Result is not 400 CZK", 400.0, result);
   1.116    }
   1.117 +
   1.118 +  /**
   1.119 +   * Verify that the CZK to USD convertor knows nothing about SKK.
   1.120 +   */
   1.121 +  public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
   1.122 +    Convertor c = createCZKtoUSD();
   1.123 +    boolean exceptionThrown = false;
   1.124 +
   1.125 +    // convert $5 to SKK, the API shall say this is not possible
   1.126 +    try {
   1.127 +      c.convert(5, Currency.getInstance("USD"), Currency.getInstance("SKK"));
   1.128 +      exceptionThrown = false;
   1.129 +    } catch (InvalidCurrencyException e) {
   1.130 +      exceptionThrown = true;
   1.131 +    }
   1.132 +    assertEquals("It should be impossible to convert to SKK with USD->CZK convertor", true, exceptionThrown);
   1.133 +
   1.134 +    // convert 500 SKK to CZK, the API shall say this is not possible
   1.135 +    try {
   1.136 +      c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   1.137 +      exceptionThrown = false;
   1.138 +    } catch (InvalidCurrencyException e) {
   1.139 +      exceptionThrown = true;
   1.140 +    }
   1.141 +    assertEquals("It should be impossible to convert from SKK with USD->CZK convertor", true, exceptionThrown);
   1.142 +
   1.143 +  }
   1.144 +
   1.145 +  /**
   1.146 +   * Verify that the CZK to SKK convertor knows nothing about USD.
   1.147 +   */
   1.148 +  public void testCannotConvertToSKKwithSKKCZKConvertor() throws Exception {
   1.149 +    Convertor c = createSKKtoCZK();
   1.150 +    boolean exceptionThrown = false;
   1.151 +
   1.152 +    // convert $5 to SKK, the API shall say this is not possible
   1.153 +    try {
   1.154 +      c.convert(5, Currency.getInstance("USD"), Currency.getInstance("SKK"));
   1.155 +      exceptionThrown = false;
   1.156 +    } catch (InvalidCurrencyException e) {
   1.157 +      exceptionThrown = true;
   1.158 +    }
   1.159 +    assertEquals("It should be impossible to convert form USD with SKK->CZK convertor", true, exceptionThrown);
   1.160 +
   1.161 +    // convert 500 CZK to USD, the API shall say this is not possible
   1.162 +    try {
   1.163 +      c.convert(500, Currency.getInstance("CZK"), Currency.getInstance("USD"));
   1.164 +      exceptionThrown = false;
   1.165 +    } catch (InvalidCurrencyException e) {
   1.166 +      exceptionThrown = true;
   1.167 +    }
   1.168 +    assertEquals("It should be impossible to convert to USD with SKK->CZK convertor", true, exceptionThrown);
   1.169 +  }
   1.170  }