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.*;
30 import org.nuiton.topia.TestHelper;
31 import org.nuiton.topia.TopiaContext;
32 import org.nuiton.topia.TopiaContextFactory;
33 import org.nuiton.topia.TopiaTestDAOHelper.TopiaTestEntityEnum;
34 import org.nuiton.topia.framework.TopiaContextImplementor;
35 import org.nuiton.topia.persistence.TopiaEntityEnum;
36 import org.nuiton.topia.replication.model.ReplicationOperationPhase;
37 import org.nuiton.topia.replication.operation.*;
38 import org.nuiton.topia.test.entities.*;
39
40 import java.io.File;
41 import java.io.IOException;
42 import java.util.Properties;
43
44
45
46
47
48
49
50
51
52 public class TopiaReplicationOperationTest extends AbstractTopiaReplicationServiceTest {
53
54
55 private static final Log log =
56 LogFactory.getLog(TopiaReplicationOperationTest.class);
57
58 protected static final TopiaEntityEnum[] contracts = {
59 TopiaTestEntityEnum.Person,
60 TopiaTestEntityEnum.Pet,
61 TopiaTestEntityEnum.Race
62 };
63
64 protected static final String entitiesList =
65 PersonImpl.class.getName() + "," +
66 PetImpl.class.getName() + "," +
67 RaceImpl.class.getName();
68
69 static protected Person person, person2;
70
71 static protected Pet pet, pet2;
72
73 static protected Race race, race2;
74
75 protected static File tesDir;
76
77 @BeforeClass
78 public static void beforeClass() throws IOException {
79 tesDir = TestHelper.getTestBasedir(TopiaReplicationOperationTest.class);
80
81 }
82
83 @AfterClass
84 public static void after() throws Exception {
85 AbstractTopiaReplicationServiceTest.after();
86 }
87
88 @Before
89 @Override
90 public void setUp() throws Exception {
91
92 super.setUp();
93
94 person = update(person);
95 person2 = update(person2);
96 pet = update(pet);
97 pet2 = update(pet2);
98 race = update(race);
99 race2 = update(race2);
100 }
101
102 @After
103 @Override
104 public void tearDown() throws Exception {
105 super.tearDown();
106 if (dstCtxt != null && !dstCtxt.isClosed()) {
107 dstCtxt.closeContext();
108 }
109 }
110
111 @Test(expected = NullPointerException.class)
112 public void testGetOperation_nullOperationClass() throws Exception {
113 getModelBuilder().getOperationProvider().getOperation((Class<? extends TopiaReplicationOperation>) null);
114 }
115
116 protected TopiaReplicationModelBuilder getModelBuilder() {
117 return service.getModelBuilder();
118 }
119
120 @Test
121 @Override
122 public void testGetOperation() throws Exception {
123
124 getOperation(UnregistredOperation.class, false);
125 getOperation(UncreatableOperation.class, true);
126 getOperation(FakeOperation.class, true);
127 getOperation(Duplicate.class, true);
128
129 getOperation(DettachAssociation.class, true);
130 }
131
132 @Test(expected = NullPointerException.class)
133 public void testCreateOperation_nullModel() throws Exception {
134 getModelBuilder().createOperation(null, null, null, null);
135 }
136
137 @Test(expected = NullPointerException.class)
138 public void testCreateOperation_nullType() throws Exception {
139
140 model = getModelBuilder().createModel(context, contracts, true);
141 getModelBuilder().createOperation(model, null, null, null);
142 }
143
144 @Test(expected = NullPointerException.class)
145 public void testCreateOperation_nullPhase() throws Exception {
146
147 model = getModelBuilder().createModel(context, contracts, true);
148 getModelBuilder().createOperation(model, TopiaTestEntityEnum.Pet, null, null);
149 }
150
151 @Test(expected = NullPointerException.class)
152 public void testCreateOperation_nullOperationClass() throws Exception {
153
154 model = getModelBuilder().createModel(context, contracts, true);
155 getModelBuilder().createOperation(model, TopiaTestEntityEnum.Pet, ReplicationOperationPhase.before, null);
156 }
157
158 @Test(expected = IllegalArgumentException.class)
159 public void testCreateOperation_noNode() throws Exception {
160
161 model = getModelBuilder().createModel(context, contracts, true);
162
163 getModelBuilder().addAfterOperation(model, TopiaTestEntityEnum.Pet, Duplicate.class);
164 }
165
166 @Test(expected = IllegalArgumentException.class)
167 public void testCreateOperation_noOperation() throws Exception {
168
169 model = getModelBuilder().createModel(context, contracts, true);
170
171 getModelBuilder().addAfterOperation(model, TopiaTestEntityEnum.Pet, UnregistredOperation.class);
172 }
173
174 @Test
175
176 public void testCreateSupportedBeforeOperation_Duplicate() throws Exception {
177 createSupportedBeforeOperation(TopiaTestEntityEnum.Person, person, Duplicate.class);
178 }
179
180
181
182
183
184
185 @Test
186
187 public void testCreateSupportedBeforeOperation_DetachAssociation() throws Exception {
188 createSupportedBeforeOperation(TopiaTestEntityEnum.Person, person, DettachAssociation.class);
189 }
190
191 @Test
192
193 public void testCreateSupportedAfterOperation_Duplicate() throws Exception {
194 createSupportedAfterOperation(TopiaTestEntityEnum.Person, person, Duplicate.class);
195 }
196
197
198
199
200
201
202 @Test
203
204 public void testCreateSupportedAfterOperation_DetachAssociation() throws Exception {
205 createSupportedAfterOperation(TopiaTestEntityEnum.Person, person, DettachAssociation.class);
206 }
207
208
209
210
211
212
213
214
215
216
217
218 @Test(expected = IllegalArgumentException.class)
219 public void testCreateOperation_wrongParameterNumber() throws Exception {
220
221 model = getModelBuilder().createModel(context, contracts, true, pet.getTopiaId());
222 getModelBuilder().addBeforeOperation(model, TopiaTestEntityEnum.Pet, FakeOperation.class);
223 }
224
225 @Test(expected = IllegalArgumentException.class)
226 public void testCreateOperation_wrongParameterNumber2() throws Exception {
227
228 model = getModelBuilder().createModel(context, contracts, true, pet.getTopiaId());
229 getModelBuilder().addBeforeOperation(model, TopiaTestEntityEnum.Pet, FakeOperation.class, String.class, String.class);
230 }
231
232 @Test(expected = IllegalArgumentException.class)
233 public void testCreateOperation_wrongParameterType() throws Exception {
234
235 model = getModelBuilder().createModel(context, contracts, true, pet.getTopiaId());
236 getModelBuilder().addBeforeOperation(model, TopiaTestEntityEnum.Pet, FakeOperation.class, Integer.class);
237 }
238
239 @Test(expected = IllegalArgumentException.class)
240 public void testCreateOperation_nullParameter() throws Exception {
241
242 model = getModelBuilder().createModel(context, contracts, true, pet.getTopiaId());
243 getModelBuilder().addBeforeOperation(model, TopiaTestEntityEnum.Pet, FakeOperation.class, (Object) null);
244 }
245
246 @Test
247 public void testCreateOperation() throws Exception {
248
249 model = getModelBuilder().createModel(context, contracts, true, pet.getTopiaId());
250 getModelBuilder().addBeforeOperation(model, TopiaTestEntityEnum.Pet, FakeOperation.class, "before");
251 getModelBuilder().addAfterOperation(model, TopiaTestEntityEnum.Race, FakeOperation.class, "after");
252 }
253
254 @Test(expected = NullPointerException.class)
255 public void testDoReplicate_nullModel() throws Exception {
256
257 service.doReplicate(null, null);
258 }
259
260 @Test(expected = NullPointerException.class)
261 public void testDoReplicate_nullDstCtxt() throws Exception {
262
263 model = getModelBuilder().createModel(context, contracts, true);
264 service.doReplicate(model, null);
265 }
266
267 @Override
268 protected TopiaContext createDb(String name) throws Exception {
269
270
271
272 Properties config = getH2Properties(name);
273
274 context = TopiaContextFactory.getContext(config);
275
276 TopiaContextImplementor tx =
277 (TopiaContextImplementor) context.beginTransaction();
278
279 person = tx.getDAO(Person.class).create(Person.PROPERTY_NAME, "pudding master");
280 race = tx.getDAO(Race.class).create(Race.PROPERTY_NAME, "race I");
281 pet = tx.getDAO(Pet.class).create(Pet.PROPERTY_NAME, "pudding", Pet.PROPERTY_PERSON, person, Pet.PROPERTY_RACE, race);
282
283 person2 = tx.getDAO(Person.class).create(Person.PROPERTY_NAME, "pudding II master");
284 pet2 = tx.getDAO(Pet.class).create(Pet.PROPERTY_NAME, "pudding II");
285 race2 = tx.getDAO(Race.class).create(Race.PROPERTY_NAME, "race II");
286
287 tx.commitTransaction();
288 tx.closeContext();
289 return context;
290 }
291
292 @Override
293 protected TopiaEntityEnum[] getContracts() {
294 return contracts;
295 }
296
297 @Override
298 protected Log getLog() {
299 return log;
300 }
301
302 protected Properties getH2Properties(String dbName) throws IOException {
303
304 Properties config = TestHelper.initTopiaContextConfiguration(tesDir, dbName);
305
306
307
308
309 config.setProperty("topia.persistence.classes", entitiesList);
310
311
312
313
314
315
316 config.setProperty("topia.service.replication", TopiaReplicationServiceImpl.class.getName());
317
318 return config;
319 }
320
321 @Override
322 protected TopiaContext createDb2(String name) throws Exception {
323 throw new UnsupportedOperationException("Not supported yet.");
324 }
325
326 @Override
327 public void testDetectTypes() throws Exception {
328 throw new UnsupportedOperationException("Not supported yet.");
329 }
330
331 @Override
332 public void testDetectAssociations() throws Exception {
333 throw new UnsupportedOperationException("Not supported yet.");
334 }
335
336 @Override
337 public void testDetectDirectDependencies() throws Exception {
338 throw new UnsupportedOperationException("Not supported yet.");
339 }
340
341 @Override
342 public void testDetectShell() throws Exception {
343 throw new UnsupportedOperationException("Not supported yet.");
344 }
345
346 @Override
347 public void testDetectDependencies() throws Exception {
348 throw new UnsupportedOperationException("Not supported yet.");
349 }
350
351 @Override
352 public void testDetectObjectsToDettach() throws Exception {
353 throw new UnsupportedOperationException("Not supported yet.");
354 }
355
356 @Override
357 public void testDetectOperations() throws Exception {
358 throw new UnsupportedOperationException("Not supported yet.");
359 }
360
361 @Override
362 public void testDoReplicate() throws Exception {
363 throw new UnsupportedOperationException("Not supported yet.");
364 }
365 }
366
367