1 26 27 package net.sourceforge.groboutils.junit.v1.parser; 28 29 import net.sourceforge.groboutils.junit.v1.iftc.*; 30 import java.lang.reflect.*; 31 32 import org.easymock.EasyMock; 33 import org.easymock.MockControl; 34 import junit.framework.Test; 35 import junit.framework.TestCase; 36 import junit.framework.TestSuite; 37 38 39 46 public class DelegateTestCreatorUTest extends TestCase 47 { 48 51 private static final Class THIS_CLASS = DelegateTestCreatorUTest.class; 52 private static final org.apache.log4j.Logger LOG = 53 org.apache.log4j.Logger.getLogger( THIS_CLASS ); 54 55 public DelegateTestCreatorUTest( String name ) 56 { 57 super( name ); 58 } 59 60 61 private static class MyTestCreator implements ITestCreator 63 { 64 int canCreateCount = 0; 65 int createTestCount = 0; 66 boolean canCreate = false; 67 Test test = null; 68 69 public Test createTest( Class theClass, Method method ) 70 { 71 ++this.createTestCount; 72 return this.test; 73 } 74 75 public boolean canCreate( Class theClass ) 76 { 77 ++this.canCreateCount; 78 return this.canCreate; 79 } 80 } 81 82 83 86 87 public void testConstructor1() 88 { 89 try 90 { 91 new DelegateTestCreator( null ); 92 fail( "Did not throw IllegalArgumentException." ); 93 } 94 catch (IllegalArgumentException e) 95 { 96 } 98 } 99 100 101 public void testConstructor2() 102 { 103 try 104 { 105 new DelegateTestCreator( new ITestCreator[0] ); 106 fail( "Did not throw IllegalArgumentException." ); 107 } 108 catch (IllegalArgumentException e) 109 { 110 } 112 } 113 114 115 public void testCanCreate1() 116 { 117 MyTestCreator tc1 = new MyTestCreator(); 118 MyTestCreator tc2 = new MyTestCreator(); 119 DelegateTestCreator dtc = new DelegateTestCreator( 120 new ITestCreator[] { tc1, tc2 } ); 121 122 boolean res = dtc.canCreate( null ); 123 124 assertTrue( 125 "Did not return correct result.", 126 !res ); 127 assertEquals( 128 "Did not call canCreate correct number of times for first instance.", 129 1, 130 tc1.canCreateCount ); 131 assertEquals( 132 "Did not call canCreate correct number of times for second instance.", 133 1, 134 tc2.canCreateCount ); 135 } 136 137 138 public void testCanCreate2() 139 { 140 MyTestCreator tc1 = new MyTestCreator(); 141 MyTestCreator tc2 = new MyTestCreator(); 142 tc1.canCreate = true; 143 DelegateTestCreator dtc = new DelegateTestCreator( 144 new ITestCreator[] { tc1, tc2 } ); 145 146 boolean res = dtc.canCreate( null ); 147 148 assertTrue( 149 "Did not return correct result.", 150 res ); 151 152 assertEquals( 154 "Did not call canCreate correct number of times for first instance.", 155 1, 156 tc1.canCreateCount ); 157 assertEquals( 158 "Did not call canCreate correct number of times for second instance.", 159 1, 160 tc2.canCreateCount ); 161 } 162 163 164 public void testCanCreate3() 165 { 166 MyTestCreator tc1 = new MyTestCreator(); 167 MyTestCreator tc2 = new MyTestCreator(); 168 tc2.canCreate = true; 169 DelegateTestCreator dtc = new DelegateTestCreator( 170 new ITestCreator[] { tc1, tc2 } ); 171 172 boolean res = dtc.canCreate( null ); 173 174 assertTrue( 175 "Did not return correct result.", 176 res ); 177 178 assertEquals( 180 "Did not call canCreate correct number of times for first instance.", 181 0, 182 tc1.canCreateCount ); 183 assertEquals( 184 "Did not call canCreate correct number of times for second instance.", 185 1, 186 tc2.canCreateCount ); 187 } 188 189 190 public void testCanCreate4() 191 { 192 MyTestCreator tc1 = new MyTestCreator(); 193 MyTestCreator tc2 = new MyTestCreator(); 194 tc1.canCreate = true; 195 tc2.canCreate = true; 196 DelegateTestCreator dtc = new DelegateTestCreator( 197 new ITestCreator[] { tc1, tc2 } ); 198 199 boolean res = dtc.canCreate( null ); 200 201 assertTrue( 202 "Did not return correct result.", 203 res ); 204 205 assertEquals( 207 "Did not call canCreate correct number of times for first instance.", 208 0, 209 tc1.canCreateCount ); 210 assertEquals( 211 "Did not call canCreate correct number of times for second instance.", 212 1, 213 tc2.canCreateCount ); 214 } 215 216 217 public void testCanCreate5() 218 { 219 MyTestCreator tc1 = new MyTestCreator(); 220 DelegateTestCreator dtc = new DelegateTestCreator( 221 new ITestCreator[] { tc1 } ); 222 223 boolean res = dtc.canCreate( null ); 224 225 assertTrue( 226 "Did not return correct result.", 227 !res ); 228 assertEquals( 229 "Did not call canCreate correct number of times for first instance.", 230 1, 231 tc1.canCreateCount ); 232 } 233 234 235 public void testCanCreate6() 236 { 237 MyTestCreator tc1 = new MyTestCreator(); 238 tc1.canCreate = true; 239 DelegateTestCreator dtc = new DelegateTestCreator( 240 new ITestCreator[] { tc1 } ); 241 242 boolean res = dtc.canCreate( null ); 243 244 assertTrue( 245 "Did not return correct result.", 246 res ); 247 assertEquals( 248 "Did not call canCreate correct number of times for first instance.", 249 1, 250 tc1.canCreateCount ); 251 } 252 253 254 255 258 259 public static Test suite() 260 { 261 InterfaceTestSuite suite = ITestCreatorUTestI.suite(); 262 263 suite.addFactory( new CxFactory( "A" ) { 266 public Object createImplObject() { 267 return new DelegateTestCreator( new ITestCreator[] { 268 new MyTestCreator(), new MyTestCreator() 269 } ); 270 } 271 } ); 272 suite.addTestSuite( THIS_CLASS ); 273 274 return suite; 275 } 276 277 public static void main( String [] args ) 278 { 279 String [] name = { THIS_CLASS.getName() }; 280 281 284 junit.textui.TestRunner.main( name ); 285 } 286 287 288 292 protected void setUp() throws Exception 293 { 294 super.setUp(); 295 296 } 298 299 300 304 protected void tearDown() throws Exception 305 { 306 308 309 super.tearDown(); 310 } 311 } 312 313 | Popular Tags |