Commit 45fd05749e62f7b05ecb8f3de106d6166a880e8e

Authored by Emerson Oliveira
1 parent 35c25fc8
Exists in master

IN PROGRESS - issue FWK-166: Possibilidade do usuário injetar

EntityManagerFactory em sua aplicação 
https://demoiselle.atlassian.net/browse/FWK-166

Testes do produtor de EntityManagerFactory, utilizando a injeção com
qualificador Name, e com o valor padrão
impl/extension/jpa/src/test/java/producer/entitymanagerfactory/multipu/DummyEntityDefault.java 0 → 100644
... ... @@ -0,0 +1,75 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package producer.entitymanagerfactory.multipu;
  38 +
  39 +import javax.persistence.Entity;
  40 +import javax.persistence.Id;
  41 +
  42 +@Entity
  43 +public class DummyEntityDefault {
  44 +
  45 + @Id
  46 + private Long id;
  47 +
  48 + private String description;
  49 +
  50 + public DummyEntityDefault() {
  51 + super();
  52 + }
  53 +
  54 + public DummyEntityDefault(String description, Long id) {
  55 + super();
  56 + this.description = description;
  57 + this.id = id;
  58 + }
  59 +
  60 + public Long getId() {
  61 + return id;
  62 + }
  63 +
  64 + public void setId(Long id) {
  65 + this.id = id;
  66 + }
  67 +
  68 + public String getDescription() {
  69 + return description;
  70 + }
  71 +
  72 + public void setDescription(String description) {
  73 + this.description = description;
  74 + }
  75 +}
... ...
impl/extension/jpa/src/test/java/producer/entitymanagerfactory/multipu/DummyEntityNamed.java 0 → 100644
... ... @@ -0,0 +1,75 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package producer.entitymanagerfactory.multipu;
  38 +
  39 +import javax.persistence.Entity;
  40 +import javax.persistence.Id;
  41 +
  42 +@Entity
  43 +public class DummyEntityNamed {
  44 +
  45 + @Id
  46 + private Long id;
  47 +
  48 + private String description;
  49 +
  50 + public DummyEntityNamed() {
  51 + super();
  52 + }
  53 +
  54 + public DummyEntityNamed(String description, Long id) {
  55 + super();
  56 + this.description = description;
  57 + this.id = id;
  58 + }
  59 +
  60 + public Long getId() {
  61 + return id;
  62 + }
  63 +
  64 + public void setId(Long id) {
  65 + this.id = id;
  66 + }
  67 +
  68 + public String getDescription() {
  69 + return description;
  70 + }
  71 +
  72 + public void setDescription(String description) {
  73 + this.description = description;
  74 + }
  75 +}
