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