task1/solution07/test/org/apidesign/apifest08/test/Task1Test.java
changeset 18 83e731257bdc
parent 6 97662396c0fd
     1.1 --- a/task1/solution07/test/org/apidesign/apifest08/test/Task1Test.java	Sun Sep 28 14:12:38 2008 +0200
     1.2 +++ b/task1/solution07/test/org/apidesign/apifest08/test/Task1Test.java	Tue Sep 30 11:59:32 2008 +0200
     1.3 @@ -26,12 +26,24 @@
     1.4      protected void tearDown() throws Exception {
     1.5      }
     1.6  
     1.7 +    //
     1.8 +    // Imagine that there are three parts of the whole system:
     1.9 +    // 1. there is someone who knows the current exchange rate
    1.10 +    // 2. there is someone who wants to do the conversion
    1.11 +    // 3. there is the API between 1. and 2. which allows them to communicate
    1.12 +    // Please design such API
    1.13 +    //
    1.14 +
    1.15      protected static final Currency CZK = Currency.getInstance( "CZK" );
    1.16      protected static final Currency SKK = Currency.getInstance( "SKK" );
    1.17      protected static final Currency USD = Currency.getInstance( "USD" );
    1.18      
    1.19      /** Create convertor that understands two currencies, CZK and
    1.20       *  USD. Make 1 USD == 17 CZK.
    1.21 +     *  USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
    1.22 +     *  e.g. those that know the exchange rate. They somehow need to create
    1.23 +     *  the objects from the API and tell them the exchange rate. The API itself
    1.24 +     *  knows nothing about any rates, before the createCZKtoUSD method is called.
    1.25       *
    1.26       * Creation of the convertor shall not require subclassing of any class
    1.27       * or interface on the client side.
    1.28 @@ -48,7 +60,11 @@
    1.29      }
    1.30  
    1.31      /** Create convertor that understands two currencies, CZK and
    1.32 -     *  SKK. Make 100 SKK == 80 CZK.
    1.33 +     *  SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
    1.34 +     *  it knows the exchange rate, and needs to use the API to create objects
    1.35 +     *  with the exchange rate. Anyone shall be ready to call this method without
    1.36 +     *  any other method being called previously. The API itself shall know
    1.37 +     *  nothing about any rates, before this method is called.
    1.38       *
    1.39       * Creation of the convertor shall not require subclassing of any class
    1.40       * or interface on the client side.
    1.41 @@ -64,6 +80,13 @@
    1.42          return new ContractImposingDelegatingConvertor( convertor ).test();
    1.43      }
    1.44  
    1.45 +    //
    1.46 +    // now the methods for group #2 follow:
    1.47 +    // this group knows nothing about exchange rates, but knows how to use
    1.48 +    // the API to do conversions. It somehow (by calling one of the factory
    1.49 +    // methods) gets objects from the API and uses them to do the conversions.
    1.50 +    //
    1.51 +
    1.52      /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    1.53       * with it.
    1.54       */
    1.55 @@ -118,5 +141,37 @@
    1.56          assertEquals( CZK, a2.getCurrency() );
    1.57      }
    1.58  
    1.59 +    /** Verify that the CZK to USD convertor knows nothing about SKK.
    1.60 +    */
    1.61 +    public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
    1.62 +        final Convertor c = createCZKtoUSD();
    1.63 +        
    1.64 +        // convert $5 to SKK, the API shall say this is not possible
    1.65 +        final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), SKK ) );
    1.66 +        final MonetaryAmount a1 = r1.getNetAmount();
    1.67 +        assertNull( a1 );
    1.68 +
    1.69 +        // convert 500 SKK to CZK, the API shall say this is not possible
    1.70 +        final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, SKK ), CZK ) );
    1.71 +        final MonetaryAmount a2 = r2.getNetAmount();
    1.72 +        assertNull( a2 );
    1.73 +    }
    1.74 +
    1.75 +    /** Verify that the CZK to SKK convertor knows nothing about USD.
    1.76 +    */
    1.77 +    public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception {
    1.78 +        final Convertor c = createSKKtoCZK();
    1.79 +        
    1.80 +        // convert $5 to SKK, the API shall say this is not possible
    1.81 +        final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), SKK ) );
    1.82 +        final MonetaryAmount a1 = r1.getNetAmount();
    1.83 +        assertNull( a1 );
    1.84 +        
    1.85 +        // convert 500 CZK to USD, the API shall say this is not possible
    1.86 +        final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, CZK ), USD ) );
    1.87 +        final MonetaryAmount a2 = r2.getNetAmount();
    1.88 +        assertNull( a2 );
    1.89 +    }
    1.90 +
    1.91  }
    1.92