1.1 --- a/task1/solution07/src/org/apidesign/apifest08/currency/Convertor.java Tue Sep 30 11:50:09 2008 +0200
1.2 +++ b/task1/solution07/src/org/apidesign/apifest08/currency/Convertor.java Tue Sep 30 11:59:32 2008 +0200
1.3 @@ -20,7 +20,7 @@
1.4 * When the need comes to extend the semantics, one subclasses the ConversionRequest and/or ConversionResult classes.
1.5 * <p>
1.6 * This method can be called as many times as you like.
1.7 - * A {@link Convertor} shall be considered immutable.
1.8 + * A {@link Convertor} shall be considered immutable wrt calls to {@link #convert(org.apidesign.apifest08.currency.Convertor.ConversionRequest).
1.9 * This method of a single {@link Convertor} can be called from many threads concurrently.
1.10 * @param req the conversion request; mustn't be null
1.11 * @return the result of carrying out the conversion request; never null
2.1 --- a/task1/solution07/test/org/apidesign/apifest08/test/Task1Test.java Tue Sep 30 11:50:09 2008 +0200
2.2 +++ b/task1/solution07/test/org/apidesign/apifest08/test/Task1Test.java Tue Sep 30 11:59:32 2008 +0200
2.3 @@ -26,12 +26,24 @@
2.4 protected void tearDown() throws Exception {
2.5 }
2.6
2.7 + //
2.8 + // Imagine that there are three parts of the whole system:
2.9 + // 1. there is someone who knows the current exchange rate
2.10 + // 2. there is someone who wants to do the conversion
2.11 + // 3. there is the API between 1. and 2. which allows them to communicate
2.12 + // Please design such API
2.13 + //
2.14 +
2.15 protected static final Currency CZK = Currency.getInstance( "CZK" );
2.16 protected static final Currency SKK = Currency.getInstance( "SKK" );
2.17 protected static final Currency USD = Currency.getInstance( "USD" );
2.18
2.19 /** Create convertor that understands two currencies, CZK and
2.20 * USD. Make 1 USD == 17 CZK.
2.21 + * USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
2.22 + * e.g. those that know the exchange rate. They somehow need to create
2.23 + * the objects from the API and tell them the exchange rate. The API itself
2.24 + * knows nothing about any rates, before the createCZKtoUSD method is called.
2.25 *
2.26 * Creation of the convertor shall not require subclassing of any class
2.27 * or interface on the client side.
2.28 @@ -48,7 +60,11 @@
2.29 }
2.30
2.31 /** Create convertor that understands two currencies, CZK and
2.32 - * SKK. Make 100 SKK == 80 CZK.
2.33 + * SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
2.34 + * it knows the exchange rate, and needs to use the API to create objects
2.35 + * with the exchange rate. Anyone shall be ready to call this method without
2.36 + * any other method being called previously. The API itself shall know
2.37 + * nothing about any rates, before this method is called.
2.38 *
2.39 * Creation of the convertor shall not require subclassing of any class
2.40 * or interface on the client side.
2.41 @@ -64,6 +80,13 @@
2.42 return new ContractImposingDelegatingConvertor( convertor ).test();
2.43 }
2.44
2.45 + //
2.46 + // now the methods for group #2 follow:
2.47 + // this group knows nothing about exchange rates, but knows how to use
2.48 + // the API to do conversions. It somehow (by calling one of the factory
2.49 + // methods) gets objects from the API and uses them to do the conversions.
2.50 + //
2.51 +
2.52 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
2.53 * with it.
2.54 */
2.55 @@ -118,5 +141,37 @@
2.56 assertEquals( CZK, a2.getCurrency() );
2.57 }
2.58
2.59 + /** Verify that the CZK to USD convertor knows nothing about SKK.
2.60 + */
2.61 + public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
2.62 + final Convertor c = createCZKtoUSD();
2.63 +
2.64 + // convert $5 to SKK, the API shall say this is not possible
2.65 + final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), SKK ) );
2.66 + final MonetaryAmount a1 = r1.getNetAmount();
2.67 + assertNull( a1 );
2.68 +
2.69 + // convert 500 SKK to CZK, the API shall say this is not possible
2.70 + final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, SKK ), CZK ) );
2.71 + final MonetaryAmount a2 = r2.getNetAmount();
2.72 + assertNull( a2 );
2.73 + }
2.74 +
2.75 + /** Verify that the CZK to SKK convertor knows nothing about USD.
2.76 + */
2.77 + public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception {
2.78 + final Convertor c = createSKKtoCZK();
2.79 +
2.80 + // convert $5 to SKK, the API shall say this is not possible
2.81 + final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), SKK ) );
2.82 + final MonetaryAmount a1 = r1.getNetAmount();
2.83 + assertNull( a1 );
2.84 +
2.85 + // convert 500 CZK to USD, the API shall say this is not possible
2.86 + final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, CZK ), USD ) );
2.87 + final MonetaryAmount a2 = r2.getNetAmount();
2.88 + assertNull( a2 );
2.89 + }
2.90 +
2.91 }
2.92