View Javadoc
1   package com.atlassian.plugin.servlet;
2   
3   import com.atlassian.plugin.servlet.util.ClassLoaderStack;
4   import junit.framework.TestCase;
5   
6   import javax.servlet.ServletContext;
7   import javax.servlet.http.HttpSession;
8   import java.util.Enumeration;
9   
10  /**
11   * @since 2.3.9
12   */
13  public class TestPluginHttpSessionWrapper extends TestCase {
14      public void testGetAttribute() throws Exception {
15          // Mock the Session
16          MockSession mockSession = new MockSession(Thread.currentThread().getContextClassLoader());
17  
18          PluginHttpSessionWrapper sessionWrapper = new PluginHttpSessionWrapper(mockSession);
19  
20          // First try getSttribute() without a new ClassLoader
21          sessionWrapper.getAttribute("foo");
22  
23          // Now put a different ClassLoader in the ContextClassLoader.
24          ClassLoader pluginClassLoader = new ClassLoader() {
25          };
26          ClassLoaderStack.push(pluginClassLoader);
27          try {
28              // The MockSession will fail if called with the wrong ClassLoader
29              sessionWrapper.getAttribute("foo");
30              // PluginHttpSessionWrapper should have temporarily popped the ContextClassLoader, but now should have pushed
31              // our MockClassLoader back
32              assertSame(pluginClassLoader, Thread.currentThread().getContextClassLoader());
33          } finally {
34              ClassLoaderStack.pop();
35          }
36      }
37  
38      private class MockSession implements HttpSession {
39          private final ClassLoader expectedClassLoader;
40  
41          public MockSession(final ClassLoader expectedClassLoader) {
42              this.expectedClassLoader = expectedClassLoader;
43          }
44  
45          public long getCreationTime() {
46              return 0;
47          }
48  
49          public String getId() {
50              return null;
51          }
52  
53          public long getLastAccessedTime() {
54              return 0;
55          }
56  
57          public ServletContext getServletContext() {
58              return null;
59          }
60  
61          public void setMaxInactiveInterval(final int interval) {
62          }
63  
64          public int getMaxInactiveInterval() {
65              return 0;
66          }
67  
68          @SuppressWarnings({"deprecation"})
69          public javax.servlet.http.HttpSessionContext getSessionContext() {
70              return null;
71          }
72  
73          public Object getAttribute(final String name) {
74              // We just care that the context ClassLoader is correct
75              assertSame(expectedClassLoader, Thread.currentThread().getContextClassLoader());
76  
77              return null;
78          }
79  
80          public Object getValue(final String name) {
81              return null;
82          }
83  
84          public Enumeration getAttributeNames() {
85              return null;
86          }
87  
88          public String[] getValueNames() {
89              return new String[0];
90          }
91  
92          public void setAttribute(final String name, final Object value) {
93          }
94  
95          public void putValue(final String name, final Object value) {
96          }
97  
98          public void removeAttribute(final String name) {
99          }
100 
101         public void removeValue(final String name) {
102         }
103 
104         public void invalidate() {
105         }
106 
107         public boolean isNew() {
108             return false;
109         }
110     }
111 }