Author: hlship
Date: Mon May 28 11:53:29 2007
New Revision: 542296

URL: http://svn.apache.org/viewvc?view=rev&rev=542296
Log:
TAPESTRY-1523: Split mock-control managing logic in TestBase so that it can be 
used in a JUnit test suite 

Added:
    
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/test/MockTester.java
Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/test/conf/testng.xml
    
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/test/TestBase.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/conf/testng.xml
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/conf/testng.xml?view=diff&rev=542296&r1=542295&r2=542296
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/conf/testng.xml (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/conf/testng.xml Mon May 28 
11:53:29 2007
@@ -15,7 +15,7 @@
    limitations under the License.
 -->
 
-<suite name="Tapestry Core"  parallel="tests" thread-count="5" 
annotations="1.5" verbose="10">
+<suite name="Tapestry Core" parallel="tests" thread-count="5" 
annotations="1.5" verbose="0">
   <parameter name="tapestry.integration-webapp" value="src/test/app1"/>
   <test name="Integration Tests">
     <packages>

Added: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/test/MockTester.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/test/MockTester.java?view=auto&rev=542296
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/test/MockTester.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/test/MockTester.java
 Mon May 28 11:53:29 2007
@@ -0,0 +1,88 @@
+// Copyright 2007 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.tapestry.ioc.test;
+
+import org.easymock.EasyMock;
+import org.easymock.IMocksControl;
+
+/**
+ * Contains core logic used by [EMAIL PROTECTED] TestBase}, allowing for mock 
objects to be used outside of a
+ * TestNG-based test suite. A <em>single</em> standard mock control is used 
for all mock
+ * instances. The control does not care about execution order, but will balk 
at any unexpected
+ * method invocations. This class is thread safe (it used a thread local to 
store the mock control).
+ */
+public final class MockTester
+{
+    private static class ThreadLocalControl extends ThreadLocal<IMocksControl>
+    {
+        @Override
+        protected IMocksControl initialValue()
+        {
+            return EasyMock.createControl();
+        }
+    }
+
+    private final ThreadLocalControl _localControl = new ThreadLocalControl();
+
+    /**
+     * Invoked after an individual unit test (i.e., a test method invocation) 
to discard the mock
+     * control.
+     */
+    public void cleanup()
+    {
+        _localControl.remove();
+    }
+
+    public IMocksControl getMocksControl()
+    {
+        return _localControl.get();
+    }
+
+    /**
+     * Creates a new mock object of the indicated type. The shared mock 
control does <strong>not</strong>
+     * check order, but does fail on any unexpected method invocations.
+     * 
+     * @param <T>
+     *            the type of the mock object
+     * @param mockClass
+     *            the class to mock
+     * @return the mock object, ready for training
+     */
+    public <T> T newMock(Class<T> mockClass)
+    {
+        return getMocksControl().createMock(mockClass);
+    }
+
+    /**
+     * Switches each mock object created by [EMAIL PROTECTED] #newMock(Class)} 
into replay mode (out of the
+     * initial training mode).
+     */
+    public void replay()
+    {
+        getMocksControl().replay();
+    }
+
+    /**
+     * Verifies that all trained methods have been invoked on all mock objects 
(created by
+     * [EMAIL PROTECTED] #newMock(Class)}, then switches each mock object back 
to training mode.
+     */
+    public void verify()
+    {
+        IMocksControl control = getMocksControl();
+
+        control.verify();
+        control.reset();
+    }
+}

Modified: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/test/TestBase.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/test/TestBase.java?view=diff&rev=542296&r1=542295&r2=542296
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/test/TestBase.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry/ioc/test/TestBase.java
 Mon May 28 11:53:29 2007
@@ -26,10 +26,13 @@
  * Extends from [EMAIL PROTECTED] org.testng.Assert} to bring in all the 
public static assert methods without
  * requiring extra imports.
  * <p>
