Commit 00cb3ec04590da2d5151a9742d6ad3f805ae2e23
1 parent
43b46f23
Exists in
master
Reativando solução após refactoring que corrige os testes.
Showing
6 changed files
with
91 additions
and
107 deletions
Show diff stats
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/AbstractEntityManagerStore.java
@@ -36,13 +36,10 @@ | @@ -36,13 +36,10 @@ | ||
36 | */ | 36 | */ |
37 | package br.gov.frameworkdemoiselle.internal.producer; | 37 | package br.gov.frameworkdemoiselle.internal.producer; |
38 | 38 | ||
39 | -import java.io.Serializable; | ||
40 | import java.util.Collections; | 39 | import java.util.Collections; |
41 | import java.util.HashMap; | 40 | import java.util.HashMap; |
42 | import java.util.Map; | 41 | import java.util.Map; |
43 | 42 | ||
44 | -import javax.annotation.PostConstruct; | ||
45 | -import javax.annotation.PreDestroy; | ||
46 | import javax.enterprise.context.RequestScoped; | 43 | import javax.enterprise.context.RequestScoped; |
47 | import javax.inject.Inject; | 44 | import javax.inject.Inject; |
48 | import javax.persistence.EntityManager; | 45 | import javax.persistence.EntityManager; |
@@ -71,7 +68,7 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle; | @@ -71,7 +68,7 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
71 | * @author serpro | 68 | * @author serpro |
72 | * | 69 | * |
73 | */ | 70 | */ |
74 | -public abstract class AbstractEntityManagerStore implements Serializable { | 71 | +public abstract class AbstractEntityManagerStore implements EntityManagerStore { |
75 | 72 | ||
76 | private static final long serialVersionUID = 1L; | 73 | private static final long serialVersionUID = 1L; |
77 | 74 | ||
@@ -90,7 +87,7 @@ public abstract class AbstractEntityManagerStore implements Serializable { | @@ -90,7 +87,7 @@ public abstract class AbstractEntityManagerStore implements Serializable { | ||
90 | @Inject | 87 | @Inject |
91 | private EntityManagerConfig configuration; | 88 | private EntityManagerConfig configuration; |
92 | 89 | ||
93 | - EntityManager getEntityManager(String persistenceUnit) { | 90 | + public EntityManager getEntityManager(String persistenceUnit) { |
94 | EntityManager entityManager = null; | 91 | EntityManager entityManager = null; |
95 | 92 | ||
96 | if (cache.containsKey(persistenceUnit)) { | 93 | if (cache.containsKey(persistenceUnit)) { |
@@ -107,19 +104,6 @@ public abstract class AbstractEntityManagerStore implements Serializable { | @@ -107,19 +104,6 @@ public abstract class AbstractEntityManagerStore implements Serializable { | ||
107 | return entityManager; | 104 | return entityManager; |
108 | } | 105 | } |
109 | 106 | ||
110 | - /** | ||
111 | - * Run this to initialize all persistence units. It's recomended this method | ||
112 | - * be annotated with {@link PostConstruct}, so it runs as soon as an EntityManager gets injected. | ||
113 | - */ | ||
114 | - public abstract void initialize(); | ||
115 | - | ||
116 | - /** | ||
117 | - * Run this to close all persistence units. It's recomended this method | ||
118 | - * be annotated with {@link PreDestroy}, so it runs as soon as the scope the EntityManager is | ||
119 | - * attached to ends. | ||
120 | - */ | ||
121 | - public abstract void terminate(); | ||
122 | - | ||
123 | void init() { | 107 | void init() { |
124 | for (String persistenceUnit : getFactory().getCache().keySet()) { | 108 | for (String persistenceUnit : getFactory().getCache().keySet()) { |
125 | getEntityManager(persistenceUnit); | 109 | getEntityManager(persistenceUnit); |
@@ -138,7 +122,11 @@ public abstract class AbstractEntityManagerStore implements Serializable { | @@ -138,7 +122,11 @@ public abstract class AbstractEntityManagerStore implements Serializable { | ||
138 | cache.clear(); | 122 | cache.clear(); |
139 | } | 123 | } |
140 | 124 | ||
141 | - Map<String, EntityManager> getCache() { | 125 | + public Map<String, EntityManager> getCache() { |
126 | + if (cache==null || cache.isEmpty()){ | ||
127 | + init(); | ||
128 | + } | ||
129 | + | ||
142 | return cache; | 130 | return cache; |
143 | } | 131 | } |
144 | 132 | ||
@@ -149,14 +137,14 @@ public abstract class AbstractEntityManagerStore implements Serializable { | @@ -149,14 +137,14 @@ public abstract class AbstractEntityManagerStore implements Serializable { | ||
149 | return factory; | 137 | return factory; |
150 | } | 138 | } |
151 | 139 | ||
152 | - protected Logger getLogger(){ | 140 | + private Logger getLogger(){ |
153 | if (logger==null){ | 141 | if (logger==null){ |
154 | logger = Beans.getReference(Logger.class); | 142 | logger = Beans.getReference(Logger.class); |
155 | } | 143 | } |
156 | return logger; | 144 | return logger; |
157 | } | 145 | } |
158 | 146 | ||
159 | - protected ResourceBundle getBundle(){ | 147 | + private ResourceBundle getBundle(){ |
160 | if (bundle==null){ | 148 | if (bundle==null){ |
161 | bundle = Beans.getReference(ResourceBundle.class , new NameQualifier("demoiselle-jpa-bundle")); | 149 | bundle = Beans.getReference(ResourceBundle.class , new NameQualifier("demoiselle-jpa-bundle")); |
162 | } | 150 | } |
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerProducer.java
@@ -37,20 +37,16 @@ | @@ -37,20 +37,16 @@ | ||
37 | package br.gov.frameworkdemoiselle.internal.producer; | 37 | package br.gov.frameworkdemoiselle.internal.producer; |
38 | 38 | ||
39 | import java.io.Serializable; | 39 | import java.io.Serializable; |
40 | -import java.util.Collections; | ||
41 | -import java.util.HashMap; | ||
42 | import java.util.Map; | 40 | import java.util.Map; |
43 | import java.util.Set; | 41 | import java.util.Set; |
44 | 42 | ||
45 | -import javax.annotation.PostConstruct; | ||
46 | -import javax.annotation.PreDestroy; | ||
47 | -import javax.enterprise.context.RequestScoped; | 43 | +import javax.enterprise.context.ApplicationScoped; |
48 | import javax.enterprise.inject.Default; | 44 | import javax.enterprise.inject.Default; |
45 | +import javax.enterprise.inject.Instance; | ||
49 | import javax.enterprise.inject.Produces; | 46 | import javax.enterprise.inject.Produces; |
50 | import javax.enterprise.inject.spi.InjectionPoint; | 47 | import javax.enterprise.inject.spi.InjectionPoint; |
51 | import javax.inject.Inject; | 48 | import javax.inject.Inject; |
52 | import javax.persistence.EntityManager; | 49 | import javax.persistence.EntityManager; |
53 | -import javax.persistence.FlushModeType; | ||
54 | 50 | ||
55 | import org.slf4j.Logger; | 51 | import org.slf4j.Logger; |
56 | 52 | ||
@@ -59,8 +55,6 @@ import br.gov.frameworkdemoiselle.annotation.Name; | @@ -59,8 +55,6 @@ import br.gov.frameworkdemoiselle.annotation.Name; | ||
59 | import br.gov.frameworkdemoiselle.configuration.Configuration; | 55 | import br.gov.frameworkdemoiselle.configuration.Configuration; |
60 | import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig; | 56 | import br.gov.frameworkdemoiselle.internal.configuration.EntityManagerConfig; |
61 | import br.gov.frameworkdemoiselle.internal.proxy.EntityManagerProxy; | 57 | import br.gov.frameworkdemoiselle.internal.proxy.EntityManagerProxy; |
62 | -import br.gov.frameworkdemoiselle.util.Beans; | ||
63 | -import br.gov.frameworkdemoiselle.util.NameQualifier; | ||
64 | import br.gov.frameworkdemoiselle.util.ResourceBundle; | 58 | import br.gov.frameworkdemoiselle.util.ResourceBundle; |
65 | 59 | ||
66 | /** | 60 | /** |
@@ -69,8 +63,7 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle; | @@ -69,8 +63,7 @@ import br.gov.frameworkdemoiselle.util.ResourceBundle; | ||
69 | * persistence.xml, demoiselle.properties or @PersistenceUnit annotation. | 63 | * persistence.xml, demoiselle.properties or @PersistenceUnit annotation. |
70 | * </p> | 64 | * </p> |
71 | */ | 65 | */ |
72 | -//TODO Concluir implementação de escopo selecionável, testes revelaram problemas na solução atual. | ||
73 | -@RequestScoped | 66 | +@ApplicationScoped |
74 | public class EntityManagerProducer implements Serializable{ | 67 | public class EntityManagerProducer implements Serializable{ |
75 | 68 | ||
76 | private static final long serialVersionUID = 1L; | 69 | private static final long serialVersionUID = 1L; |
@@ -85,13 +78,11 @@ public class EntityManagerProducer implements Serializable{ | @@ -85,13 +78,11 @@ public class EntityManagerProducer implements Serializable{ | ||
85 | @Inject | 78 | @Inject |
86 | private EntityManagerFactoryProducer factory; | 79 | private EntityManagerFactoryProducer factory; |
87 | 80 | ||
88 | - /*@Inject | ||
89 | - private Instance<AbstractEntityManagerStore> storeInstance;*/ | 81 | + @Inject |
82 | + private Instance<EntityManagerStore> storeInstance; | ||
90 | 83 | ||
91 | - private final Map<String, EntityManager> cache = Collections.synchronizedMap(new HashMap<String, EntityManager>()); | ||
92 | - | ||
93 | - /*@Inject | ||
94 | - private EntityManagerConfig configuration;*/ | 84 | + @Inject |
85 | + private EntityManagerConfig configuration; | ||
95 | 86 | ||
96 | /** | 87 | /** |
97 | * <p> | 88 | * <p> |
@@ -136,47 +127,6 @@ public class EntityManagerProducer implements Serializable{ | @@ -136,47 +127,6 @@ public class EntityManagerProducer implements Serializable{ | ||
136 | return new EntityManagerProxy(persistenceUnit); | 127 | return new EntityManagerProxy(persistenceUnit); |
137 | } | 128 | } |
138 | 129 | ||
139 | - /*public EntityManager getEntityManager(String persistenceUnit) { | ||
140 | - return getStore().getEntityManager(persistenceUnit); | ||
141 | - }*/ | ||
142 | - | ||
143 | - @PostConstruct | ||
144 | - void init() { | ||
145 | - for (String persistenceUnit : getFactory().getCache().keySet()) { | ||
146 | - getEntityManager(persistenceUnit); | ||
147 | - } | ||
148 | - } | ||
149 | - | ||
150 | - @PreDestroy | ||
151 | - void close() { | ||
152 | - //Se o produtor não possui escopo, então o ciclo de vida | ||
153 | - //de EntityManager produzidos é responsabilidade do desenvolvedor. Não | ||
154 | - //fechamos os EntityManagers aqui. | ||
155 | - //if (configuration.getEntityManagerScope() != EntityManagerScope.NOSCOPE){ | ||
156 | - for (EntityManager entityManager : cache.values()) { | ||
157 | - entityManager.close(); | ||
158 | - } | ||
159 | - //} | ||
160 | - cache.clear(); | ||
161 | - } | ||
162 | - | ||
163 | - public EntityManager getEntityManager(String persistenceUnit) { | ||
164 | - EntityManager entityManager = null; | ||
165 | - | ||
166 | - if (cache.containsKey(persistenceUnit)) { | ||
167 | - entityManager = cache.get(persistenceUnit); | ||
168 | - | ||
169 | - } else { | ||
170 | - entityManager = getFactory().create(persistenceUnit).createEntityManager(); | ||
171 | - entityManager.setFlushMode(FlushModeType.AUTO); | ||
172 | - | ||
173 | - cache.put(persistenceUnit, entityManager); | ||
174 | - this.getLogger().info(getBundle().getString("entity-manager-was-created", persistenceUnit)); | ||
175 | - } | ||
176 | - | ||
177 | - return entityManager; | ||
178 | - } | ||
179 | - | ||
180 | /** | 130 | /** |
181 | * Tries to get persistence unit name from demoiselle.properties. | 131 | * Tries to get persistence unit name from demoiselle.properties. |
182 | * | 132 | * |
@@ -212,12 +162,15 @@ public class EntityManagerProducer implements Serializable{ | @@ -212,12 +162,15 @@ public class EntityManagerProducer implements Serializable{ | ||
212 | } | 162 | } |
213 | } | 163 | } |
214 | 164 | ||
165 | + public EntityManager getEntityManager(String persistenceUnit) { | ||
166 | + return getStore().getEntityManager(persistenceUnit); | ||
167 | + } | ||
168 | + | ||
215 | public Map<String, EntityManager> getCache() { | 169 | public Map<String, EntityManager> getCache() { |
216 | - //return getStore().getCache(); | ||
217 | - return this.cache; | 170 | + return getStore().getCache(); |
218 | } | 171 | } |
219 | 172 | ||
220 | - /*private AbstractEntityManagerStore getStore(){ | 173 | + private EntityManagerStore getStore(){ |
221 | switch(configuration.getEntityManagerScope()){ | 174 | switch(configuration.getEntityManagerScope()){ |
222 | case REQUEST: | 175 | case REQUEST: |
223 | return storeInstance.select(RequestEntityManagerStore.class).get(); | 176 | return storeInstance.select(RequestEntityManagerStore.class).get(); |
@@ -234,26 +187,6 @@ public class EntityManagerProducer implements Serializable{ | @@ -234,26 +187,6 @@ public class EntityManagerProducer implements Serializable{ | ||
234 | default: | 187 | default: |
235 | return storeInstance.select(RequestEntityManagerStore.class).get(); | 188 | return storeInstance.select(RequestEntityManagerStore.class).get(); |
236 | } | 189 | } |
237 | - }*/ | ||
238 | - | ||
239 | - protected Logger getLogger(){ | ||
240 | - if (logger==null){ | ||
241 | - logger = Beans.getReference(Logger.class); | ||
242 | - } | ||
243 | - return logger; | ||
244 | } | 190 | } |
245 | 191 | ||
246 | - protected ResourceBundle getBundle(){ | ||
247 | - if (bundle==null){ | ||
248 | - bundle = Beans.getReference(ResourceBundle.class , new NameQualifier("demoiselle-jpa-bundle")); | ||
249 | - } | ||
250 | - return bundle; | ||
251 | - } | ||
252 | - | ||
253 | - private EntityManagerFactoryProducer getFactory(){ | ||
254 | - if (factory==null){ | ||
255 | - factory = Beans.getReference(EntityManagerFactoryProducer.class); | ||
256 | - } | ||
257 | - return factory; | ||
258 | - } | ||
259 | } | 192 | } |
impl/extension/jpa/src/main/java/br/gov/frameworkdemoiselle/internal/producer/EntityManagerStore.java
0 → 100644
@@ -0,0 +1,30 @@ | @@ -0,0 +1,30 @@ | ||
1 | +package br.gov.frameworkdemoiselle.internal.producer; | ||
2 | + | ||
3 | +import java.io.Serializable; | ||
4 | +import java.util.Map; | ||
5 | + | ||
6 | +import javax.annotation.PostConstruct; | ||
7 | +import javax.annotation.PreDestroy; | ||
8 | +import javax.persistence.EntityManager; | ||
9 | + | ||
10 | + | ||
11 | +public interface EntityManagerStore extends Serializable { | ||
12 | + | ||
13 | + /** | ||
14 | + * Run this to initialize all persistence units. It's recomended this method | ||
15 | + * be annotated with {@link PostConstruct}, so it runs as soon as an EntityManager gets injected. | ||
16 | + */ | ||
17 | + public abstract void initialize(); | ||
18 | + | ||
19 | + /** | ||
20 | + * Run this to close all persistence units. It's recomended this method | ||
21 | + * be annotated with {@link PreDestroy}, so it runs as soon as the scope the EntityManager is | ||
22 | + * attached to ends. | ||
23 | + */ | ||
24 | + public abstract void terminate(); | ||
25 | + | ||
26 | + Map<String, EntityManager> getCache(); | ||
27 | + | ||
28 | + public EntityManager getEntityManager(String persistenceUnit); | ||
29 | + | ||
30 | +} |
impl/extension/jpa/src/test/java/producer/NoScopedProducerTest.java
@@ -9,7 +9,6 @@ import javax.persistence.EntityManager; | @@ -9,7 +9,6 @@ import javax.persistence.EntityManager; | ||
9 | import org.jboss.arquillian.container.test.api.Deployment; | 9 | import org.jboss.arquillian.container.test.api.Deployment; |
10 | import org.jboss.arquillian.junit.Arquillian; | 10 | import org.jboss.arquillian.junit.Arquillian; |
11 | import org.jboss.shrinkwrap.api.spec.WebArchive; | 11 | import org.jboss.shrinkwrap.api.spec.WebArchive; |
12 | -import org.junit.Ignore; | ||
13 | import org.junit.Test; | 12 | import org.junit.Test; |
14 | import org.junit.runner.RunWith; | 13 | import org.junit.runner.RunWith; |
15 | 14 | ||
@@ -18,8 +17,6 @@ import br.gov.frameworkdemoiselle.internal.proxy.EntityManagerProxy; | @@ -18,8 +17,6 @@ import br.gov.frameworkdemoiselle.internal.proxy.EntityManagerProxy; | ||
18 | import br.gov.frameworkdemoiselle.util.Beans; | 17 | import br.gov.frameworkdemoiselle.util.Beans; |
19 | import br.gov.frameworkdemoiselle.util.NameQualifier; | 18 | import br.gov.frameworkdemoiselle.util.NameQualifier; |
20 | 19 | ||
21 | -//TODO Implementação de escopo selecionável tem que concluir antes de ativar esse teste | ||
22 | -@Ignore | ||
23 | @RunWith(Arquillian.class) | 20 | @RunWith(Arquillian.class) |
24 | public class NoScopedProducerTest { | 21 | public class NoScopedProducerTest { |
25 | 22 |
impl/extension/jpa/src/test/java/producer/RequestScopedProducerTest.java
@@ -9,6 +9,7 @@ import javax.persistence.EntityManager; | @@ -9,6 +9,7 @@ import javax.persistence.EntityManager; | ||
9 | import org.jboss.arquillian.container.test.api.Deployment; | 9 | import org.jboss.arquillian.container.test.api.Deployment; |
10 | import org.jboss.arquillian.junit.Arquillian; | 10 | import org.jboss.arquillian.junit.Arquillian; |
11 | import org.jboss.shrinkwrap.api.spec.WebArchive; | 11 | import org.jboss.shrinkwrap.api.spec.WebArchive; |
12 | +import org.jboss.weld.context.http.HttpRequestContext; | ||
12 | import org.junit.Test; | 13 | import org.junit.Test; |
13 | import org.junit.runner.RunWith; | 14 | import org.junit.runner.RunWith; |
14 | 15 | ||
@@ -72,6 +73,44 @@ public class RequestScopedProducerTest { | @@ -72,6 +73,44 @@ public class RequestScopedProducerTest { | ||
72 | assertTrue(m2.contains(entity)); | 73 | assertTrue(m2.contains(entity)); |
73 | } | 74 | } |
74 | 75 | ||
76 | + @Test | ||
77 | + public void produceDifferentEntityManagerPerRequest() { | ||
78 | + HttpRequestContext weldContext = Beans.getReference(HttpRequestContext.class); | ||
79 | + | ||
80 | + boolean wasNotActive = false; | ||
81 | + if (!weldContext.isActive()){ | ||
82 | + wasNotActive = true; | ||
83 | + weldContext.activate(); | ||
84 | + } | ||
85 | + | ||
86 | + EntityManager m1 = Beans.getReference(EntityManager.class, new NameQualifier("pu")); | ||
87 | + assertNotNull(m1); | ||
88 | + assertEquals(EntityManagerProxy.class, m1.getClass()); | ||
89 | + | ||
90 | + MyEntity entity = new MyEntity(); | ||
91 | + entity.setId(createId("testID")); | ||
92 | + | ||
93 | + m1.persist(entity); | ||
94 | + assertTrue(m1.contains(entity)); | ||
95 | + | ||
96 | + weldContext.invalidate(); | ||
97 | + weldContext.deactivate(); | ||
98 | + | ||
99 | + if (!weldContext.isActive()){ | ||
100 | + weldContext.activate(); | ||
101 | + } | ||
102 | + | ||
103 | + EntityManager m2 = Beans.getReference(EntityManager.class, new NameQualifier("pu")); | ||
104 | + | ||
105 | + assertTrue( m2.isOpen() ); | ||
106 | + assertTrue( !m2.contains(entity)); | ||
107 | + | ||
108 | + if (wasNotActive && weldContext.isActive()){ | ||
109 | + weldContext.invalidate(); | ||
110 | + weldContext.deactivate(); | ||
111 | + } | ||
112 | + } | ||
113 | + | ||
75 | private String createId(String id) { | 114 | private String createId(String id) { |
76 | return this.getClass().getName() + "_" + id; | 115 | return this.getClass().getName() + "_" + id; |
77 | } | 116 | } |
impl/extension/jpa/src/test/java/producer/ViewScopedProducerTest.java
@@ -10,7 +10,6 @@ import javax.persistence.EntityManager; | @@ -10,7 +10,6 @@ import javax.persistence.EntityManager; | ||
10 | import org.jboss.arquillian.container.test.api.Deployment; | 10 | import org.jboss.arquillian.container.test.api.Deployment; |
11 | import org.jboss.arquillian.junit.Arquillian; | 11 | import org.jboss.arquillian.junit.Arquillian; |
12 | import org.jboss.shrinkwrap.api.spec.WebArchive; | 12 | import org.jboss.shrinkwrap.api.spec.WebArchive; |
13 | -import org.junit.Ignore; | ||
14 | import org.junit.Test; | 13 | import org.junit.Test; |
15 | import org.junit.runner.RunWith; | 14 | import org.junit.runner.RunWith; |
16 | 15 | ||
@@ -20,8 +19,6 @@ import br.gov.frameworkdemoiselle.internal.proxy.EntityManagerProxy; | @@ -20,8 +19,6 @@ import br.gov.frameworkdemoiselle.internal.proxy.EntityManagerProxy; | ||
20 | import br.gov.frameworkdemoiselle.util.Beans; | 19 | import br.gov.frameworkdemoiselle.util.Beans; |
21 | import br.gov.frameworkdemoiselle.util.NameQualifier; | 20 | import br.gov.frameworkdemoiselle.util.NameQualifier; |
22 | 21 | ||
23 | -//TODO Implementação de escopo selecionável tem que concluir antes de ativar esse teste | ||
24 | -@Ignore | ||
25 | @RunWith(Arquillian.class) | 22 | @RunWith(Arquillian.class) |
26 | public class ViewScopedProducerTest { | 23 | public class ViewScopedProducerTest { |
27 | 24 |