hivemind-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hls...@apache.org
Subject cvs commit: jakarta-hivemind/framework/src/test/org/apache/hivemind/test TestMockClass.java
Date Sun, 05 Dec 2004 16:43:19 GMT
hlship      2004/12/05 08:43:19

  Modified:    .        .classpath status.xml
               framework/src/java/org/apache/hivemind/test
                        HiveMindTestCase.java
               framework build.xml
  Added:       framework/src/test/org/apache/hivemind/test
                        TestMockClass.java
  Log:
  Optionally support the EasyMock class extension (if present on the classpath).
  
  Revision  Changes    Path
  1.43      +2 -0      jakarta-hivemind/.classpath
  
  Index: .classpath
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/.classpath,v
  retrieving revision 1.42
  retrieving revision 1.43
  diff -u -r1.42 -r1.43
  --- .classpath	11 Nov 2004 14:13:44 -0000	1.42
  +++ .classpath	5 Dec 2004 16:43:19 -0000	1.43
  @@ -24,5 +24,7 @@
   	<classpathentry kind="lib" path="ext-package/lib/junit-3.8.1.jar"/>
   	<classpathentry kind="lib" path="ext-package/lib/jboss-j2ee-3.2.1.jar"/>
   	<classpathentry kind="lib" path="ext-package/lib/groovy-all-1.0-beta-7.jar"/>
  +	<classpathentry kind="lib" path="ext-package/lib/cglib-full-2.0.1.jar"/>
  +	<classpathentry kind="lib" path="ext-package/lib/easymockclassextension-1.1.jar"/>
   	<classpathentry kind="output" path="bin"/>
   </classpath>
  
  
  
  1.83      +4 -0      jakarta-hivemind/status.xml
  
  Index: status.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/status.xml,v
  retrieving revision 1.82
  retrieving revision 1.83
  diff -u -r1.82 -r1.83
  --- status.xml	11 Nov 2004 14:13:44 -0000	1.82
  +++ status.xml	5 Dec 2004 16:43:19 -0000	1.83
  @@ -108,6 +108,10 @@
             Added Groovy support.  Module descriptors can now be defined using Groovy scripts.
 Although
   		  it requires some additional work in building the Registry.
           </action>
  +        <action type="add" dev="HLS">
  +          Support the EasyMock Class Extension, if present on the classpath, to allow classes
