View Javadoc

1   /*
2    * #%L
3    * ToPIA :: Persistence
4    * 
5    * $Id: TopiaContextImplTest.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/topia/framework/TopiaContextImplTest.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  package org.nuiton.topia.framework;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.h2.Driver;
30  import org.hibernate.cfg.Configuration;
31  import org.hibernate.mapping.PersistentClass;
32  import org.junit.After;
33  import org.junit.Assert;
34  import org.junit.Before;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  import org.nuiton.i18n.I18n;
38  import org.nuiton.topia.TopiaContextFactory;
39  import org.nuiton.topia.TopiaDatabase;
40  import org.nuiton.topia.TopiaNotFoundException;
41  import org.nuiton.topiatest.persistence.Entity1;
42  import org.nuiton.topiatest.persistence.Entity1Impl;
43  import org.nuiton.topiatest.service.FakeService;
44  import org.nuiton.topiatest.service.TestService;
45  
46  import java.io.File;
47  import java.util.HashMap;
48  import java.util.Locale;
49  import java.util.Map;
50  import java.util.Properties;
51  
52  /**
53   * Created: 10 mai 2010
54   *
55   * @author fdesbois <fdesbois@codelutin.com>
56   * @version $Id: TopiaContextImplTest.java 2416 2012-03-02 10:08:59Z tchemit $
57   */
58  public class TopiaContextImplTest {
59  
60      private static final Log log =
61              LogFactory.getLog(TopiaContextImplTest.class);
62  
63      protected Properties properties = new Properties();
64  
65      static File testBasedir;
66  
67      @BeforeClass
68      public static void setUpClass() throws Exception {
69  
70          testBasedir = TopiaDatabase.getTestSpecificDirectory(TopiaContextImplTest.class, "dummy");
71          I18n.init(null, Locale.FRENCH);
72      }
73  
74      @Before
75      public void setUp() throws Exception {
76          properties.clear();
77      }
78  
79      @After
80      public void tearDown() throws Exception {
81      }
82  
83      @Test
84      public void testLoadServices() throws Exception {
85          if (log.isDebugEnabled()) {
86              log.debug("## testLoadServices");
87          }
88  
89          /** PREPARE DATA **/
90          properties.setProperty("topia.service.test",
91                                 TestService.class.getName());
92  
93          TopiaContextImpl context = new TopiaContextImpl();
94  
95          /** EXEC METHOD **/
96          if (log.isInfoEnabled()) {
97              log.info("test 1 : load a simple TestService from properties");
98          }
99          Map<String, TopiaService> results = context.loadServices(properties);
100         Assert.assertEquals(1, results.size());
101         Assert.assertTrue(results.containsKey("test"));
102         TopiaService service = results.get("test");
103         Assert.assertEquals(TestService.class, service.getClass());
104 
105         if (log.isInfoEnabled()) {
106             log.info("test 2 : load with wrong key : will display a WARN");
107         }
108         properties.clear();
109 
110         properties.setProperty("topia.service.fake",
111                                TestService.class.getName());
112 
113         results = context.loadServices(properties);
114         Assert.assertEquals(0, results.size());
115         Assert.assertFalse(results.containsKey("fake"));
116 
117         if (log.isInfoEnabled()) {
118             log.info("test 3 : load with fake service name : will display an ERROR");
119         }
120         properties.clear();
121 
122         properties.setProperty("topia.service.test", "FAKE");
123 
124         results = context.loadServices(properties);
125         Assert.assertEquals(0, results.size());
126         Assert.assertFalse(results.containsKey("test"));
127     }
128 
129     @Test
130     public void testGetServices() throws Exception {
131         if (log.isDebugEnabled()) {
132             log.debug("## testGetServices");
133         }
134 
135         /** PREPARE DATA **/
136         properties.setProperty("topia.service.test",
137                                TestService.class.getName());
138 
139         // Calling the constructor with properties will load the services
140         TopiaContextImpl context = new TopiaContextImpl(properties);
141 
142         // Instantiate a child context and set its parent
143         TopiaContextImpl child = new TopiaContextImpl(context);
144 
145         /** EXEC METHOD **/
146         if (log.isInfoEnabled()) {
147             log.info("test 1 : with child context");
148         }
149         Map<String, TopiaService> test1 = child.getServices();
150         Assert.assertEquals(1, test1.size());
151         Assert.assertTrue(test1.containsKey("test"));
152 
153         if (log.isInfoEnabled()) {
154             log.info("test 2 : test serviceEnabled method");
155         }
156         boolean test2 = child.serviceEnabled("test");
157         Assert.assertTrue(test2);
158 
159         if (log.isInfoEnabled()) {
160             log.info("test 3 : test getService method");
161         }
162         TopiaService test3 = child.getService("test");
163         Assert.assertEquals(TestService.class, test3.getClass());
164 
165         if (log.isInfoEnabled()) {
166             log.info("test 4 : test serviceEnabled from class TestService");
167         }
168         boolean test4 = child.serviceEnabled(TestService.class);
169         Assert.assertTrue(test4);
170 
171         if (log.isInfoEnabled()) {
172             log.info("test 5 : test getService from class TestService");
173         }
174         TestService test5 = child.getService(TestService.class);
175         Assert.assertNotNull(test5);
176 
177         if (log.isInfoEnabled()) {
178             log.info("test 6 : test serviceEnabled error with class FakeService");
179         }
180         // FakeService doesn't contains property SERVICE_NAME used by
181         // serviceEnabled method
182         // Even it's properly loaded the serviceEnabled method will return false
183         properties.clear();
184         properties.setProperty("topia.service.fake",
185                                FakeService.class.getName());
186         TopiaContextImpl otherContext = new TopiaContextImpl(properties);
187 
188         boolean test6 = otherContext.serviceEnabled(FakeService.class);
189         Assert.assertFalse(test6);
190 
191         if (log.isInfoEnabled()) {
192             log.info("test 7 : test getService with error TopiaNotFoundException" +
193                      " : service not loaded");
194         }
195         // TestService is not loaded in otherContext
196         try {
197             TestService test7 = otherContext.getService(TestService.class);
198             Assert.fail();
199         } catch (TopiaNotFoundException eee) {
200             //log.error(eee.getClass().getSimpleName() + " : " + eee.getMessage());
201             //Assert.assertEquals(TopiaNotFoundException.class, eee.getClass());
202         } catch (Exception e) {
203             Assert.fail();
204         }
205     }
206 
207     @Test
208     public void testContextHierarchy() throws Exception {
209         if (log.isDebugEnabled()) {
210             log.debug("## testContextHierarchy");
211         }
212 
213         /** PREPARE DATA **/
214         TopiaContextImpl context = new TopiaContextImpl(properties);
215 
216         /** EXEC METHODS **/
217         if (log.isInfoEnabled()) {
218             log.info("test 1 : constructor with parent context");
219         }
220         TopiaContextImpl test1 = new TopiaContextImpl(context);
221         Assert.assertEquals(context, test1.parentContext);
222 
223         if (log.isInfoEnabled()) {
224             log.info("test 2 : addChildContext");
225         }
226         TopiaContextImpl test2 = new TopiaContextImpl(properties);
227         TopiaContextImpl child2 = new TopiaContextImpl();
228         test2.addChildContext(child2);
229         Assert.assertEquals(1, test2.childContext.size());
230 
231         if (log.isInfoEnabled()) {
232             log.info("test 3 : removeChildContext");
233         }
234         TopiaContextImpl test3 = new TopiaContextImpl(properties);
235         TopiaContextImpl child3 = new TopiaContextImpl(test3);
236         test3.childContext.add(child3);
237         test3.removeChildContext(child3);
238         Assert.assertEquals(0, test3.childContext.size());
239 
240         // No remove if context is closed
241         test3.childContext.add(child3);
242         test3.closed = true;
243         test3.removeChildContext(child3);
244         Assert.assertEquals(1, test3.childContext.size());
245 
246         if (log.isInfoEnabled()) {
247             log.info("test 4 : getRootContext");
248         }
249         TopiaContextImplementor test4 = child3.getRootContext();
250         Assert.assertEquals(test3, test4);
251 
252         // Note : existing test is already done for concurrency problem on
253         // getChildContext(). Go to : http://www.nuiton.org/repositories/browse/sandbox/testTopiaPostgresError/trunk
254     }
255 //
256 //    @Test
257 //    public void testCreateSchema() throws Exception {
258 //    }
259 //
260 //    @Test
261 //    public void testShowCreateSchema() throws Exception {
262 //    }
263 //
264 //    @Test
265 //    public void testUpdateSchema() throws Exception {
266 //    }
267 //
268 //    @Test
269 //    public void testGetHibernate() throws Exception {
270 //    }
271 //
272 
273     @Test
274     public void testGetHibernateFactory() throws Exception {
275         if (log.isDebugEnabled()) {
276             log.debug("## testGetHibernateFactory");
277         }
278 
279         /** PREPARE DATA **/
280         TopiaContextImpl context = new TopiaContextImpl();
281         context.services = new HashMap<String, TopiaService>();
282 
283         String basedir = System.getenv("basedir");
284         if (basedir == null) {
285 
286             // says basedir is where we start tests.
287             basedir = new File("").getAbsolutePath();
288         }
289 
290         if (log.isDebugEnabled()) {
291             log.debug("baseDir : " + basedir);
292         }
293         File persistenceDir = new File(basedir,
294                                        "target" + File.separator +
295                                        "test-classes" + File.separator +
296                                        "org" + File.separator +
297                                        "nuiton" + File.separator +
298                                        "topiatest" + File.separator +
299                                        "persistence");
300         if (log.isDebugEnabled()) {
301             log.debug("persistenceDir : " + persistenceDir);
302         }
303         File resourcesDir = new File(basedir,
304                                      "target" + File.separator +
305                                      "test-classes");
306 
307         /** EXEC METHOD **/
308         if (log.isInfoEnabled()) {
309             log.info("test 1 : load mappings from directory");
310         }
311 
312         properties.setProperty(TopiaContextFactory.CONFIG_PERSISTENCE_DIRECTORIES,
313                                persistenceDir.getAbsolutePath());
314         context.config = properties;
315 
316         Configuration test1 = context.getHibernateConfiguration();
317         PersistentClass persistentClass =
318                 test1.getClassMapping(Entity1Impl.class.getName());
319         Assert.assertNotNull(persistentClass);
320         Assert.assertEquals(Entity1.class, persistentClass.getProxyInterface());
321 
322 //        for (Iterator<RootClass> it = test1.getClassMappings(); it.hasNext();) {
323 //            RootClass o = it.next();
324 //            log.debug("entity : " + o.getEntityName());
325 //        }
326 
327         if (log.isInfoEnabled()) {
328             log.info("test 2 : load mappings for all entities");
329         }
330         //reset from previous test
331         context = new TopiaContextImpl();
332         context.services = new HashMap<String, TopiaService>();
333         properties.clear();
334 
335         // use property TOPIA_PERSISTENCE_CLASSES
336         properties.setProperty(TopiaContextFactory.CONFIG_PERSISTENCE_CLASSES,
337                                Entity1Impl.class.getName());
338         context.config = properties;
339 
340         Configuration test2 = context.getHibernateConfiguration();
341         persistentClass = test2.getClassMapping(Entity1Impl.class.getName());
342         Assert.assertNotNull(persistentClass);
343         Assert.assertEquals(Entity1.class, persistentClass.getProxyInterface());
344 
345         if (log.isInfoEnabled()) {
346             log.info("test 3 : add properties from file");
347         }
348         //reset from previous test
349         context = new TopiaContextImpl();
350         context.services = new HashMap<String, TopiaService>();
351         properties.clear();
352 
353         // use property TOPIA_PERSISTENCE_PROPERTIES_FILE to add default
354         // properties from file
355         properties.setProperty(TopiaContextFactory.CONFIG_PERSISTENCE_PROPERTIES_FILE,
356                                resourcesDir + File.separator + "TopiaContextImpl.properties");
357         context.config = properties;
358 
359         Configuration test3 = context.getHibernateConfiguration();
360         Assert.assertEquals(
361                 test3.getProperty("hibernate.connection.driver_class"),
362                 Driver.class.getName());
363 
364         // Note : maybe add a test to load classes from services
365     }
366 
367 //    @Test
368 //    public void replicateEntity() throws Exception {
369 //
370 //        Properties configSource = TestHelper.initTopiaContextConfiguration(
371 //                testBasedir,
372 //                "/TopiaContextImpl.properties",
373 //                "replicateSource");
374 //
375 //        Properties configTarget = TestHelper.initTopiaContextConfiguration(
376 //                testBasedir,
377 //                "/TopiaContextImpl.properties",
378 //                "replicateTarget");
379 //
380 //
381 //        TopiaContext contextSource = null;
382 //        TopiaContext contextTarget = null;
383 //
384 //        try {
385 //            contextSource = TopiaContextFactory.getContext(configSource);
386 //            contextTarget = TopiaContextFactory.getContext(configTarget);
387 //
388 //            TopiaContext txSource;
389 //            TopiaContext txTarget;
390 //            PersonDAO daoSource, daoTarget;
391 //            PetDAO petDAOSource, petDAOTarget;
392 //            Person personSource, personTarget;
393 //            Pet petSource, petTarget;
394 //
395 //            txSource = contextSource.beginTransaction();
396 //            daoSource = TopiaTestDAOHelper.getPersonDAO(txSource);
397 //            petDAOSource = TopiaTestDAOHelper.getPetDAO(txSource);
398 //
399 //            personSource = daoSource.create(Person.PROPERTY_FIRSTNAME, " firstName",
400 //                                            Person.PROPERTY_NAME, " name"
401 //            );
402 //
403 //            petSource = petDAOSource.create(Pet.PROPERTY_NAME, "name",
404 //                                            Pet.PROPERTY_TYPE, "type",
405 //                                            Pet.PROPERTY_PERSON, personSource
406 //            );
407 //
408 //            personSource.addPet(petSource);
409 //
410 //            txSource.commitTransaction();
411 //
412 //            daoSource = TopiaTestDAOHelper.getPersonDAO(txSource);
413 //
414 //            personSource = daoSource.findByTopiaId(personSource.getTopiaId());
415 //            Assert.assertNotNull(personSource);
416 //
417 //            petSource = petDAOSource.findByTopiaId(petSource.getTopiaId());
418 //            Assert.assertNotNull(petSource);
419 //            Assert.assertEquals(1, personSource.sizePet());
420 //            Assert.assertEquals(petSource, personSource.getPet().iterator().next());
421 //
422 //            txTarget = contextTarget.beginTransaction();
423 //
424 //            txSource.replicateEntity(txTarget, petSource);
425 //            txSource.replicateEntity(txTarget, personSource);
426 //
427 //            txTarget.commitTransaction();
428 //
429 //            daoTarget = TopiaTestDAOHelper.getPersonDAO(txTarget);
430 //            petDAOTarget = TopiaTestDAOHelper.getPetDAO(txTarget);
431 //
432 //            personTarget = daoTarget.findByTopiaId(personSource.getTopiaId());
433 //            Assert.assertNotNull(personTarget);
434 //            Assert.assertEquals(personSource, personTarget);
435 //            Assert.assertEquals(1, personTarget.sizePet());
436 //
437 //            petTarget = petDAOTarget.findByTopiaId(petSource.getTopiaId());
438 //            Assert.assertNotNull(petTarget);
439 //            Assert.assertEquals(petSource, petTarget);
440 //
441 //            Assert.assertEquals(petTarget, personTarget.getPet().iterator().next());
442 //
443 //
444 //        } finally {
445 //            closeDb(contextSource);
446 //            closeDb(contextTarget);
447 //        }
448 //
449 //    }
450 //
451 //    protected static void closeDb(TopiaContext contextSource) {
452 //        if (contextSource != null && !contextSource.isClosed())
453 //            try {
454 //                contextSource.clear(false);
455 //            } catch (TopiaException e) {
456 //                if (log.isErrorEnabled()) {
457 //                    log.error("Could not close db " + contextSource, e);
458 //                }
459 //            }
460 //    }
461 
462 
463 //
464 //    @Test
465 //    public void testGetHibernateConfiguration() throws Exception {
466 //    }
467 //
468 //    @Test
469 //    public void testGetDAO() throws Exception {
470 //    }
471 //
472 //    @Test
473 //    public void testBeginTransaction() throws Exception {
474 //    }
475 //
476 //    @Test
477 //    public void testCommitTransaction() throws Exception {
478 //    }
479 //
480 //    @Test
481 //    public void testRollbackTransaction() throws Exception {
482 //    }
483 //
484 //    @Test
485 //    public void testCloseContext() throws Exception {
486 //    }
487 //
488 //    @Test
489 //    public void testIsClosed() throws Exception {
490 //    }
491 //
492 //    @Test
493 //    public void testFindByTopiaId() throws Exception {
494 //    }
495 //
496 //    @Test
497 //    public void testFind() throws Exception {
498 //    }
499 //
500 //    @Test
501 //    public void testFind2() throws Exception {
502 //    }
503 //
504 //    @Test
505 //    public void testExecute() throws Exception {
506 //    }
507 //
508 //    @Test
509 //    public void testAdd() throws Exception {
510 //    }
511 //
512 //    @Test
513 //    public void testImportXML() throws Exception {
514 //    }
515 //
516 //    @Test
517 //    public void testExportXML() throws Exception {
518 //    }
519 //
520 //
521 //    @Test
522 //    public void testReplicateEntity() throws Exception {
523 //    }
524 //
525 //    @Test
526 //    public void testReplicateEntities() throws Exception {
527 //    }
528 //
529 //    @Test
530 //    public void testGetFiresSupport() throws Exception {
531 //    }
532 //
533 //    @Test
534 //    public void testBackup() throws Exception {
535 //    }
536 //
537 //    @Test
538 //    public void testRestore() throws Exception {
539 //    }
540 //
541 //    @Test
542 //    public void testClear() throws Exception {
543 //    }
544 //
545 //    @Test
546 //    public void testGetPersistenceClasses() throws Exception {
547 //    }
548 //
549 //    @Test
550 //    public void testIsSchemaExist() throws Exception {
551 //    }
552 }