Commit b4c571340c3963dc40a8e149021e0bee4bf0a228
1 parent
6b40f61d
Exists in
master
Implementação de testes para verificação do DelegateCrud.
Showing
4 changed files
with
398 additions
and
0 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,156 @@ |
| 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 template; | |
| 38 | + | |
| 39 | +import static junit.framework.Assert.*; | |
| 40 | + | |
| 41 | +import java.util.List; | |
| 42 | + | |
| 43 | +import javax.enterprise.context.RequestScoped; | |
| 44 | +import javax.inject.Inject; | |
| 45 | + | |
| 46 | +import org.jboss.arquillian.container.test.api.Deployment; | |
| 47 | +import org.jboss.arquillian.junit.Arquillian; | |
| 48 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; | |
| 49 | +import org.junit.After; | |
| 50 | +import org.junit.Before; | |
| 51 | +import org.junit.Test; | |
| 52 | +import org.junit.runner.RunWith; | |
| 53 | + | |
| 54 | +import template.business.TemplateBC; | |
| 55 | +import template.crud.CrudImpl; | |
| 56 | +import template.model.DummyEntity; | |
| 57 | +import test.Tests; | |
| 58 | +import br.gov.frameworkdemoiselle.internal.context.ContextManager; | |
| 59 | +import br.gov.frameworkdemoiselle.internal.context.ManagedContext; | |
| 60 | + | |
| 61 | +@RunWith(Arquillian.class) | |
| 62 | +public class TemplateTest { | |
| 63 | + | |
| 64 | + private static final String DUMMY_NAME = "Dummy De Meu Deus"; | |
| 65 | + private static final Long INSERT_ID = 15L; | |
| 66 | + private static final Long VALID_ID = 1L; | |
| 67 | + | |
| 68 | + @Inject | |
| 69 | + private TemplateBC templateBC; | |
| 70 | + | |
| 71 | + @Inject | |
| 72 | + private CrudImpl crudImpl; | |
| 73 | + | |
| 74 | + @Deployment | |
| 75 | + public static JavaArchive createDeployment() { | |
| 76 | + | |
| 77 | + JavaArchive deployment = Tests.createDeployment(TemplateTest.class); | |
| 78 | + | |
| 79 | + return deployment; | |
| 80 | + | |
| 81 | + } | |
| 82 | + | |
| 83 | + @Before | |
| 84 | + public void initialize() { | |
| 85 | + | |
| 86 | + ContextManager.activate(ManagedContext.class, RequestScoped.class); | |
| 87 | + | |
| 88 | + this.crudImpl.resetEntities(); | |
| 89 | + | |
| 90 | + } | |
| 91 | + | |
| 92 | + @After | |
| 93 | + public void finalize() { | |
| 94 | + | |
| 95 | + ContextManager.deactivate(ManagedContext.class, RequestScoped.class); | |
| 96 | + | |
| 97 | + } | |
| 98 | + | |
| 99 | + @Test | |
| 100 | + public void testInsert() { | |
| 101 | + | |
| 102 | + assertNull(this.crudImpl.load(INSERT_ID)); | |
| 103 | + | |
| 104 | + this.templateBC.insert(new DummyEntity(INSERT_ID, DUMMY_NAME)); | |
| 105 | + | |
| 106 | + assertNotNull(this.crudImpl.load(INSERT_ID)); | |
| 107 | + | |
| 108 | + } | |
| 109 | + | |
| 110 | + @Test | |
| 111 | + public void testRemove() { | |
| 112 | + | |
| 113 | + assertNotNull(this.crudImpl.load(VALID_ID)); | |
| 114 | + | |
| 115 | + this.templateBC.delete(VALID_ID); | |
| 116 | + | |
| 117 | + assertNull(this.crudImpl.load(VALID_ID)); | |
| 118 | + | |
| 119 | + } | |
| 120 | + | |
| 121 | + @Test | |
| 122 | + public void testFindAll() { | |
| 123 | + | |
| 124 | + List<DummyEntity> listImpl = this.crudImpl.findAll(); | |
| 125 | + List<DummyEntity> listDelegate = this.templateBC.findAll(); | |
| 126 | + | |
| 127 | + assertEquals(listImpl, listDelegate); | |
| 128 | + | |
| 129 | + } | |
| 130 | + | |
| 131 | + @Test | |
| 132 | + public void testLoad() { | |
| 133 | + | |
| 134 | + DummyEntity dummyEntityImpl = this.crudImpl.load(VALID_ID); | |
| 135 | + DummyEntity dummyEntityDelegate = this.templateBC.load(VALID_ID); | |
| 136 | + | |
| 137 | + assertEquals(dummyEntityImpl, dummyEntityDelegate); | |
| 138 | + | |
| 139 | + } | |
| 140 | + | |
| 141 | + @Test | |
| 142 | + public void testUpdate() { | |
| 143 | + | |
| 144 | + DummyEntity dummyEntity = this.crudImpl.load(VALID_ID); | |
| 145 | + | |
| 146 | + assertFalse(DUMMY_NAME.equals(dummyEntity.getName())); | |
| 147 | + | |
| 148 | + dummyEntity.setName(DUMMY_NAME); | |
| 149 | + | |
| 150 | + this.templateBC.update(dummyEntity); | |
| 151 | + | |
| 152 | + assertEquals(this.crudImpl.load(VALID_ID).getName(), DUMMY_NAME); | |
| 153 | + | |
| 154 | + } | |
| 155 | + | |
| 156 | +} | ... | ... |
impl/core/src/test/java/template/business/TemplateBC.java
0 → 100644
| ... | ... | @@ -0,0 +1,11 @@ |
| 1 | +package template.business; | |
| 2 | + | |
| 3 | +import template.crud.CrudImpl; | |
| 4 | +import template.model.DummyEntity; | |
| 5 | +import br.gov.frameworkdemoiselle.stereotype.BusinessController; | |
| 6 | +import br.gov.frameworkdemoiselle.template.DelegateCrud; | |
| 7 | + | |
| 8 | +@BusinessController | |
| 9 | +public class TemplateBC extends DelegateCrud<DummyEntity, Long, CrudImpl>{ | |
| 10 | + | |
| 11 | +} | ... | ... |
| ... | ... | @@ -0,0 +1,131 @@ |
| 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 template.crud; | |
| 38 | + | |
| 39 | +import java.util.ArrayList; | |
| 40 | +import java.util.List; | |
| 41 | + | |
| 42 | +import javax.enterprise.context.RequestScoped; | |
| 43 | + | |
| 44 | +import template.model.DummyEntity; | |
| 45 | +import br.gov.frameworkdemoiselle.DemoiselleException; | |
| 46 | +import br.gov.frameworkdemoiselle.template.Crud; | |
| 47 | + | |
| 48 | +@RequestScoped | |
| 49 | +public class CrudImpl implements Crud<DummyEntity, Long> { | |
| 50 | + | |
| 51 | + private List<DummyEntity> entities; | |
| 52 | + | |
| 53 | + public CrudImpl() { | |
| 54 | + | |
| 55 | + super(); | |
| 56 | + | |
| 57 | + this.entities = new ArrayList<DummyEntity>(); | |
| 58 | + | |
| 59 | + } | |
| 60 | + | |
| 61 | + public void resetEntities() { | |
| 62 | + | |
| 63 | + this.entities.clear(); | |
| 64 | + | |
| 65 | + this.entities.add(new DummyEntity(1L, "Dummy1 Label 1")); | |
| 66 | + this.entities.add(new DummyEntity(2L, "Dummy1 Label 2")); | |
| 67 | + this.entities.add(new DummyEntity(3L, "Dummy1 Label 3")); | |
| 68 | + this.entities.add(new DummyEntity(4L, "Dummy1 Label 4")); | |
| 69 | + this.entities.add(new DummyEntity(5L, "Dummy1 Label 5")); | |
| 70 | + this.entities.add(new DummyEntity(6L, "Dummy1 Label 6")); | |
| 71 | + this.entities.add(new DummyEntity(7L, "Dummy1 Label 7")); | |
| 72 | + this.entities.add(new DummyEntity(8L, "Dummy1 Label 8")); | |
| 73 | + this.entities.add(new DummyEntity(9L, "Dummy1 Label 9")); | |
| 74 | + this.entities.add(new DummyEntity(10L, "Dummy1 Label 10")); | |
| 75 | + | |
| 76 | + } | |
| 77 | + | |
| 78 | + @Override | |
| 79 | + public void delete(Long id) { | |
| 80 | + | |
| 81 | + this.entities.remove(this.load(id)); | |
| 82 | + | |
| 83 | + } | |
| 84 | + | |
| 85 | + @Override | |
| 86 | + public List<DummyEntity> findAll() { | |
| 87 | + | |
| 88 | + return this.entities; | |
| 89 | + | |
| 90 | + } | |
| 91 | + | |
| 92 | + @Override | |
| 93 | + public DummyEntity insert(DummyEntity bean) { | |
| 94 | + | |
| 95 | + if (this.entities.add(bean)) { | |
| 96 | + return bean; | |
| 97 | + } else { | |
| 98 | + throw new DemoiselleException("Erro ao inserir entity"); | |
| 99 | + } | |
| 100 | + | |
| 101 | + } | |
| 102 | + | |
| 103 | + @Override | |
| 104 | + public DummyEntity load(Long id) { | |
| 105 | + | |
| 106 | + for (DummyEntity dummyEntity : this.entities) { | |
| 107 | + | |
| 108 | + if (dummyEntity.getId().equals(id)) { | |
| 109 | + | |
| 110 | + return dummyEntity; | |
| 111 | + | |
| 112 | + } | |
| 113 | + | |
| 114 | + } | |
| 115 | + | |
| 116 | + return null; | |
| 117 | + | |
| 118 | + } | |
| 119 | + | |
| 120 | + @Override | |
| 121 | + public DummyEntity update(DummyEntity bean) { | |
| 122 | + | |
| 123 | + DummyEntity dummyEntity = this.load(bean.getId()); | |
| 124 | + | |
| 125 | + dummyEntity.setName(bean.getName()); | |
| 126 | + | |
| 127 | + return dummyEntity; | |
| 128 | + | |
| 129 | + } | |
| 130 | + | |
| 131 | +} | ... | ... |
| ... | ... | @@ -0,0 +1,100 @@ |
| 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 template.model; | |
| 38 | + | |
| 39 | +public class DummyEntity { | |
| 40 | + | |
| 41 | + private Long id; | |
| 42 | + private String name; | |
| 43 | + | |
| 44 | + public DummyEntity(Long id, String name) { | |
| 45 | + | |
| 46 | + super(); | |
| 47 | + | |
| 48 | + this.id = id; | |
| 49 | + this.name = name; | |
| 50 | + | |
| 51 | + } | |
| 52 | + | |
| 53 | + public String getName() { | |
| 54 | + return name; | |
| 55 | + } | |
| 56 | + | |
| 57 | + public void setName(String name) { | |
| 58 | + this.name = name; | |
| 59 | + } | |
| 60 | + | |
| 61 | + public Long getId() { | |
| 62 | + return id; | |
| 63 | + } | |
| 64 | + | |
| 65 | + public void setId(Long id) { | |
| 66 | + this.id = id; | |
| 67 | + } | |
| 68 | + | |
| 69 | + @Override | |
| 70 | + public int hashCode() { | |
| 71 | + final int prime = 31; | |
| 72 | + int result = 1; | |
| 73 | + result = prime * result + ((id == null) ? 0 : id.hashCode()); | |
| 74 | + result = prime * result + ((name == null) ? 0 : name.hashCode()); | |
| 75 | + return result; | |
| 76 | + } | |
| 77 | + | |
| 78 | + @Override | |
| 79 | + public boolean equals(Object obj) { | |
| 80 | + if (this == obj) | |
| 81 | + return true; | |
| 82 | + if (obj == null) | |
| 83 | + return false; | |
| 84 | + if (getClass() != obj.getClass()) | |
| 85 | + return false; | |
| 86 | + DummyEntity other = (DummyEntity) obj; | |
| 87 | + if (id == null) { | |
| 88 | + if (other.id != null) | |
| 89 | + return false; | |
| 90 | + } else if (!id.equals(other.id)) | |
| 91 | + return false; | |
| 92 | + if (name == null) { | |
| 93 | + if (other.name != null) | |
| 94 | + return false; | |
| 95 | + } else if (!name.equals(other.name)) | |
| 96 | + return false; | |
| 97 | + return true; | |
| 98 | + } | |
| 99 | + | |
| 100 | +} | ... | ... |