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