Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *
   *  * Copyright 2010,  Unitils.org
   *  *
   *  * Licensed under the Apache License, Version 2.0 (the "License");
   *  * you may not use this file except in compliance with the License.
   *  * You may obtain a copy of the License at
   *  *
   *  *     http://www.apache.org/licenses/LICENSE-2.0
  *  *
  *  * Unless required by applicable law or agreed to in writing, software
  *  * distributed under the License is distributed on an "AS IS" BASIS,
  *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  * See the License for the specific language governing permissions and
  *  * limitations under the License.
  *
  */
 package org.unitils.mock.core;
 
 
 import static org.unitils.mock.core.proxy.ProxyFactory.createProxy;
 
 public class MockProxy<T> {
 
     /* The mock proxy instance */
     protected T proxy;
 
 
 
     /* The scenario that will record all observed invocations */
     protected Scenario scenario;
 
 
 
     public MockProxy(String mockNameClass<T> mockedTypeBehaviorDefiningInvocations oneTimeMatchingBehaviorDefiningInvocationsBehaviorDefiningInvocations alwaysMatchingBehaviorDefiningInvocationsScenario scenarioMatchingInvocationBuilder matchingInvocationBuilder) {
         this. = oneTimeMatchingBehaviorDefiningInvocations;
         this. = alwaysMatchingBehaviorDefiningInvocations;
         this. = scenario;
         this. = matchingInvocationBuilder;
         this. = createProxy(mockNamenew InvocationHandler(), mockedTypeCloneable.class);
     }
 
 
     @SuppressWarnings("unchecked")
     public T getProxy() {
         return ;
     }
 
 
     protected Object handleMockInvocation(ProxyInvocation proxyInvocationthrows Throwable {
 
         BehaviorDefiningInvocation behaviorDefiningInvocation = getMatchingBehaviorDefiningInvocation(proxyInvocation);
         MockBehavior mockBehavior = getValidMockBehavior(proxyInvocationbehaviorDefiningInvocation);
 
         ObservedInvocation observedInvocation = new ObservedInvocation(proxyInvocationbehaviorDefiningInvocationmockBehavior);
         .addObservedMockInvocation(observedInvocation);
 
         Throwable throwable = null;
         Object result = null;
         if (mockBehavior != null) {
             try {
                 result = mockBehavior.execute(proxyInvocation);
             } catch (Throwable t) {
                 throwable = t;
             }
         }
         observedInvocation.setResult(result);
 
         if (throwable != null) {
             throw throwable;
         }
         return result;
     }
 
 
         if (behaviorDefiningInvocation == null) {
             behaviorDefiningInvocation = .getMatchingBehaviorDefiningInvocation(proxyInvocation);
         }
         return behaviorDefiningInvocation;
     }
 
 
     protected MockBehavior getValidMockBehavior(ProxyInvocation proxyInvocationBehaviorDefiningInvocation behaviorDefiningInvocation) {
         if (behaviorDefiningInvocation != null) {
             MockBehavior mockBehavior = behaviorDefiningInvocation.getMockBehavior();
             assertCanExecute(mockBehaviorproxyInvocationbehaviorDefiningInvocation);
            return mockBehavior;
        }
        return getDefaultMockBehavior(proxyInvocation);
    }


    
Check whether the mock behavior can applied for this invocation

Parameters:
mockBehavior The behavior to verify, not null
proxyInvocation The invocation, not null
behaviorDefiningInvocation The invocation that defined the behavior, not null
    protected void assertCanExecute(MockBehavior mockBehaviorProxyInvocation proxyInvocationBehaviorDefiningInvocation behaviorDefiningInvocation) {
        if (!(mockBehavior instanceof ValidatableMockBehavior)) {
            return;
        }
        ValidatableMockBehavior validatableMockBehavior = (ValidatableMockBehaviormockBehavior;
        try {
            validatableMockBehavior.assertCanExecute(proxyInvocation);
        } catch (UnitilsException e) {
            e.setStackTrace(behaviorDefiningInvocation.getInvokedAtTrace());
            throw e;
        }
    }
    protected MockBehavior getDefaultMockBehavior(ProxyInvocation proxyInvocation) {
        if (proxyInvocation.getMethod().getReturnType() == .) {
            return null;
        }
        return new DefaultValueReturningMockBehavior();
    }
    protected class InvocationHandler implements ProxyInvocationHandler {
        public Object handleInvocation(ProxyInvocation invocationthrows Throwable {
            return handleMockInvocation(invocation);
        }
    }
New to GrepCode? Check out our FAQ X