View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jxpath.servlet;
19  
20  import java.util.Iterator;
21  import javax.servlet.ServletContext;
22  
23  import org.apache.commons.jxpath.JXPathContext;
24  import org.apache.commons.jxpath.Pointer;
25  import org.apache.commons.jxpath.Variables;
26  
27  import com.mockrunner.mock.web.MockHttpServletRequest;
28  import com.mockrunner.mock.web.MockHttpSession;
29  import com.mockrunner.mock.web.MockPageContext;
30  import com.mockrunner.mock.web.MockServletConfig;
31  import com.mockrunner.mock.web.MockServletContext;
32  import junit.framework.TestCase;
33  
34  /**
35   * @author Emmanuel Bourg
36   * @version $Revision: 652848 $, $Date: 2008-05-02 19:53:50 +0200 (Fr, 02 Mai 2008) $
37   */
38  public class JXPathServletContextTest extends TestCase {
39  
40      private ServletContext getServletContext() {
41          MockServletContext context = new MockServletContext();
42          context.setAttribute("app", "OK");
43  
44          return context;
45      }
46      
47      public void testServletContext() {
48          ServletContext context = getServletContext();
49          JXPathContext appContext = JXPathServletContexts.getApplicationContext(context);
50  
51          assertSame("Cached context not property returned", appContext, JXPathServletContexts.getApplicationContext(context));
52  
53          assertEquals("Application Context", "OK", appContext.getValue("app"));
54  
55          checkPointerIterator(appContext);
56  
57          // test setting a value in the context
58          appContext.setValue("/foo", "bar");
59          assertEquals("Context property", "bar", appContext.getValue("/foo"));
60  
61          // test the variables
62          Variables variables = appContext.getVariables();
63          assertNotNull("$application variable", variables.getVariable("application"));
64          assertNull("$foo variable", variables.getVariable("$foo"));
65      }
66  
67      public void testServletRequest() {
68          ServletContext context = getServletContext();
69  
70          MockHttpSession session = new MockHttpSession();
71          session.setupServletContext(context);
72          session.setUpIsNew(true);
73          Integer count = new Integer(10);
74          session.setAttribute("count", count);
75  
76          MockHttpServletRequest request = new MockHttpServletRequest();
77          request.setSession(session);
78          request.setAttribute("attr", "OK");
79          request.setupAddParameter("parm", "OK");
80          request.setupAddParameter("multiparam", new String[] { "value1", "value2" });
81          request.setupAddParameter("emptyparam", new String[0]);
82  
83          assertSame("Request session", session, request.getSession());
84  
85          JXPathContext reqContext = JXPathServletContexts.getRequestContext(request, context);
86  
87          assertSame("Cached context not property returned", reqContext, JXPathServletContexts.getRequestContext(request, context));
88  
89          JXPathContext sessionContext = JXPathServletContexts.getSessionContext(session, context);
90  
91          assertSame("Cached context not property returned", sessionContext, JXPathServletContexts.getSessionContext(session, context));
92  
93          assertEquals("Request Context Attribute", "OK", reqContext.getValue("attr"));
94  
95          assertEquals("Request Context Parameter", "OK", reqContext.getValue("parm"));
96          assertTrue("Request Context Parameter (Array)", reqContext.getValue("multiparam").getClass().isArray());
97          assertEquals("Request Context Parameter (Empty)", null, reqContext.getValue("emptyparam"));
98  
99          assertEquals("Session Context Parameter", count, sessionContext.getValue("count"));
100         assertEquals("Application Context via Request Context", "OK", reqContext.getValue("app"));
101         assertEquals("Session Context via Request Context", count, reqContext.getValue("count"));
102         assertEquals("Application Context via Session Context", "OK", sessionContext.getValue("app"));
103 
104         checkPointerIterator(reqContext);
105         checkPointerIterator(sessionContext);
106 
107         // test setting a value in the context
108         reqContext.setValue("/foo1", "bar1");
109         assertEquals("Context property", "bar1", reqContext.getValue("/foo1"));
110 
111         sessionContext.setValue("/foo2", "bar2");
112         assertEquals("Context property", "bar2", sessionContext.getValue("/foo2"));
113     }
114 
115     public void testServletRequestWithoutSession() {
116         ServletContext context = getServletContext();
117 
118         MockHttpServletRequest request = new MockHttpServletRequest();
119 
120         JXPathContext reqContext = JXPathServletContexts.getRequestContext(request, context);
121 
122         assertEquals("Application Context via Request Context", "OK", reqContext.getValue("app"));
123     }
124 
125     private void checkPointerIterator(JXPathContext context) {
126         Iterator it = context.iteratePointers("/*");
127         assertTrue("Empty context", it.hasNext());
128         while (it.hasNext())
129         {
130             Pointer pointer = (Pointer) it.next();
131             assertNotNull("null pointer", pointer);
132             assertNotNull("null path", pointer.asPath());
133         }
134     }
135 
136     public void testPageContext() {
137         MockServletContext servletContext = new MockServletContext();
138         servletContext.setAttribute("app", "app");
139 
140         MockServletConfig servletConfig = new MockServletConfig();
141         servletConfig.setServletContext(servletContext);
142 
143         MockHttpSession session = new MockHttpSession();
144         session.setupServletContext(servletContext);
145         session.setAttribute("session", "session");
146 
147         MockHttpServletRequest request = new MockHttpServletRequest();
148         request.setAttribute("request", "request");
149         request.setSession(session);
150 
151         MockPageContext pageContext = new MockPageContext();
152         pageContext.setServletConfig(servletConfig);
153         pageContext.setServletRequest(request);
154         pageContext.setAttribute("page", "page");
155 
156         assertSame("Request session", session, request.getSession());
157 
158 
159         JXPathContext context = JXPathServletContexts.getPageContext(pageContext);
160         context.setLenient(true);
161         
162         checkPointerIterator(context);
163 
164         assertEquals("Page Scope", "page", context.getValue("page"));
165         assertEquals("Request Scope", "request", context.getValue("request"));
166         assertEquals("Session Scope", "session", context.getValue("session"));
167         assertEquals("Application Scope", "app", context.getValue("app"));
168 
169         assertEquals("Explicit Page Scope", "page", context.getValue("$page/page"));
170         assertEquals("Explicit Request Scope", "request", context.getValue("$request/request"));
171         assertEquals("Explicit Session Scope", "session", context.getValue("$session/session"));
172         assertEquals("Explicit Application Scope", "app", context.getValue("$application/app"));
173 
174         // iterate through the elements of page context only (two elements expected, 'page' and the context)
175         Iterator it = context.iteratePointers("$page/*");
176         assertTrue("element not found", it.hasNext());
177         it.next();
178         it.next();
179         assertFalse("too many elements", it.hasNext());
180 
181         // test setting a value in the context
182         context.setValue("/foo1", "bar1");
183         assertEquals("Context property", "bar1", context.getValue("/foo1"));
184 
185         context.setValue("$page/foo2", "bar2");
186         assertEquals("Context property", "bar2", context.getValue("$page/foo2"));
187     }
188 }