1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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.TopiaEntityEnum;
41 import org.nuiton.topia.persistence.util.TopiaEntityIdsMap;
42 import org.nuiton.topia.replication.model.ReplicationModel;
43 import org.nuiton.topia.replication.operation.AttachAssociation;
44 import org.nuiton.topia.replication.operation.DettachAssociation;
45 import org.nuiton.topia.replication.operation.Duplicate;
46 import org.nuiton.topia.replication.operation.FakeOperation;
47 import org.nuiton.topia.replication.operation.UncreatableOperation;
48 import org.nuiton.topia.replication.operation.UnregistredOperation;
49 import org.nuiton.topia.test.entities.Person;
50 import org.nuiton.topia.test.entities.PersonImpl;
51 import org.nuiton.topia.test.entities.Pet;
52 import org.nuiton.topia.test.entities.PetImpl;
53 import org.nuiton.topia.test.entities.Race;
54 import org.nuiton.topia.test.entities.RaceImpl;
55
56 import java.io.File;
57 import java.io.IOException;
58 import java.util.Properties;
59
60
61
62
63
64
65
66
67
68 public class TopiaReplicationServiceImplTest extends AbstractTopiaReplicationServiceTest {
69
70
71 private static final Log log =
72 LogFactory.getLog(TopiaReplicationServiceImplTest.class);
73
74 protected static final TopiaEntityEnum[] contracts = {TopiaTestEntityEnum.Person, TopiaTestEntityEnum.Pet, TopiaTestEntityEnum.Race};
75
76 protected static final String entitiesList = PersonImpl.class.getName() + "," + PetImpl.class.getName() + "," + RaceImpl.class.getName();
77
78 static protected Person person, person2;
79
80 static protected Pet pet, pet2, pet3;
81
82 static protected Race race, race2, race3;
83
84 protected static File tesDir;
85
86 @BeforeClass
87 public static void beforeClass() throws IOException {
88 tesDir = TestHelper.getTestBasedir(
89 TopiaReplicationServiceImplTest.class);
90 }
91
92 @AfterClass
93 public static void after() throws Exception {
94 AbstractTopiaReplicationServiceTest.after();
95 }
96
97 @Before
98 @Override
99 public void setUp() throws Exception {
100
101 super.setUp();
102
103 person = update(person);
104 person2 = update(person2);
105 pet = update(pet);
106 pet2 = update(pet2);
107 race = update(race);
108 race2 = update(race2);
109 race3 = update(race3);
110 }
111
112 @After
113 @Override
114 public void tearDown() throws Exception {
115 super.tearDown();
116 if (dstCtxt != null && !dstCtxt.isClosed()) {
117 dstCtxt.closeContext();
118 }
119 }
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 @Test
138 @Override
139 public void testGetOperation() throws Exception {
140
141 getOperation(UnregistredOperation.class, false);
142 getOperation(UncreatableOperation.class, true);
143 getOperation(FakeOperation.class, true);
144 getOperation(Duplicate.class, true);
145 getOperation(AttachAssociation.class, true);
146 getOperation(DettachAssociation.class, true);
147 }
148
149 @Test
150 @Override
151 public void testDetectAssociations() throws Exception {
152
153 detectAssociations(person, TopiaTestEntityEnum.Person, Person.PROPERTY_PET);
154 detectAssociations(race);
155 detectAssociations(pet);
156
157 detectAssociations(person2);
158 detectAssociations(race2);
159 detectAssociations(pet2);
160 }
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 @Test
176 @Override
177 public void testDetectShell() throws Exception {
178
179 detectShell(person, TopiaTestEntityEnum.Pet, TopiaTestEntityEnum.Race);
180 detectShell(race);
181 detectShell(pet, TopiaTestEntityEnum.Person, TopiaTestEntityEnum.Race);
182 detectShell(person2);
183 detectShell(race2);
184 detectShell(pet2);
185 }
186
187 @Test
188 @Override
189 public void testDetectDependencies() throws Exception {
190
191 detectDependencies(person, new TopiaTestEntityEnum[]{TopiaTestEntityEnum.Race}, new TopiaTestEntityEnum[]{TopiaTestEntityEnum.Person}, new TopiaTestEntityEnum[]{TopiaTestEntityEnum.Pet});
192 detectDependencies(race, new TopiaTestEntityEnum[]{TopiaTestEntityEnum.Race});
193 detectDependencies(pet, new TopiaTestEntityEnum[]{TopiaTestEntityEnum.Race}, new TopiaTestEntityEnum[]{TopiaTestEntityEnum.Person}, new TopiaTestEntityEnum[]{TopiaTestEntityEnum.Pet});
194
195 detectDependencies(person2, new TopiaTestEntityEnum[]{TopiaTestEntityEnum.Person});
196 detectDependencies(race2, new TopiaTestEntityEnum[]{TopiaTestEntityEnum.Race});
197 detectDependencies(pet2, new TopiaTestEntityEnum[]{TopiaTestEntityEnum.Pet});
198 }
199
200 @Test
201 @Override
202 public void testDetectObjectsToDettach() throws Exception {
203
204 detectObjectsToDettach(person, TopiaTestEntityEnum.Person, new String[]{Person.PROPERTY_PET});
205 detectObjectsToDettach(race);
206 detectObjectsToDettach(pet, TopiaTestEntityEnum.Person, new String[]{Person.PROPERTY_PET});
207
208 detectObjectsToDettach(person2);
209 detectObjectsToDettach(race2);
210 detectObjectsToDettach(pet2);
211
212 detectObjectsToDettach(race3);
213 detectObjectsToDettach(pet3);
214 }
215
216 @Test
217 @Override
218 public void testDetectOperations() throws Exception {
219
220
221
222 detectOperations(person);
223 detectOperations(pet);
224 detectOperations(race);
225
226 detectOperations(person2);
227 detectOperations(pet2);
228 detectOperations(race2);
229
230 detectOperations(race3);
231 detectOperations(pet3);
232 }
233
234 @Test
235 @Override
236 public void testDoReplicate() throws Exception {
237
238 doReplicate(TopiaTestEntityEnum.Person, person);
239 doReplicate(TopiaTestEntityEnum.Person, person2);
240 doReplicate(TopiaTestEntityEnum.Person, person, person2);
241
242 doReplicate(TopiaTestEntityEnum.Pet, pet);
243 doReplicate(TopiaTestEntityEnum.Pet, pet2);
244 doReplicate(TopiaTestEntityEnum.Pet, pet, pet2, pet3);
245 doReplicate(TopiaTestEntityEnum.Pet, person2, pet3);
246
247 doReplicate(TopiaTestEntityEnum.Race, race);
248 doReplicate(TopiaTestEntityEnum.Race, race2);
249 doReplicate(TopiaTestEntityEnum.Race, race, race2);
250
251 }
252
253
254
255
256
257
258
259
260
261
262 @Test(expected = TopiaException.class)
263 public void testSimpleReplicateFailed() throws Exception {
264
265 TopiaContext dstRootCtxt = createDb2("testSimpleReplicateFailed");
266
267
268
269 TopiaContext srcCtxt = ctxt.beginTransaction();
270 dstCtxt = (TopiaContextImplementor) dstRootCtxt.beginTransaction();
271
272 try {
273
274 srcCtxt.replicateEntity(dstCtxt, pet);
275
276 dstCtxt.commitTransaction();
277
278 } finally {
279 srcCtxt.rollbackTransaction();
280 srcCtxt.closeContext();
281 dstCtxt.closeContext();
282 }
283 }
284
285
286
287
288
289
290
291
292
293
294 @Test
295 public void testSimpleReplicateNotSure() throws Exception {
296
297 TopiaContext dstRootCtxt = createDb2("testSimpleReplicateNotSure");
298
299
300
301 TopiaContext srcCtxt = ctxt;
302 dstCtxt = (TopiaContextImplementor) dstRootCtxt.beginTransaction();
303
304 try {
305
306
307 srcCtxt.replicateEntity(dstCtxt, race);
308
309
310
311 pet.setPerson(null);
312 srcCtxt.replicateEntity(dstCtxt, pet);
313 srcCtxt.rollbackTransaction();
314
315 srcCtxt.replicateEntity(dstCtxt, person);
316
317 dstCtxt.commitTransaction();
318 ((Pet) dstCtxt.findByTopiaId(pet.getTopiaId())).setPerson((Person) dstCtxt.findByTopiaId(person.getTopiaId()));
319 dstCtxt.commitTransaction();
320
321 srcCtxt.rollbackTransaction();
322 person = update(person);
323
324 assertEntityEquals(person, dstCtxt.findByTopiaId(person.getTopiaId()), null);
325 } finally {
326 srcCtxt.rollbackTransaction();
327
328 dstCtxt.closeContext();
329 }
330 }
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347 @Test
348 public void testSimpleReplicateSure() throws Exception {
349
350 TopiaContext dstRootCtxt = createDb2("testSimpleReplicateSure");
351
352
353
354 TopiaContext srcCtxt = ctxt;
355 dstCtxt = (TopiaContextImplementor) dstRootCtxt.beginTransaction();
356
357 try {
358
359 srcCtxt.replicateEntity(dstCtxt, race);
360
361 person.setPet(null);
362 srcCtxt.replicateEntity(dstCtxt, person);
363
364 srcCtxt.replicateEntity(dstCtxt, pet);
365 srcCtxt.rollbackTransaction();
366 dstCtxt.commitTransaction();
367
368
369
370
371 srcCtxt.rollbackTransaction();
372
373 srcCtxt.closeContext();
374 dstCtxt.closeContext();
375
376 ctxt = context.beginTransaction();
377 dstCtxt = (TopiaContextImplementor) dstRootCtxt.beginTransaction();
378
379 person = update(person);
380
381 assertEntityEquals(person, dstCtxt.findByTopiaId(person.getTopiaId()), null);
382
383 } finally {
384 dstCtxt.closeContext();
385 }
386 }
387
388 @Override
389 protected TopiaContext createDb(String name) throws Exception {
390
391
392
393 Properties config = getH2Properties(name);
394
395 context = TopiaContextFactory.getContext(config);
396
397 TopiaContextImplementor tx = (TopiaContextImplementor) context.beginTransaction();
398
399 person = tx.getDAO(Person.class).create(Person.PROPERTY_NAME, "pudding master");
400 race = tx.getDAO(Race.class).create(Race.PROPERTY_NAME, "race I");
401 pet = tx.getDAO(Pet.class).create(Pet.PROPERTY_NAME, "pudding", Pet.PROPERTY_PERSON, person, Pet.PROPERTY_RACE, race);
402
403 person2 = tx.getDAO(Person.class).create(Person.PROPERTY_NAME, "pudding II master");
404 pet2 = tx.getDAO(Pet.class).create(Pet.PROPERTY_NAME, "pudding II");
405 race2 = tx.getDAO(Race.class).create(Race.PROPERTY_NAME, "race II");
406
407 race3 = tx.getDAO(Race.class).create(Race.PROPERTY_NAME, "race III");
408 pet3 = tx.getDAO(Pet.class).create(Pet.PROPERTY_NAME, "pudding III", Pet.PROPERTY_RACE, race3);
409
410 tx.commitTransaction();
411 tx.closeContext();
412 return context;
413 }
414
415 @Override
416 protected TopiaContext createDb2(String name) throws Exception {
417
418
419
420
421
422 Properties config = getH2Properties(name);
423
424 return TopiaContextFactory.getContext(config);
425 }
426
427 @Override
428 protected TopiaEntityEnum[] getContracts() {
429 return contracts;
430 }
431
432 @Override
433 protected Log getLog() {
434 return log;
435 }
436
437 protected Properties getH2Properties(String dbName) throws IOException {
438
439
440 Properties config = TestHelper.initTopiaContextConfiguration(tesDir, dbName);
441
442
443
444
445 config.setProperty("topia.persistence.classes", entitiesList);
446
447
448
449
450
451
452 config.setProperty(TopiaReplicationServiceImpl.TOPIA_SERVICE_NAME, TopiaReplicationServiceImpl.class.getName());
453
454 return config;
455 }
456 }
457
458