1 package org.jacorb.test.common; 2 3 import org.omg.Messaging.*; 4 5 import junit.framework.*; 6 7 111 public class CallbackTestCase extends ClientServerTestCase 112 { 113 114 public CallbackTestCase(String name, ClientServerSetup setup) 115 { 116 super(name, setup); 117 } 118 119 protected abstract class ReplyHandler 120 { 121 private boolean replyReceived = false; 122 private boolean testFailed = false; 123 private String failureMessage = null; 124 125 protected ReplyHandler() { 126 } 127 128 public synchronized void wait_for_reply(long timeout) 129 { 130 try 131 { 132 long start = System.currentTimeMillis(); 133 if ( !replyReceived ) 134 { 135 this.wait( timeout ); 136 if ( !replyReceived ) 137 junit.framework.Assert.fail 138 ( "no reply within timeout (" 139 + timeout + "ms)" ); 140 } 141 if ( testFailed ) 144 junit.framework.Assert.fail( failureMessage ); 145 else 146 ; } 148 catch ( InterruptedException e ) 149 { 150 junit.framework.Assert.fail 151 ( "interrupted while waiting for reply" ); 152 } 153 finally 154 { 155 replyReceived = false; 156 testFailed = false; 157 failureMessage = null; 158 } 159 } 160 161 public synchronized void fail(String message) 162 { 163 replyReceived = true; 164 testFailed = true; 165 failureMessage = message; 166 this.notifyAll(); 167 throw new AssertionFailedError(); 168 } 169 170 public void wrong_reply( String name ) 171 { 172 fail( "wrong reply method: " + name ); 173 } 174 175 public void wrong_exception( String methodName, 176 ExceptionHolder excep_holder ) 177 { 178 try 179 { 180 excep_holder.raise_exception(); 181 } 182 catch( Exception e ) 183 { 184 fail( "unexpected exception: " 185 + methodName + ", " + e ); 186 } 187 } 188 189 public Exception getException( ExceptionHolder excep_holder ) 190 { 191 try 192 { 193 excep_holder.raise_exception(); 194 return null; 195 } 196 catch( Exception e ) 197 { 198 return e; 199 } 200 } 201 202 public synchronized void pass() 203 { 204 replyReceived = true; 205 testFailed = false; 206 failureMessage = null; 207 this.notifyAll(); 208 } 209 210 213 216 public void assertEquals(byte expected, byte actual) 217 { 218 assertEquals(null, expected, actual); 219 } 220 221 224 public void assertEquals(char expected, char actual) 225 { 226 assertEquals(null, expected, actual); 227 } 228 229 233 public void assertEquals( 234 double expected, 235 double actual, 236 double delta) 237 { 238 assertEquals(null, expected, actual, delta); 239 } 240 241 245 public void assertEquals(float expected, float actual, float delta) 246 { 247 assertEquals(null, expected, actual, delta); 248 } 249 250 253 public void assertEquals(int expected, int actual) 254 { 255 assertEquals(null, expected, actual); 256 } 257 258 261 public void assertEquals(long expected, long actual) 262 { 263 assertEquals(null, expected, actual); 264 } 265 266 270 public void assertEquals(Object expected, Object actual) 271 { 272 assertEquals(null, expected, actual); 273 } 274 275 278 public void assertEquals(String message, byte expected, byte actual) 279 { 280 assertEquals(message, new Byte (expected), new Byte (actual)); 281 } 282 283 286 public void assertEquals(String message, char expected, char actual) 287 { 288 assertEquals( 289 message, 290 new Character (expected), 291 new Character (actual)); 292 } 293 294 298 public void assertEquals( 299 String message, 300 double expected, 301 double actual, 302 double delta) 303 { 304 if (Double.isInfinite(expected)) 307 { 308 if (!(expected == actual)) 309 failNotEquals( 310 message, 311 new Double (expected), 312 new Double (actual)); 313 } 314 else if ( 315 !(Math.abs(expected - actual) <= delta)) 316 failNotEquals( 318 message, 319 new Double (expected), 320 new Double (actual)); 321 } 322 323 327 public void assertEquals( 328 String message, 329 float expected, 330 float actual, 331 float delta) 332 { 333 if (Float.isInfinite(expected)) 336 { 337 if (!(expected == actual)) 338 failNotEquals( 339 message, 340 new Float (expected), 341 new Float (actual)); 342 } 343 else if (!(Math.abs(expected - actual) <= delta)) 344 failNotEquals(message, new Float (expected), new Float (actual)); 345 } 346 347 350 public void assertEquals(String message, int expected, int actual) 351 { 352 assertEquals(message, new Integer (expected), new Integer (actual)); 353 } 354 355 358 public void assertEquals(String message, long expected, long actual) 359 { 360 assertEquals(message, new Long (expected), new Long (actual)); 361 } 362 363 367 public void assertEquals( 368 String message, 369 Object expected, 370 Object actual) 371 { 372 if (expected == null && actual == null) 373 return; 374 if (expected != null && expected.equals(actual)) 375 return; 376 failNotEquals(message, expected, actual); 377 } 378 379 382 public void assertEquals(String message, short expected, short actual) 383 { 384 assertEquals(message, new Short (expected), new Short (actual)); 385 } 386 387 390 public void assertEquals( 391 String message, 392 boolean expected, 393 boolean actual) 394 { 395 assertEquals(message, new Boolean (expected), new Boolean (actual)); 396 } 397 398 401 public void assertEquals(short expected, short actual) 402 { 403 assertEquals(null, expected, actual); 404 } 405 406 409 public void assertEquals(boolean expected, boolean actual) 410 { 411 assertEquals(null, expected, actual); 412 } 413 414 417 public void assertNotNull(Object object) 418 { 419 assertNotNull(null, object); 420 } 421 422 425 public void assertNotNull(String message, Object object) 426 { 427 assertTrue(message, object != null); 428 } 429 430 433 public void assertNull(Object object) 434 { 435 assertNull(null, object); 436 } 437 438 441 public void assertNull(String message, Object object) 442 { 443 assertTrue(message, object == null); 444 } 445 446 450 public void assertSame(Object expected, Object actual) 451 { 452 assertSame(null, expected, actual); 453 } 454 455 459 public void assertSame(String message, Object expected, Object actual) 460 { 461 if (expected == actual) 462 return; 463 failNotSame(message, expected, actual); 464 } 465 466 470 public void assertTrue(String message, boolean condition) 471 { 472 if (!condition) 473 fail(message); 474 } 475 476 480 public void assertTrue(boolean condition) 481 { 482 assertTrue(null, condition); 483 } 484 485 private void failNotEquals( String message, 486 Object expected, 487 Object actual ) 488 { 489 String formatted = ""; 490 if (message != null) 491 formatted = message + " "; 492 fail( formatted + "expected:<" + expected 493 + "> but was:<" + actual + ">"); 494 } 495 496 private void failNotSame( String message, 497 Object expected, 498 Object actual ) 499 { 500 String formatted = ""; 501 if (message != null) 502 formatted = message + " "; 503 fail(formatted + "expected same"); 504 } 505 506 } 507 508 } 509 | Popular Tags |