147d431f63a66505a645f282416659a9758a91f1cBrett Chabot/*
247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Copyright 2003-2009 OFFIS, Henri Tremblay
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;
1747d431f63a66505a645f282416659a9758a91f1cBrett Chabot
1847d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.util.ArrayList;
1947d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.util.List;
2047d431f63a66505a645f282416659a9758a91f1cBrett Chabot
2147d431f63a66505a645f282416659a9758a91f1cBrett Chabot/**
2247d431f63a66505a645f282416659a9758a91f1cBrett Chabot * Helper class to be used to keep tracks of mocks easily. See EasyMock
2347d431f63a66505a645f282416659a9758a91f1cBrett Chabot * documentation and SupportTest sample
2447d431f63a66505a645f282416659a9758a91f1cBrett Chabot */
2547d431f63a66505a645f282416659a9758a91f1cBrett Chabotpublic class EasyMockSupport {
2647d431f63a66505a645f282416659a9758a91f1cBrett Chabot
2747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /** List of all controls created */
2847d431f63a66505a645f282416659a9758a91f1cBrett Chabot    protected final List<IMocksControl> controls = new ArrayList<IMocksControl>(
2947d431f63a66505a645f282416659a9758a91f1cBrett Chabot            5);
3047d431f63a66505a645f282416659a9758a91f1cBrett Chabot
3147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
3247d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Creates a mock object that implements the given interface, order checking
3347d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * is enabled by default.
3447d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *
3547d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param <T>
3647d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the interface that the mock object should implement.
3747d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param toMock
3847d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the class of the interface that the mock object should
3947d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            implement.
4047d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @return the mock object.
4147d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
4247d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public <T> T createStrictMock(Class<T> toMock) {
4347d431f63a66505a645f282416659a9758a91f1cBrett Chabot        return createStrictControl().createMock(toMock);
4447d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
4547d431f63a66505a645f282416659a9758a91f1cBrett Chabot
4647d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
4747d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Creates a mock object that implements the given interface, order checking
4847d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * is enabled by default.
4947d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *
5047d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param name
5147d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the name of the mock object.
5247d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param toMock
5347d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the class of the interface that the mock object should
5447d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            implement.
5547d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param <T>
5647d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the interface that the mock object should implement.
5747d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @return the mock object.
5847d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @throws IllegalArgumentException
5947d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *             if the name is not a valid Java identifier.
6047d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
6147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public <T> T createStrictMock(String name, Class<T> toMock) {
6247d431f63a66505a645f282416659a9758a91f1cBrett Chabot        return createStrictControl().createMock(name, toMock);
6347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
6447d431f63a66505a645f282416659a9758a91f1cBrett Chabot
6547d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
6647d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Creates a mock object that implements the given interface, order checking
6747d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * is disabled by default.
6847d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *
6947d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param <T>
7047d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the interface that the mock object should implement.
7147d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param toMock
7247d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the class of the interface that the mock object should
7347d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            implement.
7447d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @return the mock object.
7547d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
7647d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public <T> T createMock(Class<T> toMock) {
7747d431f63a66505a645f282416659a9758a91f1cBrett Chabot        return createControl().createMock(toMock);
7847d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
7947d431f63a66505a645f282416659a9758a91f1cBrett Chabot
8047d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
8147d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Creates a mock object that implements the given interface, order checking
8247d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * is disabled by default.
8347d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *
8447d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param name
8547d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the name of the mock object.
8647d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param toMock
8747d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the class of the interface that the mock object should
8847d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            implement.
8947d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *
9047d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param <T>
9147d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the interface that the mock object should implement.
9247d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @return the mock object.
9347d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @throws IllegalArgumentException
9447d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *             if the name is not a valid Java identifier.
9547d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
9647d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public <T> T createMock(String name, Class<T> toMock) {
9747d431f63a66505a645f282416659a9758a91f1cBrett Chabot        return createControl().createMock(name, toMock);
9847d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
9947d431f63a66505a645f282416659a9758a91f1cBrett Chabot
10047d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
10147d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Creates a mock object that implements the given interface, order checking
10247d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * is disabled by default, and the mock object will return <code>0</code>,
10347d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * <code>null</code> or <code>false</code> for unexpected invocations.
10447d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *
10547d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param <T>
10647d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the interface that the mock object should implement.
10747d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param toMock
10847d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the class of the interface that the mock object should
10947d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            implement.
11047d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @return the mock object.
11147d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
11247d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public <T> T createNiceMock(Class<T> toMock) {
11347d431f63a66505a645f282416659a9758a91f1cBrett Chabot        return createNiceControl().createMock(toMock);
11447d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
11547d431f63a66505a645f282416659a9758a91f1cBrett Chabot
11647d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
11747d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Creates a mock object that implements the given interface, order checking
11847d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * is disabled by default, and the mock object will return <code>0</code>,
11947d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * <code>null</code> or <code>false</code> for unexpected invocations.
12047d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *
12147d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param name
12247d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the name of the mock object.
12347d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param toMock
12447d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the class of the interface that the mock object should
12547d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            implement.
12647d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *
12747d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @param <T>
12847d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *            the interface that the mock object should implement.
12947d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @return the mock object.
13047d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @throws IllegalArgumentException
13147d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *             if the name is not a valid Java identifier.
13247d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
13347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public <T> T createNiceMock(String name, Class<T> toMock) {
13447d431f63a66505a645f282416659a9758a91f1cBrett Chabot        return createNiceControl().createMock(name, toMock);
13547d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
13647d431f63a66505a645f282416659a9758a91f1cBrett Chabot
13747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
13847d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Creates a control, order checking is enabled by default.
13947d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *
14047d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @return the control.
14147d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
14247d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public IMocksControl createStrictControl() {
14347d431f63a66505a645f282416659a9758a91f1cBrett Chabot        IMocksControl ctrl = EasyMock.createStrictControl();
14447d431f63a66505a645f282416659a9758a91f1cBrett Chabot        controls.add(ctrl);
14547d431f63a66505a645f282416659a9758a91f1cBrett Chabot        return ctrl;
14647d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
14747d431f63a66505a645f282416659a9758a91f1cBrett Chabot
14847d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
14947d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Creates a control, order checking is disabled by default.
15047d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *
15147d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @return the control.
15247d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
15347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public IMocksControl createControl() {
15447d431f63a66505a645f282416659a9758a91f1cBrett Chabot        IMocksControl ctrl = EasyMock.createControl();
15547d431f63a66505a645f282416659a9758a91f1cBrett Chabot        controls.add(ctrl);
15647d431f63a66505a645f282416659a9758a91f1cBrett Chabot        return ctrl;
15747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
15847d431f63a66505a645f282416659a9758a91f1cBrett Chabot
15947d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
16047d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Creates a control, order checking is disabled by default, and the mock
16147d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * objects created by this control will return <code>0</code>,
16247d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * <code>null</code> or <code>false</code> for unexpected invocations.
16347d431f63a66505a645f282416659a9758a91f1cBrett Chabot     *
16447d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * @return the control.
16547d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
16647d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public IMocksControl createNiceControl() {
16747d431f63a66505a645f282416659a9758a91f1cBrett Chabot        IMocksControl ctrl = EasyMock.createNiceControl();
16847d431f63a66505a645f282416659a9758a91f1cBrett Chabot        controls.add(ctrl);
16947d431f63a66505a645f282416659a9758a91f1cBrett Chabot        return ctrl;
17047d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
17147d431f63a66505a645f282416659a9758a91f1cBrett Chabot
17247d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
17347d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Switches all registered mock objects (more exactly: the controls of the
17447d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * mock objects) to replay mode. For details, see the EasyMock
17547d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * documentation.
17647d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
17747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void replayAll() {
17847d431f63a66505a645f282416659a9758a91f1cBrett Chabot        for (IMocksControl c : controls) {
17947d431f63a66505a645f282416659a9758a91f1cBrett Chabot            c.replay();
18047d431f63a66505a645f282416659a9758a91f1cBrett Chabot        }
18147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
18247d431f63a66505a645f282416659a9758a91f1cBrett Chabot
18347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
18447d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Resets all registered mock objects (more exactly: the controls of the
18547d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * mock objects). For details, see the EasyMock documentation.
18647d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
18747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void resetAll() {
18847d431f63a66505a645f282416659a9758a91f1cBrett Chabot        for (IMocksControl c : controls) {
18947d431f63a66505a645f282416659a9758a91f1cBrett Chabot            c.reset();
19047d431f63a66505a645f282416659a9758a91f1cBrett Chabot        }
19147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
19247d431f63a66505a645f282416659a9758a91f1cBrett Chabot
19347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
19447d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Verifies all registered mock objects (more exactly: the controls of the
19547d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * mock objects).
19647d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
19747d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void verifyAll() {
19847d431f63a66505a645f282416659a9758a91f1cBrett Chabot        for (IMocksControl c : controls) {
19947d431f63a66505a645f282416659a9758a91f1cBrett Chabot            c.verify();
20047d431f63a66505a645f282416659a9758a91f1cBrett Chabot        }
20147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
20247d431f63a66505a645f282416659a9758a91f1cBrett Chabot
20347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
20447d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Resets all registered mock objects (more exactly: the controls of the
20547d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * mock objects) and turn them to a mock with nice behavior. For details,
20647d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * see the EasyMock documentation.
20747d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
20847d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void resetAllToNice() {
20947d431f63a66505a645f282416659a9758a91f1cBrett Chabot        for (IMocksControl c : controls) {
21047d431f63a66505a645f282416659a9758a91f1cBrett Chabot            c.resetToNice();
21147d431f63a66505a645f282416659a9758a91f1cBrett Chabot        }
21247d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
21347d431f63a66505a645f282416659a9758a91f1cBrett Chabot
21447d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
21547d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Resets all registered mock objects (more exactly: the controls of the
21647d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * mock objects) and turn them to a mock with default behavior. For details,
21747d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * see the EasyMock documentation.
21847d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
21947d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void resetAllToDefault() {
22047d431f63a66505a645f282416659a9758a91f1cBrett Chabot        for (IMocksControl c : controls) {
22147d431f63a66505a645f282416659a9758a91f1cBrett Chabot            c.resetToDefault();
22247d431f63a66505a645f282416659a9758a91f1cBrett Chabot        }
22347d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
22447d431f63a66505a645f282416659a9758a91f1cBrett Chabot
22547d431f63a66505a645f282416659a9758a91f1cBrett Chabot    /**
22647d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * Resets all registered mock objects (more exactly: the controls of the
22747d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * mock objects) and turn them to a mock with strict behavior. For details,
22847d431f63a66505a645f282416659a9758a91f1cBrett Chabot     * see the EasyMock documentation.
22947d431f63a66505a645f282416659a9758a91f1cBrett Chabot     */
23047d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void resetAllToStrict() {
23147d431f63a66505a645f282416659a9758a91f1cBrett Chabot        for (IMocksControl c : controls) {
23247d431f63a66505a645f282416659a9758a91f1cBrett Chabot            c.resetToStrict();
23347d431f63a66505a645f282416659a9758a91f1cBrett Chabot        }
23447d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
23547d431f63a66505a645f282416659a9758a91f1cBrett Chabot
23647d431f63a66505a645f282416659a9758a91f1cBrett Chabot}
237