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.util.metadata.annotations;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.fail;
22  
23  import org.junit.Test;
24  import org.springframework.beans.BeansException;
25  import org.springframework.context.ApplicationContext;
26  
27  import ch.elca.el4j.core.context.ModuleApplicationContext;
28  
29  // Checkstyle: MagicNumber off
30  
31  /**
32   * JUnit test for the GenericMetaDataAdvisor class in combination with
33   * Java 5 Annotations.
34   *
35   * Please be sure to compile the Commons Attributes before launching this test.
36   *
37   * @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/util/metadata/annotations/GenericAttributesTest.java $
38   *
39   * @author Raphael Boog (RBO)
40   */
41  public class GenericAttributesTest {
42  	/**
43  	 * Is the spring bean config location prefix for this test.
44  	 */
45  	public static final String SPRING_BEAN_CONFIG_LOCATION
46  		= "classpath:scenarios/util/metadata/annotations/";
47  	
48  	/**
49  	 * This test loads an application context with an AutoProxy, a
50  	 * GenericAttributeAdvisor and a class with declared attributes. The
51  	 * interceptor will be injected via contructor argument to the advisor. It
52  	 * calculates the result of a calculation which is only correct if the
53  	 * interception worked correctly.
54  	 */
55  	@Test
56  	public void testInterceptorInjectedViaConstructor() {
57  
58  		ApplicationContext ac = new ModuleApplicationContext(
59  			SPRING_BEAN_CONFIG_LOCATION + "beansViaConstructor.xml", false);
60  
61  		Foo foo = (Foo) ac.getBean("foo");
62  
63  		// Although we multiply the FooImpl.BASE number with 0, the result is 15
64  		// since the method interceptor multiplies the FooImpl.BASE number with
65  		// 5 instead of 0.
66  
67  		assertEquals("The result should be " + FooImpl.BASE * 5 + ".",
68  			FooImpl.BASE * 5, foo.test(0));
69  
70  	}
71  
72  	/**
73  	 * This test loads an application context with an AutoProxy, a
74  	 * GenericAttributeAdvisor and a class with declared attributes. The
75  	 * interceptor will be injected via setter method to the advisor. It
76  	 * calculates the result of a calculation which is only correct if the
77  	 * interception worked correctly.
78  	 */
79  	@Test
80  	public void testInterceptorInjectedViaSetter() {
81  
82  		ApplicationContext ac = new ModuleApplicationContext(
83  			SPRING_BEAN_CONFIG_LOCATION + "beansViaSetter.xml", false);
84  
85  		Foo foo = (Foo) ac.getBean("foo");
86  
87  		// Although we multiply the FooImpl.BASE number with 0, the result is 15
88  		// since the method interceptor multiplies the FooImpl.BASE number with
89  		// 5 instead of 0.
90  		assertEquals("The result should be " + FooImpl.BASE * 5 + ".",
91  			FooImpl.BASE * 5, foo.test(0));
92  	}
93  
94  	/**
95  	 * This test loads an application context with an AutoProxy, a
96  	 * GenericAttributeAdvisor and a class with declared attributes.
97  	 * Additionally, the AttributeSource and the Attributes implementation are
98  	 * injected via configuration file and not set automatically like in the
99  	 * other tests. It calculates the result of a calculation which is only
100 	 * correct if the interception worked correctly.
101 	 */
102 	@Test
103 	public void testConfiguredAttributeSource() {
104 
105 		ApplicationContext ac = new ModuleApplicationContext(
106 			SPRING_BEAN_CONFIG_LOCATION
107 				+ "beansWithConfiguredMetaDataSource.xml", false);
108 
109 		Foo foo = (Foo) ac.getBean("foo");
110 
111 		// Although we multiply the FooImpl.BASE number with 0, the result is 15
112 		// since the method interceptor multiplies the FooImpl.BASE number with
113 		// 5 instead of 0.
114 		assertEquals("The result should be " + FooImpl.BASE * 5 + ".",
115 			FooImpl.BASE * 5, foo.test(0));
116 	}
117 
118 	/**
119 	 * This test loads an application context with a class having declared
120 	 * attributes, an AutoProxy and two GenericAttributeAdvisor with each
121 	 * defined a different ExampleAttribute and a different Interceptor. The
122 	 * order of the advisors can be adjusted by setting the property "order" in
123 	 * the configuration file. The advisor with the lower number will be invoked
124 	 * first. Therefore, in our case, "exampleInterceptorTwo" has to be the real
125 	 * interceptor for the target method.
126 	 */
127 	@Test
128 	public void testTwoAdvisors() {
129 
130 		ApplicationContext ac = new ModuleApplicationContext(
131 			SPRING_BEAN_CONFIG_LOCATION + "beansTwoAdvisors.xml", false);
132 
133 		Foo foo = (Foo) ac.getBean("foo");
134 
135 		// Although we multiply the FooImpl.BASE number with 0, the result is
136 		// 270 since the method interceptor multiplies the FooImpl.BASE number
137 		// with 90 instead of 0.
138 		assertEquals("The result should be " + FooImpl.BASE * 9
139 			* ExampleAnnotationTwo.CONSTANT_FACTOR + ".", FooImpl.BASE * 9
140 			* ExampleAnnotationTwo.CONSTANT_FACTOR, foo.test(0));
141 
142 	}
143 
144 	/**
145 	 * This test loads an application context with an AutoProxy, one
146 	 * GenericAttributeAdvisor, a class with declared attributes, but no
147 	 * interceptor are defined. This test checks whether an exception is thrown.
148 	 */
149 	@Test
150 	public void testAdvisorWithNoInterceptor() {
151 		// Checkstyle: EmptyBlock off
152 		try {
153 			new ModuleApplicationContext(
154 				SPRING_BEAN_CONFIG_LOCATION + "beansNoInterceptor.xml", false);
155 			fail("A BaseException should have been thrown.");
156 		} catch (BeansException e) {
157 			// Expected behaviour
158 		}
159 		// Checkstyle: EmptyBlock on
160 	}
161 
162 	/**
163 	 * This test loads an application context with an AutoProxy, one
164 	 * GenericAttributeAdvisor, a class with declared attributes, but no
165 	 * intercepting Attributes are defined. This test checks whether an
166 	 * exception is thrown.
167 	 */
168 	@Test
169 	public void testAdvisorWithNoInterceptingAttributes() {
170 		// Checkstyle: EmptyBlock off
171 		try {
172 			new ModuleApplicationContext(
173 				SPRING_BEAN_CONFIG_LOCATION
174 					+ "beansNoInterceptingMetaData.xml", false);
175 			fail("A BaseRTException should have been thrown");
176 		} catch (Exception e) {
177 			// Expected behaviour
178 		}
179 		// Checkstyle: EmptyBlock on
180 	}
181 	
182 	/**
183 	 * This test does the same as method
184 	 * <code>testInterceptorInjectedViaConstructor</code> but it invokes the
185 	 * method with an inner class parameter. This should test if methods which
186 	 * has inner classes as parameter as also intercepted.
187 	 */
188 	@Test
189 	public void testInterceptorInjectedViaConstructorInnerClassAsParameter() {
190 
191 		ApplicationContext ac = new ModuleApplicationContext(
192 			SPRING_BEAN_CONFIG_LOCATION + "beansViaConstructor.xml", false);
193 
194 		Foo foo = (Foo) ac.getBean("foo");
195 
196 		FooImpl.Bar fooBar = new FooImpl.Bar();
197 		
198 		// Although we multiply the FooImpl.BASE number with 0, the result is 15
199 		// since the method interceptor multiplies the FooImpl.BASE number with
200 		// 5 instead of 0.
201 
202 		assertEquals("The result should be " + FooImpl.BASE * 5 + ".",
203 			FooImpl.BASE * 5, foo.test(0, fooBar));
204 
205 	}
206 }
207 //Checkstyle: MagicNumber on