All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockito.internal.debugging.DebuggingInfo Maven / Gradle / Ivy

There is a newer version: 2.0.2-beta
Show newest version
/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package org.mockito.internal.debugging;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.mockito.internal.invocation.Invocation;
import org.mockito.internal.invocation.InvocationMatcher;
import org.mockito.internal.util.MockitoLogger;

public class DebuggingInfo {

    private final List unusedStubs = new LinkedList();
    private final List unstubbedInvocations = new LinkedList();

    private boolean collectingData;

    public void addStubbedInvocation(Invocation invocation) {
        if (!collectingData) {
            return;
        }
        
        Iterator unstubbedIterator = unstubbedInvocations.iterator();
        while(unstubbedIterator.hasNext()) {
            InvocationMatcher unstubbed = unstubbedIterator.next();
            if (unstubbed.getInvocation().equals(invocation)) {
                unstubbedIterator.remove();
            }
        }
        
        unusedStubs.add(invocation);
    }

    public void addPotentiallyUnstubbed(InvocationMatcher invocationMatcher) {
        if (!collectingData) {
            return;
        }
        unstubbedInvocations.add(invocationMatcher);
    }
    
    public void reportUsedStub(InvocationMatcher invocationMatcher) {
        Iterator i = unusedStubs.iterator();
        while(i.hasNext()) {
            Invocation invocation = i.next();
            if (invocationMatcher.matches(invocation)) {
                i.remove();
            }
        }
    }

    public void collectData() {
        collectingData = true;
    }

    public void clearData() {
        collectingData = false;
        unstubbedInvocations.clear();
        unusedStubs.clear();
    }

    public void printWarnings(MockitoLogger logger) {
        if (hasData()) {
            new WarningsPrinterImpl(unusedStubs, unstubbedInvocations, false).print(logger);
        }
    }

    public boolean hasData() {
        return !unusedStubs.isEmpty() || !unstubbedInvocations.isEmpty();
    }

    public String getWarnings(boolean warnAboutUnstubbed) {
        final StringBuilder sb = new StringBuilder();
        if (hasData()) {
            new WarningsPrinterImpl(unusedStubs, unstubbedInvocations, warnAboutUnstubbed).print(new MockitoLogger() {
                public void log(Object what) {
                    sb.append(what);
                }});
        }
        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy