1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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 }