1 5 package test.mockobjects.constraint; 6 7 import com.mockobjects.constraint.*; 8 import com.mockobjects.dynamic.Mock; 9 import com.mockobjects.util.AssertMo; 10 11 import test.mockobjects.dynamic.DummyInterface; 12 13 import java.util.EventObject ; 14 15 import junit.framework.*; 16 17 public class ConstraintsTest extends TestCase 18 { 19 class True implements Constraint { 20 public boolean eval( Object o ) { return true; } 21 } 22 23 class False implements Constraint { 24 public boolean eval( Object o ) { return false; } 25 } 26 27 29 public ConstraintsTest( String test ) { 30 super(test); 31 } 32 33 public void testIsNull() { 34 Constraint p = new IsNull(); 35 36 assertTrue( p.eval(null) ); 37 assertTrue( !p.eval(new Object ()) ); 38 } 39 40 public void testIsSame() { 41 Object o1 = new Object (); 42 Object o2 = new Object (); 43 Constraint p = new IsSame(o1); 44 45 assertTrue( p.eval(o1) ); 46 assertTrue( !p.eval(o2) ); 47 } 48 49 public void testIsEqual() { 50 Integer i1 = new Integer (1); 51 Integer i2 = new Integer (2); 52 Constraint p = new IsEqual(i1); 53 54 assertTrue( p.eval(i1) ); 55 assertTrue( p.eval( new Integer (1) ) ); 56 assertTrue( !p.eval(i2) ); 57 } 58 59 public void testIsEqualObjectArray() { 60 String [] s1 = new String [] { "a", "b" }; 61 String [] s2 = new String [] { "a", "b" }; 62 String [] s3 = new String [] { "c", "d" }; 63 String [] s4 = new String [] { "a", "b", "c", "d" }; 64 65 Constraint p = new IsEqual(s1); 66 67 assertTrue( "Should equal itself", p.eval(s1) ); 68 assertTrue( "Should equal a similar array", p.eval( s2 ) ); 69 assertTrue( "Should not equal a different array", !p.eval(s3) ); 70 assertTrue( "Should not equal a different sized array", !p.eval(s4) ); 71 } 72 73 public void testIsEqualToStringForNestedConstraint() { 74 assertEquals("Should get an obvious toString to reflect nesting if viewed in a debugger", 75 " = = NestedConstraint", new IsEqual(new IsEqual("NestedConstraint")).toString()); 76 } 77 public void testIsEqualToStringOnProxyArgument() { 78 Mock mockDummyInterface = new Mock(DummyInterface.class, "MockName"); 80 Constraint p = new IsEqual(mockDummyInterface.proxy()); 81 82 AssertMo.assertIncludes("Should get resolved toString() with no expectation error", "MockName", p.toString()); 83 } 84 85 public void testIsEqualEquals() throws Exception { 86 assertEquals("Should be equal", new IsEqual("a"), new IsEqual("a")); 87 assertFalse("Should not be equal - same type different values", new IsEqual("a").equals(new IsEqual("b"))); 88 assertFalse("Should not be equal - different type", new IsEqual("a").equals("b")); 89 } 90 91 public void testIsGreaterThan() { 92 Constraint p = new IsGreaterThan( new Integer (1) ); 93 94 assertTrue( !p.eval( new Integer (0) ) ); 95 assertTrue( !p.eval( new Integer (1) ) ); 96 assertTrue( p.eval( new Integer (2) ) ); 97 } 98 99 public void testIsLessThan() { 100 Constraint p = new IsLessThan( new Integer (1) ); 101 102 assertTrue( p.eval( new Integer (0) ) ); 103 assertTrue( !p.eval( new Integer (1) ) ); 104 assertTrue( !p.eval( new Integer (2) ) ); 105 } 106 107 public void testIsAnything() { 108 Constraint p = new IsAnything(); 109 assertTrue( p.eval(null) ); 110 assertTrue( p.eval( new Object () ) ); 111 } 112 113 public void testIsInstanceOf() { 114 Constraint p = new IsInstanceOf( Number .class ); 115 assertTrue( p.eval( new Integer (1) ) ); 116 assertTrue( p.eval( new Double (1.0) ) ); 117 assertTrue( !p.eval("a string") ); 118 assertTrue( !p.eval(null) ); 119 } 120 121 public void testIsNot() { 122 Constraint p = new IsNot( new True() ); 123 assertTrue( !p.eval(null) ); 124 assertTrue( !p.eval( new Object () ) ); 125 } 126 127 public void testAnd() { 128 Object o = new Object (); 129 assertTrue( new And( new True(), new True() ).eval(o) ); 130 assertTrue( !new And( new False(), new True() ).eval(o) ); 131 assertTrue( !new And( new True(), new False() ).eval(o) ); 132 assertTrue( !new And( new False(), new False() ).eval(o) ); 133 } 134 135 public void testOr() { 136 Object o = new Object (); 137 assertTrue( new Or( new True(), new True() ).eval(o) ); 138 assertTrue( new Or( new False(), new True() ).eval(o) ); 139 assertTrue( new Or( new True(), new False() ).eval(o) ); 140 assertTrue( !new Or( new False(), new False() ).eval(o) ); 141 } 142 143 public void testIsEventFrom() { 144 Object o = new Object (); 145 EventObject ev = new EventObject (o); 146 EventObject ev2 = new EventObject ( new Object () ); 147 148 Constraint p = new IsEventFrom(o); 149 150 assertTrue( p.eval(ev) ); 151 assertTrue( "p should eval to false for an event not from o", 152 !p.eval(ev2) ); 153 assertTrue( "p should eval to false for objects that are not events", 154 !p.eval(o) ); 155 } 156 157 private static class DerivedEvent extends EventObject { 158 public DerivedEvent( Object source ) { 159 super(source); 160 } 161 } 162 163 public void testIsEventSubtypeFrom() { 164 Object o = new Object (); 165 DerivedEvent good_ev = new DerivedEvent(o); 166 DerivedEvent wrong_source = new DerivedEvent(new Object ()); 167 EventObject wrong_type = new EventObject (o); 168 EventObject wrong_source_and_type = new EventObject (new Object ()); 169 170 Constraint p = new IsEventFrom( DerivedEvent.class, o ); 171 172 assertTrue( p.eval(good_ev) ); 173 assertTrue( "p should eval to false for an event not from o", 174 !p.eval(wrong_source) ); 175 assertTrue( "p should eval to false for an event of the wrong type", 176 !p.eval(wrong_type) ); 177 assertTrue( "p should eval to false for an event of the wrong type "+ 178 "and from the wrong source", 179 !p.eval(wrong_source_and_type) ); 180 } 181 182 public void testIsCloseTo() { 183 Constraint p = new IsCloseTo( 1.0, 0.5 ); 184 185 assertTrue( p.eval( new Double (1.0) ) ); 186 assertTrue( p.eval( new Double (0.5) ) ); 187 assertTrue( p.eval( new Double (1.5) ) ); 188 189 assertTrue( p.eval( new Float (1.0) ) ); 190 assertTrue( p.eval( new Integer (1) ) ); 191 192 assertTrue( "number too large", !p.eval( new Double (2.0) ) ); 193 assertTrue( "number too small", !p.eval( new Double (0.0) ) ); 194 195 try { 196 p.eval("wrong type"); 197 fail("ClassCastException expected for wrong type of argument"); 198 } 199 catch( ClassCastException ex ) { 200 } 202 } 203 } 204 | Popular Tags |