task4/solution12/test/org/apidesign/apifest08/test/Task1Test.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 45 task3/solution12/test/org/apidesign/apifest08/test/Task1Test.java@251d0ed461fb
permissions -rw-r--r--
Copying structure for task4
japod@6
     1
package org.apidesign.apifest08.test;
japod@6
     2
japod@6
     3
import java.util.Currency;
japod@6
     4
japod@6
     5
import junit.framework.TestCase;
japod@6
     6
japod@6
     7
import org.apidesign.apifest08.currency.Convertor;
japod@20
     8
import org.apidesign.apifest08.currency.exceptions.InvalidCurrencyException;
japod@20
     9
import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
japod@6
    10
japod@6
    11
/**
japod@6
    12
 * Finish the Convertor API, and then write bodies of methods inside of this class to match the given tasks. To fullfil
japod@6
    13
 * your task, use the API define in the <code>org.apidesign.apifest08.currency</code> package. Do not you reflection, or
japod@6
    14
 * other hacks as your code shall run without any runtime permissions.
japod@6
    15
 */
japod@6
    16
public class Task1Test extends TestCase {
japod@6
    17
  public Task1Test(String testName) {
japod@6
    18
    super(testName);
japod@6
    19
  }
japod@6
    20
japod@6
    21
  @Override
japod@6
    22
  protected void setUp() throws Exception {
japod@6
    23
  }
japod@6
    24
japod@6
    25
  @Override
japod@6
    26
  protected void tearDown() throws Exception {
japod@6
    27
  }
japod@6
    28
japod@20
    29
  //
japod@20
    30
  // Imagine that there are three parts of the whole system:
japod@20
    31
  // 1. there is someone who knows the current exchange rate
japod@20
    32
  // 2. there is someone who wants to do the conversion
japod@20
    33
  // 3. there is the API between 1. and 2. which allows them to communicate
japod@20
    34
  // Please design such API
japod@20
    35
  //
japod@20
    36
japod@6
    37
  /**
japod@20
    38
   * Create convertor that understands two currencies, CZK and USD. Make 1 USD == 17 CZK. This is a method provided for
japod@20
    39
   * #1 group - e.g. those that know the exchange rate. They somehow need to create the objects from the API and tell
japod@20
    40
   * them the exchange rate. The API itself knows nothing about any rates, before the createCZKtoUSD method is called.
japod@20
    41
   * Creation of the convertor shall not require subclassing of any class or interface on the client side.
japod@6
    42
   * @return prepared convertor ready for converting USD to CZK and CZK to USD
japod@6
    43
   */
japod@6
    44
  public static Convertor createCZKtoUSD() {
japod@20
    45
    // set exchange rates
japod@20
    46
    Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), 17d, 1d);
japod@20
    47
japod@20
    48
    // create new instance
japod@20
    49
    Convertor convertor = null;
japod@20
    50
    try {
japod@20
    51
      convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("CZK"));
japod@20
    52
    } catch (UnknownConvertorException e) {
japod@20
    53
      e.printStackTrace();
japod@20
    54
    }
japod@20
    55
japod@20
    56
    return convertor;
japod@6
    57
  }
japod@6
    58
japod@6
    59
  /**
japod@20
    60
   * Create convertor that understands two currencies, CZK and SKK. Make 100 SKK == 80 CZK. Again this is method for the
japod@20
    61
   * #1 group - it knows the exchange rate, and needs to use the API to create objects with the exchange rate. Anyone
japod@20
    62
   * shall be ready to call this method without any other method being called previously. The API itself shall know
japod@20
    63
   * nothing about any rates, before this method is called. Creation of the convertor shall not require subclassing of
japod@20
    64
   * any class or interface on the client side.
japod@6
    65
   * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
japod@6
    66
   */
japod@6
    67
  public static Convertor createSKKtoCZK() {
japod@20
    68
    // set exchange rates
japod@20
    69
    Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 80d, 100d);
japod@20
    70
japod@20
    71
    // create new instance
japod@20
    72
    Convertor convertor = null;
japod@20
    73
    try {
japod@20
    74
      convertor = Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
japod@20
    75
    } catch (UnknownConvertorException e) {
japod@20
    76
      e.printStackTrace();
japod@20
    77
    }
japod@20
    78
japod@20
    79
    return convertor;
japod@6
    80
  }
japod@6
    81
japod@20
    82
  //
japod@20
    83
  // now the methods for group #2 follow:
japod@20
    84
  // this group knows nothing about exchange rates, but knows how to use
japod@20
    85
  // the API to do conversions. It somehow (by calling one of the factory
japod@20
    86
  // methods) gets objects from the API and uses them to do the conversions.
japod@20
    87
  //
japod@20
    88
japod@6
    89
  /**
japod@6
    90
   * Use the convertor from <code>createCZKtoUSD</code> method and do few conversions with it.
japod@6
    91
   */