(not just
  +          interfaces) to be mocked.
  +        </action>
       </release>
   
      <release version="1.0" date="Sep 22 2004">
  
  
  
  1.19      +64 -1     jakarta-hivemind/framework/src/java/org/apache/hivemind/test/HiveMindTestCase.java
  
  Index: HiveMindTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/src/java/org/apache/hivemind/test/HiveMindTestCase.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- HiveMindTestCase.java	10 Nov 2004 13:26:21 -0000	1.18
  +++ HiveMindTestCase.java	5 Dec 2004 16:43:19 -0000	1.19
  @@ -33,6 +33,7 @@
   import org.apache.hivemind.impl.LocationImpl;
   import org.apache.hivemind.impl.RegistryBuilder;
   import org.apache.hivemind.impl.XmlModuleDescriptorProvider;
  +import org.apache.hivemind.service.ClassFabUtils;
   import org.apache.hivemind.util.ClasspathResource;
   import org.apache.hivemind.util.PropertyUtils;
   import org.apache.hivemind.util.URLResource;
  @@ -44,6 +45,7 @@
   import org.apache.oro.text.regex.Perl5Compiler;
   import org.apache.oro.text.regex.Perl5Matcher;
   import org.easymock.MockControl;
  +import org.easymock.classextension.MockClassControl;
   
   /**
    * Contains some support for creating HiveMind tests; this is useful enough that has been
moved into
  @@ -67,6 +69,60 @@
   
       private List _controls = new ArrayList();
   
  +    /** @since 1.1 */
  +    interface MockControlFactory
  +    {
  +        public MockControl newControl(Class mockClass);
  +    }
  +
  +    /** @since 1.1 */
  +    private static class InterfaceMockControlFactory implements MockControlFactory
  +    {
  +        public MockControl newControl(Class mockClass)
  +        {
  +            return MockControl.createStrictControl(mockClass);
  +        }
  +    }
  +
  +    /** @since 1.1 */
  +    private static class ClassMockControlFactory implements MockControlFactory
  +    {
  +        public MockControl newControl(Class mockClass)
  +        {
  +            return MockClassControl.createStrictControl(mockClass);
  +        }
  +    }
  +
  +    /** @since 1.1 */
  +    static class PlaceholderClassMockControlFactory implements MockControlFactory
  +    {
  +        public MockControl newControl(Class mockClass)
  +        {
  +            throw new RuntimeException(
  +                    "Unable to instantiate EasyMock control for "
  +                            + mockClass
  +                            + "; ensure that easymockclassextension-1.1.jar and cglib-full-2.0.1.jar
are on the classpath.");
  +        }
  +    }
  +
  +    /** @since 1.1 */
  +    private static final MockControlFactory _interfaceMockControlFactory = new InterfaceMockControlFactory();
  +
  +    /** @since 1.1 */
  +    private static MockControlFactory _classMockControlFactory;
  +
  +    static
  +    {
  +        try
  +        {
  +            _classMockControlFactory = new ClassMockControlFactory();
  +        }
  +        catch (NoClassDefFoundError ex)
  +        {
  +            _classMockControlFactory = new PlaceholderClassMockControlFactory();
  +        }
  +    }
  +
       /**
        * Returns the given file as a {@link Resource}from the classpath. Typically, this
is to find
        * files in the same folder as the invoking class.
  @@ -400,10 +456,17 @@
        * Creates a <em>managed</em> control via
        * {@link MockControl#createStrictControl(java.lang.Class)}. The created control is
remembered,
        * and will be invoked by {@link #replayControls()},{@link #verifyControls()}, etc..
  +     * <p>
  +     * The class to mock may be either an interface or a class. The EasyMock class extension
  +     * (easymockclassextension-1.1.jar) and CGLIB (cglib-full-2.01.jar) must be present
in the
  +     * latter case (new since 1.1).
        */
       protected MockControl newControl(Class mockClass)
       {
  -        MockControl result = MockControl.createStrictControl(mockClass);
  +        MockControlFactory factory = mockClass.isInterface() ? _interfaceMockControlFactory
  +                : _classMockControlFactory;
  +
  +        MockControl result = factory.newControl(mockClass);
   
           addControl(result);
   
  
  
  
  1.16      +12 -11    jakarta-hivemind/framework/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-hivemind/framework/build.xml,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- build.xml	16 Nov 2004 18:51:43 -0000	1.15
  +++ build.xml	5 Dec 2004 16:43:19 -0000	1.16
  @@ -26,17 +26,18 @@
   	<import file="${hivebuild.dir}/javadoc-report.xml"/>
   	<import file="${hivebuild.dir}/clover-report.xml"/>
     
  -	<target name="compile">
  -		<ibiblio-dependency artifact="commons-logging" version="1.0.3" group="commons-logging"/>
  -		<ibiblio-dependency artifact="javassist" version="${javassist.version}" group="javassist"/>
  -		<ibiblio-dependency artifact="servletapi" version="2.3" group="servletapi"/>
  -		<ibiblio-dependency artifact="oro" version="2.0.6" group="oro"/>
  -		<ibiblio-dependency artifact="log4j" version="1.2.7" group="log4j"/>
  -		<ibiblio-dependency artifact="easymock" version="1.1" group="easymock"/>
  -		<ibiblio-dependency artifact="junit" version="3.8.1" group="junit"/>
  -		
  -		<default-compile/>
  -	</target>
  +  <target name="compile">
  +    <ibiblio-dependency artifact="commons-logging" version="1.0.3" group="commons-logging"/>
  +    <ibiblio-dependency artifact="javassist" version="${javassist.version}" group="javassist"/>
  +    <ibiblio-dependency artifact="servletapi" version="2.3" group="servletapi"/>
  +    <ibiblio-dependency artifact="oro" version="2.0.6" group="oro"/>
  +    <ibiblio-dependency artifact="log4j" version="1.2.7" group="log4j"/>
  +    <ibiblio-dependency artifact="easymock" version="1.1" group="easymock"/>
  +    <ibiblio-dependency artifact="easymockclassextension" version="1.1" group="easymock"/>
  +    <ibiblio-dependency artifact="cglib-full" version="2.0.1" group="cglib" />
  +    <ibiblio-dependency artifact="junit" version="3.8.1" group="junit"/>
  +    <default-compile/>
  +  </target>
     
   	
   	<target name="run-tests" description="Runs JUnit tests."
  
  
  
  1.1                  jakarta-hivemind/framework/src/test/org/apache/hivemind/test/TestMockClass.java
  
  Index: TestMockClass.java
  ===================================================================
  //Copyright 2004 The Apache Software Foundation
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //     http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
  // distributed under the License is distributed on an "AS IS" BASIS,
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  // See the License for the specific language governing permissions and
  // limitations under the License.
  
  package org.apache.hivemind.test;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import org.easymock.MockControl;
  
  /**
   * Tests {@link org.apache.hivemind.test.HiveMindTestCase}'s ability to generate a mock
for a class
   * as well as an interface.
   * 
   * @author Howard M. Lewis Ship
   * @since 1.1
   */
  public class TestMockClass extends HiveMindTestCase
  {
      public void testMockForClass()
      {
          MockControl c = newControl(ArrayList.class);
          List l = (List) c.getMock();
  
          l.size();
          c.setReturnValue(5);
  
          replayControls();
  
          // We're not actually testing the List, we're testing the ability to create a mock
          // for ArrayList
  
          assertEquals(5, l.size());
  
          verifyControls();
      }
  
      /**
       * Test the placeholder, which is used when the easymockclassextension is not available.
       */
  
      public void testPlaceholder()
      {
          MockControlFactory f = new PlaceholderClassMockControlFactory();
  
          try
          {
              f.newControl(ArrayList.class);
              unreachable();
          }
          catch (RuntimeException ex)
          {
              assertEquals(
                      "Unable to instantiate EasyMock control for class java.util.ArrayList;
ensure that easymockclassextension-1.1.jar and cglib-full-2.0.1.jar are on the classpath.",
                      ex.getMessage());
          }
      }
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-cvs-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-cvs-help@jakarta.apache.org


Mime
View raw message