Author: tchemit Date: 2013-12-20 17:27:35 +0100 (Fri, 20 Dec 2013) New Revision: 2956 Url: http://nuiton.org/projects/topia/repository/revisions/2956 Log: refs #2893: Refactor packages (third pass) Added: trunk/topia-it/src/test/java/org/nuiton/topia/persistence/internal/ trunk/topia-it/src/test/java/org/nuiton/topia/persistence/internal/TopiaDaoTest.java Removed: trunk/topia-it/src/test/java/org/nuiton/topia/persistence/TopiaDaoTest.java Deleted: trunk/topia-it/src/test/java/org/nuiton/topia/persistence/TopiaDaoTest.java =================================================================== --- trunk/topia-it/src/test/java/org/nuiton/topia/persistence/TopiaDaoTest.java 2013-12-20 16:19:35 UTC (rev 2955) +++ trunk/topia-it/src/test/java/org/nuiton/topia/persistence/TopiaDaoTest.java 2013-12-20 16:27:35 UTC (rev 2956) @@ -1,204 +0,0 @@ -/* - * #%L - * ToPIA :: Persistence - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2004 - 2010 CodeLutin, Chatellier Eric - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -package org.nuiton.topia.persistence; - -import java.util.List; -import java.util.Map; - -import org.hamcrest.CoreMatchers; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.nuiton.topia.it.legacy.TopiaDatabase; -import org.nuiton.topia.it.legacy.TopiaTestTopiaPersistenceContext; -import org.nuiton.topia.it.legacy.test.entities.Person; -import org.nuiton.topia.it.legacy.test.entities.PersonTopiaDao; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; - -/** - * Test on {@link TopiaDao}. - * <p/> - * Last update : $Date$ - * By : $Author$ - * - * @author chatellier - * @version $Revision$ - */ -public class TopiaDaoTest { // Should not be located in topia-it module - - @Rule - public final TopiaDatabase db = new TopiaDatabase(); - - protected TopiaTestTopiaPersistenceContext context; - - protected PersonTopiaDao dao; - - @Before - public void setup() throws TopiaException { - - context = db.beginTransaction(); - dao = context.getPersonDao(); - } - - /** - * Test de creer une entité et de verifier qu'elle est - * présente dans la persistence au sein de la transaction. - * - * @throws Exception if any exception while test - */ - @Test - public void testCreateAndFindInTransaction() throws Exception { - - // appel 1 find all - createPerson("toto"); - List<Person> allPerson = dao.findAll(); - Assert.assertEquals(1, allPerson.size()); - context.commit(); - - // recherce la personne créée dans la même transaction - Person person2 = createPerson("titi"); - allPerson = dao.findAll(); - Assert.assertEquals(2, allPerson.size()); - Assert.assertThat(allPerson, CoreMatchers.hasItem(person2)); - - context.rollback(); - - // meme test apres roolback - Person person3 = createPerson("tata"); - allPerson = dao.findAll(); - Assert.assertEquals(2, allPerson.size()); - Assert.assertThat(allPerson, CoreMatchers.hasItem(person3)); - - context.commit(); - } - - @Test - public void findAllLazyByQuery() throws TopiaException { - - Assert.assertEquals(dao.count(), 0); - - createPersons(101); - - Map<String, Object> emptyArgs = Maps.newHashMap(); - - dao.setBatchSize(100); - Iterable<Person> allByLazy = dao.findAllLazy( - "FROM " + dao.getTopiaEntityEnum().getImplementationFQN() + " ORDER BY id", emptyArgs); - - List<Person> actual = Lists.newArrayList(); - - for (Person person : allByLazy) { - actual.add(person); - } - Assert.assertEquals(dao.count(), actual.size()); - - dao.setBatchSize(54); - allByLazy = dao.findAllLazy( - "FROM " + dao.getTopiaEntityEnum().getImplementationFQN() + " ORDER BY id", emptyArgs); - - actual = Lists.newArrayList(); - - for (Person person : allByLazy) { - actual.add(person); - } - Assert.assertEquals(dao.count(), actual.size()); - - dao.setBatchSize(49); - allByLazy = dao.findAllLazy( - "FROM " + dao.getTopiaEntityEnum().getImplementationFQN() + " ORDER BY id", emptyArgs); - - actual = Lists.newArrayList(); - - for (Person person : allByLazy) { - actual.add(person); - } - Assert.assertEquals(dao.count(), actual.size()); - - dao.setBatchSize(101); - allByLazy = dao.findAllLazy( - "FROM " + dao.getTopiaEntityEnum().getImplementationFQN() + " ORDER BY id", emptyArgs); - - actual = Lists.newArrayList(); - - for (Person person : allByLazy) { - actual.add(person); - } - Assert.assertEquals(dao.count(), actual.size()); - - dao.setBatchSize(102); - allByLazy = dao.findAllLazy( - "FROM " + dao.getTopiaEntityEnum().getImplementationFQN() + " ORDER BY id", emptyArgs); - - actual = Lists.newArrayList(); - - for (Person person : allByLazy) { - actual.add(person); - } - Assert.assertEquals(dao.count(), actual.size()); - } - - @Test - public void iterateOnTopiaDAO() throws TopiaException { - - createPersons(1999); - - List<Person> excepted = dao.findAll(); - - List<Person> actual = Lists.newArrayList(); - - for (Person person : dao) { - Assert.assertThat(excepted, CoreMatchers.hasItem(person)); - actual.add(person); - } - Assert.assertEquals(excepted.size(), actual.size()); - - dao.setBatchSize(54); - - actual = Lists.newArrayList(); - - for (Person person : dao) { - Assert.assertThat(excepted, CoreMatchers.hasItem(person)); - actual.add(person); - } - Assert.assertEquals(excepted.size(), actual.size()); - - } - - protected void createPersons(int number) throws TopiaException { - for (int i = 0; i < number; i++) { - createPerson("toto" + i); - } - - context.commit(); - } - - protected Person createPerson(String name) throws TopiaException { - return dao.create(Person.PROPERTY_NAME, name); - } -} Copied: trunk/topia-it/src/test/java/org/nuiton/topia/persistence/internal/TopiaDaoTest.java (from rev 2954, trunk/topia-it/src/test/java/org/nuiton/topia/persistence/TopiaDaoTest.java) =================================================================== --- trunk/topia-it/src/test/java/org/nuiton/topia/persistence/internal/TopiaDaoTest.java (rev 0) +++ trunk/topia-it/src/test/java/org/nuiton/topia/persistence/internal/TopiaDaoTest.java 2013-12-20 16:27:35 UTC (rev 2956) @@ -0,0 +1,206 @@ +/* + * #%L + * ToPIA :: Persistence + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2004 - 2010 CodeLutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.topia.persistence.internal; + +import java.util.List; +import java.util.Map; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.nuiton.topia.it.legacy.TopiaDatabase; +import org.nuiton.topia.it.legacy.TopiaTestTopiaPersistenceContext; +import org.nuiton.topia.it.legacy.test.entities.Person; +import org.nuiton.topia.it.legacy.test.entities.PersonTopiaDao; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import org.nuiton.topia.persistence.TopiaDao; +import org.nuiton.topia.persistence.TopiaException; + +/** + * Test on {@link TopiaDao}. + * <p/> + * Last update : $Date$ + * By : $Author$ + * + * @author chatellier + * @version $Revision$ + */ +public class TopiaDaoTest { // Should not be located in topia-it module + + @Rule + public final TopiaDatabase db = new TopiaDatabase(); + + protected TopiaTestTopiaPersistenceContext context; + + protected PersonTopiaDao dao; + + @Before + public void setup() throws TopiaException { + + context = db.beginTransaction(); + dao = context.getPersonDao(); + } + + /** + * Test de creer une entité et de verifier qu'elle est + * présente dans la persistence au sein de la transaction. + * + * @throws Exception if any exception while test + */ + @Test + public void testCreateAndFindInTransaction() throws Exception { + + // appel 1 find all + createPerson("toto"); + List<Person> allPerson = dao.findAll(); + Assert.assertEquals(1, allPerson.size()); + context.commit(); + + // recherce la personne créée dans la même transaction + Person person2 = createPerson("titi"); + allPerson = dao.findAll(); + Assert.assertEquals(2, allPerson.size()); + Assert.assertThat(allPerson, CoreMatchers.hasItem(person2)); + + context.rollback(); + + // meme test apres roolback + Person person3 = createPerson("tata"); + allPerson = dao.findAll(); + Assert.assertEquals(2, allPerson.size()); + Assert.assertThat(allPerson, CoreMatchers.hasItem(person3)); + + context.commit(); + } + + @Test + public void findAllLazyByQuery() throws TopiaException { + + Assert.assertEquals(dao.count(), 0); + + createPersons(101); + + Map<String, Object> emptyArgs = Maps.newHashMap(); + + dao.setBatchSize(100); + Iterable<Person> allByLazy = dao.findAllLazy( + "FROM " + dao.getTopiaEntityEnum().getImplementationFQN() + " ORDER BY id", emptyArgs); + + List<Person> actual = Lists.newArrayList(); + + for (Person person : allByLazy) { + actual.add(person); + } + Assert.assertEquals(dao.count(), actual.size()); + + dao.setBatchSize(54); + allByLazy = dao.findAllLazy( + "FROM " + dao.getTopiaEntityEnum().getImplementationFQN() + " ORDER BY id", emptyArgs); + + actual = Lists.newArrayList(); + + for (Person person : allByLazy) { + actual.add(person); + } + Assert.assertEquals(dao.count(), actual.size()); + + dao.setBatchSize(49); + allByLazy = dao.findAllLazy( + "FROM " + dao.getTopiaEntityEnum().getImplementationFQN() + " ORDER BY id", emptyArgs); + + actual = Lists.newArrayList(); + + for (Person person : allByLazy) { + actual.add(person); + } + Assert.assertEquals(dao.count(), actual.size()); + + dao.setBatchSize(101); + allByLazy = dao.findAllLazy( + "FROM " + dao.getTopiaEntityEnum().getImplementationFQN() + " ORDER BY id", emptyArgs); + + actual = Lists.newArrayList(); + + for (Person person : allByLazy) { + actual.add(person); + } + Assert.assertEquals(dao.count(), actual.size()); + + dao.setBatchSize(102); + allByLazy = dao.findAllLazy( + "FROM " + dao.getTopiaEntityEnum().getImplementationFQN() + " ORDER BY id", emptyArgs); + + actual = Lists.newArrayList(); + + for (Person person : allByLazy) { + actual.add(person); + } + Assert.assertEquals(dao.count(), actual.size()); + } + + @Test + public void iterateOnTopiaDAO() throws TopiaException { + + createPersons(1999); + + List<Person> excepted = dao.findAll(); + + List<Person> actual = Lists.newArrayList(); + + for (Person person : dao) { + Assert.assertThat(excepted, CoreMatchers.hasItem(person)); + actual.add(person); + } + Assert.assertEquals(excepted.size(), actual.size()); + + dao.setBatchSize(54); + + actual = Lists.newArrayList(); + + for (Person person : dao) { + Assert.assertThat(excepted, CoreMatchers.hasItem(person)); + actual.add(person); + } + Assert.assertEquals(excepted.size(), actual.size()); + + } + + protected void createPersons(int number) throws TopiaException { + for (int i = 0; i < number; i++) { + createPerson("toto" + i); + } + + context.commit(); + } + + protected Person createPerson(String name) throws TopiaException { + return dao.create(Person.PROPERTY_NAME, name); + } +}