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.IOException;
1947d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.io.Serializable;
2047d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.lang.reflect.Method;
2147d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.util.HashMap;
2247d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport java.util.Map;
2347d431f63a66505a645f282416659a9758a91f1cBrett Chabot
2447d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport org.easymock.ArgumentsMatcher;
2547d431f63a66505a645f282416659a9758a91f1cBrett Chabotimport org.easymock.MockControl;
2647d431f63a66505a645f282416659a9758a91f1cBrett Chabot
2747d431f63a66505a645f282416659a9758a91f1cBrett Chabot@SuppressWarnings("deprecation")
2847d431f63a66505a645f282416659a9758a91f1cBrett Chabotpublic class LegacyMatcherProvider implements Serializable {
2947d431f63a66505a645f282416659a9758a91f1cBrett Chabot
3047d431f63a66505a645f282416659a9758a91f1cBrett Chabot    private static final long serialVersionUID = -4143082656571251917L;
3147d431f63a66505a645f282416659a9758a91f1cBrett Chabot
3247d431f63a66505a645f282416659a9758a91f1cBrett Chabot    private ArgumentsMatcher defaultMatcher;
3347d431f63a66505a645f282416659a9758a91f1cBrett Chabot
3447d431f63a66505a645f282416659a9758a91f1cBrett Chabot    private boolean defaultMatcherSet;
3547d431f63a66505a645f282416659a9758a91f1cBrett Chabot
3647d431f63a66505a645f282416659a9758a91f1cBrett Chabot    private transient Map<Method, ArgumentsMatcher> matchers = new HashMap<Method, ArgumentsMatcher>();
3747d431f63a66505a645f282416659a9758a91f1cBrett Chabot
3847d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public ArgumentsMatcher getMatcher(Method method) {
3947d431f63a66505a645f282416659a9758a91f1cBrett Chabot        if (!matchers.containsKey(method)) {
4047d431f63a66505a645f282416659a9758a91f1cBrett Chabot            if (!defaultMatcherSet) {
4147d431f63a66505a645f282416659a9758a91f1cBrett Chabot                setDefaultMatcher(MockControl.EQUALS_MATCHER);
4247d431f63a66505a645f282416659a9758a91f1cBrett Chabot            }
4347d431f63a66505a645f282416659a9758a91f1cBrett Chabot            matchers.put(method, defaultMatcher);
4447d431f63a66505a645f282416659a9758a91f1cBrett Chabot        }
4547d431f63a66505a645f282416659a9758a91f1cBrett Chabot        return matchers.get(method);
4647d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
4747d431f63a66505a645f282416659a9758a91f1cBrett Chabot
4847d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void setDefaultMatcher(ArgumentsMatcher matcher) {
4947d431f63a66505a645f282416659a9758a91f1cBrett Chabot        if (defaultMatcherSet) {
5047d431f63a66505a645f282416659a9758a91f1cBrett Chabot            throw new RuntimeExceptionWrapper(
5147d431f63a66505a645f282416659a9758a91f1cBrett Chabot                    new IllegalStateException(
5247d431f63a66505a645f282416659a9758a91f1cBrett Chabot                            "default matcher can only be set once directly after creation of the MockControl"));
5347d431f63a66505a645f282416659a9758a91f1cBrett Chabot        }
5447d431f63a66505a645f282416659a9758a91f1cBrett Chabot        defaultMatcher = matcher;
5547d431f63a66505a645f282416659a9758a91f1cBrett Chabot        defaultMatcherSet = true;
5647d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
5747d431f63a66505a645f282416659a9758a91f1cBrett Chabot
5847d431f63a66505a645f282416659a9758a91f1cBrett Chabot    public void setMatcher(Method method, ArgumentsMatcher matcher) {
5947d431f63a66505a645f282416659a9758a91f1cBrett Chabot        if (matchers.containsKey(method) && matchers.get(method) != matcher) {
6047d431f63a66505a645f282416659a9758a91f1cBrett Chabot            throw new RuntimeExceptionWrapper(new IllegalStateException(
6147d431f63a66505a645f282416659a9758a91f1cBrett Chabot                    "for method "
6247d431f63a66505a645f282416659a9758a91f1cBrett Chabot                            + method.getName()
6347d431f63a66505a645f282416659a9758a91f1cBrett Chabot                            + "("
6447d431f63a66505a645f282416659a9758a91f1cBrett Chabot                            + (method.getParameterTypes().length == 0 ? ""
6547d431f63a66505a645f282416659a9758a91f1cBrett Chabot                                    : "...")
6647d431f63a66505a645f282416659a9758a91f1cBrett Chabot                            + "), a matcher has already been set"));
6747d431f63a66505a645f282416659a9758a91f1cBrett Chabot        }
6847d431f63a66505a645f282416659a9758a91f1cBrett Chabot        matchers.put(method, matcher);
6947d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
7047d431f63a66505a645f282416659a9758a91f1cBrett Chabot
7147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    @SuppressWarnings("unchecked")
7247d431f63a66505a645f282416659a9758a91f1cBrett Chabot    private void readObject(java.io.ObjectInputStream stream)
7347d431f63a66505a645f282416659a9758a91f1cBrett Chabot            throws IOException, ClassNotFoundException {
7447d431f63a66505a645f282416659a9758a91f1cBrett Chabot        stream.defaultReadObject();
7547d431f63a66505a645f282416659a9758a91f1cBrett Chabot        Map<MethodSerializationWrapper, ArgumentsMatcher> map = (Map<MethodSerializationWrapper, ArgumentsMatcher>) stream
7647d431f63a66505a645f282416659a9758a91f1cBrett Chabot                .readObject();
7747d431f63a66505a645f282416659a9758a91f1cBrett Chabot        matchers = new HashMap<Method, ArgumentsMatcher>(map.size());
7847d431f63a66505a645f282416659a9758a91f1cBrett Chabot        for (Map.Entry<MethodSerializationWrapper, ArgumentsMatcher> entry : map
7947d431f63a66505a645f282416659a9758a91f1cBrett Chabot                .entrySet()) {
8047d431f63a66505a645f282416659a9758a91f1cBrett Chabot            try {
8147d431f63a66505a645f282416659a9758a91f1cBrett Chabot                Method method = entry.getKey().getMethod();
8247d431f63a66505a645f282416659a9758a91f1cBrett Chabot                matchers.put(method, entry.getValue());
8347d431f63a66505a645f282416659a9758a91f1cBrett Chabot            } catch (NoSuchMethodException e) {
8447d431f63a66505a645f282416659a9758a91f1cBrett Chabot                // ///CLOVER:OFF
8547d431f63a66505a645f282416659a9758a91f1cBrett Chabot                throw new IOException(e.toString());
8647d431f63a66505a645f282416659a9758a91f1cBrett Chabot                // ///CLOVER:ON
8747d431f63a66505a645f282416659a9758a91f1cBrett Chabot            }
8847d431f63a66505a645f282416659a9758a91f1cBrett Chabot        }
8947d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
9047d431f63a66505a645f282416659a9758a91f1cBrett Chabot
9147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    private void writeObject(java.io.ObjectOutputStream stream)
9247d431f63a66505a645f282416659a9758a91f1cBrett Chabot            throws IOException {
9347d431f63a66505a645f282416659a9758a91f1cBrett Chabot        stream.defaultWriteObject();
9447d431f63a66505a645f282416659a9758a91f1cBrett Chabot        Map<MethodSerializationWrapper, ArgumentsMatcher> map = new HashMap<MethodSerializationWrapper, ArgumentsMatcher>(
9547d431f63a66505a645f282416659a9758a91f1cBrett Chabot                matchers.size());
9647d431f63a66505a645f282416659a9758a91f1cBrett Chabot        for (Map.Entry<Method, ArgumentsMatcher> matcher : matchers.entrySet()) {
9747d431f63a66505a645f282416659a9758a91f1cBrett Chabot            map.put(new MethodSerializationWrapper(matcher.getKey()), matcher
9847d431f63a66505a645f282416659a9758a91f1cBrett Chabot                    .getValue());
9947d431f63a66505a645f282416659a9758a91f1cBrett Chabot        }
10047d431f63a66505a645f282416659a9758a91f1cBrett Chabot        stream.writeObject(map);
10147d431f63a66505a645f282416659a9758a91f1cBrett Chabot    }
10247d431f63a66505a645f282416659a9758a91f1cBrett Chabot}
103