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

Groovy example source code file (StrictExpectation.groovy)

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

Java - Groovy tags/keywords

assertionfailederror, assertionfailederror, closure, closure, demand, end, exception, list, no, no, still, strictexpectation, strictexpectation

The Groovy StrictExpectation.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
 * 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 in the sequence of recording.
 *  @see LooseExpectation
 *  @author Dierk Koenig
class StrictExpectation {
    Demand fDemand  = null
    int fCallSpecIdx = 0
    List fCalls      = []

    StrictExpectation(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)
        if (!fCalls[fCallSpecIdx]) fCalls[fCallSpecIdx] = 0

        if (fCallSpecIdx >= fDemand.recorded.size()) {
            throw new AssertionFailedError("No more calls to '$name' expected at this point. End of demands.")

        def call = fDemand.recorded[fCallSpecIdx]
        if (name != {                             // if name does not match...
            def open = call.range.from - fCalls[fCallSpecIdx]
            if ( open > 0) {                                 // ... if we haven't reached the minimum, yet -> Exception
                throw new AssertionFailedError("No call to '$name' expected at this point. "+
                "Still $open call(s) to '${}' expected.")
            } else {                                         // ... proceed finding
                return match(name)

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

        // store the behavior for returning
        def result = call.behavior

        // proceed to next callSpec if we need to
        if (fCalls[fCallSpecIdx] >= ) fCallSpecIdx++

        return result

    /** 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 StrictExpectation.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
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.