... ...
impl/extension/jpa/src/test/java/producer/entitymanagerfactory/multipu/EntityManagerFactoryProducerMultiPersistenceUnitTest.java 0 → 100644
... ... @@ -0,0 +1,260 @@
  1 +/*
  2 + * Demoiselle Framework
  3 + * Copyright (C) 2010 SERPRO
  4 + * ----------------------------------------------------------------------------
  5 + * This file is part of Demoiselle Framework.
  6 + *
  7 + * Demoiselle Framework is free software; you can redistribute it and/or
  8 + * modify it under the terms of the GNU Lesser General Public License version 3
  9 + * as published by the Free Software Foundation.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License version 3
  17 + * along with this program; if not, see <http://www.gnu.org/licenses/>
  18 + * or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + * Fifth Floor, Boston, MA 02110-1301, USA.
  20 + * ----------------------------------------------------------------------------
  21 + * Este arquivo é parte do Framework Demoiselle.
  22 + *
  23 + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + * do Software Livre (FSF).
  26 + *
  27 + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + * para maiores detalhes.
  31 + *
  32 + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
  34 + * ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 + */
  37 +package producer.entitymanagerfactory.multipu;
  38 +
  39 +import static org.junit.Assert.assertNotNull;
  40 +import static org.junit.Assert.assertTrue;
  41 +
  42 +import java.util.List;
  43 +
  44 +import javax.persistence.EntityManager;
  45 +import javax.persistence.EntityManagerFactory;
  46 +import javax.persistence.PersistenceException;
  47 +import javax.persistence.Query;
  48 +
  49 +import org.jboss.arquillian.container.test.api.Deployment;
  50 +import org.jboss.arquillian.junit.Arquillian;
  51 +import org.jboss.shrinkwrap.api.spec.WebArchive;
  52 +import org.junit.Test;
  53 +import org.junit.runner.RunWith;
  54 +
  55 +import br.gov.frameworkdemoiselle.util.Beans;
  56 +import br.gov.frameworkdemoiselle.util.NameQualifier;
  57 +import test.Tests;
  58 +
  59 +@RunWith(Arquillian.class)
  60 +public class EntityManagerFactoryProducerMultiPersistenceUnitTest {
  61 +
  62 + private static final String PATH = "src/test/resources/producer/entitymanagerfactory/multi-pu";
  63 +
  64 + private final String descriptionOne = "Entity for Test One.";
  65 + private final String descriptionTwo = "Entity for Test Two.";
  66 +
  67 + private List<?> listOne;
  68 + private List<?> listTwo;
  69 + private List<?> listAll;
  70 +
  71 + private Query queryOne;
  72 + private Query queryTwo;
  73 + private Query queryAll;
  74 +
  75 + @Deployment
  76 + public static WebArchive createDeployment() {
  77 + WebArchive deployment = Tests.createDeployment(EntityManagerFactoryProducerMultiPersistenceUnitTest.class);
  78 + deployment.addAsResource(Tests.createFileAsset(PATH + "/persistence.xml"), "META-INF/persistence.xml");
  79 + deployment.addAsResource(Tests.createFileAsset(PATH + "/demoiselle.properties"), "demoiselle.properties");
  80 +
  81 + return deployment;
  82 + }
  83 +
  84 + @Test
  85 + public void produceDefaultEntityManagerFactory() {
  86 + EntityManagerFactory emf = Beans.getReference(EntityManagerFactory.class);
  87 + assertNotNull(emf);
  88 + assertTrue(emf.getMetamodel().getEntities().toString().contains(DummyEntityDefault.class.getSimpleName()));
  89 +
  90 + EntityManager em = emf.createEntityManager();
  91 +
  92 + DummyEntityDefault entityOne = new DummyEntityDefault(descriptionOne, 1L);
  93 + DummyEntityDefault entityTwo = new DummyEntityDefault(descriptionTwo, 2L);
  94 +
  95 + String jpqlOne = "select ded from DummyEntityDefault ded where ded.description like :descriptionOne";
  96 + String jpqlTwo = "select ded from DummyEntityDefault ded where ded.description like :descriptionTwo";
  97 + String jpqlAll = "select ded from DummyEntityDefault as ded";
  98 +
  99 + loadListBegin(em, entityOne, entityTwo, jpqlOne, jpqlTwo, jpqlAll);
  100 + assertTrue(listOne.size() == 1);
  101 + assertTrue(((DummyEntityDefault) listOne.get(0)).getId() == 1L);
  102 + assertTrue(((DummyEntityDefault) listOne.get(0)).getDescription().equals(descriptionOne));
  103 +
  104 + assertTrue(listTwo.size() == 1);
  105 + assertTrue(((DummyEntityDefault) listTwo.get(0)).getId() == 2L);
  106 + assertTrue(((DummyEntityDefault) listTwo.get(0)).getDescription().equals(descriptionTwo));
  107 +
  108 + assertTrue(listAll.size() == 2);
  109 +
  110 + loadListUpdate(em, entityOne, entityTwo);
  111 + assertTrue(listOne.size() == 0);
  112 + assertTrue(listTwo.size() == 0);
  113 + assertTrue(listAll.size() == 2);
  114 +
  115 + DummyEntityDefault ded = em.find(DummyEntityDefault.class, 1L);
  116 + assertTrue(ded.getDescription().equals("Entity for test one with description modified."));
  117 +
  118 + removeAndLoadList(em, entityOne, false);
  119 + assertTrue(listAll.size() == 1);
  120 +
  121 + removeAndLoadList(em, entityTwo, true);
  122 + assertTrue(listAll.size() == 0);
  123 +
  124 + }
  125 +
  126 + @Test
  127 + public void produceNamedOneEntityManagerFactory() {
  128 + EntityManagerFactory emf = Beans.getReference(EntityManagerFactory.class, new NameQualifier("pu"));
  129 + assertNotNull(emf);
  130 + assertTrue(emf.getMetamodel().getEntities().toString().contains(DummyEntityDefault.class.getSimpleName()));
  131 +
  132 + EntityManager em = emf.createEntityManager();
  133 +
  134 + DummyEntityDefault entityOne = new DummyEntityDefault(descriptionOne, 1L);
  135 + DummyEntityDefault entityTwo = new DummyEntityDefault(descriptionTwo, 2L);
  136 +
  137 + String jpqlOne = "select ded from DummyEntityDefault ded where ded.description like :descriptionOne";
  138 + String jpqlTwo = "select ded from DummyEntityDefault ded where ded.description like :descriptionTwo";
  139 + String jpqlAll = "select ded from DummyEntityDefault as ded";
  140 +
  141 + loadListBegin(em, entityOne, entityTwo, jpqlOne, jpqlTwo, jpqlAll);
  142 + assertTrue(listOne.size() == 1);
  143 + assertTrue(((DummyEntityDefault) listOne.get(0)).getId() == 1L);
  144 + assertTrue(((DummyEntityDefault) listOne.get(0)).getDescription().equals(descriptionOne));
  145 +
  146 + assertTrue(listTwo.size() == 1);
  147 + assertTrue(((DummyEntityDefault) listTwo.get(0)).getId() == 2L);
  148 + assertTrue(((DummyEntityDefault) listTwo.get(0)).getDescription().equals(descriptionTwo));
  149 +
  150 + assertTrue(listAll.size() == 2);
  151 +
  152 + loadListUpdate(em, entityOne, entityTwo);
  153 + assertTrue(listOne.size() == 0);
  154 + assertTrue(listTwo.size() == 0);
  155 + assertTrue(listAll.size() == 2);
  156 +
  157 + DummyEntityDefault ded = em.find(DummyEntityDefault.class, 1L);
  158 + assertTrue(ded.getDescription().equals("Entity for test one with description modified."));
  159 +
  160 + removeAndLoadList(em, entityOne, false);
  161 + assertTrue(listAll.size() == 1);
  162 +
  163 + removeAndLoadList(em, entityTwo, true);
  164 + assertTrue(listAll.size() == 0);
  165 + }
  166 +
  167 + @Test
  168 + public void produceNamedTwoEntityManagerFactory() {
  169 + EntityManagerFactory emf = Beans.getReference(EntityManagerFactory.class, new NameQualifier("pu2"));
  170 + assertNotNull(emf);
  171 + assertTrue(emf.getMetamodel().getEntities().toString().contains(DummyEntityNamed.class.getSimpleName()));
  172 +
  173 + EntityManager em = emf.createEntityManager();
  174 +
  175 + DummyEntityNamed entityOne = new DummyEntityNamed(descriptionOne, 1L);
  176 + DummyEntityNamed entityTwo = new DummyEntityNamed(descriptionTwo, 2L);
  177 +
  178 + String jpqlOne = "select den from DummyEntityNamed den where den.description like :descriptionOne";
  179 + String jpqlTwo = "select den from DummyEntityNamed den where den.description like :descriptionTwo";
  180 + String jpqlAll = "select den from DummyEntityNamed as den";
  181 +
  182 + loadListBegin(em, entityOne, entityTwo, jpqlOne, jpqlTwo, jpqlAll);
  183 + assertTrue(listOne.size() == 1);
  184 + assertTrue(((DummyEntityNamed) listOne.get(0)).getId() == 1L);
  185 + assertTrue(((DummyEntityNamed) listOne.get(0)).getDescription().equals(descriptionOne));
  186 +
  187 + assertTrue(listTwo.size() == 1);
  188 + assertTrue(((DummyEntityNamed) listTwo.get(0)).getId() == 2L);
  189 + assertTrue(((DummyEntityNamed) listTwo.get(0)).getDescription().equals(descriptionTwo));
  190 +
  191 + assertTrue(listAll.size() == 2);
  192 +
  193 + loadListUpdate(em, entityOne, entityTwo);
  194 + assertTrue(listOne.size() == 0);
  195 + assertTrue(listTwo.size() == 0);
  196 + assertTrue(listAll.size() == 2);
  197 +
  198 + DummyEntityNamed den = em.find(DummyEntityNamed.class, 1L);
  199 + assertTrue(den.getDescription().equals("Entity for test one with description modified."));
  200 +
  201 + removeAndLoadList(em, entityOne, false);
  202 + assertTrue(listAll.size() == 1);
  203 +
  204 + removeAndLoadList(em, entityTwo, true);
  205 + assertTrue(listAll.size() == 0);
  206 + }
  207 +
  208 + @SuppressWarnings("unused")
  209 + @Test(expected = PersistenceException.class)
  210 + public void produceNamedInexistentEntityManagerFactory() {
  211 + EntityManagerFactory emf = Beans.getReference(EntityManagerFactory.class, new NameQualifier("pu3"));
  212 + }
  213 +
  214 + private void loadListBegin(EntityManager em, Object entityOne, Object entityTwo,
  215 + String jpqlOne, String jpqlTwo, String jpqlAll) {
  216 + em.getTransaction().begin();
  217 +
  218 + em.persist(entityOne);
  219 + em.persist(entityTwo);
  220 +
  221 + queryOne = em.createQuery(jpqlOne).setParameter("descriptionOne", descriptionOne);
  222 + queryTwo = em.createQuery(jpqlTwo).setParameter("descriptionTwo", descriptionTwo);
  223 + queryAll = em.createQuery(jpqlAll);
  224 +
  225 + listOne = queryOne.getResultList();
  226 + listTwo = queryTwo.getResultList();
  227 + listAll = queryAll.getResultList();
  228 + }
  229 +
  230 + private void loadListUpdate(EntityManager em, DummyEntityDefault entityOne, DummyEntityDefault entityTwo) {
  231 + entityOne.setDescription("Entity for test one with description modified.");
  232 + em.merge(entityOne);
  233 + entityTwo.setDescription("Entity for test two with description modified.");
  234 + em.merge(entityTwo);
  235 +
  236 + listOne = queryOne.getResultList();
  237 + listTwo = queryTwo.getResultList();
  238 + listAll = queryAll.getResultList();
  239 + }
  240 +
  241 + private void loadListUpdate(EntityManager em, DummyEntityNamed entityOne, DummyEntityNamed entityTwo) {
  242 + entityOne.setDescription("Entity for test one with description modified.");
  243 + em.merge(entityOne);
  244 + entityTwo.setDescription("Entity for test two with description modified.");
  245 + em.merge(entityTwo);
  246 +
  247 + listOne = queryOne.getResultList();
  248 + listTwo = queryTwo.getResultList();
  249 + listAll = queryAll.getResultList();
  250 + }
  251 +
  252 + private void removeAndLoadList(EntityManager em, Object entityOne, boolean commit) {
  253 + em.remove(entityOne);
  254 + listAll = queryAll.getResultList();
  255 + if(commit) {
  256 + em.getTransaction().commit();
  257 + }
  258 + }
  259 +
  260 +}
... ...