View Javadoc
1   /*
2    * #%L
3    * ToPIA :: Service Replication
4    * $Id$
5    * $HeadURL$
6    * %%
7    * Copyright (C) 2004 - 2014 CodeLutin
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as 
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  package org.nuiton.topia.replication;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.junit.After;
30  import org.junit.AfterClass;
31  import org.junit.Before;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  import org.nuiton.topia.TestHelper;
35  import org.nuiton.topia.TopiaContext;
36  import org.nuiton.topia.TopiaContextFactory;
37  import org.nuiton.topia.TopiaException;
38  import org.nuiton.topia.TopiaTestDAOHelper.TopiaTestEntityEnum;
39  import org.nuiton.topia.framework.TopiaContextImplementor;
40  import org.nuiton.topia.persistence.TopiaEntity;
41  import org.nuiton.topia.persistence.TopiaEntityEnum;
42  import org.nuiton.topia.test.entities.Person;
43  import org.nuiton.topia.test.entities.PersonImpl;
44  import org.nuiton.topia.test.entities.Pet;
45  import org.nuiton.topia.test.entities.PetImpl;
46  import org.nuiton.topia.test.entities.Race;
47  import org.nuiton.topia.test.entities.RaceImpl;
48  
49  import java.io.File;
50  import java.io.IOException;
51  import java.util.Properties;
52  
53  /**
54   * TopiaReplicationServiceImplTest on model TopiaTest
55   * 
56   * Created: 07 jun. 09 17:14:22
57   *
58   * @author tchemit &lt;chemit@codelutin.com&gt;
59   * @since 2.2.0
60   */
61  public class TopiaReplicationServiceImplAllTest extends AbstractTopiaReplicationServiceTest {
62  
63      /** Logger */
64      private static final Log log = LogFactory.getLog(TopiaReplicationServiceImplTest.class);
65  
66      protected static final TopiaEntityEnum[] contracts = {
67              TopiaTestEntityEnum.Person,
68              TopiaTestEntityEnum.Pet,
69              TopiaTestEntityEnum.Race
70      };
71  
72      protected static final String entitiesList =
73              PersonImpl.class.getName() + "," +
74              PetImpl.class.getName() + "," +
75              RaceImpl.class.getName();
76  
77      static protected Person person, person2;
78  
79      static protected Pet pet, pet2, pet3;
80  
81      static protected Race race, race2, race3;
82  
83      protected static File tesDir;
84  
85      @BeforeClass
86      public static void beforeClass() throws IOException {
87          tesDir = TestHelper.getTestBasedir(TopiaReplicationServiceImplAllTest.class);
88      }
89      
90      @AfterClass
91      public static void after() throws Exception {
92          AbstractTopiaReplicationServiceTest.after();
93      }
94  
95      @Before
96      @Override
97      public void setUp() throws Exception {
98  
99          super.setUp();
100 
101         person = update(person);
102         person2 = update(person2);
103         pet = update(pet);
104         pet2 = update(pet2);
105         race = update(race);
106         race2 = update(race2);
107         race3 = update(race3);
108     }
109 
110     @After
111     @Override
112     public void tearDown() throws Exception {
113         super.tearDown();
114         if (dstCtxt != null && !dstCtxt.isClosed()) {
115             dstCtxt.closeContext();
116         }
117     }
118 
119 //    @Ignore
120 
121     @Test
122     @Override
123     public void testDetectTypes() throws Exception {
124 
125         detectTypes(race, Race.class);
126         detectTypes(pet, Pet.class, Person.class, Race.class);
127         detectTypes(person, Pet.class, Person.class, Race.class);
128 
129         detectTypes(pet2, Pet.class);
130         detectTypes(person2, Person.class);
131         detectTypes(race2, Race.class);
132 
133         detectTypes(race3, Race.class);
134         detectTypes(pet3, Pet.class, Race.class);
135     }
136 
137 //    @Ignore
138 
139     @Test
140     @Override
141     public void testGetOperation() throws Exception {
142     }
143 
144 //    @Ignore
145 
146     @Test
147     @Override
148     public void testDetectAssociations() throws Exception {
149 
150         detectAssociations(person, TopiaTestEntityEnum.Person, Person.PROPERTY_PET);
151         detectAssociations(race);
152         detectAssociations(pet);
153 
154         detectAssociations(person2);
155         detectAssociations(race2);
156         detectAssociations(pet2);
157 
158     }
159 
160 //    @Ignore
161 
162     @Test
163     @Override
164     public void testDetectDirectDependencies() throws Exception {
165 
166         detectDirectDependencies(person);
167         detectDirectDependencies(race);
168         detectDirectDependencies(pet, TopiaTestEntityEnum.Pet, Pet.PROPERTY_PERSON, TopiaTestEntityEnum.Pet, Pet.PROPERTY_RACE);
169 
170         detectDirectDependencies(person2);
171         detectDirectDependencies(race2);
172         detectDirectDependencies(pet2);
173     }
174 
175 //    @Ignore
176 
177     @Test
178     @Override
179     public void testDetectShell() throws Exception {
180 
181         detectShell(person, TopiaTestEntityEnum.Pet, TopiaTestEntityEnum.Race);
182         detectShell(race);
183         detectShell(pet, TopiaTestEntityEnum.Person, TopiaTestEntityEnum.Race);
184         detectShell(person2, TopiaTestEntityEnum.Pet, TopiaTestEntityEnum.Race);
185         detectShell(race2);
186         detectShell(pet2, TopiaTestEntityEnum.Person, TopiaTestEntityEnum.Race);
187     }
188 
189 //    @Ignore
190 
191     @Test
192     @Override
193     public void testDetectDependencies() throws Exception {
194 
195         detectDependencies(null,
196                            new TopiaTestEntityEnum[]{TopiaTestEntityEnum.Race}, new TopiaTestEntityEnum[]{TopiaTestEntityEnum.Person}, new TopiaTestEntityEnum[]{TopiaTestEntityEnum.Pet});
197     }
198 
199 //    @Ignore
200 
201     @Test
202     @Override
203     public void testDetectObjectsToDettach() throws Exception {
204 
205         detectObjectsToDettach(null, TopiaTestEntityEnum.Person, new String[]{Person.PROPERTY_PET});
206     }
207 
208 //    @Ignore
209 
210     @Test
211     @Override
212     public void testDetectOperations() throws Exception {
213 
214         detectOperations(null);
215     }
216 
217 //    @Ignore
218 
219     @Test
220     @Override
221     public void testDoReplicate() throws Exception {
222 
223         doReplicateAll();
224 
225     }
226 
227     @Override
228     protected TopiaContext createDb(String name) throws Exception {
229 
230 //        File localDB = new File(getTestDir(getClass()), "db_" + name);
231 
232         Properties config = getH2Properties(name);
233 
234         context = TopiaContextFactory.getContext(config);
235 
236         TopiaContextImplementor tx = (TopiaContextImplementor) context.beginTransaction();
237 
238         person = tx.getDAO(Person.class).create(Person.PROPERTY_NAME, "pudding master");
239         race = tx.getDAO(Race.class).create(Race.PROPERTY_NAME, "race I");
240         pet = tx.getDAO(Pet.class).create(Pet.PROPERTY_NAME, "pudding", Pet.PROPERTY_PERSON, person, Pet.PROPERTY_RACE, race);
241 
242         person2 = tx.getDAO(Person.class).create(Person.PROPERTY_NAME, "pudding II master");
243         pet2 = tx.getDAO(Pet.class).create(Pet.PROPERTY_NAME, "pudding II");
244         race2 = tx.getDAO(Race.class).create(Race.PROPERTY_NAME, "race II");
245 
246         race3 = tx.getDAO(Race.class).create(Race.PROPERTY_NAME, "race III");
247         pet3 = tx.getDAO(Pet.class).create(Pet.PROPERTY_NAME, "pudding III", Pet.PROPERTY_RACE, race3);
248 
249         tx.commitTransaction();
250         tx.closeContext();
251         return context;
252     }
253 
254     @Override
255     protected TopiaContext createDb2(String name) throws Exception {
256 
257 //        File localDB = new File(getTestDir(getClass()), "db_" + name);
258 
259         Properties config = getH2Properties(name);
260 
261         return TopiaContextFactory.getContext(config);
262     }
263 
264     @Override
265     protected TopiaEntityEnum[] getContracts() {
266         return contracts;
267     }
268 
269     @Override
270     protected Log getLog() {
271         return log;
272     }
273 
274     protected Properties getH2Properties(String dbName) throws IOException {
275 
276         Properties config =  TestHelper.initTopiaContextConfiguration(tesDir, dbName);
277 
278 //        config.setProperty("hibernate.show_sql", "false");
279 //        config.setProperty("hibernate.hbm2ddl.auto", "create");
280 
281         config.setProperty("topia.persistence.classes", entitiesList);
282 //        config.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
283 //        config.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
284 //        config.setProperty("hibernate.connection.url", "jdbc:h2:file:" + f.getAbsolutePath() + ";create=true");
285 //        config.setProperty("hibernate.connection.username", "sa");
286 //        config.setProperty("hibernate.connection.password", "");
287 
288         config.setProperty(TopiaReplicationServiceImpl.TOPIA_SERVICE_NAME, TopiaReplicationServiceImpl.class.getName());
289 
290         return config;
291     }
292 
293 
294     @Override
295     protected void createModel(TopiaEntity entity) throws TopiaException {
296         model = getModelBuilder().createModelForAll(getContracts());
297     }
298 
299     @Override
300     protected void prepareModel(String... ids) throws TopiaException {
301         model = service.prepareForAll(getContracts());
302     }
303 
304 }
305 
306