View Javadoc

1   package com.atlassian.plugin.servlet;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   import java.util.concurrent.ConcurrentHashMap;
6   import java.util.concurrent.ConcurrentMap;
7   
8   import javax.servlet.ServletContext;
9   
10  import junit.framework.TestCase;
11  
12  import com.atlassian.plugin.Plugin;
13  import com.atlassian.plugin.servlet.PluginServletContextWrapper;
14  import com.mockobjects.dynamic.C;
15  import com.mockobjects.dynamic.Mock;
16  
17  public class TestPluginServletContextWrapper extends TestCase
18  {
19      Mock mockServletContext;
20      Mock mockPlugin;
21      
22      ConcurrentMap<String, Object> attributes;
23      Map<String, String> initParams;
24      
25      ServletContext contextWrapper;
26      
27      public void setUp()
28      {
29          mockServletContext = new Mock(ServletContext.class);
30          mockServletContext.expectAndReturn("getAttribute", C.eq("wrapped"), "wrapped value");
31          
32          mockPlugin = new Mock(Plugin.class);
33          
34          attributes = new ConcurrentHashMap<String, Object>();
35          initParams = new HashMap<String, String>();
36          
37          contextWrapper = new PluginServletContextWrapper((Plugin) mockPlugin.proxy(), (ServletContext) mockServletContext.proxy(), attributes, initParams);
38      }
39      
40      public void testPutAttribute()
41      {
42          // if set attribute is called on the wrapped context it will throw an 
43          // exception since it is not expecting it
44          contextWrapper.setAttribute("attr", "value");
45          assertEquals("value", contextWrapper.getAttribute("attr"));
46      }
47      
48      public void testGetAttributeDelegatesToWrappedContext()
49      {
50          assertEquals("wrapped value", contextWrapper.getAttribute("wrapped"));
51      }
52  
53      public void testPutAttributeOverridesWrapperContextAttribute()
54      {
55          // if set attribute is called on the wrapped context it will throw an 
56          // exception since it is not expecting it
57          contextWrapper.setAttribute("wrapped", "value");
58          assertEquals("value", contextWrapper.getAttribute("wrapped"));
59      }    
60  }