147d431f63a66505a645f282416659a9758a91f1cBrett Chabot/*
247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Copyright 2001-2009 OFFIS, Tammo Freese
347d431f63a66505a645f282416659a9758a91f1cBrett Chabot *
447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Licensed under the Apache License, Version 2.0 (the "License");
547d431f63a66505a645f282416659a9758a91f1cBrett Chabot * you may not use this file except in compliance with the License.
647d431f63a66505a645f282416659a9758a91f1cBrett Chabot * You may obtain a copy of the License at
747d431f63a66505a645f282416659a9758a91f1cBrett Chabot *
847d431f63a66505a645f282416659a9758a91f1cBrett Chabot *     http://www.apache.org/licenses/LICENSE-2.0
947d431f63a66505a645f282416659a9758a91f1cBrett Chabot *
1047d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Unless required by applicable law or agreed to in writing, software
1147d431f63a66505a645f282416659a9758a91f1cBrett Chabot * distributed under the License is distributed on an "AS IS" BASIS,
1247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * See the License for the specific language governing permissions and
1447d431f63a66505a645f282416659a9758a91f1cBrett Chabot * limitations under the License.
1547d431f63a66505a645f282416659a9758a91f1cBrett Chabot */
1647d431f63a66505a645f282416659a9758a91f1cBrett Chabotpackage org.easymock.internal;
1747d431f63a66505a645f282416659a9758a91f1cBrett Chabot
1847d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.io.Serializable;
1947d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.lang.reflect.Method;
2047d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.util.concurrent.locks.ReentrantLock;
2147d431f63a66505a645f282416659a9758a91f1cBrett Chabot
2247d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport org.easymock.IAnswer;
2347d431f63a66505a645f282416659a9758a91f1cBrett Chabot
2447d431f63a66505a645f282416659a9758a91f1cBrett Chabotpublic class ReplayState implements IMocksControlState, Serializable {
2547d431f63a66505a645f282416659a9758a91f1cBrett Chabot
2647d431f63a66505a645f282416659a9758a91f1cBrett Chabot    private static final long serialVersionUID = 6314142602251047572L;
2747d431f63a66505a645f282416659a9758a91f1cBrett Chabot
2847d431f63a66505a645f282416659a9758a91f1cBrett Chabot    private final IMocksBehavior behavior;
2947d431f63a66505a645f282416659a9758a91f1cBrett Chabot
3047d431f63a66505a645f282416659a9758a91f1cBrett Chabot    private final ReentrantLock lock = new ReentrantLock();
3147d431f63a66505a645f282416659a9758a91f1cBrett Chabot
3247d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public ReplayState(IMocksBehavior behavior) {
3347d431f63a66505a645f282416659a9758a91f1cBrett Chabot        this.behavior = behavior;
3447d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
3547d431f63a66505a645f282416659a9758a91f1cBrett Chabot
3647d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public Object invoke(Invocation invocation) throws Throwable {
3747d431f63a66505a645f282416659a9758a91f1cBrett Chabot
3847d431f63a66505a645f282416659a9758a91f1cBrett Chabot        behavior.checkThreadSafety();
3947d431f63a66505a645f282416659a9758a91f1cBrett Chabot
4047d431f63a66505a645f282416659a9758a91f1cBrett Chabot        if (behavior.isThreadSafe()) {
4147d431f63a66505a645f282416659a9758a91f1cBrett Chabot            // If thread safe, synchronize the mock
4247d431f63a66505a645f282416659a9758a91f1cBrett Chabot            lock.lock();
4347d431f63a66505a645f282416659a9758a91f1cBrett Chabot            try {
4447d431f63a66505a645f282416659a9758a91f1cBrett Chabot                return invokeInner(invocation);
4547d431f63a66505a645f282416659a9758a91f1cBrett Chabot            }
4647d431f63a66505a645f282416659a9758a91f1cBrett Chabot            finally {
4747d431f63a66505a645f282416659a9758a91f1cBrett Chabot                lock.unlock();
4847d431f63a66505a645f282416659a9758a91f1cBrett Chabot            }
4947d431f63a66505a645f282416659a9758a91f1cBrett Chabot        }
5047d431f63a66505a645f282416659a9758a91f1cBrett Chabot
5147d431f63a66505a645f282416659a9758a91f1cBrett Chabot        return invokeInner(invocation);
5247d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
5347d431f63a66505a645f282416659a9758a91f1cBrett Chabot
5447d431f63a66505a645f282416659a9758a91f1cBrett Chabot    private Object invokeInner(Invocation invocation) throws Throwable {
5547d431f63a66505a645f282416659a9758a91f1cBrett Chabot        LastControl.pushCurrentInvocation(invocation);
5647d431f63a66505a645f282416659a9758a91f1cBrett Chabot        try {
5747d431f63a66505a645f282416659a9758a91f1cBrett Chabot            Result result = behavior.addActual(invocation);
5847d431f63a66505a645f282416659a9758a91f1cBrett Chabot            try {
5947d431f63a66505a645f282416659a9758a91f1cBrett Chabot                return result.answer();
6047d431f63a66505a645f282416659a9758a91f1cBrett Chabot            } catch (Throwable t) {
6147d431f63a66505a645f282416659a9758a91f1cBrett Chabot                if (result.shouldFillInStackTrace()) {
6247d431f63a66505a645f282416659a9758a91f1cBrett Chabot                    throw new ThrowableWrapper(t);
6347d431f63a66505a645f282416659a9758a91f1cBrett Chabot                }
6447d431f63a66505a645f282416659a9758a91f1cBrett Chabot                throw t;
6547d431f63a66505a645f282416659a9758a91f1cBrett Chabot            }
6647d431f63a66505a645f282416659a9758a91f1cBrett Chabot        } finally {
6747d431f63a66505a645f282416659a9758a91f1cBrett Chabot            LastControl.popCurrentInvocation();
6847d431f63a66505a645f282416659a9758a91f1cBrett Chabot        }
6947d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
7047d431f63a66505a645f282416659a9758a91f1cBrett Chabot
7147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void verify() {
7247d431f63a66505a645f282416659a9758a91f1cBrett Chabot        behavior.verify();
7347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
7447d431f63a66505a645f282416659a9758a91f1cBrett Chabot
7547d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void replay() {
7647d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
7747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
7847d431f63a66505a645f282416659a9758a91f1cBrett Chabot
7947d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void callback(Runnable runnable) {
8047d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
8147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
8247d431f63a66505a645f282416659a9758a91f1cBrett Chabot
8347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void checkOrder(boolean value) {
8447d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
8547d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
8647d431f63a66505a645f282416659a9758a91f1cBrett Chabot
8747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void makeThreadSafe(boolean threadSafe) {
8847d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
8947d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
9047d431f63a66505a645f282416659a9758a91f1cBrett Chabot
9147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void checkIsUsedInOneThread(boolean shouldBeUsedInOneThread) {
9247d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
9347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
9447d431f63a66505a645f282416659a9758a91f1cBrett Chabot
9547d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void andReturn(Object value) {
9647d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
9747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
9847d431f63a66505a645f282416659a9758a91f1cBrett Chabot
9947d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void andThrow(Throwable throwable) {
10047d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
10147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
10247d431f63a66505a645f282416659a9758a91f1cBrett Chabot
10347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void andAnswer(IAnswer<?> answer) {
10447d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
10547d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
10647d431f63a66505a645f282416659a9758a91f1cBrett Chabot
10747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void andDelegateTo(Object answer) {
10847d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
10947d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
11047d431f63a66505a645f282416659a9758a91f1cBrett Chabot
11147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void andStubReturn(Object value) {
11247d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
11347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
11447d431f63a66505a645f282416659a9758a91f1cBrett Chabot
11547d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void andStubThrow(Throwable throwable) {
11647d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
11747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
11847d431f63a66505a645f282416659a9758a91f1cBrett Chabot
11947d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void andStubAnswer(IAnswer<?> answer) {
12047d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
12147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
12247d431f63a66505a645f282416659a9758a91f1cBrett Chabot
12347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void andStubDelegateTo(Object delegateTo) {
12447d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
12547d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
12647d431f63a66505a645f282416659a9758a91f1cBrett Chabot
12747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void asStub() {
12847d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
12947d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
13047d431f63a66505a645f282416659a9758a91f1cBrett Chabot
13147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void times(Range range) {
13247d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
13347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
13447d431f63a66505a645f282416659a9758a91f1cBrett Chabot
13547d431f63a66505a645f282416659a9758a91f1cBrett Chabot    @SuppressWarnings("deprecation")
13647d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void setMatcher(Method method, org.easymock.ArgumentsMatcher matcher) {
13747d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
13847d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
13947d431f63a66505a645f282416659a9758a91f1cBrett Chabot
14047d431f63a66505a645f282416659a9758a91f1cBrett Chabot    @SuppressWarnings("deprecation")
14147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void setDefaultMatcher(org.easymock.ArgumentsMatcher matcher) {
14247d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
14347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
14447d431f63a66505a645f282416659a9758a91f1cBrett Chabot
14547d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void setDefaultReturnValue(Object value) {
14647d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
14747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
14847d431f63a66505a645f282416659a9758a91f1cBrett Chabot
14947d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void setDefaultThrowable(Throwable throwable) {
15047d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
15147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
15247d431f63a66505a645f282416659a9758a91f1cBrett Chabot
15347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void setDefaultVoidCallable() {
15447d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
15547d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
15647d431f63a66505a645f282416659a9758a91f1cBrett Chabot
15747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    private void throwWrappedIllegalStateException() {
15847d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throw new RuntimeExceptionWrapper(new IllegalStateException(
15947d431f63a66505a645f282416659a9758a91f1cBrett Chabot                "This method must not be called in replay state."));
16047d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
16147d431f63a66505a645f282416659a9758a91f1cBrett Chabot
16247d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void assertRecordState() {
16347d431f63a66505a645f282416659a9758a91f1cBrett Chabot        throwWrappedIllegalStateException();
16447d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
16547d431f63a66505a645f282416659a9758a91f1cBrett Chabot}
166