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.context;
19  
20  import static org.junit.Assert.fail;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.junit.Test;
25  import org.springframework.beans.factory.BeanDefinitionStoreException;
26  import org.springframework.beans.factory.NoSuchBeanDefinitionException;
27  
28  import ch.elca.el4j.core.context.ModuleApplicationContext;
29  
30  
31  /**
32   * JUnit Test Class for the ModuleApplicationContext.
33   *
34   * @svnLink $Revision: 3881 $;$Date: 2009-08-04 15:22:05 +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/context/ModuleApplicationContextTest.java $
35   *
36   * @author Raphael Boog (RBO)
37   */
38  public class ModuleApplicationContextTest {
39  
40  	/** The static logger. */
41  	private static Logger s_logger
42  		= LoggerFactory.getLogger(ModuleApplicationContextTest.class);
43  	
44  	/** Test configuration. */
45  	String m_fileName1 = "classpath:scenarios/core/context/beans1.xml";
46  
47  	/** Test configuration. */
48  	String m_fileName2 = "classpath:scenarios/core/context/beans2.xml";
49  
50  	/** Test configuration. */
51  	String m_fileName3 = "classpath:scenarios/core/context/beans3.xml";
52  
53  	/** Test configuration. */
54  	String m_allFileNamesInClasspath = "classpath:scenarios/core/context/*.xml";
55  
56  	/** Test configuration. */
57  	String m_allFileNamesInBothClasspaths
58  		= "classpath:scenarios/core/context/**/*.xml";
59  
60  	/** Mandatory configurations. */
61  	String m_mandatoryFiles = "classpath*:mandatory/*.xml";
62  
63  	/**
64  	 * This test takes a null String as inclusive configuration file and checks
65  	 * if a NullPointerException is thrown.
66  	 *
67  	 */
68  	@Test
69  	public void testInclusiveFileNameNull() {
70  		// Checkstyle: EmptyBlock off
71  		try {
72  			new ModuleApplicationContext((String) null, false);
73  			fail("NullPointerException should have been thrown.");
74  		} catch (NullPointerException e) {
75  		}
76  		// Checkstyle: EmptyBlock on
77  	}
78  
79  	/**
80  	 * This test takes one inclusive configuration file with one bean defined,
81  	 * loads it into one ApplicationContext with one bean and checks if the bean
82  	 * was loaded.
83  	 */
84  	@Test
85  	public void testOneInclusiveFileName() {
86  		ModuleApplicationContext tac
87  			= new ModuleApplicationContext(m_fileName1, false);
88  		tac.getBean("Bean1");
89  	}
90  
91  	/**
92  	 * This test takes two inclusive configuration files with one bean defined
93  	 * each, loads them into one ApplicationContext and checks if the two beans
94  	 * were loaded.
95  	 */
96  	@Test
97  	public void testTwoInclusiveFileNames() {
98  		ModuleApplicationContext tac = new ModuleApplicationContext(
99  				new String[] {m_fileName1, m_fileName2 }, false);
100 
101 		tac.getBean("Bean1");
102 		tac.getBean("Bean2");
103 	}
104 
105 	/**
106 	 * This test takes one inclusive configuration file, a null String as
107 	 * exclusive configuration file and checks if a NullPointerException is
108 	 * thrown.
109 	 */
110 	@Test
111 	public void testOneInclusiveFileNameExclusiveFileNamesIsNull() {
112 		// Checkstyle: EmptyBlock off
113 		try {
114 			new ModuleApplicationContext(m_fileName1, (String) null, false);
115 			fail("NullPointerException should have been thrown.");
116 		} catch (NullPointerException e) {
117 		}
118 		// Checkstyle: EmptyBlock on
119 	}
120 
121 	/**
122 	 * This test takes one inclusive configuration file, an empty String as
123 	 * exclusive configuration file, loads them into one ApplicationContext and
124 	 * checks if the defined bean was loaded.
125 	 */
126 	@Test
127 	public void testOneInclusiveFileNameExclusiveFileNamesIsEmpty() {
128 		ModuleApplicationContext tac = new ModuleApplicationContext(m_fileName1,
129 				"", false);
130 		tac.getBean("Bean1");
131 	}
132 
133 	/**
134 	 * This test takes an inclusive configuration files with one bean defined,
135 	 * another exclusive configuration file, loads them into one
136 	 * ApplicationContext and checks if the bean was loaded and no exception is
137 	 * thrown since the exclusive configuration file defines a bean which is not
138 	 * in an inclusive configuration file.
139 	 */
140 	@Test
141 	public void testOneInclusiveFileNamesAnotherExclusiveFileName() {
142 		ModuleApplicationContext tac
143 			= new ModuleApplicationContext(m_fileName1, m_fileName2, false);
144 		tac.getBean("Bean1");
145 		// Checkstyle: EmptyBlock off
146 		try {
147 			tac.getBean("Bean2");
148 			fail("NoSuchBeanDefinitionException should have been thrown.");
149 		} catch (NoSuchBeanDefinitionException e) {
150 		}
151 		// Checkstyle: EmptyBlock on
152 	}
153 
154 	/**
155 	 * This test takes two inclusive configuration files, one of them as
156 	 * exclusive configuration file, loads them into one ApplicationContext and
157 	 * checks if one bean was loaded and the other not.
158 	 */
159 	@Test
160 	public void testTwoInclusiveFileNamesOneExclusiveFileName() {
161 		ModuleApplicationContext tac = new ModuleApplicationContext(
162 				new String[] {m_fileName1, m_fileName2 }, m_fileName2, false);
163 		tac.getBean("Bean1");
164 		// Checkstyle: EmptyBlock off
165 		try {
166 			tac.getBean("Bean2");
167 			fail("NoSuchBeanDefinitionException should have been thrown.");
168 		} catch (NoSuchBeanDefinitionException e) {
169 		}
170 		// Checkstyle: EmptyBlock on
171 	}
172 
173 	/**
174 	 * This test takes all configuration files in the classpath via '*.xml'
175 	 * ant-style pattern, two exclusive configuration files in this classpath,
176 	 * loads them into one ApplicationContext and checks if the correct bean was
177 	 * loaded and the three other beans were not loaded.
178 	 */
179 	@Test
180 	public void testAllInClasspathFileNamesMinusTwoExclusiveFileNames() {
181 		ModuleApplicationContext tac = new ModuleApplicationContext(
182 			m_allFileNamesInClasspath,
183 			new String[] {m_fileName1, m_fileName3}, false);
184 		// Checkstyle: EmptyBlock off
185 		try {
186 			tac.getBean("Bean1");
187 			fail("NoSuchBeanDefinitionException should have been thrown.");
188 		} catch (NoSuchBeanDefinitionException e) {
189 		}
190 
191 		tac.getBean("Bean2");
192 
193 		try {
194 			tac.getBean("Bean3");
195 			fail("NoSuchBeanDefinitionException should have been thrown.");
196 		} catch (NoSuchBeanDefinitionException e) {
197 		}
198 
199 		try {
200 			tac.getBean("Bean4");
201 			fail("NoSuchBeanDefinitionException should have been thrown.");
202 		} catch (NoSuchBeanDefinitionException e) {
203 		}
204 		// Checkstyle: EmptyBlock on
205 	}
206 
207 	/**
208 	 * This test takes all configuration files in the classpath via '*.xml'
209 	 * ant-style pattern, all configuration files via '*.xml' ant-style pattern
210 	 * in the classpath as exclusive configuration files, loads them into one
211 	 * ApplicationContext and checks if no bean was loaded.
212 	 */
213 	@Test
214 	public void testAllInClasspathFileNamesMinusAllInClasspathFileNames() {
215 		ModuleApplicationContext tac = new ModuleApplicationContext(
216 				m_allFileNamesInClasspath, m_allFileNamesInClasspath, false);
217 		// Checkstyle: EmptyBlock off
218 		try {
219 			tac.getBean("Bean1");
220 			fail("NoSuchBeanDefinitionException should have been thrown.");
221 		} catch (NoSuchBeanDefinitionException e) {
222 		}
223 
224 		try {
225 			tac.getBean("Bean2");
226 			fail("NoSuchBeanDefinitionException should have been thrown.");
227 		} catch (NoSuchBeanDefinitionException e) {
228 		}
229 
230 		try {
231 			tac.getBean("Bean3");
232 			fail("NoSuchBeanDefinitionException should have been thrown.");
233 		} catch (NoSuchBeanDefinitionException e) {
234 		}
235 
236 		try {
237 			tac.getBean("Bean4");
238 			fail("NoSuchBeanDefinitionException should have been thrown.");
239 		} catch (NoSuchBeanDefinitionException e) {
240 		}
241 		// Checkstyle: EmptyBlock on
242 	}
243 
244 	/**
245 	 * This test takes all configuration files in both classpaths via '*\*.xml'
246 	 * ant-style pattern, all configuration files via '*.xml' ant-style pattern
247 	 * in one of the filepaths as exclusive configuration files, loads them into
248 	 * one ApplicationContext and checks if the correct bean was loaded.
249 	 */
250 	@Test
251 	public void testAllInBothClasspathsMinusAllInFilepath1() {
252 		ModuleApplicationContext tac = new ModuleApplicationContext(
253 			m_allFileNamesInBothClasspaths, m_allFileNamesInClasspath, false);
254 		// Checkstyle: EmptyBlock off
255 		try {
256 			tac.getBean("Bean1");
257 			fail("NoSuchBeanDefinitionExceptiona should have been thrown.");
258 		} catch (NoSuchBeanDefinitionException e) {
259 		}
260 
261 		try {
262 			tac.getBean("Bean2");
263 			fail("NoSuchBeanDefinitionExceptionb should have been thrown.");
264 		} catch (NoSuchBeanDefinitionException e) {
265 		}
266 
267 		try {
268 			tac.getBean("Bean3");
269 			fail("NoSuchBeanDefinitionExceptionc should have been thrown.");
270 		} catch (NoSuchBeanDefinitionException e) {
271 		}
272 
273 		try {
274 			tac.getBean("Bean4");
275 			fail("NoSuchBeanDefinitionExceptiond should have been thrown.");
276 		} catch (NoSuchBeanDefinitionException e) {
277 		}
278 
279 		tac.getBean("Bean5");
280 		// Checkstyle: EmptyBlock on
281 	}
282 
283 	/**
284 	 * This test takes an existing file and a non existing file, tries to load
285 	 * them into one ApplicationContext and checks if the correct bean was
286 	 * loaded and no exception was thrown. However, a logger warning should have
287 	 * been displayed.
288 	 */
289 	@Test
290 	public void testOneExistingInclusiveFileNamePlusOneNonExistingFileName() {
291 		ModuleApplicationContext tac = new ModuleApplicationContext(
292 			new String[] {m_fileName1, "non existing file path"}, false);
293 		tac.getBean("Bean1");
294 	}
295 
296 	/**
297 	 * This test takes twice the same configuration file, sets
298 	 * allowBeanOverridingDefinition to false and checks if a
299 	 * BeanDefinitionStoreException is thrown.
300 	 */
301 	@Test
302 	public void testBeanDefinitionOverridingIsFalse() {
303 		// Checkstyle: EmptyBlock off
304 		try {
305 			new ModuleApplicationContext(
306 				new String[] {m_fileName1, m_fileName1}, false);
307 			fail("BeanDefinitionStoreException should have been thrown.");
308 		} catch (BeanDefinitionStoreException e) {
309 		}
310 		// Checkstyle: EmptyBlock on
311 	}
312 	
313 	/**
314 	 * This test has to be verified by the user. Between the two warn logger
315 	 * outputs, no other warn logger should appear. Especially not the one
316 	 * saying that 'classpath*:mandatory/*.xml' was not loaded.
317 	 */
318 	@Test
319 	public void testMandatoryWarning() {
320 		s_logger.warn("There should be no message with level at least 'WARN'"
321 				+ "between THIS");
322 		new ModuleApplicationContext(
323 					new String[] {m_mandatoryFiles, m_fileName1}, false);
324 		s_logger.warn("and THIS message.");
325 	}
326 }