2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

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.shale.test.mock;
19  
20  import java.util.Enumeration;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import javax.portlet.PortletContext;
25  import javax.portlet.PortletSession;
26  
27  /***
28   * <p> Mock implementation of <code>PortletSession</code>. </p>
29   * 
30   * $Id: MockPortletSession.java 516091 2007-03-08 16:25:17Z greddin $
31   */
32  public class MockPortletSession implements PortletSession {
33  
34      // ------------------------------------------------------------ Constructors
35  
36      /***
37       * <p> Configure a default instance. </p>
38       */
39      public MockPortletSession() {
40  
41          super();
42  
43      }
44  
45  
46      /***
47       * <p> Configure a session instance associated with the specified servlet
48       * context. </p>
49       *
50       * @param servletContext The associated servlet context
51       */
52      public MockPortletSession(PortletContext portletContext) {
53  
54          super();
55          this.portletContext = portletContext;
56  
57      }
58  
59  
60      // ----------------------------------------------------- Mock Object Methods
61  
62      /***
63       * <p> Set the <code>PortletContext</code> associated with this session.
64       * </p>
65       *
66       * @param servletContext The associated servlet context
67       */
68      public void setPortletContext(PortletContext portletContext) {
69  
70          this.portletContext = portletContext;
71  
72      }
73  
74      // ------------------------------------------------------ Instance Variables
75  
76      private Map portletAttributes = new HashMap();
77      private Map applicationAttributes = new HashMap();
78      private String id = "123";
79      private PortletContext portletContext = null;
80  
81  
82      // ---------------------------------------------------------- Public Methods
83  
84      /***
85       * <p> Set the session identifier of this session. </p>
86       *
87       * @param id The new session identifier
88       */
89      public void setId(String id) {
90  
91          this.id = id;
92  
93      }
94  
95  
96      // -------------------------------------------------- PortletSession Methods
97  
98  
99      /*** {@inheritDoc} */
100     public Object getAttribute(String name) {
101 
102         return getAttribute(name, PORTLET_SCOPE);
103 
104     }
105 
106 
107     /*** {@inheritDoc} */
108     public Object getAttribute(String name, int scope) {
109 
110         if (scope == PORTLET_SCOPE) {
111             return portletAttributes.get(name);
112         } else if (scope == APPLICATION_SCOPE) {
113             return applicationAttributes.get(name);
114         }
115 
116         throw new IllegalArgumentException("Scope constant " + scope
117                 + " not recognized");
118 
119     }
120 
121 
122     /*** {@inheritDoc} */
123     public Enumeration getAttributeNames() {
124 
125         return getAttributeNames(PORTLET_SCOPE);
126 
127     }
128 
129 
130     /*** {@inheritDoc} */
131     public Enumeration getAttributeNames(int scope) {
132 
133         if (scope == PORTLET_SCOPE) {
134             return new MockEnumeration(portletAttributes.keySet().iterator());
135         } else if (scope == APPLICATION_SCOPE) {
136             return new MockEnumeration(applicationAttributes.keySet()
137                     .iterator());
138         }
139 
140         throw new IllegalArgumentException("Scope constant " + scope
141                 + " not recognized");
142 
143     }
144 
145 
146     /*** {@inheritDoc} */
147     public long getCreationTime() {
148 
149         throw new UnsupportedOperationException();
150 
151     }
152 
153 
154     /*** {@inheritDoc} */
155     public String getId() {
156 
157         return this.id;
158 
159     }
160 
161 
162     /*** {@inheritDoc} */
163     public long getLastAccessedTime() {
164 
165         throw new UnsupportedOperationException();
166 
167     }
168 
169 
170     /*** {@inheritDoc} */
171     public int getMaxInactiveInterval() {
172 
173         throw new UnsupportedOperationException();
174 
175     }
176 
177 
178     /*** {@inheritDoc} */
179     public PortletContext getPortletContext() {
180 
181         return portletContext;
182     }
183 
184 
185     /*** {@inheritDoc} */
186     public void invalidate() {
187 
188         throw new UnsupportedOperationException();
189 
190     }
191 
192 
193     /*** {@inheritDoc} */
194     public boolean isNew() {
195 
196         throw new UnsupportedOperationException();
197 
198     }
199 
200 
201     /*** {@inheritDoc} */
202     public void removeAttribute(String name) {
203 
204         removeAttribute(name, PORTLET_SCOPE);
205 
206     }
207 
208 
209     /*** {@inheritDoc} */
210     public void removeAttribute(String name, int scope) {
211 
212         Map attributes;
213         if (scope == PORTLET_SCOPE) {
214             attributes = portletAttributes;
215         } else if (scope == APPLICATION_SCOPE) {
216             attributes = applicationAttributes;
217         } else {
218             throw new IllegalArgumentException("Scope constant " + scope
219                     + " not recognized");
220         }
221         if (attributes.containsKey(name)) {
222             attributes.remove(name);
223         }
224 
225     }
226 
227 
228     /*** {@inheritDoc} */
229     public void setAttribute(String name, Object value) {
230 
231         setAttribute(name, value, PORTLET_SCOPE);
232 
233     }
234 
235 
236     /*** {@inheritDoc} */
237     public void setAttribute(String name, Object value, int scope) {
238 
239         if (name == null) {
240             throw new IllegalArgumentException("Attribute name cannot be null");
241         }
242         if (value == null) {
243             removeAttribute(name, scope);
244             return;
245         }
246 
247         Map attributes;
248         if (scope == PORTLET_SCOPE) {
249             attributes = portletAttributes;
250         } else if (scope == APPLICATION_SCOPE) {
251             attributes = applicationAttributes;
252         } else {
253             throw new IllegalArgumentException("Scope constant " + scope
254                     + " not recognized");
255         }
256         attributes.put(name, value);
257 
258     }
259 
260 
261     /*** {@inheritDoc} */
262     public void setMaxInactiveInterval(int arg0) {
263 
264         throw new UnsupportedOperationException();
265 
266     }
267 
268 }