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

com.fitbur.mockito.internal.progress.ArgumentMatcherStorageImpl Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */

package com.fitbur.mockito.internal.progress;

import com.fitbur.mockito.ArgumentMatcher;
import com.fitbur.mockito.internal.matchers.And;
import com.fitbur.mockito.internal.matchers.LocalizedMatcher;
import com.fitbur.mockito.internal.matchers.Not;
import com.fitbur.mockito.internal.matchers.Or;

import static com.fitbur.mockito.exceptions.Reporter.incorrectUseOfAdditionalMatchers;
import static com.fitbur.mockito.exceptions.Reporter.misplacedArgumentMatcher;
import static com.fitbur.mockito.exceptions.Reporter.reportNoSubMatchersFound;

import java.util.*;

public class ArgumentMatcherStorageImpl implements ArgumentMatcherStorage {

    public static final int TWO_SUB_MATCHERS = 2;
    public static final int ONE_SUB_MATCHER = 1;
    private final Stack matcherStack = new Stack();
    
    public void reportMatcher(ArgumentMatcher matcher) {
        matcherStack.push(new LocalizedMatcher(matcher));
    }

    public List pullLocalizedMatchers() {
        if (matcherStack.isEmpty()) {
            return Collections.emptyList();
        }
        
        List matchers = new ArrayList(matcherStack);
        matcherStack.clear();
        return matchers;
    }

    /* (non-Javadoc)
    * @see com.fitbur.mockito.internal.progress.ArgumentMatcherStorage#reportAnd()
    */
    public void reportAnd() {
        assertStateFor("And(?)", TWO_SUB_MATCHERS);
        And and = new And(popLastArgumentMatchers(TWO_SUB_MATCHERS));
        matcherStack.push(new LocalizedMatcher(and));
    }

    /* (non-Javadoc)
     * @see com.fitbur.mockito.internal.progress.ArgumentMatcherStorage#reportOr()
     */
    public void reportOr() {
        assertStateFor("Or(?)", TWO_SUB_MATCHERS);
        Or or = new Or(popLastArgumentMatchers(TWO_SUB_MATCHERS));
        matcherStack.push(new LocalizedMatcher(or));
    }

    /* (non-Javadoc)
     * @see com.fitbur.mockito.internal.progress.ArgumentMatcherStorage#reportNot()
     */
    public void reportNot() {
        assertStateFor("Not(?)", ONE_SUB_MATCHER);
        Not not = new Not(popLastArgumentMatchers(ONE_SUB_MATCHER).get(0));
        matcherStack.push(new LocalizedMatcher(not));
    }

    private void assertStateFor(String additionalMatcherName, int subMatchersCount) {
        assertMatchersFoundFor(additionalMatcherName);
        assertIncorrectUseOfAdditionalMatchers(additionalMatcherName, subMatchersCount);
    }

    private List popLastArgumentMatchers(int count) {
        LinkedList result = new LinkedList();
        for (int i = 0; i < count; i++) {
            result.addFirst(matcherStack.pop().getMatcher());
        }
        return result;
    }

    private void assertMatchersFoundFor(String additionalMatcherName) {
        if (matcherStack.isEmpty()) {
            matcherStack.clear();
            throw reportNoSubMatchersFound(additionalMatcherName);
        }
    }

    private void assertIncorrectUseOfAdditionalMatchers(String additionalMatcherName, int count) {
        if(matcherStack.size() < count) {
            ArrayList lastMatchers = new ArrayList(matcherStack);
            matcherStack.clear();
            throw incorrectUseOfAdditionalMatchers(additionalMatcherName, count, lastMatchers);
        }
    }

    /* (non-Javadoc)
     * @see com.fitbur.mockito.internal.progress.ArgumentMatcherStorage#validateState()
     */
    public void validateState() {
        if (!matcherStack.isEmpty()) {
            ArrayList lastMatchers = new ArrayList(matcherStack);
            matcherStack.clear();
            throw misplacedArgumentMatcher(lastMatchers);
        }
    }

    /* (non-Javadoc)
     * @see com.fitbur.mockito.internal.progress.ArgumentMatcherStorage#reset()
     */
    public void reset() {
        matcherStack.clear();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy