• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2001-2009 OFFIS, Tammo Freese
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.easymock.internal;
17 
18 import java.io.IOException;
19 import java.io.Serializable;
20 import java.lang.reflect.Method;
21 import java.util.HashMap;
22 import java.util.Map;
23 
24 import org.easymock.ArgumentsMatcher;
25 import org.easymock.MockControl;
26 
27 @SuppressWarnings("deprecation")
28 public class LegacyMatcherProvider implements Serializable {
29 
30     private static final long serialVersionUID = -4143082656571251917L;
31 
32     private ArgumentsMatcher defaultMatcher;
33 
34     private boolean defaultMatcherSet;
35 
36     private transient Map<Method, ArgumentsMatcher> matchers = new HashMap<Method, ArgumentsMatcher>();
37 
getMatcher(Method method)38     public ArgumentsMatcher getMatcher(Method method) {
39         if (!matchers.containsKey(method)) {
40             if (!defaultMatcherSet) {
41                 setDefaultMatcher(MockControl.EQUALS_MATCHER);
42             }
43             matchers.put(method, defaultMatcher);
44         }
45         return matchers.get(method);
46     }
47 
setDefaultMatcher(ArgumentsMatcher matcher)48     public void setDefaultMatcher(ArgumentsMatcher matcher) {
49         if (defaultMatcherSet) {
50             throw new RuntimeExceptionWrapper(
51                     new IllegalStateException(
52                             "default matcher can only be set once directly after creation of the MockControl"));
53         }
54         defaultMatcher = matcher;
55         defaultMatcherSet = true;
56     }
57 
setMatcher(Method method, ArgumentsMatcher matcher)58     public void setMatcher(Method method, ArgumentsMatcher matcher) {
59         if (matchers.containsKey(method) && matchers.get(method) != matcher) {
60             throw new RuntimeExceptionWrapper(new IllegalStateException(
61                     "for method "
62                             + method.getName()
63                             + "("
64                             + (method.getParameterTypes().length == 0 ? ""
65                                     : "...")
66                             + "), a matcher has already been set"));
67         }
68         matchers.put(method, matcher);
69     }
70 
71     @SuppressWarnings("unchecked")
readObject(java.io.ObjectInputStream stream)72     private void readObject(java.io.ObjectInputStream stream)
73             throws IOException, ClassNotFoundException {
74         stream.defaultReadObject();
75         Map<MethodSerializationWrapper, ArgumentsMatcher> map = (Map<MethodSerializationWrapper, ArgumentsMatcher>) stream
76                 .readObject();
77         matchers = new HashMap<Method, ArgumentsMatcher>(map.size());
78         for (Map.Entry<MethodSerializationWrapper, ArgumentsMatcher> entry : map
79                 .entrySet()) {
80             try {
81                 Method method = entry.getKey().getMethod();
82                 matchers.put(method, entry.getValue());
83             } catch (NoSuchMethodException e) {
84                 // ///CLOVER:OFF
85                 throw new IOException(e.toString());
86                 // ///CLOVER:ON
87             }
88         }
89     }
90 
writeObject(java.io.ObjectOutputStream stream)91     private void writeObject(java.io.ObjectOutputStream stream)
92             throws IOException {
93         stream.defaultWriteObject();
94         Map<MethodSerializationWrapper, ArgumentsMatcher> map = new HashMap<MethodSerializationWrapper, ArgumentsMatcher>(
95                 matchers.size());
96         for (Map.Entry<Method, ArgumentsMatcher> matcher : matchers.entrySet()) {
97             map.put(new MethodSerializationWrapper(matcher.getKey()), matcher
98                     .getValue());
99         }
100         stream.writeObject(map);
101     }
102 }
103