1 3 package test.jmock.core; 4 5 import junit.framework.TestCase; 6 import org.jmock.core.Constraint; 7 import org.jmock.core.MockObjectSupportTestCase; 8 import org.jmock.util.Dummy; 9 import test.jmock.core.testsupport.AlwaysFalse; 10 import test.jmock.core.testsupport.AlwaysTrue; 11 12 13 public class MockObjectSupportTestCaseTest extends TestCase 14 { 15 private static final String DUMMY_NAME = "DUMMY NAME"; 16 17 Constraint trueConstraint = AlwaysTrue.INSTANCE; 18 Constraint falseConstraint = AlwaysFalse.INSTANCE; 19 20 21 interface ExampleInterface 22 { 23 void method1(); 24 } 25 26 27 MockObjectSupportTestCase testCase; 28 29 public void setUp() { 30 testCase = new MockObjectSupportTestCase() {}; 31 } 32 33 public void testCanBeConstructedWithAName() { 34 String name = "NAME"; 35 36 testCase = new MockObjectSupportTestCase(name) {}; 37 38 assertEquals("name", name, testCase.getName()); 39 } 40 41 public void testCanCreateNamedDummyObjects() { 42 Object dummy = testCase.newDummy(DUMMY_NAME); 43 44 assertEquals("should return name from toString", 45 DUMMY_NAME, dummy.toString()); 46 } 47 48 public void testCanCreateNamedDummyObjectsThatImplementASpecificInterface() { 49 Object dummy = testCase.newDummy(ExampleInterface.class, DUMMY_NAME); 50 51 assertTrue("should be instanceof ExampleInterface", 52 dummy instanceof ExampleInterface); 53 assertEquals("should return name from toString", 54 DUMMY_NAME, dummy.toString()); 55 } 56 57 public void testGeneratesUsefulNamesForDummiesFromTheDummiedInterface() { 58 Object dummy = testCase.newDummy(ExampleInterface.class); 59 60 assertEquals("should return name from toString", 61 "dummyExampleInterface", dummy.toString()); 62 } 63 64 public void testHasConvenienceConstantForIsAnything() { 65 assertConstraintTrue(MockObjectSupportTestCase.ANYTHING, new Object ()); 66 assertConstraintTrue(MockObjectSupportTestCase.ANYTHING, new Object ()); 67 } 68 69 public void testHasConvenienceConstantForIsNull() { 70 assertConstraintTrue(MockObjectSupportTestCase.NULL, null); 71 assertConstraintFalse(MockObjectSupportTestCase.NULL, "not null"); 72 } 73 74 public void testHasConvenienceConstantForNotNull() { 75 assertConstraintTrue(MockObjectSupportTestCase.NOT_NULL, "not null"); 76 assertConstraintFalse(MockObjectSupportTestCase.NOT_NULL, null ); 77 } 78 79 public void testHasConvenienceMethodForCreatingIsEqualConstraints() { 80 String stringValue = new String ("STRING VALUE"); 81 82 assertConstraintTrue(testCase.eq(stringValue), stringValue); 83 } 84 85 public void testConvenienceMethodForCreatingIsEqualConstraintsIsOverloadedForPrimitiveTypes() { 86 assertConstraintTrue(testCase.eq(true), new Boolean (true)); 87 assertConstraintTrue(testCase.eq(false), new Boolean (false)); 88 assertConstraintTrue(testCase.eq((byte)1), new Byte ((byte)1)); 89 assertConstraintTrue(testCase.eq((short)1), new Short ((short)1)); 90 assertConstraintTrue(testCase.eq('a'), new Character ('a')); 91 assertConstraintTrue(testCase.eq(1), new Integer (1)); 92 assertConstraintTrue(testCase.eq(1L), new Long (1L)); 93 assertConstraintTrue(testCase.eq(1.0F), new Float (1.0F)); 94 assertConstraintTrue(testCase.eq(1.0), new Double (1.0)); 95 96 assertConstraintFalse(testCase.eq(true), new Boolean (false)); 97 assertConstraintFalse(testCase.eq(false), new Boolean (true)); 98 assertConstraintFalse(testCase.eq((byte)1), new Byte ((byte)2)); 99 assertConstraintFalse(testCase.eq((short)1), new Short ((short)2)); 100 assertConstraintFalse(testCase.eq('a'), new Character ('b')); 101 assertConstraintFalse(testCase.eq(1), new Integer (2)); 102 assertConstraintFalse(testCase.eq(1L), new Long (2L)); 103 assertConstraintFalse(testCase.eq(1.0F), new Float (2.0F)); 104 assertConstraintFalse(testCase.eq(1.0), new Double (2.0)); 105 } 106 107 public void testHasConvenienceMethodForCreatingIsSameConstraints() { 108 Object o1 = Dummy.newDummy("o1"); 109 Object o2 = Dummy.newDummy("o2"); 110 111 assertConstraintTrue(testCase.same(o1), o1); 112 assertConstraintFalse(testCase.same(o1), o2); 113 } 114 115 public void testHasConvenienceMethodForCreatingIsAConstraints() { 116 String aString = "a string"; 117 118 assertConstraintTrue(testCase.isA(String .class), aString); 119 assertConstraintTrue(testCase.isA(Object .class), aString); 120 assertConstraintFalse(testCase.isA(Integer .class), aString); 121 } 122 123 public void testHasConvenienceMethodForCreatingStringContainsConstraints() { 124 assertConstraintTrue(testCase.stringContains("fruit"), "fruitcake"); 125 assertConstraintFalse(testCase.stringContains("chocolate chips"), "fruitcake"); 126 } 127 128 public void testHasConvenienceMethodForLogicalNegationOfConstraints() { 129 assertConstraintTrue(testCase.not(testCase.eq("hello")), "world"); 130 assertConstraintFalse(testCase.not(testCase.eq("hello")), "hello"); 131 } 132 133 public void testHasConvenienceMethodForLogicalConjunctionOfConstraints() { 134 Object ignored = new Object (); 135 136 assertConstraintTrue(testCase.and(trueConstraint, trueConstraint), ignored); 137 assertConstraintFalse(testCase.and(trueConstraint, falseConstraint), ignored); 138 assertConstraintFalse(testCase.and(falseConstraint, trueConstraint), ignored); 139 assertConstraintFalse(testCase.and(falseConstraint, falseConstraint), ignored); 140 } 141 142 public void testHasConvenienceMethodForLogicalDisjunctionOfConstraints() { 143 Object ignored = new Object (); 144 145 assertConstraintTrue(testCase.or(trueConstraint, trueConstraint), ignored); 146 assertConstraintTrue(testCase.or(trueConstraint, falseConstraint), ignored); 147 assertConstraintTrue(testCase.or(falseConstraint, trueConstraint), ignored); 148 assertConstraintFalse(testCase.or(falseConstraint, falseConstraint), ignored); 149 } 150 151 private void assertConstraintTrue( Constraint constraint, Object argument ) { 152 assertTrue("expected <" + constraint + "> to return true when passed <" + argument + ">", 153 constraint.eval(argument)); 154 } 155 156 private void assertConstraintFalse( Constraint constraint, Object argument ) { 157 assertFalse("expected <" + constraint + "> to return false when passed <" + argument + ">", 158 constraint.eval(argument)); 159 } 160 } 161 | Popular Tags |