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