r2126 - trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence
Author: echatellier Date: 2010-10-05 10:42:08 +0200 (Tue, 05 Oct 2010) New Revision: 2126 Url: http://nuiton.org/repositories/revision/topia/2126 Log: Add test about findAll() to find entities created during non commited transaction Added: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java Added: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java =================================================================== --- trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java (rev 0) +++ trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java 2010-10-05 08:42:08 UTC (rev 2126) @@ -0,0 +1,114 @@ +/* + * #%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.io.IOException; +import java.net.URL; +import java.util.List; +import java.util.Properties; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assert; +import org.junit.Test; +import org.junit.matchers.JUnitMatchers; +import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.TopiaContextFactory; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.TopiaTestDAOHelper; +import org.nuiton.topia.framework.TopiaUtilTest; +import org.nuiton.topia.test.entities.Person; +import org.nuiton.topia.test.entities.PersonDAO; +import org.nuiton.util.Resource; + +/** + * Test on {@link TopiaDAO}. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class TopiaDAOTest { + + private static final Log log = LogFactory.getLog(TopiaUtilTest.class); + + protected TopiaContext getRootContext() throws TopiaException, IOException { + Properties conf = new Properties(); + URL url = Resource.getURL("TopiaContextImpl.properties"); + if (log.isDebugEnabled()) { + log.debug(url); + } + conf.load(url.openStream()); + conf.setProperty("hibernate.connection.url", "jdbc:h2:file://tmp/topiautiltest_" + System.currentTimeMillis()); + conf.setProperty("topia.persistence.classes", TopiaTestDAOHelper.getImplementationClassesAsString()); + + TopiaContext rootContext = (TopiaContext) TopiaContextFactory.getContext(conf); + return rootContext; + } + + /** + * Test de creer une entité et de verifier qu'elle est + * présente dans la persistence au sein de la transaction. + * + * @throws TopiaException + * @throws IOException + */ + @Test + public void testCreateAndFindInTransaction() throws TopiaException, IOException { + + TopiaContext rootContext = getRootContext(); + TopiaContext context = rootContext.beginTransaction(); + + PersonDAO personDAO = TopiaTestDAOHelper.getPersonDAO(context); + + // appel 1 find all + Person person = personDAO.create(); + person.setName("toto"); + List<Person> allPerson = personDAO.findAll(); + Assert.assertEquals(1, allPerson.size()); + context.commitTransaction(); + + // recherce la personne créée dans la même transaction + Person person2 = personDAO.create(); + person2.setName("titi"); + Assert.assertEquals(2, allPerson.size()); + Assert.assertThat(allPerson, JUnitMatchers.hasItem(person2)); + + context.rollbackTransaction(); + + // meme test apres roolback + Person person3 = personDAO.create(); + person3.setName("tata"); + allPerson = personDAO.findAll(); + Assert.assertEquals(2, allPerson.size()); + Assert.assertThat(allPerson, JUnitMatchers.hasItem(person3)); + + context.commitTransaction(); + rootContext.closeContext(); + } +} Property changes on: trunk/topia-persistence/src/test/java/org/nuiton/topia/persistence/TopiaDAOTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL
participants (1)
-
echatellier@users.nuiton.org