|
| 1 | +package org.baeldung.persistence.service; |
| 2 | + |
| 3 | +import net.sf.ehcache.CacheManager; |
| 4 | +import org.baeldung.config.PersistenceJPAConfigL2Cache; |
| 5 | +import org.baeldung.persistence.model.Bar; |
| 6 | +import org.baeldung.persistence.model.Foo; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.test.context.ContextConfiguration; |
| 12 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 13 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 14 | +import org.springframework.transaction.PlatformTransactionManager; |
| 15 | +import org.springframework.transaction.support.TransactionTemplate; |
| 16 | + |
| 17 | +import javax.persistence.EntityManager; |
| 18 | +import javax.persistence.PersistenceContext; |
| 19 | +import javax.persistence.Query; |
| 20 | + |
| 21 | +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; |
| 22 | +import static org.hamcrest.Matchers.greaterThan; |
| 23 | +import static org.junit.Assert.assertThat; |
| 24 | + |
| 25 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 26 | +@ContextConfiguration(classes = { PersistenceJPAConfigL2Cache.class }, loader = AnnotationConfigContextLoader.class) |
| 27 | +public class SecondLevelCacheIntegrationTest { |
| 28 | + |
| 29 | + @PersistenceContext |
| 30 | + private EntityManager entityManager; |
| 31 | + @Autowired |
| 32 | + private FooService fooService; |
| 33 | + @Autowired |
| 34 | + private PlatformTransactionManager platformTransactionManager; |
| 35 | + |
| 36 | + @Before |
| 37 | + public final void before() { |
| 38 | + entityManager.getEntityManagerFactory().getCache().evictAll(); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public final void givenEntityIsLoaded_thenItIsCached() { |
| 43 | + final Foo foo = new Foo(randomAlphabetic(6)); |
| 44 | + fooService.create(foo); |
| 45 | + fooService.findOne(foo.getId()); |
| 46 | + final int size = CacheManager.ALL_CACHE_MANAGERS.get(0) |
| 47 | + .getCache("org.baeldung.persistence.model.Foo").getSize(); |
| 48 | + assertThat(size, greaterThan(0)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public final void givenBarIsUpdatedInNativeQuery_thenFoosAreNotEvicted() { |
| 53 | + final Foo foo = new Foo(randomAlphabetic(6)); |
| 54 | + fooService.create(foo); |
| 55 | + fooService.findOne(foo.getId()); |
| 56 | + |
| 57 | + new TransactionTemplate(platformTransactionManager).execute(status -> { |
| 58 | + final Bar bar = new Bar(randomAlphabetic(6)); |
| 59 | + entityManager.persist(bar); |
| 60 | + final Query nativeQuery = entityManager.createNativeQuery("update BAR set NAME = :updatedName where ID = :id"); |
| 61 | + nativeQuery.setParameter("updatedName", "newName"); |
| 62 | + nativeQuery.setParameter("id", bar.getId()); |
| 63 | + nativeQuery.unwrap(org.hibernate.SQLQuery.class).addSynchronizedEntityClass(Bar.class); |
| 64 | + return nativeQuery.executeUpdate(); |
| 65 | + }); |
| 66 | + |
| 67 | + final int size = CacheManager.ALL_CACHE_MANAGERS.get(0) |
| 68 | + .getCache("org.baeldung.persistence.model.Foo").getSize(); |
| 69 | + assertThat(size, greaterThan(0)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public final void givenCacheableQueryIsExecuted_thenItIsCached() { |
| 74 | + new TransactionTemplate(platformTransactionManager).execute(status -> { |
| 75 | + return entityManager.createQuery("select f from Foo f") |
| 76 | + .setHint("org.hibernate.cacheable", true) |
| 77 | + .getResultList(); |
| 78 | + }); |
| 79 | + |
| 80 | + final int size = CacheManager.ALL_CACHE_MANAGERS.get(0) |
| 81 | + .getCache("org.hibernate.cache.internal.StandardQueryCache").getSize(); |
| 82 | + assertThat(size, greaterThan(0)); |
| 83 | + } |
| 84 | +} |
0 commit comments