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.aop;
19  
20  import static org.junit.Assert.assertEquals;
21  
22  import org.junit.Test;
23  import org.springframework.context.ApplicationContext;
24  
25  import ch.elca.el4j.core.context.ModuleApplicationContext;
26  
27  /**
28   * This class tests the {@link
29   * ch.elca.el4j.core.aop.ExclusiveBeanNameAutoProxyCreator}.
30   *
31   * @svnLink $Revision: 3875 $;$Date: 2009-08-04 14:35:53 +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/aop/ExclusiveBeanNameAutoProxyCreatorTest.java $
32   *
33   * @author Andreas Bur (ABU)
34   */
35  public class ExclusiveBeanNameAutoProxyCreatorTest {
36  
37  	/** String returned by proxied beans. */
38  	public static final String PROXIED = "proxied";
39  	
40  	/** Bean A. */
41  	private static final String BEAN_A = "beanA";
42  	
43  	/** Bean B. */
44  	private static final String BEAN_B = "beanB";
45  	
46  	/** Bean foo. */
47  	private static final String FOO = "foo";
48  	
49  	/** Bean bar. */
50  	private static final String BAR = "bar";
51  	
52  	/** Bean foobar. */
53  	private static final String FOOBAR = "foobar";
54  	
55  	/** Registered beans. */
56  	private static final String[] BEAN_NAMES = {
57  		BEAN_A, BEAN_B, FOO, BAR, FOOBAR
58  	};
59  	
60  	/** Configuration without any beans to exclude. */
61  	private static final String EMPTY_EXCLUSIVE_CONFIG
62  		= "classpath:scenarios/core/aop/emptyExclusiveList.xml";
63  	
64  	/** Configuration that exclude all beans. */
65  	private static final String EXCLUDE_ALL_CONFIG
66  		= "classpath:scenarios/core/aop/excludeAll.xml";
67  	
68  	/** Configuration that excludes only bean 'foobar'. */
69  	private static final String INCLUDE_ALL_BUT_FOOBAR_CONFIG
70  		= "classpath:scenarios/core/aop/includeAllButFoobar.xml";
71  	
72  	/** Configuration that includes all beans but those beginning with 'foo'. */
73  	private static final String INCLUDE_ALL_BUT_FOO_PREFIX_CONFIG
74  		= "classpath:scenarios/core/aop/includeAllButFooPrefix.xml";
75  	
76  	/** Configuration that includes all beans but those ending with 'bar'. */
77  	private static final String INCLUDE_ALL_BUT_BAR_SUFFIX_CONFIG
78  		= "classpath:scenarios/core/aop/includeAllButBarSuffix.xml";
79  	
80  	/**
81  	 * Configuration that includes all beans starting with 'bean' but those
82  	 * ending with 'a'.
83  	 */
84  	private static final String
85  	INCLUDE_ALL_STARTING_WITH_BEAN_BUT_SUFFIX_A_CONFIG
86  		= "classpath:scenarios/core/aop/"
87  		+ "includeAllStartingWithBeanButSuffixA.xml";
88  	
89  	/**
90  	 * Tests the configuration that doesn't exclude any beans.
91  	 */
92  	@Test
93  	public void testIncludesOnly() {
94  		ApplicationContext appCtx = createAppContext(EMPTY_EXCLUSIVE_CONFIG);
95  		
96  		for (int i = 0; i < BEAN_NAMES.length; i++) {
97  			assertAdvised(appCtx, BEAN_NAMES[i]);
98  		}
99  	}
100 
101 	/**
102 	 * Tehsts the configuration that excludes all beans.
103 	 */
104 	@Test
105 	public void testExcludeAll() {
106 		ApplicationContext appCtx = createAppContext(EXCLUDE_ALL_CONFIG);
107 		
108 		for (int i = 0; i < BEAN_NAMES.length; i++) {
109 			assertNotAdvised(appCtx, BEAN_NAMES[i]);
110 		}
111 	}
112 
113 	/**
114 	 * Tests the configuration that includes all beans but the one named
115 	 * 'foobar'.
116 	 */
117 	@Test
118 	public void testIncludeAllButFoobar() {
119 		ApplicationContext appCtx = createAppContext(
120 				INCLUDE_ALL_BUT_FOOBAR_CONFIG);
121 		
122 		assertAdvised(appCtx, BEAN_A);
123 		assertAdvised(appCtx, BEAN_B);
124 		assertAdvised(appCtx, FOO);
125 		assertAdvised(appCtx, BAR);
126 		assertNotAdvised(appCtx, FOOBAR);
127 	}
128 	
129 	/**
130 	 * Tests the configuration that includes all beans but those starting with
131 	 * 'foo'.
132 	 *
133 	 */
134 	@Test
135 	public void testIncludeAllButFooPrefix() {
136 		ApplicationContext appCtx = createAppContext(
137 				INCLUDE_ALL_BUT_FOO_PREFIX_CONFIG);
138 		
139 		assertAdvised(appCtx, BEAN_A);
140 		assertAdvised(appCtx, BEAN_B);
141 		assertNotAdvised(appCtx, FOO);
142 		assertAdvised(appCtx, BAR);
143 		assertNotAdvised(appCtx, FOOBAR);
144 	}
145 	
146 	/**
147 	 * Tests the configuration that includes all beans but those ending with
148 	 * 'bar'.
149 	 */
150 	@Test
151 	public void testIncludeAllButBarSuffix() {
152 		ApplicationContext appCtx = createAppContext(
153 				INCLUDE_ALL_BUT_BAR_SUFFIX_CONFIG);
154 		
155 		assertAdvised(appCtx, BEAN_A);
156 		assertAdvised(appCtx, BEAN_B);
157 		assertAdvised(appCtx, FOO);
158 		assertNotAdvised(appCtx, BAR);
159 		assertNotAdvised(appCtx, FOOBAR);
160 	}
161 
162 	/**
163 	 * Tests the configuration that includes all beans which start with
164 	 * 'bean' but don't end with 'a'.
165 	 */
166 	@Test
167 	public void testIncludeAllStartingWithBeanButSuffixA() {
168 		ApplicationContext appCtx = createAppContext(
169 				INCLUDE_ALL_STARTING_WITH_BEAN_BUT_SUFFIX_A_CONFIG);
170 		
171 		assertNotAdvised(appCtx, BEAN_A);
172 		assertAdvised(appCtx, BEAN_B);
173 		assertNotAdvised(appCtx, FOO);
174 		assertNotAdvised(appCtx, BAR);
175 		assertNotAdvised(appCtx, FOOBAR);
176 	}
177 
178 	/**
179 	 * Asserts that the bean with the given name is advised in the given
180 	 * application context.
181 	 *
182 	 * @param appCtx
183 	 *      The application context.
184 	 * @param beanName
185 	 *      The name of the bean which is asserted to be advised.
186 	 */
187 	protected void assertAdvised(ApplicationContext appCtx, String beanName) {
188 		Bean bean = (Bean) appCtx.getBean(beanName);
189 		assertEquals("Bean '" + beanName + "' has not been advised.",
190 				PROXIED, bean.getBeanName());
191 	}
192 	
193 	/**
194 	 * Asserts that the bean with the given name is not advised in the given
195 	 * application context.
196 	 *
197 	 * @param appCtx
198 	 *      The application context.
199 	 * @param beanName
200 	 *      The name of the bean which is asserted not to be advised.
201 	 */
202 	protected void assertNotAdvised(
203 			ApplicationContext appCtx, String beanName) {
204 		Bean bean = (Bean) appCtx.getBean(beanName);
205 		assertEquals("Bean '" + beanName + "' has not been advised.",
206 				beanName, bean.getBeanName());
207 	}
208 	
209 	/**
210 	 * Crates a new application context for the given configuration file.
211 	 *
212 	 * @param config
213 	 *      The configuration file's location.
214 	 *
215 	 * @return Returns a newly created application context.
216 	 */
217 	private ApplicationContext createAppContext(String config) {
218 		return new ModuleApplicationContext(config, false);
219 	}
220 }