View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2006 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.tests.services.persistence.generic.dao;
18  
19  import static org.junit.Assert.assertTrue;
20  
21  import org.junit.Test;
22  import org.springframework.context.ApplicationContext;
23  
24  import ch.elca.el4j.core.context.ModuleApplicationContext;
25  import ch.elca.el4j.services.persistence.generic.dao.DaoRegistry;
26  import ch.elca.el4j.services.persistence.generic.dao.GenericDao;
27  import ch.elca.el4j.services.persistence.generic.dao.impl.DefaultDaoRegistry;
28  
29  /**
30   * @svnLink $Revision: 4010 $;$Date: 2009-12-01 10:59:54 +0100 (Di, 01. Dez 2009) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/test/java/ch/elca/el4j/tests/services/persistence/generic/dao/DaoAutocollectionTest.java $
31   */
32  public class DaoAutocollectionTest {
33  
34  	@Test
35  	public void testDaoAutocollection() {
36  		ApplicationContext ac = new ModuleApplicationContext(
37  			new String[] {"scenarios/core/dao/springConfig.xml"}, false);
38  		
39  		//System.out.println("beans: "+StringUtils.arrayToCommaDelimitedString(ac.getBeanDefinitionNames()));
40  		
41  		DaoRegistry registry = (DaoRegistry) ac.getBean("registry");
42  		
43  		GenericDao<?> dao = registry.getFor(String.class);
44  		//System.out.println("registry: "+DataDumper.dump(((DefaultDaoRegistry)registry).getDaos()));
45  		
46  		// dao = registry.getFor(String.class);
47  		assertTrue(dao != null);
48  		assertTrue(dao instanceof Dao1);
49  		
50  		dao = registry.getFor(Long.class);
51  		assertTrue(dao != null);
52  		assertTrue(dao instanceof Dao2);
53  		
54  		assertTrue(ac.getBeanNamesForType(GenericDao.class).length == 2);
55  	}
56  	
57  	/**
58  	 * Test the dao name pattern matching functionality.
59  	 */
60  	@Test
61  	public void testDaoPatternMatching() {
62  		ApplicationContext ac =	new ModuleApplicationContext(
63  			new String[] {"scenarios/core/dao/springConfig.xml"}, false);
64  		
65  		// Pattern is "ti*"
66  		DaoRegistry registry = (DaoRegistry) ac.getBean("registryWithFilter");
67  		
68  		// This one is called "titi". It should work.
69  		GenericDao<?> dao = registry.getFor(String.class);
70  		assertTrue(dao != null);
71  		assertTrue(dao instanceof Dao1);
72  		
73  		// This one should not match.
74  		dao = registry.getFor(Long.class);
75  		assertTrue(dao == null);
76  
77  	}
78  	
79  	/**
80  	 * Test if {@link DefaultDaoRegistry} blocks until Spring context is ready.
81  	 */
82  	@Test
83  	public void testConcurrent() {
84  		final ApplicationContext ac = new ModuleApplicationContext(
85  			new String[] {"scenarios/core/dao/springConfig.xml", "scenarios/core/dao/concurrentConfig.xml"}, false);
86  		
87  		ImpatientClass impatientClass = (ImpatientClass) ac.getBean("impatientClass");
88  		
89  		assertTrue("DaoRegistry didn't wait for completely initialized Spring context", impatientClass.join());
90  	}
91  	
92  }