- * Provides common mock factory and mock trainer methods. A single 
<em>standard</em> mock control
- * is used for all mock objects. Standard mocks do not care about the exact 
order in which methods
- * are invoked, though they are as rigourous as strict mocks when checking 
that parameters are the
- * correct values.
+ * Provides a common mock factory method, [EMAIL PROTECTED] #newMock(Class)}. 
A single <em>standard</em>
+ * mock control is used for all mock objects. Standard mocks do not care about 
the exact order in
+ * which methods are invoked, though they are as rigourous as strict mocks 
when checking that
+ * parameters are the correct values.
+ * <p>
+ * This base class is created with the intention of use within a TestNG test 
suite; if using JUnit,
+ * you can get the same functionality using [EMAIL PROTECTED] MockTester}.
  * <p>
  * This class is thread safe (it uses a thread local to store the mock 
control). In theory, this
  * should allow TestNG to execute tests in parallel. Unfortunately, as of this 
writing (TestNG 5.1
@@ -37,6 +40,7 @@
  * some tests are dropped, and so Tapestry does not make use of TestNG 
parallel execution.
  * 
  * @see EasyMock#createControl()
+ * @see MockTester
  */
 public class TestBase extends Assert
 {
@@ -49,14 +53,14 @@
         }
     }
 
-    private final ThreadLocalControl _localControl = new ThreadLocalControl();
+    private final MockTester _tester = new MockTester();
 
     /**
      * Returns the [EMAIL PROTECTED] IMocksControl} for this thread.
      */
     protected final IMocksControl getMocksControl()
     {
-        return _localControl.get();
+        return _tester.getMocksControl();
     }
 
     /**
@@ -65,7 +69,7 @@
     @AfterMethod(alwaysRun = true)
     public final void discardMockControl()
     {
-        _localControl.remove();
+        _tester.cleanup();
     }
 
     /**
@@ -80,39 +84,37 @@
      */
     protected final <T> T newMock(Class<T> mockClass)
     {
-        return getMocksControl().createMock(mockClass);
+        return _tester.newMock(mockClass);
     }
 
     /**
-     * Replay's each mock object created by [EMAIL PROTECTED] #newMock(Class)}.
+     * Switches each mock object created by [EMAIL PROTECTED] #newMock(Class)} 
into replay mode (out of the
+     * initial training mode).
      */
     protected final void replay()
     {
-        getMocksControl().replay();
+        _tester.replay();
     }
 
     /**
-     * Verifies each created mock object, then resets the mock for additional 
training.
+     * Verifies that all trained methods have been invoked on all mock objects 
(created by
+     * [EMAIL PROTECTED] #newMock(Class)}, then switches each mock object back 
to training mode.
      */
     protected final void verify()
     {
-        IMocksControl control = getMocksControl();
-
-        control.verify();
-        control.reset();
+        _tester.verify();
     }
 
     /**
-     * Trains a mock object to throw an exception (for the most recent method 
call). Generally,
-     * using [EMAIL PROTECTED] #expect(Object)}.andThrow() is preferred, but 
that doesn't work for void
-     * methods.
+     * Convienience for [EMAIL PROTECTED] EasyMock#expectLastCall()} with
+     * [EMAIL PROTECTED] IExpectationSetters#andThrow(Throwable)}.
      * 
      * @param throwable
-     *            the exception to be thrown by the most recent method call on 
the mock
+     *            the exception to be thrown by the most recent method call on 
any mock
      */
     protected final void setThrowable(Throwable throwable)
     {
-        getMocksControl().andThrow(throwable);
+        EasyMock.expectLastCall().andThrow(throwable);
     }
 
     /**
@@ -125,13 +127,17 @@
         fail("This code should not be reachable.");
     }
 
+    /**
+     * Convienience for [EMAIL PROTECTED] EasyMock#expect(Object)}.
+     * 
+     * @param <T>
+     * @param value
+     * @return expectation setter, for setting return value, etc.
+     */
     @SuppressWarnings("unchecked")
     protected final <T> IExpectationSetters<T> expect(T value)
     {
-        // value will have been evaluated, we can then return the control to 
string together
-        // andReturn() or etc. calls
-
-        return getMocksControl();
+        return EasyMock.expect(value);
     }
 
     /**


Reply via email to