How to use method of org.easymock.internal.Result class

Best Easymock code snippet using org.easymock.internal.Result.

Run Easymock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 * Copyright 2001-2021 the original author or authors.
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 */
16package org.easymock.internal;
17
18import org.easymock.IAnswer;
19
20import java.io.Serializable;
21import java.lang.reflect.Array;
22import java.lang.reflect.InvocationTargetException;
23import java.lang.reflect.Method;
24
25/**
26 * @author OFFIS, Tammo Freese
27 */
28public final class Result implements IAnswer<Object>, Serializable {
29
30    private static final long serialVersionUID = 5476251941213917681L;
31
32    private final IAnswer<?> value;
33
34    private final boolean shouldFillInStackTrace;
35
36    private Result(IAnswer<?> value, boolean shouldFillInStackTrace) {
37        this.value = value;
38        this.shouldFillInStackTrace = shouldFillInStackTrace;
39    }
40
41    public static Result createThrowResult(final Throwable throwable) {
42        class ThrowingAnswer implements IAnswer<Object>, Serializable {
43
44            private static final long serialVersionUID = -332797751209289222L;
45
46            public Object answer() throws Throwable {
47                throw throwable;
48            }
49
50            @Override
51            public String toString() {
52                return "Answer throwing " + throwable;
53            }
54        }
55        return new Result(new ThrowingAnswer(), true);
56    }
57
58    public static Result createReturnResult(final Object value) {
59        class ReturningAnswer implements IAnswer<Object>, Serializable {
60
61            private static final long serialVersionUID = 6973893913593916866L;
62
63            public Object answer() {
64                return value;
65            }
66
67            @Override
68            public String toString() {
69                return "Answer returning " + value;
70            }
71        }
72        return new Result(new ReturningAnswer(), true);
73    }
74
75    public static Result createDelegatingResult(final Object value) {
76        class DelegatingAnswer implements IAnswer<Object>, Serializable {
77
78            private static final long serialVersionUID = -5699326678580460103L;
79
80            public Object answer() throws Throwable {
81                Invocation invocation = LastControl.getCurrentInvocation();
82                try {
83                    Method m = invocation.getMethod();
84                    if(!m.isAccessible()) {
85                        m.setAccessible(true);
86                    }
87                    return m.invoke(value, convertVarargs(m, invocation.getArguments()));
88                } catch (IllegalArgumentException e) {
89                    throw new IllegalArgumentException("Delegation to object [" + value
90                            + "] is not implementing the mocked method [" + invocation.getMethod() + "]", e);
91                } catch (InvocationTargetException e) {
92                    throw e.getCause();
93                }
94            }
95
96            private Object[] convertVarargs(Method method, Object[] arguments) {
97                if(!method.isVarArgs()) {
98                    return arguments;
99                }
100                Class<?>[] paramTypes = method.getParameterTypes();
101                Object[] packedArguments = new Object[paramTypes.length];
102
103                int normalArgLength = paramTypes.length - 1;
104                int varargLength = arguments.length - paramTypes.length + 1;
105
106                // Copy all the non varargs arguments
107                System.arraycopy(arguments, 0, packedArguments, 0, normalArgLength);
108
109                // Now copy the varargs ones to a new array
110                Object varargs = Array.newInstance(paramTypes[packedArguments.length-1].getComponentType(), varargLength);
111                for (int i = 0; i < varargLength; i++) {
112                    Array.set(varargs, i, arguments[normalArgLength + i]);
113                }
114
115                // Put the vararg at the last place
116                packedArguments[packedArguments.length - 1] = varargs;
117                return packedArguments;
118            }
119
120            @Override
121            public String toString() {
122                return "Delegated to " + value;
123            }
124        }
125        return new Result(new DelegatingAnswer(), false);
126    }
127
128    public static Result createAnswerResult(IAnswer<?> answer) {
129        return new Result(answer, false);
130    }
131
132    public Object answer() throws Throwable {
133        return value.answer();
134    }
135
136    public boolean shouldFillInStackTrace() {
137        return shouldFillInStackTrace;
138    }
139
140    @Override
141    public String toString() {
142        return value.toString();
143    }
144}
145
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)