Java tutorial
/* * Copyright (C) 2012 Mathieu Cambillau <cambillaum@gmail.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU 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 Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.cambillaum.jpapersistor.persistence.dao; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaQuery; import org.cambillaum.jpapersistor.persistence.configuration.PersistenceConfiguration; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.transaction.annotation.Transactional; /** * * @author Mathieu Cambillau <cambillaum@gmail.com> */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = PersistenceConfiguration.class, loader = AnnotationConfigContextLoader.class) public class PersonDAOTest { private Person person; @Autowired private EntityManager em; @Autowired private PersonDAO personDAO; @Before public void setUp() { this.person = new Person("someBody"); } @Test @Transactional public void testSave() { Person savedPerson = this.personDAO.save(this.person); this.em.clear(); Person retrievedPerson = this.em.find(Person.class, savedPerson.getId()); assertPerson(this.person, retrievedPerson); } @Test @Transactional public void testUpdate() { Person savedPerson = this.em.merge(this.person); this.em.flush(); this.person.setName("newName"); savedPerson.setName(this.person.getName()); this.personDAO.update(savedPerson); this.em.flush(); this.em.clear(); Person retrievedPerson = this.em.find(Person.class, savedPerson.getId()); assertPerson(this.person, retrievedPerson); } @Test @Transactional public void testDelete() { Person savedPerson = this.em.merge(this.person); this.em.flush(); this.personDAO.delete(savedPerson); this.em.flush(); this.em.clear(); Person retrievedPerson = this.em.find(Person.class, savedPerson.getId()); assertNull(retrievedPerson); } @Test @Transactional public void testFindById() { Person savedPerson = this.em.merge(this.person); this.em.flush(); this.em.clear(); Person retrievedPerson = this.personDAO.findById(savedPerson.getId()); assertPerson(this.person, retrievedPerson); } @Test @Transactional public void testFindAll() { this.em.merge(this.person); this.em.flush(); this.em.clear(); List<Person> retrievedPersons = this.personDAO.findAll(); assertEquals(1, retrievedPersons.size()); Person retrievedPerson = retrievedPersons.get(0); assertPerson(this.person, retrievedPerson); } @Test @Transactional public void testFindOneWithTypedQuery() { this.em.persist(this.person); this.em.flush(); this.em.clear(); TypedQuery<Person> query = this.em.createQuery("Select p from " + Person.class.getName() + " p", Person.class); Person retrievedPerson = this.personDAO.findOne(query); assertPerson(this.person, retrievedPerson); } @Test @Transactional public void testFindOneWithCriteriaQuery() { this.em.persist(this.person); this.em.flush(); this.em.clear(); CriteriaQuery<Person> criteriaQuery = this.em.getCriteriaBuilder().createQuery(Person.class); criteriaQuery.from(Person.class); Person retrievedPerson = this.personDAO.findOne(criteriaQuery); assertPerson(this.person, retrievedPerson); } @Test @Transactional public void testFindManyWithTypedQuery() { this.em.persist(this.person); this.em.flush(); this.em.clear(); TypedQuery<Person> query = this.em.createQuery("Select p from " + Person.class.getName() + " p", Person.class); List<Person> retrievedPersons = this.personDAO.findMany(query); assertEquals(1, retrievedPersons.size()); Person retrievedPerson = retrievedPersons.get(0); assertPerson(this.person, retrievedPerson); } @Test @Transactional public void testFindManyWithCriteriaQuery() { this.em.persist(this.person); this.em.flush(); this.em.clear(); CriteriaQuery<Person> criteriaQuery = this.em.getCriteriaBuilder().createQuery(Person.class); criteriaQuery.from(Person.class); List<Person> retrievedPersons = this.personDAO.findMany(criteriaQuery); assertEquals(1, retrievedPersons.size()); Person retrievedPerson = retrievedPersons.get(0); assertPerson(this.person, retrievedPerson); } private void assertPerson(Person expected, Person actual) { assertEquals(expected.getName(), actual.getName()); } }