    1   package org.apache.stratum.messenger;
    2
    3   /*
    4    * Copyright 2001-2005 The Apache Software Foundation or its licensors,
    5    * as applicable.
    6    *
    7    * Licensed under the Apache License, Version 2.0 (the "License");
    8    * you may not use this file except in compliance with the License.
    9    * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19
    20  import junit.framework.TestCase;
    21
    22  import org.apache.commons.configuration.Configuration;
    23  import org.apache.commons.configuration.PropertiesConfiguration;
    24  import org.apache.log4j.Category;
    25
    26  import com.mockobjects.ExpectationCounter;
    27  import com.mockobjects.ExpectationValue;
    28  import com.mockobjects.Verifiable;
    29  import com.mockobjects.util.Verifier;
    30
    31  /***
    32   * Test cases to exercise the MessengerComponent.
    33   *
    34   * @author <a href="mailto:eric@dobbse.net">Eric Dobbs</a>
    35   * @version $Id: MessengerComponentTest.java 264191 2005-08-29 18:07:52Z henning $
    36   *
    37   * @see MessengerComponent
    38   */
    39  public class MessengerComponentTest
    40          extends TestCase
    41  {
    42      /*** TODO: DOCUMENT ME! */
    43      private static Category log = Category.getInstance(MessengerComponentTest.class);
    44
    45      /*** TODO: DOCUMENT ME! */
    46      private static MessengerComponent messengerComponent = new MessengerComponent();
    47
    48      /*** TODO: DOCUMENT ME! */
    49      private static String CONFIG = "src/test-conf/Messenger.properties";
    50
    51      /*** TODO: DOCUMENT ME! */
    52      private static Configuration config;
    53
    54      /***
    55       * Constructor.
    56       *
    57       * @param name String name of the method to be tested.
    58       */
    59      public MessengerComponentTest(String name)
    60      {
    61          super(name);
    62      }
    63
    64      /***
    65       * Verify that MessengerComponent.configure() only asks for one value from the given Configuration, and that the property
    66       * requested is "messenger.xml.url"
    67       */
    68      public void testConfigure()
    69      {
    70          MockConfiguration mockConfiguration = new MockConfiguration();
    71          mockConfiguration.setExpectedGetStringCalls(1);
    72          mockConfiguration.setExpectedGetStringValue("messenger.xml.url");
    73
    74          try
    75          {
    76              messengerComponent.configure(mockConfiguration);
    77          }
    78          catch (Exception e)
    79          {
    80              String errorMessage = "Exception should not have been thrown: " + e.getMessage();
    81              log.error(errorMessage, e);
    82              fail(errorMessage);
    83          }
    84
    85          mockConfiguration.verify();
    86      }
    87
    88      /***
    89       * Verify that a null value in the configuration will cause an Exception to be thrown.  That's about all that makes sense to
    90       * test for MessengerComponent.initialize().  Other testing would only repeat tests already covered in the Messenger test
    91       * code.
    92       */
    93      public void testInitialize()
    94      {
    95          MockConfiguration mockConfiguration = new MockConfiguration();
    96          mockConfiguration.setupGetString(null);
    97
    98          try
    99          {
    100             messengerComponent.configure(mockConfiguration);
    101             messengerComponent.initialize();
    102             fail("Exception should have been thrown");
    103         }
    104         catch (Exception e)
    105         {
    106             //this space intentionally left blank
    107         }
    108     }
    109
    110     /***
    111      * Need to mock the Configuration.getString() method for testing the configure method.  See <a
    112      * href="http://www.mockobjects.com">www.mockobjects.com</a> for more information about mock objects.
    113      */
    114     static class MockConfiguration
    115             extends PropertiesConfiguration
    116             implements Verifiable
    117     {
    118         /*** TODO: DOCUMENT ME! */
    119         private ExpectationCounter getStringCalls = new ExpectationCounter("mockConfiguration.getString");
    120
    121         /*** TODO: DOCUMENT ME! */
    122         private ExpectationValue getStringValue = new ExpectationValue("mockConfiguration.getString");
    123
    124         /*** TODO: DOCUMENT ME! */
    125         private String getStringReturnValue = null;
    126
    127         /***
    128          * TODO: DOCUMENT ME!
    129          *
    130          * @param callCount TODO: DOCUMENT ME!
    131          */
    132         public void setExpectedGetStringCalls(int callCount)
    133         {
    134             getStringCalls.setExpected(callCount);
    135         }
    136
    137         /***
    138          * TODO: DOCUMENT ME!
    139          *
    140          * @param key TODO: DOCUMENT ME!
    141          */
    142         public void setExpectedGetStringValue(String key)
    143         {
    144             getStringValue.setExpected(key);
    145         }
    146
    147         /***
    148          * TODO: DOCUMENT ME!
    149          *
    150          * @param getString TODO: DOCUMENT ME!
    151          */
    152         public void setupGetString(String getString)
    153         {
    154             getStringReturnValue = getString;
    155         }
    156
    157         /***
    158          * TODO: DOCUMENT ME!
    159          */
    160         public void verify()
    161         {
    162             Verifier.verifyObject(this);
    163         }
    164
    165         /***
    166          * TODO: DOCUMENT ME!
    167          *
    168          * @param key TODO: DOCUMENT ME!
    169          *
    170          * @return TODO: DOCUMENT ME!
    171          */
    172         public String getString(String key)
    173         {
    174             getStringCalls.inc();
    175             getStringValue.setActual(key);
    176
    177             return getStringReturnValue;
    178         }
    179     }
    180 }