001// Copyright 2006, 2007, 2008, 2010, 2011, 2012 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.ioc.test;
016
017import org.easymock.*;
018import org.testng.annotations.AfterMethod;
019
020/**
021 * Manages a set of EasyMock mock objects. Used as a base class for test cases.
022 * <p/>
023 * Extends from {@link org.testng.Assert} to bring in all the public static assert methods without requiring extra
024 * imports.
025 * <p/>
026 * Provides a common mock factory method, {@link #newMock(Class)}. A single <em>standard</em> mock control is used for
027 * all mock objects. Standard mocks do not care about the exact order in which methods are invoked, though they are as
028 * rigorous as strict mocks when checking that parameters are the correct values.
029 * <p/>
030 * This base class is created with the intention of use within a TestNG test suite; if using JUnit, you can get the same
031 * functionality using {@link MockTester}.
032 * <p/>
033 * This class is thread safe (it uses a thread local to store the mock control). In theory, this should allow TestNG to
034 * execute tests in parallel.
035 * <p>
036 * This class was originally in the tapestry-ioc module as was moved to tapestry-test; the package name was not changed
037 * to ensure backwards compatibility.
038 * 
039 * @see org.easymock.EasyMock#createControl()
040 * @see org.apache.tapestry5.ioc.test.MockTester
041 * @deprecated In 5.4, with no replacement
042 */
043public class TestBase extends TestUtils
044{
045    private final MockTester tester = new MockTester();
046
047    /**
048     * @return the {@link IMocksControl} for this thread.
049     */
050    protected final IMocksControl getMocksControl()
051    {
052        return tester.getMocksControl();
053    }
054
055    /**
056     * Discards any mock objects created during the test.
057     */
058    @AfterMethod(alwaysRun = true)
059    public final void discardMockControl()
060    {
061        tester.cleanup();
062    }
063
064    /**
065     * Creates a new mock object of the indicated type. The shared mock control does <strong>not</strong> check order,
066     * but does fail on any unexpected method invocations.
067     * 
068     * @param <T>
069     *            the type of the mock object
070     * @param mockClass
071     *            the class to mock
072     * @return the mock object, ready for training
073     */
074    protected final <T> T newMock(Class<T> mockClass)
075    {
076        return tester.newMock(mockClass);
077    }
078
079    /**
080     * Switches each mock object created by {@link #newMock(Class)} into replay mode (out of the initial training
081     * mode).
082     */
083    protected final void replay()
084    {
085        tester.replay();
086    }
087
088    /**
089     * Verifies that all trained methods have been invoked on all mock objects (created by {@link #newMock(Class)}, then
090     * switches each mock object back to training mode.
091     */
092    protected final void verify()
093    {
094        tester.verify();
095    }
096
097    /**
098     * Convienience for {@link EasyMock#expectLastCall()} with {@link IExpectationSetters#andThrow(Throwable)}.
099     * 
100     * @param throwable
101     *            the exception to be thrown by the most recent method call on any mock
102     */
103    protected static void setThrowable(Throwable throwable)
104    {
105        EasyMock.expectLastCall().andThrow(throwable);
106    }
107
108    /**
109     * Convienience for {@link EasyMock#expectLastCall()} with
110     * {@link IExpectationSetters#andAnswer(org.easymock.IAnswer)}.
111     * 
112     * @param answer
113     *            callback for the most recent method invocation
114     */
115    protected static void setAnswer(IAnswer<?> answer)
116    {
117        EasyMock.expectLastCall().andAnswer(answer);
118    }
119
120    /**
121     * Convenience for {@link EasyMock#expect(Object)}.
122     * 
123     * @param value to expect
124     * @return expectation setter, for setting return value, etc.
125     */
126    @SuppressWarnings("unchecked")
127    protected static <T> IExpectationSetters<T> expect(T value)
128    {
129        return EasyMock.expect(value);
130    }
131
132    /**
133     * A factory method to create EasyMock Capture objects.
134     * @return new Capture
135     */
136    @SuppressWarnings({"UnusedDeclaration"})
137    protected static <T> Capture<T> newCapture()
138    {
139        return new Capture<T>();
140    }
141}