1 3 package test.jmock.core.stub; 4 5 import junit.framework.AssertionFailedError; 6 import junit.framework.TestCase; 7 import org.jmock.core.Invocation; 8 import org.jmock.core.stub.DefaultResultStub; 9 import org.jmock.expectation.AssertMo; 10 import test.jmock.core.testsupport.MethodFactory; 11 12 13 public class DefaultResultStubTest extends TestCase { 14 static final Object [] NO_ARG_VALUES = new Object [0]; 15 16 private static MethodFactory METHOD_FACTORY = new MethodFactory(); 17 18 private DefaultResultStub stub; 19 20 public DefaultResultStubTest( String name ) { 21 super(name); 22 } 23 24 public void setUp() { 25 stub = new DefaultResultStub(); 26 } 27 28 public void testWritesDescritionToStringBuffer() { 29 AssertMo.assertIncludes("contains expected description", 30 "returns a default value", 31 stub.describeTo(new StringBuffer ()).toString()); 32 } 33 34 public void testReturnsUsefulDefaultResultsForBasicTypes() 35 throws Throwable 36 { 37 assertHasRegisteredValue(stub, boolean.class, Boolean.FALSE); 38 assertHasRegisteredValue(stub, void.class, null); 39 assertHasRegisteredValue(stub, byte.class, new Byte ((byte)0)); 40 assertHasRegisteredValue(stub, short.class, new Short ((short)0)); 41 assertHasRegisteredValue(stub, int.class, new Integer (0)); 42 assertHasRegisteredValue(stub, long.class, new Long (0L)); 43 assertHasRegisteredValue(stub, char.class, new Character ('\0')); 44 assertHasRegisteredValue(stub, float.class, new Float (0.0F)); 45 assertHasRegisteredValue(stub, double.class, new Double (0.0)); 46 assertHasRegisteredValue(stub, Boolean .class, Boolean.FALSE); 47 assertHasRegisteredValue(stub, Byte .class, new Byte ((byte)0)); 48 assertHasRegisteredValue(stub, Short .class, new Short ((short)0)); 49 assertHasRegisteredValue(stub, Integer .class, new Integer (0)); 50 assertHasRegisteredValue(stub, Long .class, new Long (0L)); 51 assertHasRegisteredValue(stub, Character .class, new Character ('\0')); 52 assertHasRegisteredValue(stub, Float .class, new Float (0.0F)); 53 assertHasRegisteredValue(stub, Double .class, new Double (0.0)); 54 assertHasRegisteredValue(stub, String .class, ""); 55 } 56 57 public void testReturnsEmptyArrayForAllArrayTypes() 58 throws Throwable 59 { 60 int[] defaultArrayForPrimitiveType = 61 (int[])stub.invoke(invocationReturning(int[].class)); 62 assertEquals("should be empty array", 0, defaultArrayForPrimitiveType.length); 63 64 Object [] defaultArrayForReferenceType = 65 (Object [])stub.invoke(invocationReturning(Object [].class)); 66 assertEquals("should be empty array", 0, defaultArrayForReferenceType.length); 67 } 68 69 public interface InterfaceType { 70 int returnInt(); 71 } 72 73 public void testReturnsProxyOfNewMockObjectWithSameDefaultResultStubForInterfaceTypes() 75 throws Throwable 76 { 77 int intResult = -1; 78 79 stub.addResult(int.class, new Integer (intResult)); 80 81 InterfaceType result = (InterfaceType)stub.invoke(invocationReturning(InterfaceType.class)); 82 83 assertEquals("int result from 'null' interface implementation", 84 intResult, result.returnInt()); 85 } 86 87 public void testDefaultResultsCanBeExplicitlyOverriddenByType() 88 throws Throwable 89 { 90 int newDefaultIntResult = 20; 91 String newDefaultStringResult = "hello"; 92 93 stub.addResult(String .class, newDefaultStringResult); 94 stub.addResult(int.class, new Integer (newDefaultIntResult)); 95 96 assertEquals("expected registered value for string result type", 97 newDefaultStringResult, stub.invoke(invocationReturning(String .class))); 98 99 assertEquals("expected registered value for int result type", 100 new Integer (newDefaultIntResult), stub.invoke(invocationReturning(int.class))); 101 } 102 103 public void testAnExplicitlyRegisteredResultOverridesThePreviousResultForTheSameType() 104 throws Throwable 105 { 106 stub.addResult(String .class, "result1"); 107 stub.addResult(String .class, "result2"); 108 109 assertEquals("expected second result", 110 "result2", stub.invoke(invocationReturning(String .class))); 111 } 112 113 class UnsupportedReturnType 114 { 115 } 116 117 public void testInvocationWithAnUnregisteredReturnTypeCausesAnAssertionFailedError() 118 throws Throwable 119 { 120 Class unsupportedReturnType = UnsupportedReturnType.class; 121 Class [] supportedReturnTypes = { 122 boolean.class, byte.class, char.class, short.class, int.class, long.class, 123 float.class, double.class, 124 Boolean .class, Byte .class, Character .class, Short .class, Integer .class, Long .class, 125 Float .class, Double .class, 126 String .class 127 }; 128 129 try { 130 stub.invoke(invocationReturning(unsupportedReturnType)); 131 } 132 catch (AssertionFailedError ex) { 133 String message = ex.getMessage(); 134 135 AssertMo.assertIncludes("message should include name of unsupported type", 136 unsupportedReturnType.getName(), message); 137 138 for (int i = 0; i < supportedReturnTypes.length; i++) { 139 AssertMo.assertIncludes("message should include names of expected types", 140 supportedReturnTypes[i].getName(), message); 141 } 142 return; 143 } 144 145 fail("should have failed"); 146 } 147 148 public void assertHasRegisteredValue( DefaultResultStub defaultResultStub, 149 Class resultType, 150 Object resultValue ) 151 throws Throwable 152 { 153 assertEquals("expected " + resultValue + " to be returned", 154 resultValue, defaultResultStub.invoke(invocationReturning(resultType))); 155 } 156 157 public void assertHasNotRegisteredReturnType( DefaultResultStub defaultResultStub, 158 Class resultType ) 159 throws Throwable 160 { 161 try { 162 defaultResultStub.invoke(invocationReturning(resultType)); 163 fail("stub should not support return type " + resultType); 164 } 165 catch (AssertionFailedError expected) { 166 return; 167 } 168 } 169 170 private Invocation invocationReturning( Class resultType ) { 171 return new Invocation("INVOKED-OBJECT", 172 METHOD_FACTORY.newMethodReturning(resultType), 173 NO_ARG_VALUES); 174 } 175 } 176 | Popular Tags |