Commit 1af13b2b9e2e9ec54cc41b1495e3f309c2c20a37

Authored by Emerson Oliveira
1 parent 45fd0574
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 de injeção do EntityManagerFactory com nome do persistence unit
sendo lido do arquivo persistence.xml
impl/extension/jpa/src/test/java/producer/entitymanagerfactory/uniquepu/DummyEntity.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.uniquepu;
  38 +
  39 +import javax.persistence.Entity;
  40 +import javax.persistence.Id;
  41 +
  42 +@Entity
  43 +public class DummyEntity {
  44 +
  45 + @Id
  46 + private Long id;
  47 +
  48 + private String description;
  49 +
  50 + public DummyEntity() {
  51 + super();
  52 + }
  53 +
  54 + public DummyEntity(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/uniquepu/EntityManagerFactoryProducerUniquePersistenceUnitTest.java 0 → 100644
... ... @@ -0,0 +1,165 @@
  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.uniquepu;
  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.Query;
  47 +
  48 +import org.jboss.arquillian.container.test.api.Deployment;
  49 +import org.jboss.arquillian.junit.Arquillian;
  50 +import org.jboss.shrinkwrap.api.spec.WebArchive;
  51 +import org.junit.Test;
  52 +import org.junit.runner.RunWith;
  53 +
  54 +import javax.persistence.PersistenceException;
  55 +
  56 +import br.gov.frameworkdemoiselle.util.Beans;
  57 +import br.gov.frameworkdemoiselle.util.NameQualifier;
  58 +import test.Tests;
  59 +
  60 +@RunWith(Arquillian.class)
  61 +public class EntityManagerFactoryProducerUniquePersistenceUnitTest {
  62 +
  63 + private static final String PATH = "src/test/resources/producer/entitymanagerfactory/unique-pu";
  64 +
  65 + private final String descriptionOne = "Entity for Test One.";
  66 + private final String descriptionTwo = "Entity for Test Two.";
  67 +
  68 + private List<?> listOne;
  69 + private List<?> listTwo;
  70 + private List<?> listAll;
  71 +
  72 + private Query queryOne;
  73 + private Query queryTwo;
  74 + private Query queryAll;
  75 +
  76 + @Deployment
  77 + public static WebArchive createDeployment() {
  78 + WebArchive deployment = Tests.createDeployment(EntityManagerFactoryProducerUniquePersistenceUnitTest.class);
  79 + deployment.addAsResource(Tests.createFileAsset(PATH + "/persistence.xml"), "META-INF/persistence.xml");
  80 + return deployment;
  81 + }
  82 +
  83 + @Test
  84 + public void produceDefaultEntityManagerFactory() {
  85 + EntityManagerFactory emf = Beans.getReference(EntityManagerFactory.class);
  86 + assertNotNull(emf);
  87 + assertTrue(emf.getMetamodel().getEntities().toString().contains(DummyEntity.class.getSimpleName()));
  88 +
  89 + EntityManager em = emf.createEntityManager();
  90 +
  91 + DummyEntity entityOne = new DummyEntity(descriptionOne, 1L);
  92 + DummyEntity entityTwo = new DummyEntity(descriptionTwo, 2L);
  93 +
  94 + String jpqlOne = "select ded from DummyEntity ded where ded.description like :descriptionOne";
  95 + String jpqlTwo = "select ded from DummyEntity ded where ded.description like :descriptionTwo";
  96 + String jpqlAll = "select ded from DummyEntity as ded";
  97 +
  98 + loadListBegin(em, entityOne, entityTwo, jpqlOne, jpqlTwo, jpqlAll);
  99 + assertTrue(listOne.size() == 1);
  100 + assertTrue(((DummyEntity) listOne.get(0)).getId() == 1L);
  101 + assertTrue(((DummyEntity) listOne.get(0)).getDescription().equals(descriptionOne));
  102 +
  103 + assertTrue(listTwo.size() == 1);
  104 + assertTrue(((DummyEntity) listTwo.get(0)).getId() == 2L);
  105 + assertTrue(((DummyEntity) listTwo.get(0)).getDescription().equals(descriptionTwo));
  106 +
  107 + assertTrue(listAll.size() == 2);
  108 +
  109 + loadListUpdate(em, entityOne, entityTwo);
  110 + assertTrue(listOne.size() == 0);
  111 + assertTrue(listTwo.size() == 0);
  112 + assertTrue(listAll.size() == 2);
  113 +
  114 + DummyEntity ded = em.find(DummyEntity.class, 1L);
  115 + assertTrue(ded.getDescription().equals("Entity for test one with description modified."));
  116 +
  117 + removeAndLoadList(em, entityOne, false);
  118 + assertTrue(listAll.size() == 1);
  119 +
  120 + removeAndLoadList(em, entityTwo, true);
  121 + assertTrue(listAll.size() == 0);
  122 +
  123 + }
  124 +
  125 + private void loadListBegin(EntityManager em, Object entityOne, Object entityTwo,
  126 + String jpqlOne, String jpqlTwo, String jpqlAll) {
  127 + em.getTransaction().begin();
  128 +
  129 + em.persist(entityOne);
  130 + em.persist(entityTwo);
  131 +
  132 + queryOne = em.createQuery(jpqlOne).setParameter("descriptionOne", descriptionOne);
  133 + queryTwo = em.createQuery(jpqlTwo).setParameter("descriptionTwo", descriptionTwo);
  134 + queryAll = em.createQuery(jpqlAll);
  135 +
  136 + listOne = queryOne.getResultList();
  137 + listTwo = queryTwo.getResultList();
  138 + listAll = queryAll.getResultList();
  139 + }
  140 +
  141 + private void loadListUpdate(EntityManager em, DummyEntity entityOne, DummyEntity entityTwo) {
  142 + entityOne.setDescription("Entity for test one with description modified.");
  143 + em.merge(entityOne);
  144 + entityTwo.setDescription("Entity for test two with description modified.");
  145 + em.merge(entityTwo);
  146 +
  147 + listOne = queryOne.getResultList();
  148 + listTwo = queryTwo.getResultList();
  149 + listAll = queryAll.getResultList();
  150 + }
  151 +
  152 + private void removeAndLoadList(EntityManager em, Object entityOne, boolean commit) {
  153 + em.remove(entityOne);
  154 + listAll = queryAll.getResultList();
  155 + if(commit) {
  156 + em.getTransaction().commit();
  157 + }
  158 + }
  159 +
  160 + @SuppressWarnings("unused")
  161 + @Test(expected=PersistenceException.class)
  162 + public void produceNamedInexistentEntityManagerFactory() {
  163 + EntityManagerFactory emf = Beans.getReference(EntityManagerFactory.class, new NameQualifier("pu2"));
  164 + }
  165 +}
... ...