1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.guice;
20
21 import com.google.inject.*;
22 import com.google.inject.name.Names;
23 import com.google.inject.spi.Message;
24 import com.google.inject.spi.TypeEncounter;
25 import org.apache.shiro.guice.aop.ShiroAopModule;
26 import org.apache.shiro.guice.web.ShiroWebModule;
27 import org.apache.shiro.SecurityUtils;
28 import org.apache.shiro.aop.DefaultAnnotationResolver;
29 import org.apache.shiro.crypto.BlowfishCipherService;
30 import org.easymock.Capture;
31 import org.easymock.IMocksControl;
32 import org.junit.Test;
33
34 import java.util.Collections;
35 import java.util.Map;
36
37 import static org.easymock.EasyMock.*;
38 import static org.junit.Assert.*;
39
40
41
42
43
44
45
46
47
48
49
50 public class BeanTypeListenerTest {
51 @Test
52 public void testUnmatchedPackage() throws Exception {
53 assertFalse(BeanTypeListener.MATCHER.matches(TypeLiteral.get(GuiceEnvironment.class)));
54 assertFalse(BeanTypeListener.MATCHER.matches(TypeLiteral.get(ShiroWebModule.class)));
55 assertFalse(BeanTypeListener.MATCHER.matches(TypeLiteral.get(ShiroAopModule.class)));
56 }
57
58 @Test
59 public void testMatchedPackage() throws Exception {
60 assertTrue(BeanTypeListener.MATCHER.matches(TypeLiteral.get(SecurityUtils.class)));
61 assertTrue(BeanTypeListener.MATCHER.matches(TypeLiteral.get(DefaultAnnotationResolver.class)));
62 assertTrue(BeanTypeListener.MATCHER.matches(TypeLiteral.get(BlowfishCipherService.class)));
63 }
64
65 @Test
66 public void testPropertySetting() throws Exception {
67 IMocksControl control = createControl();
68 TypeEncounter<SomeInjectableBean> encounter = control.createMock(TypeEncounter.class);
69
70 Provider<Injector> injectorProvider = control.createMock(Provider.class);
71 Injector injector = control.createMock(Injector.class);
72
73 expect(encounter.getProvider(Injector.class)).andReturn(injectorProvider);
74
75 expect(injectorProvider.get()).andReturn(injector).anyTimes();
76
77 Capture<MembersInjector<SomeInjectableBean>> capture = new Capture<MembersInjector<SomeInjectableBean>>();
78 encounter.register(and(anyObject(MembersInjector.class), capture(capture)));
79
80 SecurityManager securityManager = control.createMock(SecurityManager.class);
81 String property = "myPropertyValue";
82
83 expect(injector.getInstance(Key.get(SecurityManager.class))).andReturn(securityManager);
84 expect(injector.getInstance(Key.get(String.class, Names.named("shiro.myProperty")))).andReturn(property);
85 expect(injector.getInstance(Key.get(String.class, Names.named("shiro.unavailableProperty"))))
86 .andThrow(new ConfigurationException(Collections.singleton(new Message("Not Available!"))));
87 expect((Map)injector.getInstance(BeanTypeListener.MAP_KEY)).andReturn(Collections.EMPTY_MAP).anyTimes();
88
89 control.replay();
90
91 BeanTypeListener underTest = new BeanTypeListener();
92
93 underTest.hear(TypeLiteral.get(SomeInjectableBean.class), encounter);
94
95 SomeInjectableBean bean = new SomeInjectableBean();
96
97 capture.getValue().injectMembers(bean);
98
99 assertSame(securityManager, bean.securityManager);
100 assertSame(property, bean.myProperty);
101 assertNull(bean.unavailableProperty);
102
103 control.verify();
104 }
105
106 public static class SomeInjectableBean {
107 private SecurityManager securityManager;
108 private String myProperty;
109 private String unavailableProperty;
110
111 public void setSecurityManager(SecurityManager securityManager) {
112
113 this.securityManager = securityManager;
114 }
115
116 public void setMyProperty(String myProperty) {
117
118 this.myProperty = myProperty;
119 }
120
121 public void setUnavailableProperty(String unavailableProperty) {
122
123 this.unavailableProperty = unavailableProperty;
124 }
125 }
126 }