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  package org.apache.commons.chain.web.portlet;
18  
19  
20  import org.apache.commons.chain.web.MockEnumeration;
21  
22  import javax.portlet.PortletContext;
23  import javax.portlet.PortletSession;
24  import javax.portlet.PortletContext;
25  import java.util.Enumeration;
26  import java.util.HashMap;
27  import java.util.Map;
28  import java.util.Date;
29  
30  
31  
32  // Mock Object for PortletSession
33  public class MockPortletSession implements PortletSession {
34  
35  
36      private Date creationTime     = new Date();
37      private Date lastAccessedTime = creationTime;
38  
39      private PortletContext context = null;
40      private int maxInactiveInterval = 100;
41      private boolean newSession = true;
42      private String id = "mockId" + creationTime.getTime();
43      private Map portletScope = new HashMap();
44      private Map applicationScope = new HashMap();
45  
46  
47      public MockPortletSession() {
48          this(null);
49      }
50  
51  
52      public MockPortletSession(PortletContext context) {
53          this.context = (context == null ? new MockPortletContext() : context);
54      }
55  
56  
57      // --------------------------------------------------------- Public Methods
58  
59  
60      public void setPortletContext(PortletContext context) {
61          this.context = context;
62      }
63  
64      public void setNew(boolean newSession) {
65          this.newSession = newSession;
66      }
67  
68      public void setNew(String id) {
69          this.id = id;
70      }
71  
72  
73  
74      // ---------------------------------------------------- PortletSession Methods
75  
76  
77      public Object getAttribute(String name) {
78          accessed();
79          return getAttribute(name, PortletSession.PORTLET_SCOPE);
80      }
81  
82      public Object getAttribute(String name, int scope) {
83          accessed();
84          return getScope(scope).get(name);
85      }
86  
87  
88      public Enumeration getAttributeNames() {
89          accessed();
90          return getAttributeNames(PortletSession.PORTLET_SCOPE);
91      }
92  
93      public Enumeration getAttributeNames(int scope) {
94          accessed();
95          return new MockEnumeration(getScope(scope).keySet().iterator());
96      }
97  
98  
99      public long getCreationTime() {
100         accessed();
101         return creationTime.getTime();
102     }
103 
104 
105     public String getId() {
106         accessed();
107         return id;
108     }
109 
110 
111     public long getLastAccessedTime() {
112         return lastAccessedTime.getTime();
113     }
114 
115 
116     public int getMaxInactiveInterval() {
117         accessed();
118         return maxInactiveInterval;
119     }
120 
121 
122     public PortletContext getPortletContext() {
123         accessed();
124         return context;
125     }
126 
127     public void invalidate() {
128         throw new UnsupportedOperationException();
129     }
130 
131     public boolean isNew() {
132         accessed();
133         return newSession;
134     }
135 
136     public void removeAttribute(String name) {
137         accessed();
138         removeAttribute(name, PortletSession.PORTLET_SCOPE);
139     }
140 
141     public void removeAttribute(String name, int scope) {
142         accessed();
143         getScope(scope).remove(name);
144     }
145 
146     public void setAttribute(String name, Object value) {
147         accessed();
148         setAttribute(name, value, PortletSession.PORTLET_SCOPE);
149     }
150 
151     public void setAttribute(String name, Object value, int scope) {
152         accessed();
153         getScope(scope).put(name, value);
154     }
155 
156     public void setMaxInactiveInterval(int interval) {
157         accessed();
158         this.maxInactiveInterval = interval;
159     }
160 
161     private void accessed() {
162         lastAccessedTime = new Date();
163     }
164 
165     private Map getScope(int scope) {
166         if (scope == PortletSession.PORTLET_SCOPE) {
167             return portletScope;
168         } else if (scope == PortletSession.APPLICATION_SCOPE) {
169            return applicationScope;
170         } else {
171            throw new IllegalArgumentException("Invalid scope: " + scope);
172         }
173     }
174 
175 }