package org.jmock.test.unit.internal;
import org.hamcrest.Matcher;
import org.jmock.internal.ReturnDefaultValueAction;
import org.junit.Test;
import javax.xml.ws.handler.LogicalMessageContext;
import java.beans.beancontext.BeanContext;
import java.beans.beancontext.BeanContextServices;
import java.util.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.jmock.test.unit.internal.ReturnDefaultValueActionTests.invocationReturning;
/**
* @author Steve Freeman 2013 http://www.jmock.org
* https://github.com/jmock-developers/jmock-library/issues/9
*/
public class ReturnDefaultCollectionTests {
abstract static class AbstractCollection implements Collection {}
private static final Matcher<Object> IS_PROXY_CLASS = hasProperty("canonicalName", containsString("Proxy"));
private final ReturnDefaultValueAction action = new ReturnDefaultValueAction();
@SuppressWarnings("unchecked")
@Test public void
returnsANewInstanceForEachCall() throws Throwable {
final ArrayList<Object> firstInstance = returnedArrayList();
firstInstance.add(new Object());
assertThat(returnedArrayList(), is(empty()));
}
@Test public void
returnsNewInstanceOfIterableClasses() throws Throwable {
returnsInstanceForType(ArrayList.class, ArrayList.class);
returnsInstanceForType(PriorityQueue.class, PriorityQueue.class);
}
@Test public void
returnsNewInstanceOfMapClasses() throws Throwable {
returnsInstanceForType(HashMap.class, HashMap.class);
returnsInstanceForType(Properties.class, Properties.class);
}
@Test public void
returnsNewInstanceConformingToCollectionInterface() throws Throwable {
returnsInstanceForType(List.class, LinkedList.class);
returnsInstanceForType(Set.class, TreeSet.class);
returnsInstanceForType(NavigableSet.class, TreeSet.class);
returnsInstanceForType(SortedSet.class, TreeSet.class);
returnsInstanceForType(Queue.class, LinkedList.class);
returnsInstanceForType(Deque.class, LinkedList.class);
}
// https://github.com/jmock-developers/jmock-library/issues/46
@Test public void
imposterisesBeanContextTypesToAvoidMissingTypesInAndroid() throws Throwable {
assertThat(action.invoke(invocationReturning(BeanContext.class)).getClass(), IS_PROXY_CLASS);
assertThat(action.invoke(invocationReturning(BeanContextServices.class)).getClass(), IS_PROXY_CLASS);
}
@Test public void
returnsNewInstanceConformingToMapType() throws Throwable {
returnsInstanceForType(Map.class, TreeMap.class);
returnsInstanceForType(SortedMap.class, TreeMap.class);
returnsInstanceForType(NavigableMap.class, TreeMap.class);
}
@Test public void
imposterisesUnsupportedMapTypes() throws Throwable {
assertThat(action.invoke(invocationReturning(LogicalMessageContext.class)).getClass(), IS_PROXY_CLASS);
}
@Test public void
returnsNullForAbstractCollections() throws Throwable {
assertThat(action.invoke(invocationReturning(AbstractCollection.class)), is(nullValue()));
}
private void returnsInstanceForType(Class<?> declaredType, Class<?> expectedType) throws Throwable {
assertThat(
action.invoke(invocationReturning(declaredType)),
instanceOf(expectedType));
}
@SuppressWarnings("unchecked")
private ArrayList<Object> returnedArrayList() throws Throwable {
return (ArrayList<Object>) action.invoke(invocationReturning(ArrayList.class));
}
}
/* Copyright (c) 2000-2006 jMock.org
*/
package org.jmock.test.unit.internal;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.hamcrest.StringDescription;
import org.jmock.api.Imposteriser;
import org.jmock.api.Invocation;
import org.jmock.internal.ReturnDefaultValueAction;
import org.jmock.lib.JavaReflectionImposteriser;
import org.jmock.test.unit.support.AssertThat;
import org.jmock.test.unit.support.MethodFactory;
public class ReturnDefaultValueActionTests extends TestCase {
static final Object[] NO_ARG_VALUES = new Object[0];
static final MethodFactory METHOD_FACTORY = new MethodFactory();
private ReturnDefaultValueAction action;
@Override public void setUp() {
action = new ReturnDefaultValueAction();
}
public void testWritesDescritionToStringBuffer() {
AssertThat.stringIncludes("contains expected description",
"returns a default value",
StringDescription.toString(action));
}
public void testReturnsUsefulDefaultResultsForBasicTypes()
throws Throwable
{
assertHasRegisteredValue(action, boolean.class, Boolean.FALSE);
assertHasRegisteredValue(action, void.class, null);
assertHasRegisteredValue(action, byte.class, new Byte((byte)0));
assertHasRegisteredValue(action, short.class, new Short((short)0));
assertHasRegisteredValue(action, int.class, new Integer(0));
assertHasRegisteredValue(action, long.class, new Long(0L));
assertHasRegisteredValue(action, char.class, new Character('\0'));
assertHasRegisteredValue(action, float.class, new Float(0.0F));
assertHasRegisteredValue(action, double.class, new Double(0.0));
assertHasRegisteredValue(action, Boolean.class, Boolean.FALSE);
assertHasRegisteredValue(action, Byte.class, new Byte((byte)0));
assertHasRegisteredValue(action, Short.class, new Short((short)0));
assertHasRegisteredValue(action, Integer.class, new Integer(0));
assertHasRegisteredValue(action, Long.class, new Long(0L));
assertHasRegisteredValue(action, Character.class, new Character('\0'));
assertHasRegisteredValue(action, Float.class, new Float(0.0F));
assertHasRegisteredValue(action, Double.class, new Double(0.0));
assertHasRegisteredValue(action, String.class, "");
assertNotNull( "should return an object for Object return type",
action.invoke(invocationReturning(Object.class)) );
}
public void testReturnsEmptyArrayForAllArrayTypes()
throws Throwable
{
int[] defaultArrayForPrimitiveType =
(int[])action.invoke(invocationReturning(int[].class));
assertEquals("should be empty array", 0, defaultArrayForPrimitiveType.length);
Object[] defaultArrayForReferenceType =
(Object[])action.invoke(invocationReturning(Object[].class));
assertEquals("should be empty array", 0, defaultArrayForReferenceType.length);
}
public interface InterfaceType {
int returnInt();
}
// Inspired by http://www.c2.com/cgi/wiki?JavaNullProxy
public void testIfImposteriserCanImposteriseReturnTypeReturnsNewMockObjectWithSameReturnDefaultValueAction() throws Throwable {
Imposteriser imposteriser = new JavaReflectionImposteriser() {
@Override
public boolean canImposterise(Class<?> c) {
return c == InterfaceType.class;
}
};
action = new ReturnDefaultValueAction(imposteriser);
int intResult = -1;
action.addResult(int.class, new Integer(intResult));
InterfaceType result = (InterfaceType)action.invoke(invocationReturning(InterfaceType.class));
assertEquals("int result from 'null' interface implementation",
intResult, result.returnInt());
assertEquals("should not have returned a mock Runnable because the imposteriser cannot imposterise it",
null, action.invoke(invocationReturning(Runnable.class)));
}
public void testDefaultResultsCanBeExplicitlyOverriddenByType() throws Throwable {
int newDefaultIntResult = 20;
String newDefaultStringResult = "hello";
action.addResult(String.class, newDefaultStringResult);
action.addResult(int.class, new Integer(newDefaultIntResult));
assertEquals("expected registered value for string result type",
newDefaultStringResult, action.invoke(invocationReturning(String.class)));
assertEquals("expected registered value for int result type",
new Integer(newDefaultIntResult), action.invoke(invocationReturning(int.class)));
}
public void testAnExplicitlyRegisteredResultOverridesThePreviousResultForTheSameType() throws Throwable {
action.addResult(String.class, "result1");
action.addResult(String.class, "result2");
assertEquals("expected second result",
"result2", action.invoke(invocationReturning(String.class)));
}
class UnsupportedReturnType
{
}
public void testInvocationWithAnUnsupportedReturnTypeReturnsNull()
throws Throwable
{
Class<?> unsupportedReturnType = UnsupportedReturnType.class;
Object result = action.invoke(invocationReturning(unsupportedReturnType));
assertNull("should have returned null", result);
}
public void assertHasRegisteredValue(ReturnDefaultValueAction action,
Class<?> resultType,
Object resultValue )
throws Throwable
{
assertEquals("expected " + resultValue + " to be returned",
resultValue, action.invoke(invocationReturning(resultType)));
}
public void assertHasNotRegisteredReturnType( ReturnDefaultValueAction action,
Class<?> resultType )
throws Throwable
{
try {
action.invoke(invocationReturning(resultType));
fail("action should not support return type " + resultType);
}
catch (AssertionFailedError expected) {
return;
}
}
private Invocation invocationReturning(Class<?> resultType) {
return new Invocation("INVOKED-OBJECT",
METHOD_FACTORY.newMethodReturning(resultType),
NO_ARG_VALUES);
}
}