Line 0
Link Here
|
|
|
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.catalina.session; |
18 |
|
19 |
import java.beans.PropertyChangeListener; |
20 |
import java.io.IOException; |
21 |
import java.util.ArrayList; |
22 |
import java.util.Arrays; |
23 |
import java.util.List; |
24 |
|
25 |
import javax.servlet.ServletException; |
26 |
import javax.servlet.http.HttpServlet; |
27 |
import javax.servlet.http.HttpServletRequest; |
28 |
import javax.servlet.http.HttpServletResponse; |
29 |
|
30 |
import org.apache.catalina.Context; |
31 |
import org.apache.catalina.LifecycleException; |
32 |
import org.apache.catalina.Manager; |
33 |
import org.apache.catalina.Session; |
34 |
import org.apache.catalina.Store; |
35 |
import org.apache.catalina.startup.Tomcat; |
36 |
import org.apache.catalina.startup.TomcatBaseTest; |
37 |
import org.junit.After; |
38 |
import org.junit.Assert; |
39 |
import org.junit.Before; |
40 |
import org.junit.Test; |
41 |
|
42 |
public class TestPersistentManager extends TomcatBaseTest { |
43 |
|
44 |
private final String ACTIVITY_CHECK = "org.apache.catalina.session.StandardSession.ACTIVITY_CHECK"; |
45 |
|
46 |
private String oldActivityCheck; |
47 |
|
48 |
@Before |
49 |
public void setActivityCheck() { |
50 |
oldActivityCheck = System.setProperty(ACTIVITY_CHECK, "true"); |
51 |
} |
52 |
|
53 |
@After |
54 |
public void resetActivityCheck() { |
55 |
if (oldActivityCheck != null) { |
56 |
System.setProperty(ACTIVITY_CHECK, oldActivityCheck); |
57 |
} else { |
58 |
System.clearProperty(ACTIVITY_CHECK); |
59 |
} |
60 |
} |
61 |
|
62 |
@Test |
63 |
public void backsUpOnce() throws IOException, LifecycleException { |
64 |
|
65 |
// Setup Tomcat instance |
66 |
Tomcat tomcat = getTomcatInstance(); |
67 |
// Must have a real docBase - just use temp |
68 |
Context ctx = tomcat.addContext("", |
69 |
System.getProperty("java.io.tmpdir")); |
70 |
|
71 |
Tomcat.addServlet(ctx, "DummyServlet", new DummyServlet()); |
72 |
ctx.addServletMapping("/dummy", "DummyServlet"); |
73 |
|
74 |
PersistentManager manager = new PersistentManager(); |
75 |
DummyStore store = new DummyStore(); |
76 |
|
77 |
manager.setStore(store); |
78 |
manager.setMaxIdleBackup(0); |
79 |
manager.setDistributable(true); |
80 |
ctx.setManager(manager); |
81 |
tomcat.start(); |
82 |
String sessionId = getUrl("http://localhost:" + getPort() + "/dummy") |
83 |
.toString(); |
84 |
sleepABit(); |
85 |
|
86 |
manager.processPersistenceChecks(); |
87 |
Assert.assertEquals(Arrays.asList(sessionId), store.getSavedIds()); |
88 |
|
89 |
// session was not accessed, so no save will be performed |
90 |
manager.processPersistenceChecks(); |
91 |
Assert.assertEquals(Arrays.asList(sessionId), store.getSavedIds()); |
92 |
|
93 |
// access session |
94 |
manager.findSession(sessionId).access(); |
95 |
manager.findSession(sessionId).endAccess(); |
96 |
sleepABit(); |
97 |
|
98 |
// session was accessed, so it will be saved once again |
99 |
manager.processPersistenceChecks(); |
100 |
Assert.assertEquals(Arrays.asList(sessionId, sessionId), |
101 |
store.getSavedIds()); |
102 |
|
103 |
// session was not accessed, so once again no save will happen |
104 |
manager.processPersistenceChecks(); |
105 |
Assert.assertEquals(Arrays.asList(sessionId, sessionId), |
106 |
store.getSavedIds()); |
107 |
} |
108 |
|
109 |
private void sleepABit() { |
110 |
try { |
111 |
Thread.sleep(1000); |
112 |
} catch (InterruptedException e) { |
113 |
// ignore Interrupt |
114 |
} |
115 |
} |
116 |
|
117 |
private static class DummyServlet extends HttpServlet { |
118 |
|
119 |
private static final long serialVersionUID = -3696433049266123995L; |
120 |
|
121 |
@Override |
122 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
123 |
throws ServletException, IOException { |
124 |
resp.getWriter().print(req.getSession().getId()); |
125 |
} |
126 |
|
127 |
} |
128 |
|
129 |
private static class DummyStore implements Store { |
130 |
|
131 |
private Manager manager; |
132 |
private List<String> savedIds = new ArrayList<String>(); |
133 |
|
134 |
List<String> getSavedIds() { |
135 |
return savedIds; |
136 |
} |
137 |
|
138 |
@Override |
139 |
public Manager getManager() { |
140 |
return this.manager; |
141 |
} |
142 |
|
143 |
@Override |
144 |
public void setManager(Manager manager) { |
145 |
this.manager = manager; |
146 |
} |
147 |
|
148 |
@Override |
149 |
public int getSize() throws IOException { |
150 |
return 0; |
151 |
} |
152 |
|
153 |
@Override |
154 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
155 |
} |
156 |
|
157 |
@Override |
158 |
public String[] keys() throws IOException { |
159 |
return null; |
160 |
} |
161 |
|
162 |
@Override |
163 |
public Session load(String id) throws ClassNotFoundException, |
164 |
IOException { |
165 |
// TODO Auto-generated method stub |
166 |
return null; |
167 |
} |
168 |
|
169 |
@Override |
170 |
public void remove(String id) throws IOException { |
171 |
} |
172 |
|
173 |
@Override |
174 |
public void clear() throws IOException { |
175 |
} |
176 |
|
177 |
@Override |
178 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
179 |
} |
180 |
|
181 |
@Override |
182 |
public void save(Session session) throws IOException { |
183 |
savedIds.add(session.getId()); |
184 |
} |
185 |
|
186 |
} |
187 |
} |