View Javadoc

1   /*
2    * #%L
3    * ToPIA :: Persistence
4    * 
5    * $Id: DeleteEntityTest.java 2416 2012-03-02 10:08:59Z tchemit $
6    * $HeadURL: http://svn.nuiton.org/svn/topia/tags/topia-2.8-rc-1/topia-persistence/src/test/java/org/nuiton/topiatest/deletetest/DeleteEntityTest.java $
7    * %%
8    * Copyright (C) 2004 - 2010 CodeLutin
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23   * #L%
24   */
25  
26  
27  /**
28   * DeleteEntityTest.java
29   *
30   * Created: 4 juin 2009
31   *
32   * @author Florian Desbois <fdesbois@codelutin.com>
33   * @version $Revision: 2416 $
34   *
35   * Mise a jour: $Date: 2012-03-02 11:08:59 +0100 (Fri, 02 Mar 2012) $
36   * par : $Author: tchemit $
37   */
38  
39  package org.nuiton.topiatest.deletetest;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  import org.junit.Rule;
44  import org.junit.Test;
45  import org.nuiton.topia.TopiaContext;
46  import org.nuiton.topia.TopiaDatabase;
47  import org.nuiton.topia.TopiaException;
48  import org.nuiton.topia.TopiaTestDAOHelper;
49  import org.nuiton.topia.generator.DAOAbstractTransformer;
50  import org.nuiton.topiatest.Personne;
51  import org.nuiton.topiatest.PersonneDAO;
52  
53  import static org.junit.Assert.assertEquals;
54  import static org.junit.Assert.assertNotNull;
55  import static org.junit.Assert.assertNull;
56  
57  /**
58   * Deleting tests with DAO and Entities generated with ToPIA (diagram
59   * delete-test in topiatest.zargo). Different case of deleting, with inheritance
60   * or NMultiplicity relationship between two entities, or both. Initiate from an
61   * issue with DAOAbstractGenerator delete method generation. Tests with H2
62   * Database. Configuration in src/test/resources/TopiaContextImpl.properties
63   */
64  public class DeleteEntityTest {
65  
66      private static final Log log = LogFactory.getLog(DeleteEntityTest.class);
67  
68      @Rule
69      public final TopiaDatabase db = new TopiaDatabase();
70  
71      /**
72       * Test for deleting entities with inheritance. Delete from the DAO linked
73       * with the subclass entity and from the DAO linked with the superclass
74       * entity. In the test model, the two entities have NMultiplicity
75       * relationship without association class entity.
76       *
77       * @throws TopiaException if any exception while manipulating db
78       */
79      @Test
80      public void testDeleteEntityWithInheritance() throws TopiaException {
81          log.debug("START TEST : testDeleteEntityWithInheritance");
82  
83          TopiaContext transaction = db.beginTransaction();
84  
85          log.debug("DAO : PersonneDAO");
86          PersonneDAO dao = TopiaTestDAOHelper.getPersonneDAO(transaction);
87  
88          log.debug("CREATE PERSONNE : Bob Marley");
89          Personne personne = dao.create(Personne.PROPERTY_NAME, "Bob Marley");
90          transaction.commitTransaction();
91          String idPersonne = personne.getTopiaId();
92          assertNotNull(idPersonne);
93          log.debug("ENTITY PERSONNE SAVED !");
94  
95          log.debug("DELETE PERSONNE");
96          dao.delete(personne);
97          transaction.commitTransaction();
98          Personne res = dao.findByTopiaId(idPersonne);
99          assertNull(res);
100         log.debug("ENTITY PERSONNE DELETED !");
101 
102         log.debug("CREATE PERSONNE : Ziggy Marley");
103         Personne personne2 = dao.create(Personne.PROPERTY_NAME, "Ziggy Marley");
104         transaction.commitTransaction();
105         String idPersonne2 = personne2.getTopiaId();
106         assertNotNull(idPersonne2);
107         log.debug("ENTITY PERSONNE SAVED !");
108 
109         log.debug("DAO parent (abstract) : PartyDAO");
110         Party2DAO dao2 = TopiaTestDAOHelper.getParty2DAO(transaction);
111 
112         log.debug("DELETE PERSONNE with PartyDAO");
113         dao2.delete(personne2);
114         transaction.commitTransaction();
115         Party2 res2 = dao2.findByTopiaId(idPersonne2);
116         assertNull(res2);
117         log.debug("ENTITY PERSONNE DELETED !");
118 
119 
120     }
121 
122     /**
123      * Test for deleting entities with NMultiplicity relation without
124      * association class entity. Test DAO generation for deleting references
125      * between two entities with NMultiplicity relation. In the test model, the
126      * two entities have both inheritance.
127      *
128      * @throws TopiaException if any exception while manipulating db
129      * @see DAOAbstractTransformer
130      */
131     @Test
132     public void testDeleteEntityWithManyToManyRelation() throws TopiaException {
133         log.debug("START TEST : testDeleteEntityWithManyToManyRelation");
134 
135         TopiaContext transaction = db.beginTransaction();
136 
137         PersonneDAO dao = TopiaTestDAOHelper.getPersonneDAO(transaction);
138 
139         log.debug("CREATE PERSONNE : Bob Marley");
140         Personne personne = dao.create(Personne.PROPERTY_NAME, "Bob Marley");
141         transaction.commitTransaction();
142         String idPersonne = personne.getTopiaId();
143         assertNotNull(idPersonne);
144         log.debug("ENTITY PERSONNE SAVED !");
145 
146         Contact2DAO contactDAO = TopiaTestDAOHelper.getContact2DAO(transaction);
147 
148         log.debug("CREATE CONTACT : jaja@codelutin.com");
149         Contact2 contact = contactDAO.create(Contact2.PROPERTY_CONTACT_VALUE, "jaja@codelutin.com");
150         transaction.commitTransaction();
151         String idContact = contact.getTopiaId();
152         assertNotNull(idContact);
153         log.debug("ENTITY CONTACT SAVED !");
154 
155         log.debug("ADD CONTACT TO PERSONNE");
156         personne.addContacts(contact);
157         transaction.commitTransaction();
158         assertEquals(1, personne.getContacts().size());
159         log.debug("CONTACT ADDED !");
160 
161         log.debug("DELETE PERSONNE");
162         dao.delete(personne);
163         transaction.commitTransaction();
164         Personne res = dao.findByTopiaId(idPersonne);
165         assertNull(res);
166         log.debug("ENTITY PERSONNE DELETED !");
167 
168         assertEquals(0, contact.getParty2().size());
169 
170         log.debug("DELETE CONTACT");
171         contactDAO.delete(contact);
172         transaction.commitTransaction();
173         Contact2 res2 = contactDAO.findByTopiaId(idContact);
174         assertNull(res2);
175         log.debug("ENTITY PERSONNE DELETED !");
176 
177     }
178 
179 }