dobbs       2002/06/21 21:27:33

  Modified:    .        project.xml
  Added:       src/java/org/apache/stratum/messenger
                        MessengerComponent.java
               src/test/org/apache/stratum/messenger
                        MessengerComponentTest.java
  Log:
  adding component for use with Commons Messenger
  
  Revision  Changes    Path
  1.27      +18 -0     jakarta-turbine-stratum/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-stratum/project.xml,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- project.xml       17 Jun 2002 13:04:08 -0000      1.26
  +++ project.xml       22 Jun 2002 04:27:33 -0000      1.27
  @@ -74,6 +74,12 @@
         <email>[EMAIL PROTECTED]</email>
         <organization>Tucana</organization>
       </developer>
  +    <developer>
  +      <name>Eric Dobbs</name>
  +      <id>dobbs</id>
  +      <email>[EMAIL PROTECTED]</email>
  +      <organization/>
  +    </developer>
     </developers>
   
     <dependencies>
  @@ -146,6 +152,18 @@
       <dependency>
         <id>ojb</id>
         <version>0.5.200</version>
  +    </dependency>
  +    <dependency>
  +      <id>commons-messenger</id>
  +      <version>1.0-dev</version>
  +    </dependency>
  +    <dependency>
  +      <id>jms</id>
  +      <version>1.0.2b</version>
  +    </dependency>
  +    <dependency>
  +      <id>mockobjects</id>
  +      <version>0.1</version>
       </dependency>
     </dependencies>
   
  
  
  
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/messenger/MessengerComponent.java
  
  Index: MessengerComponent.java
  ===================================================================
  package org.apache.stratum.messenger;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.commons.configuration.Configuration;
  import org.apache.commons.lang.exception.NestableException;
  
  import org.apache.stratum.lifecycle.Configurable;
  import org.apache.stratum.lifecycle.Initializable;
  
  import org.apache.commons.messenger.MessengerManager;
  
  /**
   * This class is the Messenger component.  It is an adaptor that
   * allows the ComponentLoader to startup MessengerManager.  Client
   * code wishing to use Messenger should ask MessengerManager directly
   * for Messenger instances.
   *
   * @see org.apache.commons.messenger.MessengerManager
   * @see org.apache.commons.messenger.Messenger
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Eric Dobbs</a>
   * @version $Id: MessengerComponent.java,v 1.1 2002/06/22 04:27:33 dobbs Exp $
   */
  public class MessengerComponent
      implements Configurable, Initializable
  {
      /** The key for property 'messenger.xml.url' */
      private static final String MESSENGER_XML_URL = "messenger.xml.url";
      
      /** The location of the Messenger.xml file */ 
      private String messengerXmlUrl;
  
      /**
       * Configure MessengerManager.  MessengerManager requires a URL to
       * a Messenger.xml file.  The messenger.properties file should
       * define one property named 'messenger.xml.url' identifying the
       * location of Messenger.xml.
       * @param configuration Configuration object containing the
       * 'messenger.xml.url' property
       */
      public void configure(Configuration configuration) 
      {
          messengerXmlUrl = configuration.getString(MESSENGER_XML_URL);
      }
  
      /**
       * Initialize the MessengerManager.
       * @exception Exception is thrown if the file defined in
       * 'messenger.xml.url' can't be found.  JMSException will be
       * thrown if MessengerManager.configure is unhappy with the value
       * from 'messenger.xml.url'
       */
      public void initialize() throws Exception
      {
          if (messengerXmlUrl == null)
          {
              throw new Exception("Can't find property '"
                                  + MESSENGER_XML_URL
                                  + "' in the properties file");
          }
          MessengerManager.configure(messengerXmlUrl);
      }
  }
  
  
  
  1.1                  
jakarta-turbine-stratum/src/test/org/apache/stratum/messenger/MessengerComponentTest.java
  
  Index: MessengerComponentTest.java
  ===================================================================
  package org.apache.stratum.messenger;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.IOException;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import com.mockobjects.ExpectationCounter;
  import com.mockobjects.ExpectationValue;
  import com.mockobjects.Verifiable;
  import com.mockobjects.util.Verifier;
  import org.apache.log4j.Category;
  import org.apache.commons.configuration.Configuration;
  import org.apache.commons.configuration.PropertiesConfiguration;
  import org.apache.stratum.component.ComponentLoader;
  
  /**
   * Test cases to exercise the MessengerComponent.
   * @see MessengerComponent
   * @author <a href="mailto:[EMAIL PROTECTED]";>Eric Dobbs</a>
   * @version $Id: MessengerComponentTest.java,v 1.1 2002/06/22 04:27:33 dobbs Exp $
   */
  public class MessengerComponentTest extends TestCase
  {
      private static Category log  = 
Category.getInstance(MessengerComponentTest.class);
      private static MessengerComponent messengerComponent =
          new MessengerComponent();
      private static String CONFIG = "src/test-conf/Messenger.properties";
      private static Configuration config;
  
      /**
       * Constructor.
       * @param name String name of the method to be tested.
       */
      public MessengerComponentTest(String name)
      {
          super(name);
      }
  
      /**
       * Verify that MessengerComponent.configure() only asks for one
       * value from the given Configuration, and that the property
       * requested is "messenger.xml.url"
       */
      public void testConfigure()
      {
          MockConfiguration mockConfiguration = new MockConfiguration();
          mockConfiguration.setExpectedGetStringCalls(1);
          mockConfiguration.setExpectedGetStringValue("messenger.xml.url");
  
          try
          {
              messengerComponent.configure(mockConfiguration);
          }
          catch (Exception e)
          {
              String errorMessage = "Exception should not have been thrown: "
                  + e.getMessage();
              log.error(errorMessage,e);
              fail(errorMessage);
          }
  
          mockConfiguration.verify();
      }
  
      /**
       * Verify that a null value in the configuration will cause an
       * Exception to be thrown.  That's about all that makes sense to
       * test for MessengerComponent.initialize().  Other testing would
       * only repeat tests already covered in the Messenger test code.
       */
      public void testInitialize()
      {
          MockConfiguration mockConfiguration = new MockConfiguration();
          mockConfiguration.setupGetString(null);
          try
          {
              messengerComponent.configure(mockConfiguration);
              messengerComponent.initialize();
              fail("Exception should have been thrown");
          }
          catch (Exception e)
          {
              //this space intentionally left blank
          }
      }
  
  
      /**
       * Need to mock the Configuration.getString() method for testing
       * the configure method.  See
       * <a href="http://www.mockobjects.com";>www.mockobjects.com</a>
       * for more information about mock objects.
       */
      static class MockConfiguration
          extends PropertiesConfiguration
          implements Verifiable
      {
          private ExpectationCounter getStringCalls =
              new ExpectationCounter("mockConfiguration.getString");
          private ExpectationValue getStringValue =
              new ExpectationValue("mockConfiguration.getString");
          private String getStringReturnValue = null;
          
          public void setExpectedGetStringCalls(int callCount)
          {
              getStringCalls.setExpected(callCount);
          }
          public void setExpectedGetStringValue(String key)
          {
              getStringValue.setExpected(key);
          }
          public void setupGetString(String getString)
          {
              getStringReturnValue = getString;
          }
          
          public void verify()
          {
              Verifier.verifyObject(this);
          }
          
          public String getString(String key)
          {
              getStringCalls.inc();
              getStringValue.setActual(key);
              return getStringReturnValue;
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to