1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5
6package org.mockito.internal.progress;
7
8import org.mockito.MockSettings;
9import org.mockito.exceptions.Reporter;
10import org.mockito.internal.configuration.GlobalConfiguration;
11import org.mockito.internal.debugging.Localized;
12import org.mockito.internal.debugging.LocationImpl;
13import org.mockito.internal.listeners.MockingProgressListener;
14import org.mockito.internal.listeners.MockingStartedListener;
15import org.mockito.invocation.Invocation;
16import org.mockito.invocation.Location;
17import org.mockito.verification.VerificationMode;
18
19@SuppressWarnings("unchecked")
20public class MockingProgressImpl implements MockingProgress {
21
22    private final Reporter reporter = new Reporter();
23    private final ArgumentMatcherStorage argumentMatcherStorage = new ArgumentMatcherStorageImpl();
24
25    IOngoingStubbing iOngoingStubbing;
26    private Localized<VerificationMode> verificationMode;
27    private Location stubbingInProgress = null;
28    private MockingProgressListener listener;
29
30    public void reportOngoingStubbing(IOngoingStubbing iOngoingStubbing) {
31        this.iOngoingStubbing = iOngoingStubbing;
32    }
33
34    public IOngoingStubbing pullOngoingStubbing() {
35        IOngoingStubbing temp = iOngoingStubbing;
36        iOngoingStubbing = null;
37        return temp;
38    }
39
40    public void verificationStarted(VerificationMode verify) {
41        validateState();
42        resetOngoingStubbing();
43        verificationMode = new Localized(verify);
44    }
45
46    /* (non-Javadoc)
47     * @see org.mockito.internal.progress.MockingProgress#resetOngoingStubbing()
48     */
49    public void resetOngoingStubbing() {
50        iOngoingStubbing = null;
51    }
52
53    public VerificationMode pullVerificationMode() {
54        if (verificationMode == null) {
55            return null;
56        }
57
58        VerificationMode temp = verificationMode.getObject();
59        verificationMode = null;
60        return temp;
61    }
62
63    public void stubbingStarted() {
64        validateState();
65        stubbingInProgress = new LocationImpl();
66    }
67
68    public void validateState() {
69        validateMostStuff();
70
71        //validate stubbing:
72        if (stubbingInProgress != null) {
73            Location temp = stubbingInProgress;
74            stubbingInProgress = null;
75            reporter.unfinishedStubbing(temp);
76        }
77    }
78
79    private void validateMostStuff() {
80        //State is cool when GlobalConfiguration is already loaded
81        //this cannot really be tested functionally because I cannot dynamically mess up org.mockito.configuration.MockitoConfiguration class
82        GlobalConfiguration.validate();
83
84        if (verificationMode != null) {
85            Location location = verificationMode.getLocation();
86            verificationMode = null;
87            reporter.unfinishedVerificationException(location);
88        }
89
90        getArgumentMatcherStorage().validateState();
91    }
92
93    public void stubbingCompleted(Invocation invocation) {
94        stubbingInProgress = null;
95    }
96
97    public String toString() {
98        return  "iOngoingStubbing: " + iOngoingStubbing +
99        ", verificationMode: " + verificationMode +
100        ", stubbingInProgress: " + stubbingInProgress;
101    }
102
103    public void reset() {
104        stubbingInProgress = null;
105        verificationMode = null;
106        getArgumentMatcherStorage().reset();
107    }
108
109    public ArgumentMatcherStorage getArgumentMatcherStorage() {
110        return argumentMatcherStorage;
111    }
112
113    public void mockingStarted(Object mock, Class classToMock) {
114        if (listener != null && listener instanceof MockingStartedListener) {
115            ((MockingStartedListener) listener).mockingStarted(mock, classToMock);
116        }
117        validateMostStuff();
118    }
119
120    public void setListener(MockingProgressListener listener) {
121        this.listener = listener;
122    }
123}