View Javadoc

1   package com.atlassian.plugin.servlet;
2   
3   import junit.framework.TestCase;
4   
5   import javax.servlet.http.HttpSession;
6   import javax.servlet.ServletContext;
7   
8   import java.util.Enumeration;
9   
10  import com.atlassian.plugin.servlet.util.ClassLoaderStack;
11  
12  /**
13   * @since 2.3.9
14   */
15  public class TestPluginHttpSessionWrapper extends TestCase
16  {
17      public void testGetAttribute() throws Exception
18      {
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          ClassLoaderStack.push(pluginClassLoader);
30          try
31          {
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              assertSame(pluginClassLoader, Thread.currentThread().getContextClassLoader());
37          }
38          finally
39          {
40              ClassLoaderStack.pop();
41          }
42      }
43  
44      private class MockSession implements HttpSession
45      {
46          private final ClassLoader expectedClassLoader;
47  
48          public MockSession(final ClassLoader expectedClassLoader)
49          {
50              this.expectedClassLoader = expectedClassLoader;
51          }
52  
53          public long getCreationTime()
54          {
55              return 0;
56          }
57  
58          public String getId()
59          {
60              return null;
61          }
62  
63          public long getLastAccessedTime()
64          {
65              return 0;
66          }
67  
68          public ServletContext getServletContext()
69          {
70              return null;
71          }
72  
73          public void setMaxInactiveInterval(final int interval)
74          {
75          }
76  
77          public int getMaxInactiveInterval()
78          {
79              return 0;
80          }
81  
82          @SuppressWarnings ({ "deprecation" })
83          public javax.servlet.http.HttpSessionContext getSessionContext()
84          {
85              return null;
86          }
87  
88          public Object getAttribute(final String name)
89          {
90              // We just care that the context ClassLoader is correct
91              assertSame(expectedClassLoader, Thread.currentThread().getContextClassLoader());
92  
93              return null;
94          }
95  
96          public Object getValue(final String name)
97          {
98              return null;
99          }
100 
101         public Enumeration getAttributeNames()
102         {
103             return null;
104         }
105 
106         public String[] getValueNames()
107         {
108             return new String[0];
109         }
110 
111         public void setAttribute(final String name, final Object value)
112         {
113         }
114 
115         public void putValue(final String name, final Object value)
116         {
117         }
118 
119         public void removeAttribute(final String name)
120         {
121         }
122 
123         public void removeValue(final String name)
124         {
125         }
126 
127         public void invalidate()
128         {
129         }
130 
131         public boolean isNew()
132         {
133             return false;
134         }
135     }
136 }