01: package org.acegisecurity.config;
02:
03: import java.beans.FeatureDescriptor;
04: import java.lang.reflect.Field;
05: import java.util.Arrays;
06:
07: import javax.servlet.Filter;
08:
09: import junit.framework.TestCase;
10:
11: import org.acegisecurity.ui.AccessDeniedHandler;
12: import org.acegisecurity.ui.AccessDeniedHandlerImpl;
13: import org.acegisecurity.ui.ExceptionTranslationFilter;
14: import org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint;
15: import org.springframework.beans.PropertyValue;
16: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
17: import org.springframework.beans.factory.config.RuntimeBeanReference;
18: import org.springframework.beans.factory.support.RootBeanDefinition;
19: import org.springframework.context.ApplicationContext;
20: import org.springframework.context.support.ClassPathXmlApplicationContext;
21: import org.springframework.util.ReflectionUtils;
22:
23: public class ExceptionTranslationParserTests extends TestCase {
24:
25: public void testParsingBeanReferences() {
26: ApplicationContext context = new ClassPathXmlApplicationContext(
27: "org/acegisecurity/config/exception-translation-beanref.xml");
28: ConfigurableListableBeanFactory factory = (ConfigurableListableBeanFactory) context
29: .getAutowireCapableBeanFactory();
30: String[] beanNames = factory
31: .getBeanNamesForType(ExceptionTranslationFilter.class);
32: assertEquals(1, beanNames.length);
33: RootBeanDefinition def = (RootBeanDefinition) factory
34: .getBeanDefinition(beanNames[0]);
35: assertEquals(ExceptionTranslationFilter.class.getName(), def
36: .getBeanClassName());
37: // check collaborators
38: PropertyValue accessDeniedHandler = def.getPropertyValues()
39: .getPropertyValue("accessDeniedHandler");
40: assertNotNull(accessDeniedHandler);
41: assertEquals(accessDeniedHandler.getValue(),
42: new RuntimeBeanReference("theBeanToUse"));
43: PropertyValue entryPoint = def.getPropertyValues()
44: .getPropertyValue("authenticationEntryPoint");
45: assertNotNull(entryPoint);
46: assertEquals(entryPoint.getValue(), new RuntimeBeanReference(
47: "authenticationProcessingFilterEntryPoint"));
48: }
49:
50: public void testRuntimeBeanDependencies() {
51: ApplicationContext context = new ClassPathXmlApplicationContext(
52: "org/acegisecurity/config/exception-translation-beanref.xml");
53: ExceptionTranslationFilter filter = (ExceptionTranslationFilter) context
54: .getBean("exceptionTranslationFilter");
55: AuthenticationProcessingFilterEntryPoint entryPoint = (AuthenticationProcessingFilterEntryPoint) filter
56: .getAuthenticationEntryPoint();
57: assertEquals("/acegilogin.jsp", entryPoint.getLoginFormUrl());
58: assertFalse(entryPoint.getForceHttps());
59:
60: }
61:
62: public void testAutoDetectionOfDefaultDependencies()
63: throws Exception {
64: ApplicationContext context = new ClassPathXmlApplicationContext(
65: "org/acegisecurity/config/exception-translation-autodetect-handler.xml");
66: ExceptionTranslationFilter filter = (ExceptionTranslationFilter) context
67: .getBean("exceptionTranslationFilter");
68: Field field = makeAccessibleAndGetFieldByName(filter.getClass()
69: .getDeclaredFields(), "accessDeniedHandler");
70: assertTrue(field.get(filter) instanceof AccessDeniedHandler);
71: AccessDeniedHandlerImpl accessDeniedHandler = (AccessDeniedHandlerImpl) field
72: .get(filter);
73: Field f = makeAccessibleAndGetFieldByName(accessDeniedHandler
74: .getClass().getDeclaredFields(), "errorPage");
75: assertEquals("errorPage", f.getName());
76: String value = (String) f.get(accessDeniedHandler);
77: assertEquals("/accessDenied.jsp", value);
78: }
79:
80: private Field makeAccessibleAndGetFieldByName(Field[] fields,
81: String name) {
82: Field field = null;
83: for (int i = 0, n = fields.length; i < n; i++) {
84: ReflectionUtils.makeAccessible(fields[i]);
85: if (fields[i].getName().equals(name)) {
86: return fields[i];
87: }
88: }
89: return field;
90: }
91:
92: }
|