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