Home | History | Annotate | Download | only in progress
      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.internal.configuration.GlobalConfiguration;
      9 import org.mockito.internal.debugging.Localized;
     10 import org.mockito.internal.debugging.LocationImpl;
     11 import org.mockito.internal.exceptions.Reporter;
     12 import org.mockito.invocation.Location;
     13 import org.mockito.listeners.MockCreationListener;
     14 import org.mockito.listeners.MockitoListener;
     15 import org.mockito.listeners.VerificationListener;
     16 import org.mockito.mock.MockCreationSettings;
     17 import org.mockito.stubbing.OngoingStubbing;
     18 import org.mockito.verification.VerificationMode;
     19 import org.mockito.verification.VerificationStrategy;
     20 
     21 import java.util.LinkedHashSet;
     22 import java.util.Set;
     23 
     24 import static org.mockito.internal.exceptions.Reporter.unfinishedStubbing;
     25 import static org.mockito.internal.exceptions.Reporter.unfinishedVerificationException;
     26 
     27 @SuppressWarnings("unchecked")
     28 public class MockingProgressImpl implements MockingProgress {
     29 
     30     private final ArgumentMatcherStorage argumentMatcherStorage = new ArgumentMatcherStorageImpl();
     31 
     32     private OngoingStubbing<?> ongoingStubbing;
     33     private Localized<VerificationMode> verificationMode;
     34     private Location stubbingInProgress = null;
     35     private VerificationStrategy verificationStrategy;
     36     private final Set<MockitoListener> listeners = new LinkedHashSet<MockitoListener>();
     37 
     38     public MockingProgressImpl() {
     39         this.verificationStrategy = getDefaultVerificationStrategy();
     40     }
     41 
     42     public static VerificationStrategy getDefaultVerificationStrategy() {
     43         return new VerificationStrategy() {
     44             public VerificationMode maybeVerifyLazily(VerificationMode mode) {
     45                 return mode;
     46             }
     47         };
     48     }
     49 
     50     public void reportOngoingStubbing(OngoingStubbing ongoingStubbing) {
     51         this.ongoingStubbing = ongoingStubbing;
     52     }
     53 
     54     public OngoingStubbing<?> pullOngoingStubbing() {
     55         OngoingStubbing<?> temp = ongoingStubbing;
     56         ongoingStubbing = null;
     57         return temp;
     58     }
     59 
     60     @Override
     61     public Set<VerificationListener> verificationListeners() {
     62         final LinkedHashSet<VerificationListener> verificationListeners = new LinkedHashSet<VerificationListener>();
     63 
     64         for (MockitoListener listener : listeners) {
     65             if (listener instanceof VerificationListener) {
     66                 verificationListeners.add((VerificationListener) listener);
     67             }
     68         }
     69 
     70         return verificationListeners;
     71     }
     72 
     73 
     74     public void verificationStarted(VerificationMode verify) {
     75         validateState();
     76         resetOngoingStubbing();
     77         verificationMode = new Localized(verify);
     78     }
     79 
     80     /* (non-Javadoc)
     81      * @see org.mockito.internal.progress.MockingProgress#resetOngoingStubbing()
     82      */
     83     public void resetOngoingStubbing() {
     84         ongoingStubbing = null;
     85     }
     86 
     87     public VerificationMode pullVerificationMode() {
     88         if (verificationMode == null) {
     89             return null;
     90         }
     91 
     92         VerificationMode temp = verificationMode.getObject();
     93         verificationMode = null;
     94         return temp;
     95     }
     96 
     97     public void stubbingStarted() {
     98         validateState();
     99         stubbingInProgress = new LocationImpl();
    100     }
    101 
    102     public void validateState() {
    103         validateMostStuff();
    104 
    105         //validate stubbing:
    106         if (stubbingInProgress != null) {
    107             Location temp = stubbingInProgress;
    108             stubbingInProgress = null;
    109             throw unfinishedStubbing(temp);
    110         }
    111     }
    112 
    113     private void validateMostStuff() {
    114         //State is cool when GlobalConfiguration is already loaded
    115         //this cannot really be tested functionally because I cannot dynamically mess up org.mockito.configuration.MockitoConfiguration class
    116         GlobalConfiguration.validate();
    117 
    118         if (verificationMode != null) {
    119             Location location = verificationMode.getLocation();
    120             verificationMode = null;
    121             throw unfinishedVerificationException(location);
    122         }
    123 
    124         getArgumentMatcherStorage().validateState();
    125     }
    126 
    127     public void stubbingCompleted() {
    128         stubbingInProgress = null;
    129     }
    130 
    131     public String toString() {
    132         return  "ongoingStubbing: " + ongoingStubbing +
    133         ", verificationMode: " + verificationMode +
    134         ", stubbingInProgress: " + stubbingInProgress;
    135     }
    136 
    137     public void reset() {
    138         stubbingInProgress = null;
    139         verificationMode = null;
    140         getArgumentMatcherStorage().reset();
    141     }
    142 
    143     public ArgumentMatcherStorage getArgumentMatcherStorage() {
    144         return argumentMatcherStorage;
    145     }
    146 
    147     public void mockingStarted(Object mock, MockCreationSettings settings) {
    148         for (MockitoListener listener : listeners) {
    149             if (listener instanceof MockCreationListener) {
    150                 ((MockCreationListener) listener).onMockCreated(mock, settings);
    151             }
    152         }
    153         validateMostStuff();
    154     }
    155 
    156     public void addListener(MockitoListener listener) {
    157         for (MockitoListener existing : listeners) {
    158             if (existing.getClass().equals(listener.getClass())) {
    159                 Reporter.redundantMockitoListener(listener.getClass().getSimpleName());
    160             }
    161         }
    162         this.listeners.add(listener);
    163     }
    164 
    165     public void removeListener(MockitoListener listener) {
    166         this.listeners.remove(listener);
    167     }
    168 
    169     public void setVerificationStrategy(VerificationStrategy strategy) {
    170         this.verificationStrategy = strategy;
    171     }
    172 
    173     public VerificationMode maybeVerifyLazily(VerificationMode mode) {
    174         return this.verificationStrategy.maybeVerifyLazily(mode);
    175     }
    176 
    177     public void clearListeners() {
    178         listeners.clear();
    179     }
    180 
    181      /*
    182 
    183      //TODO 545 thread safety of all mockito
    184 
    185      use cases:
    186         - single threaded execution throughout
    187         - single threaded mock creation, stubbing & verification, multi-threaded interaction with mock
    188         - thread per test case
    189 
    190      */
    191 }
    192