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;

Author(s):
Tim Ducheyne
Filip Neven
    protected boolean removeWhenUsed;
    public BehaviorDefiningInvocations(boolean removeWhenUsed) {
        this. = removeWhenUsed;
    }
    public void addBehaviorDefiningInvocation(BehaviorDefiningInvocation behaviorDefiningInvocation) {
        .add(behaviorDefiningInvocation);
    }
    public void clear() {
    }


    
First we find all behavior defining invocations that have matching argument matchers and take the one with the highest matching score (identity match scores higher than an equals match). If there are 2 invocations with the same score, we take the invocation with the lowest nr of not-null (default) arguments. If both have the same nr of not-null arguments, the first one is returned. E.g. myMethod(null, null); myMethod("a", null); The second one will be returned if the given proxy invocation has the value "a" as first argument.

Parameters:
proxyInvocation The actual invocation to match with, not null
Returns:
The behavior defining invocation that matches best with the actual invocation, null if none found
        BehaviorDefiningInvocation bestMatchingBehaviorDefiningInvocation = null;
        int bestMatchingScore = -1;
        for (BehaviorDefiningInvocation behaviorDefiningInvocation : ) {
            int matchingScore = behaviorDefiningInvocation.matches(proxyInvocation);
            if (matchingScore == -1) {
                // no match
                continue;
            }
            if (matchingScore < bestMatchingScore) {
                // there is a better match
                continue;
            }
            if (matchingScore > bestMatchingScore) {
                // better match
                bestMatchingScore = matchingScore;
                bestMatchingBehaviorDefiningInvocation = behaviorDefiningInvocation;
                continue;
            }
            if (matchingScore == bestMatchingScore) {
                // same score, nr of not-null values determines the best match
                int nrOfNotNullArguments = behaviorDefiningInvocation.getNrOfNotNullArguments();
                int bestMatchingNrOfNotNullArguments = bestMatchingBehaviorDefiningInvocation.getNrOfNotNullArguments();
                if (nrOfNotNullArguments > bestMatchingNrOfNotNullArguments) {
                    bestMatchingScore = matchingScore;
                    bestMatchingBehaviorDefiningInvocation = behaviorDefiningInvocation;
                }
            }
        }
        if ( && bestMatchingBehaviorDefiningInvocation != null) {
            .remove(bestMatchingBehaviorDefiningInvocation);
        }
        return bestMatchingBehaviorDefiningInvocation;
    }
New to GrepCode? Check out our FAQ X