Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* JMockit Expectations
* Copyright (c) 2006-2010 Rogério Liesenfeld
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package mockit.internal.expectations;
import java.lang.reflect.*;
import java.util.*;
import mockit.*;
import mockit.external.asm.Type;
import mockit.internal.expectations.invocation.*;
import mockit.internal.util.*;
public final class Expectation
{
final RecordPhase recordPhase;
public final ExpectedInvocation invocation;
public final InvocationConstraints constraints;
private InvocationResults results;
Expectation(RecordPhase recordPhase, ExpectedInvocation invocation, boolean nonStrict)
{
this.recordPhase = recordPhase;
this.invocation = invocation;
constraints = new InvocationConstraints(nonStrict);
}
Expectation(Expectation other)
{
recordPhase = other.recordPhase;
invocation = other.invocation;
constraints = new InvocationConstraints(other.constraints);
results = other.results;
}
public InvocationResults getResults()
{
if (results == null) {
results = new InvocationResults(invocation, constraints);
}
return results;
}
Object produceResult(Object invokedObject, Object[] invocationArgs) throws Throwable
{
if (results == null) {
return invocation.getDefaultValueForReturnType(null);
}
return results.produceResult(invokedObject, invocationArgs);
}
AssertionError verifyConstraints()
{
return constraints.verify(invocation);
}
public void addReturnValueOrValues(Object value)
{
boolean valueIsACollection = value instanceof Collection>;
if (
(valueIsACollection || value instanceof Iterator>) &&
!hasReturnValueOfType(value.getClass())
) {
if (valueIsACollection) {
Collection> values = (Collection>) value;
getResults().addReturnValues(values.toArray(new Object[values.size()]));
}
else {
getResults().addDeferredReturnValues((Iterator>) value);
}
return;
}
addSingleReturnValue(value);
}
private void addSingleReturnValue(Object value)
{
validateReturnValues(value, (Object) null);
substituteCascadedMockToBeReturnedIfNeeded(value);
getResults().addReturnValue(value);
}
private boolean hasReturnValueOfType(Class> typeToBeReturned)
{
Class> returnClass = getReturnType();
return returnClass != null && returnClass.isAssignableFrom(typeToBeReturned);
}
private Class> getReturnType()
{
Type invocationReturnType = Type.getReturnType(invocation.getMethodNameAndDescription());
return Utilities.getClassForType(invocationReturnType);
}
private void validateReturnValues(Object firstValue, Object... remainingValues)
{
if (hasVoidReturnType()) {
validateReturnValueForConstructorOrVoidMethod(firstValue);
if (remainingValues != null) {
for (Object anotherValue : remainingValues) {
validateReturnValueForConstructorOrVoidMethod(anotherValue);
}
}
}
}
private boolean hasVoidReturnType()
{
return invocation.getMethodNameAndDescription().endsWith(")V");
}
private void validateReturnValueForConstructorOrVoidMethod(Object value)
{
if (value != null && !(value instanceof Delegate)) {
throw new IllegalArgumentException(
"Non-null return value specified for constructor or void method");
}
}
private void substituteCascadedMockToBeReturnedIfNeeded(Object value)
{
if (invocation.overrideDefaultCascadedMockIfAny(value)) {
recordPhase.setNextInstanceToMatch(null);
}
}
public void addSequenceOfReturnValues(Object firstValue, Object[] remainingValues)
{
validateReturnValues(firstValue, remainingValues);
InvocationResults sequence = getResults();
if (remainingValues == null) {
sequence.addReturnValue(firstValue);
}
else if (!addReturnValueForSequenceOfValues(firstValue, remainingValues)) {
sequence.addReturnValue(firstValue);
sequence.addReturnValues(remainingValues);
}
}
private boolean addReturnValueForSequenceOfValues(Object first, Object[] remaining)
{
Class> rt = getReturnType();
if (rt != null) {
int n = 1 + remaining.length;
if (rt.isArray()) {
if (first == null || !first.getClass().isArray()) {
addArrayAsReturnValue(rt.getComponentType(), n, first, remaining);
return true;
}
}
else if (Iterator.class.isAssignableFrom(rt)) {
addIteratorAsReturnValue(first, remaining, n);
return true;
}
else if (Iterable.class.isAssignableFrom(rt)) {
if (rt.isAssignableFrom(List.class)) {
addReturnValues(new ArrayList