View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Test Cases::
42   * Test package matching
43   * injects on classes in shiro package and sub packages
44   * excludes classes in shiro-guice package and sub packages
45   * Test that properties are set properly
46   * ensure optional
47   * ensure property names are correct
48   * ensure "named" properties require a name, and unnamed do not
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 }