1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.listeners;
6
7import org.mockito.invocation.DescribedInvocation;
8import org.mockito.invocation.Invocation;
9import org.mockito.listeners.MethodInvocationReport;
10
11/**
12 * Report on a method call
13 */
14public class NotifiedMethodInvocationReport implements MethodInvocationReport {
15    private final Invocation invocation;
16    private Object returnedValue;
17    private Throwable throwable;
18
19
20    /**
21     * Build a new {@link org.mockito.listeners.MethodInvocationReport} with a return value.
22     *
23     *
24     * @param invocation Information on the method call
25     * @param returnedValue The value returned by the method invocation
26     */
27    public NotifiedMethodInvocationReport(Invocation invocation, Object returnedValue) {
28        this.invocation = invocation;
29        this.returnedValue = returnedValue;
30    }
31
32    /**
33     * Build a new {@link org.mockito.listeners.MethodInvocationReport} with a return value.
34     *
35     *
36     * @param invocation Information on the method call
37     * @param throwable Tha throwable raised by the method invocation
38     */
39    public NotifiedMethodInvocationReport(Invocation invocation, Throwable throwable) {
40        this.invocation = invocation;
41        this.throwable = throwable;
42    }
43
44    public DescribedInvocation getInvocation() {
45        return invocation;
46    }
47
48    public Object getReturnedValue() {
49        return returnedValue;
50    }
51
52    public Throwable getThrowable() {
53        return throwable;
54    }
55
56    public boolean threwException() {
57        return throwable != null;
58    }
59
60    public String getLocationOfStubbing() {
61        return (invocation.stubInfo() == null) ? null : invocation.stubInfo().stubbedAt().toString();
62    }
63
64
65    public boolean equals(Object o) {
66        if (this == o) return true;
67        if (o == null || getClass() != o.getClass()) return false;
68
69        NotifiedMethodInvocationReport that = (NotifiedMethodInvocationReport) o;
70
71        if (invocation != null ? !invocation.equals(that.invocation) : that.invocation != null) return false;
72        if (returnedValue != null ? !returnedValue.equals(that.returnedValue) : that.returnedValue != null)
73            return false;
74        if (throwable != null ? !throwable.equals(that.throwable) : that.throwable != null) return false;
75
76        return true;
77    }
78
79    public int hashCode() {
80        int result = invocation != null ? invocation.hashCode() : 0;
81        result = 31 * result + (returnedValue != null ? returnedValue.hashCode() : 0);
82        result = 31 * result + (throwable != null ? throwable.hashCode() : 0);
83        return result;
84    }
85}
86