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.test.ioc; 014 015import org.easymock.EasyMock; 016import org.easymock.IMocksControl; 017 018/** 019 * Contains core logic used by {@link TestBase}, allowing for mock objects to be used outside of a TestNG-based test 020 * suite. A <em>single</em> standard mock control is used for all mock instances. The control does not care about 021 * execution order, but will balk at any unexpected method invocations. This class is thread safe (it used a thread 022 * local to store the mock control). 023 * 024 * This class was originally in the tapestry-ioc module as was moved to tapestry-test; the package name was not changed 025 * to ensure backwards compatibility. 026 * @deprecated In 5.4, with no replacement 027 */ 028public final class MockTester 029{ 030 private static class ThreadLocalControl extends ThreadLocal<IMocksControl> 031 { 032 @Override 033 protected IMocksControl initialValue() 034 { 035 return EasyMock.createControl(); 036 } 037 } 038 039 private final ThreadLocalControl localControl = new ThreadLocalControl(); 040 041 /** 042 * Invoked after an individual unit test (i.e., a test method invocation) to discard the mock control. 043 */ 044 public synchronized void cleanup() 045 { 046 localControl.remove(); 047 } 048 049 public synchronized IMocksControl getMocksControl() 050 { 051 return localControl.get(); 052 } 053 054 /** 055 * Creates a new mock object of the indicated type. The shared mock control does <strong>not</strong> check order, 056 * but does fail on any unexpected method invocations. 057 * 058 * @param <T> 059 * the type of the mock object 060 * @param mockClass 061 * the class to mock 062 * @return the mock object, ready for training 063 */ 064 public <T> T newMock(Class<T> mockClass) 065 { 066 return getMocksControl().createMock(mockClass.getSimpleName(), mockClass); 067 } 068 069 /** 070 * Switches each mock object created by {@link #newMock(Class)} into replay mode (out of the initial training 071 * mode). 072 */ 073 public void replay() 074 { 075 getMocksControl().replay(); 076 } 077 078 /** 079 * Verifies that all trained methods have been invoked on all mock objects (created by {@link #newMock(Class)}, then 080 * switches each mock object back to training mode. 081 */ 082 public void verify() 083 { 084 IMocksControl control = getMocksControl(); 085 086 control.verify(); 087 control.reset(); 088 } 089}