@@ -367,13 +367,12 @@ public void setExceptionListener(ExceptionListener exceptionListener) throws JMS
367
367
// Prepare base JMS ConnectionFactory
368
368
// - createConnection(1st) -> TestConnection,
369
369
// - createConnection(2nd and next) -> FailingTestConnection
370
- TestConnection testCon = new TestConnection ();
371
370
FailingTestConnection failingCon = new FailingTestConnection ();
372
371
AtomicInteger createConnectionMethodCounter = new AtomicInteger ();
373
372
ConnectionFactory cf = mock (ConnectionFactory .class );
374
373
given (cf .createConnection ()).willAnswer (invocation -> {
375
374
int methodInvocationCounter = createConnectionMethodCounter .incrementAndGet ();
376
- return methodInvocationCounter == 1 ? testCon : failingCon ;
375
+ return ( methodInvocationCounter >= 4 ? failingCon : new TestConnection ()) ;
377
376
});
378
377
379
378
// Prepare SingleConnectionFactory (setReconnectOnException())
@@ -382,18 +381,43 @@ public void setExceptionListener(ExceptionListener exceptionListener) throws JMS
382
381
scf .setReconnectOnException (true );
383
382
Field conField = ReflectionUtils .findField (SingleConnectionFactory .class , "connection" );
384
383
conField .setAccessible (true );
384
+ assertThat (scf .isRunning ()).isFalse ();
385
385
386
386
// Get connection (1st)
387
387
Connection con1 = scf .getConnection ();
388
388
assertThat (createConnectionMethodCounter .get ()).isEqualTo (1 );
389
- assertThat (con1 ).isNotNull ();
390
389
assertThat (con1 .getExceptionListener ()).isNotNull ();
391
- assertThat (con1 ).isSameAs (testCon );
390
+ assertThat (con1 ).isSameAs (conField .get (scf ));
391
+ assertThat (scf .isRunning ()).isTrue ();
392
+
392
393
// Get connection again, the same should be returned (shared connection till some problem)
393
394
Connection con2 = scf .getConnection ();
394
395
assertThat (createConnectionMethodCounter .get ()).isEqualTo (1 );
395
396
assertThat (con2 .getExceptionListener ()).isNotNull ();
396
397
assertThat (con2 ).isSameAs (con1 );
398
+ assertThat (scf .isRunning ()).isTrue ();
399
+
400
+ // Explicit stop should reset connection
401
+ scf .stop ();
402
+ assertThat (conField .get (scf )).isNull ();
403
+ assertThat (scf .isRunning ()).isFalse ();
404
+ Connection con3 = scf .getConnection ();
405
+ assertThat (createConnectionMethodCounter .get ()).isEqualTo (2 );
406
+ assertThat (con3 .getExceptionListener ()).isNotNull ();
407
+ assertThat (con3 ).isNotSameAs (con2 );
408
+ assertThat (scf .isRunning ()).isTrue ();
409
+
410
+ // Explicit stop-and-restart should refresh connection
411
+ scf .stop ();
412
+ assertThat (conField .get (scf )).isNull ();
413
+ assertThat (scf .isRunning ()).isFalse ();
414
+ scf .start ();
415
+ assertThat (scf .isRunning ()).isTrue ();
416
+ assertThat (conField .get (scf )).isNotNull ();
417
+ Connection con4 = scf .getConnection ();
418
+ assertThat (createConnectionMethodCounter .get ()).isEqualTo (3 );
419
+ assertThat (con4 .getExceptionListener ()).isNotNull ();
420
+ assertThat (con4 ).isNotSameAs (con3 );
397
421
398
422
// Invoke reset connection to simulate problem with connection
399
423
// - SCF exception listener should be invoked -> connection should be set to null
@@ -405,16 +429,21 @@ public void setExceptionListener(ExceptionListener exceptionListener) throws JMS
405
429
// - JMSException should be returned from FailingTestConnection
406
430
// - connection should be still null (no new connection without exception listener like before fix)
407
431
assertThatExceptionOfType (JMSException .class ).isThrownBy (() -> scf .getConnection ());
408
- assertThat (createConnectionMethodCounter .get ()).isEqualTo (2 );
432
+ assertThat (createConnectionMethodCounter .get ()).isEqualTo (4 );
409
433
assertThat (conField .get (scf )).isNull ();
410
434
411
435
// Attempt to get connection again -> FailingTestConnection should be returned
412
- // - no JMSException is thrown, exception listener should be present
413
- Connection con3 = scf .getConnection ();
414
- assertThat (createConnectionMethodCounter .get ()).isEqualTo (3 );
415
- assertThat (con3 ).isNotNull ();
416
- assertThat (con3 ).isSameAs (failingCon );
417
- assertThat (con3 .getExceptionListener ()).isNotNull ();
436
+ // - no JMSException is thrown, exception listener should be present
437
+ Connection con5 = scf .getConnection ();
438
+ assertThat (createConnectionMethodCounter .get ()).isEqualTo (5 );
439
+ assertThat (con5 ).isNotNull ();
440
+ assertThat (con5 ).isSameAs (failingCon );
441
+ assertThat (con5 .getExceptionListener ()).isNotNull ();
442
+ assertThat (con5 ).isNotSameAs (con4 );
443
+
444
+ scf .destroy ();
445
+ assertThat (conField .get (scf )).isNull ();
446
+ assertThat (scf .isRunning ()).isFalse ();
418
447
}
419
448
420
449
@ Test
0 commit comments