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.*;
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   * TopiaReplicationServiceImplTest on model TopiaTest
46   * 
47   * Created: 07 jun. 09 17:14:22
48   *
49   * @author tchemit &lt;chemit@codelutin.com&gt;
50   * @since 2.2.0
51   */
52  public class TopiaReplicationOperationTest extends AbstractTopiaReplicationServiceTest {
53  
54      /** Logger */
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 //        getOperation(AttachAssociation.class, true);
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         // le noeud Pet n'existe pas
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         // le noeud Pet n'existe pas
171         getModelBuilder().addAfterOperation(model, TopiaTestEntityEnum.Pet, UnregistredOperation.class);
172     }
173 
174     @Test
175 //    @Test(expected = UnsupportedOperationException.class)
176     public void testCreateSupportedBeforeOperation_Duplicate() throws Exception {
177         createSupportedBeforeOperation(TopiaTestEntityEnum.Person, person, Duplicate.class);
178     }
179 
180 //    @Test(expected = UnsupportedOperationException.class)
181 //    public void testCreateUnsupportedBeforeOperation_AttachAssociation() throws Exception {
182 //        createUnsupportedBeforeOperation(TopiaTestEntityEnum.Person, person, AttachAssociation.class);
183 //    }
184 
185     @Test
186 //    @Test(expected = UnsupportedOperationException.class)
187     public void testCreateSupportedBeforeOperation_DetachAssociation() throws Exception {
188         createSupportedBeforeOperation(TopiaTestEntityEnum.Person, person, DettachAssociation.class);
189     }
190 
191     @Test
192 //    @Test(expected = UnsupportedOperationException.class)
193     public void testCreateSupportedAfterOperation_Duplicate() throws Exception {
194         createSupportedAfterOperation(TopiaTestEntityEnum.Person, person, Duplicate.class);
195     }
196 
197 //    @Test(expected = UnsupportedOperationException.class)
198 //    public void testCreateSupportedAfterOperation_AttachAssociation() throws Exception {
199 //        createUnsupportedAfterOperation(TopiaTestEntityEnum.Person, person, AttachAssociation.class);
200 //    }
201 
202     @Test
203 //    @Test(expected = UnsupportedOperationException.class)
204     public void testCreateSupportedAfterOperation_DetachAssociation() throws Exception {
205         createSupportedAfterOperation(TopiaTestEntityEnum.Person, person, DettachAssociation.class);
206     }
207 
208 //    @Test(expected = UnsupportedOperationException.class)
209 //    public void testCreateUnsupportedBeforeOperation_UncreatableOperation() throws Exception {
210 //        createUnsupportedBeforeOperation(TopiaTestEntityEnum.Person, person, UncreatableOperation.class);
211 //    }
212 
213 //    @Test(expected = UnsupportedOperationException.class)
214 //    public void testCreateUnsupportedAfterOperation_UncreatableOperation() throws Exception {
215 //        createUnsupportedAfterOperation(TopiaTestEntityEnum.Person, person, UncreatableOperation.class);
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 //        File localDB = new File(getTestDir(getClass()), "db_" + name);
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 //        config.setProperty("hibernate.show_sql", "false");
307 //        config.setProperty("hibernate.hbm2ddl.auto", "create");
308 
309         config.setProperty("topia.persistence.classes", entitiesList);
310 //        config.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
311 //        config.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
312 //        config.setProperty("hibernate.connection.url", "jdbc:h2:file:" + f.getAbsolutePath() + ";create=true");
313 //        config.setProperty("hibernate.connection.username", "sa");
314 //        config.setProperty("hibernate.connection.password", "");
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