japod@6
    92
  public void testCurrencyCZKUSD() throws Exception {
japod@6
    93
    Convertor c = createCZKtoUSD();
japod@6
    94
    // convert $5 to CZK using c:
japod@20
    95
    double result = c.convert(5, Currency.getInstance("USD"), Currency.getInstance("CZK"));
japod@6
    96
    assertEquals("Result is not 85 CZK", 85.0, result);
japod@6
    97
japod@6
    98
    // convert $8 to CZK
japod@20
    99
    result = c.convert(8, Currency.getInstance("USD"), Currency.getInstance("CZK"));
japod@6
   100
    assertEquals("Result is not 136 CZK", 136.0, result);
japod@6
   101
japod@6
   102
    // convert 1003CZK to USD
japod@20
   103
    result = c.convert(1003, Currency.getInstance("CZK"), Currency.getInstance("USD"));
japod@6
   104
    assertEquals("Result is not 59 USD", 59.0, result);
japod@6
   105
  }
japod@6
   106
japod@6
   107
  /**
japod@6
   108
   * Use the convertor from <code>createSKKtoCZK</code> method and do few conversions with it.
japod@6
   109
   */
japod@6
   110
  public void testCurrencySKKCZK() throws Exception {
japod@6
   111
    Convertor c = createSKKtoCZK();
japod@6
   112
japod@6
   113
    // convert 16CZK using c:
japod@20
   114
    double result = c.convert(16, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
japod@6
   115
    assertEquals("Result is not 20 SKK", 20.0, result);
japod@6
   116
japod@6
   117
    // convert 500SKK to CZK
japod@20
   118
    result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
japod@6
   119
    assertEquals("Result is not 400 CZK", 400.0, result);
japod@6
   120
  }
japod@20
   121
japod@20
   122
  /**
japod@20
   123
   * Verify that the CZK to USD convertor knows nothing about SKK.
japod@20
   124
   */
japod@20
   125
  public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
japod@20
   126
    Convertor c = createCZKtoUSD();
japod@20
   127
    boolean exceptionThrown = false;
japod@20
   128
japod@20
   129
    // convert $5 to SKK, the API shall say this is not possible
japod@20
   130
    try {
japod@20
   131
      c.convert(5, Currency.getInstance("USD"), Currency.getInstance("SKK"));
japod@20
   132
      exceptionThrown = false;
japod@20
   133
    } catch (InvalidCurrencyException e) {
japod@20
   134
      exceptionThrown = true;
japod@20
   135
    }
japod@20
   136
    assertEquals("It should be impossible to convert to SKK with USD->CZK convertor", true, exceptionThrown);
japod@20
   137
japod@20
   138
    // convert 500 SKK to CZK, the API shall say this is not possible
japod@20
   139
    try {
japod@20
   140
      c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
japod@20
   141
      exceptionThrown = false;
japod@20
   142
    } catch (InvalidCurrencyException e) {
japod@20
   143
      exceptionThrown = true;
japod@20
   144
    }
japod@20
   145
    assertEquals("It should be impossible to convert from SKK with USD->CZK convertor", true, exceptionThrown);
japod@20
   146
japod@20
   147
  }
japod@20
   148
japod@20
   149
  /**
japod@20
   150
   * Verify that the CZK to SKK convertor knows nothing about USD.
japod@20
   151
   */
japod@20
   152
  public void testCannotConvertToSKKwithSKKCZKConvertor() throws Exception {
japod@20
   153
    Convertor c = createSKKtoCZK();
japod@20
   154
    boolean exceptionThrown = false;
japod@20
   155
japod@20
   156
    // convert $5 to SKK, the API shall say this is not possible
japod@20
   157
    try {
japod@20
   158
      c.convert(5, Currency.getInstance("USD"), Currency.getInstance("SKK"));
japod@20
   159
      exceptionThrown = false;
japod@20
   160
    } catch (InvalidCurrencyException e) {
japod@20
   161
      exceptionThrown = true;
japod@20
   162
    }
japod@20
   163
    assertEquals("It should be impossible to convert form USD with SKK->CZK convertor", true, exceptionThrown);
japod@20
   164
japod@20
   165
    // convert 500 CZK to USD, the API shall say this is not possible
japod@20
   166
    try {
japod@20
   167
      c.convert(500, Currency.getInstance("CZK"), Currency.getInstance("USD"));
japod@20
   168
      exceptionThrown = false;
japod@20
   169
    } catch (InvalidCurrencyException e) {
japod@20
   170
      exceptionThrown = true;
japod@20
   171
    }
japod@20
   172
    assertEquals("It should be impossible to convert to USD with SKK->CZK convertor", true, exceptionThrown);
japod@20
   173
  }
japod@6
   174
}