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) 2005 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  
18  package ch.elca.el4j.tests.core.beans;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.Map;
24  
25  import org.junit.Test;
26  import org.springframework.context.ApplicationContext;
27  
28  import ch.elca.el4j.core.beans.BeanLocator;
29  import ch.elca.el4j.core.context.ModuleApplicationContext;
30  
31  /**
32   * This test tests the <code>BeanLocator</code> class.
33   *
34   * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/test/java/ch/elca/el4j/tests/core/beans/BeanLocatorTest.java $
35   *
36   * @author Martin Zeltner (MZE)
37   */
38  public class BeanLocatorTest {
39  	
40  	/** The application context. */
41  	final ApplicationContext m_appContext;
42  
43  	/**
44  	 * Default constructor.
45  	 */
46  	public BeanLocatorTest() {
47  		m_appContext = new ModuleApplicationContext(
48  			"classpath:scenarios/core/beans/*.xml", false);
49  	}
50  
51  	/**
52  	 * This test locates classes of specific types.
53  	 */
54  	@Test
55  	public void testLocateClassesOfSpecificTypes() {
56  		BeanLocator beanLocator = (BeanLocator) m_appContext
57  				.getBean("locateClassesOfSpecificTypes");
58  		Map beans = beanLocator.getBeans();
59  		String[] expectedBeans = new String[] {"classB1", "classB2",
60  			"classB3", "fourthB", "classC", "secondC", "thirdC", "classW1",
61  			"classW2", "thirdW"};
62  		checkIfAllExpectedBeansAreInMap(beans, expectedBeans);
63  	}
64  
65  	/**
66  	 * This test locates classes of specific types and excludes some beans.
67  	 */
68  	@Test
69  	public void testLocateClassesOfSpecificTypesPlusExcludeList() {
70  		BeanLocator beanLocator = (BeanLocator) m_appContext
71  				.getBean("locateClassesOfSpecificTypesPlusExcludeList");
72  		Map beans = beanLocator.getBeans();
73  		String[] expectedBeans = new String[] {"fourthB", "secondC", "thirdC",
74  			"thirdW"};
75  		checkIfAllExpectedBeansAreInMap(beans, expectedBeans);
76  	}
77  
78  	/**
79  	 * This test locates classes of specific types and includes some beans.
80  	 */
81  	@Test
82  	public void testLocateClassesOfSpecificTypesPlusIncludeList() {
83  		BeanLocator beanLocator = (BeanLocator) m_appContext
84  				.getBean("locateClassesOfSpecificTypesPlusIncludeList");
85  		Map beans = beanLocator.getBeans();
86  		String[] expectedBeans = new String[] {"classC", "secondC", "thirdC",
87  			"thirdW"};
88  		checkIfAllExpectedBeansAreInMap(beans, expectedBeans);
89  	}
90  
91  	/**
92  	 * This test locates classes of specific types and includes and excludes
93  	 * some beans.
94  	 */
95  	@Test
96  	public void testLocateClassesOfSpecificTypesPlusIncludeAndExcludeList() {
97  		BeanLocator beanLocator = (BeanLocator) m_appContext.getBean(
98  				"locateClassesOfSpecificTypesPlusIncludeAndExcludeList");
99  		Map beans = beanLocator.getBeans();
100 		String[] expectedBeans = new String[] {"classB2", "classB3", "classC",
101 			"secondC", "thirdC", "firstV", "classW2"};
102 
103 		checkIfAllExpectedBeansAreInMap(beans, expectedBeans);
104 	}
105 
106 	/**
107 	 * This method checks if a map contains the right number of beans and if the
108 	 * expected beans are present in the given map.
109 	 *
110 	 * @param beans
111 	 *            Are the beans found by <code>BeanLocator</code>
112 	 * @param expectedBeans
113 	 *            Are names of the expected beans.
114 	 */
115 	private void checkIfAllExpectedBeansAreInMap(Map beans,
116 			String[] expectedBeans) {
117 		int expectedNumberOfBeans = (expectedBeans != null
118 				? expectedBeans.length : 0);
119 		assertEquals("There is not the expected number of beans.",
120 				expectedNumberOfBeans, beans.size());
121 
122 		for (int i = 0; i < expectedNumberOfBeans; i++) {
123 			String beanName = expectedBeans[i];
124 			assertTrue("Bean with name '" + beanName
125 					+ "' is not present in map.", beans.containsKey(beanName));
126 		}
127 	}
128 }