• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockito.internal.progress;
7 
8 import org.mockito.MockSettings;
9 import org.mockito.exceptions.Reporter;
10 import org.mockito.internal.configuration.GlobalConfiguration;
11 import org.mockito.internal.debugging.Localized;
12 import org.mockito.internal.debugging.LocationImpl;
13 import org.mockito.internal.listeners.MockingProgressListener;
14 import org.mockito.internal.listeners.MockingStartedListener;
15 import org.mockito.invocation.Invocation;
16 import org.mockito.invocation.Location;
17 import org.mockito.verification.VerificationMode;
18 
19 @SuppressWarnings("unchecked")
20 public 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 
reportOngoingStubbing(IOngoingStubbing iOngoingStubbing)30     public void reportOngoingStubbing(IOngoingStubbing iOngoingStubbing) {
31         this.iOngoingStubbing = iOngoingStubbing;
32     }
33 
pullOngoingStubbing()34     public IOngoingStubbing pullOngoingStubbing() {
35         IOngoingStubbing temp = iOngoingStubbing;
36         iOngoingStubbing = null;
37         return temp;
38     }
39 
verificationStarted(VerificationMode verify)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      */
resetOngoingStubbing()49     public void resetOngoingStubbing() {
50         iOngoingStubbing = null;
51     }
52 
pullVerificationMode()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 
stubbingStarted()63     public void stubbingStarted() {
64         validateState();
65         stubbingInProgress = new LocationImpl();
66     }
67 
validateState()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 
validateMostStuff()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 
stubbingCompleted(Invocation invocation)93     public void stubbingCompleted(Invocation invocation) {
94         stubbingInProgress = null;
95     }
96 
toString()97     public String toString() {
98         return  "iOngoingStubbing: " + iOngoingStubbing +
99         ", verificationMode: " + verificationMode +
100         ", stubbingInProgress: " + stubbingInProgress;
101     }
102 
reset()103     public void reset() {
104         stubbingInProgress = null;
105         verificationMode = null;
106         getArgumentMatcherStorage().reset();
107     }
108 
getArgumentMatcherStorage()109     public ArgumentMatcherStorage getArgumentMatcherStorage() {
110         return argumentMatcherStorage;
111     }
112 
mockingStarted(Object mock, Class classToMock)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 
setListener(MockingProgressListener listener)120     public void setListener(MockingProgressListener listener) {
121         this.listener = listener;
122     }
123 }