@@ -297,20 +297,27 @@ public void becomeUser_shouldChangeLocaleWhenBecomeAnotherUser() {
297297 public void evictEntity_shouldClearTheEntityFromCaches () {
298298 TestTransaction .end ();
299299
300- // Load the person name so that it is stored in the cache
301- PersonName name = Context .getPersonService ().getPersonName (PERSON_NAME_ID_2 );
300+ // Statistics are disabled by default; enable them so hit counts below are meaningful
301+ boolean statisticsEnabled = sf .getStatistics ().isStatisticsEnabled ();
302+ sf .getStatistics ().setStatisticsEnabled (true );
303+ try {
304+ // Load the person name so that it is stored in the cache
305+ PersonName name = Context .getPersonService ().getPersonName (PERSON_NAME_ID_2 );
302306
303- // Clear session so that the first-level cache is empty
304- Context .flushSession ();
305- Context .clearSession ();
307+ // Clear session so that the first-level cache is empty
308+ Context .flushSession ();
309+ Context .clearSession ();
306310
307- // evictEntity
308- Context .evictEntity (name );
311+ // evictEntity
312+ Context .evictEntity (name );
309313
310- // Assert that the entity name has been removed from cache
311- long hitCount = sf .getStatistics ().getSecondLevelCacheHitCount ();
312- Context .getPersonService ().getPersonName (PERSON_NAME_ID_2 );
313- assertThat (sf .getStatistics ().getSecondLevelCacheHitCount (), is (hitCount ));
314+ // Assert that the entity name has been removed from cache
315+ long hitCount = sf .getStatistics ().getSecondLevelCacheHitCount ();
316+ Context .getPersonService ().getPersonName (PERSON_NAME_ID_2 );
317+ assertThat (sf .getStatistics ().getSecondLevelCacheHitCount (), is (hitCount ));
318+ } finally {
319+ sf .getStatistics ().setStatisticsEnabled (statisticsEnabled );
320+ }
314321 }
315322
316323 /**
@@ -320,23 +327,30 @@ public void evictEntity_shouldClearTheEntityFromCaches() {
320327 public void evictAllEntities_shouldClearAllEntityFromCaches () {
321328 TestTransaction .end ();
322329
323- // Load person names so that they are stored in the cache
324- Context .getPersonService ().getPersonName (PERSON_NAME_ID_2 );
325- Context .getPersonService ().getPersonName (PERSON_NAME_ID_8 );
326-
327- // Clear session so that the first-level cache is empty
328- Context .flushSession ();
329- Context .clearSession ();
330-
331- // evictAllEntities
332- Context .evictAllEntities (PERSON_NAME_CLASS );
333-
334- // Assert that the class entities have been removed from cache
335- long hitCount = sf .getStatistics ().getSecondLevelCacheHitCount ();
336- Context .getPersonService ().getPersonName (PERSON_NAME_ID_2 );
337- assertThat (sf .getStatistics ().getSecondLevelCacheHitCount (), is (hitCount ));
338- Context .getPersonService ().getPersonName (PERSON_NAME_ID_8 );
339- assertThat (sf .getStatistics ().getSecondLevelCacheHitCount (), is (hitCount ));
330+ // Statistics are disabled by default; enable them so hit counts below are meaningful
331+ boolean statisticsEnabled = sf .getStatistics ().isStatisticsEnabled ();
332+ sf .getStatistics ().setStatisticsEnabled (true );
333+ try {
334+ // Load person names so that they are stored in the cache
335+ Context .getPersonService ().getPersonName (PERSON_NAME_ID_2 );
336+ Context .getPersonService ().getPersonName (PERSON_NAME_ID_8 );
337+
338+ // Clear session so that the first-level cache is empty
339+ Context .flushSession ();
340+ Context .clearSession ();
341+
342+ // evictAllEntities
343+ Context .evictAllEntities (PERSON_NAME_CLASS );
344+
345+ // Assert that the class entities have been removed from cache
346+ long hitCount = sf .getStatistics ().getSecondLevelCacheHitCount ();
347+ Context .getPersonService ().getPersonName (PERSON_NAME_ID_2 );
348+ assertThat (sf .getStatistics ().getSecondLevelCacheHitCount (), is (hitCount ));
349+ Context .getPersonService ().getPersonName (PERSON_NAME_ID_8 );
350+ assertThat (sf .getStatistics ().getSecondLevelCacheHitCount (), is (hitCount ));
351+ } finally {
352+ sf .getStatistics ().setStatisticsEnabled (statisticsEnabled );
353+ }
340354 }
341355
342356 /**
@@ -346,26 +360,33 @@ public void evictAllEntities_shouldClearAllEntityFromCaches() {
346360 public void clearEntireCache_shouldClearEntireCache () {
347361 TestTransaction .end ();
348362
349- // Load person names and patient so that they are stored in the cache
350- Context .getPersonService ().getPersonName (PERSON_NAME_ID_2 );
351- Context .getPersonService ().getPersonName (PERSON_NAME_ID_8 );
352- Context .getPatientService ().getPatient (PERSON_NAME_ID_2 );
353-
354- // Clear session so that the first-level cache is empty
355- Context .flushSession ();
356- Context .clearSession ();
357-
358- // clearEntireCache
359- Context .clearEntireCache ();
360-
361- // Assert that all entities have been removed from cache
362- long hitCount = sf .getStatistics ().getSecondLevelCacheHitCount ();
363- Context .getPersonService ().getPersonName (PERSON_NAME_ID_2 );
364- assertThat (sf .getStatistics ().getSecondLevelCacheHitCount (), is (hitCount ));
365- Context .getPersonService ().getPersonName (PERSON_NAME_ID_8 );
366- assertThat (sf .getStatistics ().getSecondLevelCacheHitCount (), is (hitCount ));
367- Context .getPatientService ().getPatient (PERSON_NAME_ID_2 );
368- assertThat (sf .getStatistics ().getSecondLevelCacheHitCount (), is (hitCount ));
363+ // Statistics are disabled by default; enable them so hit counts below are meaningful
364+ boolean statisticsEnabled = sf .getStatistics ().isStatisticsEnabled ();
365+ sf .getStatistics ().setStatisticsEnabled (true );
366+ try {
367+ // Load person names and patient so that they are stored in the cache
368+ Context .getPersonService ().getPersonName (PERSON_NAME_ID_2 );
369+ Context .getPersonService ().getPersonName (PERSON_NAME_ID_8 );
370+ Context .getPatientService ().getPatient (PERSON_NAME_ID_2 );
371+
372+ // Clear session so that the first-level cache is empty
373+ Context .flushSession ();
374+ Context .clearSession ();
375+
376+ // clearEntireCache
377+ Context .clearEntireCache ();
378+
379+ // Assert that all entities have been removed from cache
380+ long hitCount = sf .getStatistics ().getSecondLevelCacheHitCount ();
381+ Context .getPersonService ().getPersonName (PERSON_NAME_ID_2 );
382+ assertThat (sf .getStatistics ().getSecondLevelCacheHitCount (), is (hitCount ));
383+ Context .getPersonService ().getPersonName (PERSON_NAME_ID_8 );
384+ assertThat (sf .getStatistics ().getSecondLevelCacheHitCount (), is (hitCount ));
385+ Context .getPatientService ().getPatient (PERSON_NAME_ID_2 );
386+ assertThat (sf .getStatistics ().getSecondLevelCacheHitCount (), is (hitCount ));
387+ } finally {
388+ sf .getStatistics ().setStatisticsEnabled (statisticsEnabled );
389+ }
369390 }
370391
371392 /**
0 commit comments