home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (LooseExpectation.groovy)

This example Groovy source code file (LooseExpectation.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

assertionfailederror, closure, closure, demand, demand, end, list, looseexpectation, looseexpectation, no, no

The Groovy LooseExpectation.groovy source code

 * Copyright 2003-2010 the original author or authors.
 * 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package groovy.mock.interceptor

import junit.framework.AssertionFailedError
import org.codehaus.groovy.runtime.DefaultGroovyMethods

 *  Expects demanded call cardinalities to match demanded ranges.
 *  The calls are allowed to be out of the recorded sequence.
 *  If a method is demanded multiple times, the ranges are filled by order of recording.
 *  @see StrictExpectation
 *  @author Dierk Koenig
class LooseExpectation {
    Demand fDemand  = null
    List fCalls      = []

    LooseExpectation(Demand demand) {
        fDemand = demand

     * Match the requested method name against eligible demands.
     * Fail early if no match possible.
     * Return the demand's behavior closure on match.
     * Also skips over names matching ignore filter, if any.
    Closure match(String name) {
        def filter = fDemand.ignore.keySet().find{ DefaultGroovyMethods.grep([name], it) }
        if (filter) return fDemand.ignore.get(filter)
        def callIndex = 0
        // find first eligible callSpec
        while (! isEligible(name, callIndex) ) callIndex++

        // register the call
        fCalls[callIndex] += 1

        return fDemand.recorded[callIndex].behavior

    boolean isEligible(String name, int i) {
        def calls = fDemand.recorded
        if (i >= calls.size())  {
            throw new AssertionFailedError("No more calls to '$name' expected at this point. End of demands.")
        if (calls[i].name != name)              return false
        if (null == fCalls[i])                  fCalls[i] = 0
        if (fCalls[i] >= calls[i].range.to)     return false
        return true

    /** verify all calls are in expected range */ 
    void verify() {

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy LooseExpectation.groovy source code file:

new blog posts


Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.