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.Key;
22  import com.google.inject.OutOfScopeException;
23  import com.google.inject.Provider;
24  import org.apache.shiro.session.Session;
25  import org.apache.shiro.subject.Subject;
26  import org.apache.shiro.util.ThreadContext;
27  import org.junit.Test;
28  
29  import static org.easymock.EasyMock.*;
30  import static org.junit.Assert.assertSame;
31  
32  public class ShiroSessionScopeTest {
33      @Test
34      public void testScope() throws Exception {
35          Subject subject = createMock(Subject.class);
36          try {
37              ThreadContext.bind(subject);
38  
39              final Key<SomeClass> key = Key.get(SomeClass.class);
40              Provider<SomeClass> mockProvider = createMock(Provider.class);
41              Session session = createMock(Session.class);
42  
43              SomeClass retuned = new SomeClass();
44  
45              expect(subject.getSession()).andReturn(session);
46              expect(session.getAttribute(key)).andReturn(null);
47              expect(mockProvider.get()).andReturn(retuned);
48  
49              expect(subject.getSession()).andReturn(session);
50              expect(session.getAttribute(key)).andReturn(retuned);
51  
52  
53              replay(subject, mockProvider, session);
54  
55              ShiroSessionScope underTest = new ShiroSessionScope();
56  
57              // first time the session doesn't contain it, we expect the provider to be invoked
58              assertSame(retuned, underTest.scope(key, mockProvider).get());
59              // second time the session does contain it, we expect the provider to not be invoked
60              assertSame(retuned, underTest.scope(key, mockProvider).get());
61  
62              verify(subject, mockProvider, session);
63          } finally {
64              ThreadContext.unbindSubject();
65          }
66  
67      }
68  
69      @Test(expected = OutOfScopeException.class)
70      public void testOutOfScope() throws Exception {
71          ShiroSessionScope underTest = new ShiroSessionScope();
72  
73          Provider<SomeClass> mockProvider = createMock(Provider.class);
74  
75          replay(mockProvider);
76  
77          underTest.scope(Key.get(SomeClass.class), mockProvider).get();
78      }
79  
80  
81      static class SomeClass {
82  
83      }
84  }