alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

EasyMock example source code file (Results.java)

This example EasyMock source code file (Results.java) 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 - EasyMock tags/keywords

arraylist, arraylist, illegalstateexception, linkedlist, linkedlist, list, override, range, range, result, results, runtimeexceptionwrapper, string, string, util

The EasyMock Results.java source code

/*
 * Copyright (c) 2001-2007 OFFIS, Tammo Freese.
 * This program is made available under the terms of the MIT License.
 */
package org.easymock.internal;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Results {

    private int callCount;

    private LinkedList<Range> ranges = new LinkedList();

    private List<Result> results = new ArrayList();

    public void add(Result result, Range range) {
        if (!ranges.isEmpty()) {
            Range lastRange = ranges.getLast();
            if (!lastRange.hasFixedCount())
                throw new RuntimeExceptionWrapper(
                        new IllegalStateException(
                                "last method called on mock already has a non-fixed count set."));
        }
        ranges.add(range);
        results.add(result);
    }

    public Result next() {
        int currentPosition = 0;
        for (int i = 0; i < ranges.size(); i++) {
            Range interval = ranges.get(i);
            if (interval.hasOpenCount()) {
                callCount += 1;
                return results.get(i);
            }
            currentPosition += interval.getMaximum();
            if (currentPosition > callCount) {
                callCount += 1;
                return results.get(i);
            }
        }
        return null;
    }

    public boolean hasValidCallCount() {
        return getMainInterval().contains(getCallCount());
    }

    @Override
    public String toString() {
        return getMainInterval().expectedAndActual(getCallCount());
    }

    private Range getMainInterval() {
        int min = 0, max = 0;

        for (Range interval : ranges) {
            min += interval.getMinimum();
            if (interval.hasOpenCount() || max == Integer.MAX_VALUE) {
                max = Integer.MAX_VALUE;
            } else {
                max += interval.getMaximum();
            }
        }

        return new Range(min, max);
    }

    public int getCallCount() {
        return callCount;
    }
}

Other EasyMock examples (source code examples)

Here is a short list of links related to this EasyMock Results.java source code file:

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.