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.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
58 assertSame(retuned, underTest.scope(key, mockProvider).get());
59
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 }