Skip to content

Commit

Permalink
GEODE-8221: Refactor tests to run in appropriate projects. (apache#5244)
Browse files Browse the repository at this point in the history
  • Loading branch information
jake-at-work authored Jun 11, 2020
1 parent d82e30d commit cb5990c
Show file tree
Hide file tree
Showing 22 changed files with 603 additions and 622 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@
import org.apache.geode.modules.session.catalina.DeltaSessionManager;
import org.apache.geode.modules.session.catalina.PeerToPeerCacheLifecycleListener;

public abstract class TestSessionsBase {
public abstract class AbstractSessionsTest {
protected static int port;
private static EmbeddedTomcat server;
private static StandardWrapper servlet;
private static Region<String, HttpSession> region;
protected static DeltaSessionManager sessionManager;

// Set up the servers we need
protected static void setupServer(DeltaSessionManager manager) throws Exception {
protected static void setupServer(final DeltaSessionManager manager) throws Exception {
FileUtils.copyDirectory(Paths.get("..", "resources", "integrationTest", "tomcat").toFile(),
new File("./tomcat"));
port = SocketUtils.findAvailableTcpPort();
server = new EmbeddedTomcat(port, "JVM-1");

PeerToPeerCacheLifecycleListener p2pListener = new PeerToPeerCacheLifecycleListener();
final PeerToPeerCacheLifecycleListener p2pListener = new PeerToPeerCacheLifecycleListener();
p2pListener.setProperty(MCAST_PORT, "0");
p2pListener.setProperty(LOG_LEVEL, "config");
server.getEmbedded().addLifecycleListener(p2pListener);
Expand Down Expand Up @@ -91,19 +91,20 @@ public void setup() {
region.clear();
}

private WebResponse setCallbackAndExecuteGet(Callback callback) throws IOException, SAXException {
private WebResponse setCallbackAndExecuteGet(final Callback callback)
throws IOException, SAXException {
servlet.getServletContext().setAttribute("callback", callback);

WebConversation wc = new WebConversation();
WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
final WebConversation wc = new WebConversation();
final WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
req.setParameter("cmd", QueryCommand.CALLBACK.name());
req.setParameter("param", "callback");

return wc.getResponse(req);
}

private WebRequest prepareRequest(String key, String value) {
WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
private WebRequest prepareRequest(final String key, final String value) {
final WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
req.setParameter("cmd", QueryCommand.SET.name());
req.setParameter("param", key);
req.setParameter("value", value);
Expand All @@ -116,11 +117,11 @@ private WebRequest prepareRequest(String key, String value) {
*/
@Test
public void testSanity() throws Exception {
WebConversation wc = new WebConversation();
WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
final WebConversation wc = new WebConversation();
final WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
req.setParameter("cmd", QueryCommand.GET.name());
req.setParameter("param", "null");
WebResponse response = wc.getResponse(req);
final WebResponse response = wc.getResponse(req);

assertEquals("JSESSIONID", response.getNewCookieNames()[0]);
}
Expand All @@ -133,12 +134,12 @@ public void testSanity() throws Exception {
@Test
public void testCallback() throws Exception {
final String helloWorld = "Hello World";
Callback c = (request, response) -> {
PrintWriter out = response.getWriter();
final Callback c = (request, response) -> {
final PrintWriter out = response.getWriter();
out.write(helloWorld);
};

WebResponse response = setCallbackAndExecuteGet(c);
final WebResponse response = setCallbackAndExecuteGet(c);
assertEquals(helloWorld, response.getText());
}

Expand All @@ -147,14 +148,14 @@ public void testCallback() throws Exception {
*/
@Test
public void testIsNew() throws Exception {
Callback c = (request, response) -> {
HttpSession session = request.getSession();
final Callback c = (request, response) -> {
final HttpSession session = request.getSession();
response.getWriter().write(Boolean.toString(session.isNew()));
};
servlet.getServletContext().setAttribute("callback", c);

WebConversation wc = new WebConversation();
WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
final WebConversation wc = new WebConversation();
final WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));

req.setParameter("cmd", QueryCommand.CALLBACK.name());
req.setParameter("param", "callback");
Expand All @@ -172,13 +173,13 @@ public void testIsNew() throws Exception {
*/
@Test
public void testSessionPersists1() throws Exception {
String key = "value_testSessionPersists1";
String value = "Foo";
final String key = "value_testSessionPersists1";
final String value = "Foo";

WebConversation wc = new WebConversation();
WebRequest req = prepareRequest(key, value);
final WebConversation wc = new WebConversation();
final WebRequest req = prepareRequest(key, value);
WebResponse response = wc.getResponse(req);
String sessionId = response.getNewCookieValue("JSESSIONID");
final String sessionId = response.getNewCookieValue("JSESSIONID");

assertNotNull("No apparent session cookie", sessionId);

Expand All @@ -196,11 +197,11 @@ public void testSessionPersists1() throws Exception {
*/
@Test
public void testInvalidate() throws Exception {
String key = "value_testInvalidate";
String value = "Foo";
final String key = "value_testInvalidate";
final String value = "Foo";

WebConversation wc = new WebConversation();
WebRequest req = prepareRequest(key, value);
final WebConversation wc = new WebConversation();
final WebRequest req = prepareRequest(key, value);
wc.getResponse(req);

// Invalidate the session
Expand All @@ -212,7 +213,7 @@ public void testInvalidate() throws Exception {
// The attribute should not be accessible now...
req.setParameter("cmd", QueryCommand.GET.name());
req.setParameter("param", key);
WebResponse response = wc.getResponse(req);
final WebResponse response = wc.getResponse(req);

assertEquals("", response.getText());
}
Expand All @@ -225,11 +226,11 @@ public void testSessionExpiration1() throws Exception {
// TestSessions only live for a second
sessionManager.setMaxInactiveInterval(1);

String key = "value_testSessionExpiration1";
String value = "Foo";
final String key = "value_testSessionExpiration1";
final String value = "Foo";

WebConversation wc = new WebConversation();
WebRequest req = prepareRequest(key, value);
final WebConversation wc = new WebConversation();
final WebRequest req = prepareRequest(key, value);
wc.getResponse(req);

// Sleep a while
Expand All @@ -238,7 +239,7 @@ public void testSessionExpiration1() throws Exception {
// The attribute should not be accessible now...
req.setParameter("cmd", QueryCommand.GET.name());
req.setParameter("param", key);
WebResponse response = wc.getResponse(req);
final WebResponse response = wc.getResponse(req);

assertEquals("", response.getText());
}
Expand All @@ -263,11 +264,11 @@ public void testSessionExpiration2() {
@Test
public void testSessionExpirationByContainer() throws Exception {

String key = "value_testSessionExpiration1";
String value = "Foo";
final String key = "value_testSessionExpiration1";
final String value = "Foo";

WebConversation wc = new WebConversation();
WebRequest req = prepareRequest(key, value);
final WebConversation wc = new WebConversation();
final WebRequest req = prepareRequest(key, value);
wc.getResponse(req);

// Set the session timeout of this one session.
Expand All @@ -281,7 +282,7 @@ public void testSessionExpirationByContainer() throws Exception {
// Do a request, which should cause the session to be expired
req.setParameter("cmd", QueryCommand.GET.name());
req.setParameter("param", key);
WebResponse response = wc.getResponse(req);
final WebResponse response = wc.getResponse(req);

assertEquals("", response.getText());
}
Expand All @@ -291,13 +292,13 @@ public void testSessionExpirationByContainer() throws Exception {
*/
@Test
public void testRemoveAttribute() throws Exception {
String key = "value_testRemoveAttribute";
String value = "Foo";
final String key = "value_testRemoveAttribute";
final String value = "Foo";

WebConversation wc = new WebConversation();
WebRequest req = prepareRequest(key, value);
final WebConversation wc = new WebConversation();
final WebRequest req = prepareRequest(key, value);
WebResponse response = wc.getResponse(req);
String sessionId = response.getNewCookieValue("JSESSIONID");
final String sessionId = response.getNewCookieValue("JSESSIONID");

// Implicitly remove the attribute
req.removeParameter("value");
Expand All @@ -317,13 +318,13 @@ public void testRemoveAttribute() throws Exception {
*/
@Test
public void testBasicRegion() throws Exception {
String key = "value_testBasicRegion";
String value = "Foo";
final String key = "value_testBasicRegion";
final String value = "Foo";

WebConversation wc = new WebConversation();
WebRequest req = prepareRequest(key, value);
WebResponse response = wc.getResponse(req);
String sessionId = response.getNewCookieValue("JSESSIONID");
final WebConversation wc = new WebConversation();
final WebRequest req = prepareRequest(key, value);
final WebResponse response = wc.getResponse(req);
final String sessionId = response.getNewCookieValue("JSESSIONID");

assertEquals(value, region.get(sessionId).getAttribute(key));
}
Expand All @@ -333,13 +334,13 @@ public void testBasicRegion() throws Exception {
*/
@Test
public void testRegionInvalidate() throws Exception {
String key = "value_testRegionInvalidate";
String value = "Foo";
final String key = "value_testRegionInvalidate";
final String value = "Foo";

WebConversation wc = new WebConversation();
WebRequest req = prepareRequest(key, value);
WebResponse response = wc.getResponse(req);
String sessionId = response.getNewCookieValue("JSESSIONID");
final WebConversation wc = new WebConversation();
final WebRequest req = prepareRequest(key, value);
final WebResponse response = wc.getResponse(req);
final String sessionId = response.getNewCookieValue("JSESSIONID");

// Invalidate the session
req.removeParameter("param");
Expand All @@ -357,15 +358,15 @@ public void testRegionInvalidate() throws Exception {
@Test
public void testMultipleAttributeUpdates() throws Exception {
final String key = "value_testMultipleAttributeUpdates";
Callback c = (request, response) -> {
HttpSession session = request.getSession();
final Callback c = (request, response) -> {
final HttpSession session = request.getSession();
for (int i = 0; i < 1000; i++) {
session.setAttribute(key, Integer.toString(i));
}
};

WebResponse response = setCallbackAndExecuteGet(c);
String sessionId = response.getNewCookieValue("JSESSIONID");
final WebResponse response = setCallbackAndExecuteGet(c);
final String sessionId = response.getNewCookieValue("JSESSIONID");
assertEquals("999", region.get(sessionId).getAttribute(key));
}

Expand All @@ -374,13 +375,13 @@ public void testMultipleAttributeUpdates() throws Exception {
*/
@Test
public void testCommitSessionValveInvalidSession() throws Exception {
Callback c = (request, response) -> {
HttpSession session = request.getSession();
final Callback c = (request, response) -> {
final HttpSession session = request.getSession();
session.invalidate();
response.getWriter().write("done");
};

WebResponse response = setCallbackAndExecuteGet(c);
final WebResponse response = setCallbackAndExecuteGet(c);
assertEquals("done", response.getText());
}

Expand All @@ -389,12 +390,12 @@ public void testCommitSessionValveInvalidSession() throws Exception {
*/
@Test
public void testExtraSessionsNotCreated() throws Exception {
Callback c = (request, response) -> {
final Callback c = (request, response) -> {
// Do nothing with sessions
response.getWriter().write("done");
};

WebResponse response = setCallbackAndExecuteGet(c);
final WebResponse response = setCallbackAndExecuteGet(c);
assertEquals("done", response.getText());
assertEquals("The region should be empty", 0, region.size());
}
Expand All @@ -405,14 +406,14 @@ public void testExtraSessionsNotCreated() throws Exception {
*/
@Test
public void testLastAccessedTime() throws Exception {
Callback c = (request, response) -> {
HttpSession session = request.getSession();
final Callback c = (request, response) -> {
final HttpSession session = request.getSession();
// Hack to expose the session to our test context
session.getServletContext().setAttribute("session", session);
session.setAttribute("lastAccessTime", session.getLastAccessedTime());
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
} catch (final InterruptedException ex) {
// Ignore.
}
session.setAttribute("somethingElse", 1);
Expand All @@ -421,16 +422,16 @@ public void testLastAccessedTime() throws Exception {
};
servlet.getServletContext().setAttribute("callback", c);

WebConversation wc = new WebConversation();
WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));
final WebConversation wc = new WebConversation();
final WebRequest req = new GetMethodWebRequest(String.format("http://localhost:%d/test", port));

// Execute the callback
req.setParameter("cmd", QueryCommand.CALLBACK.name());
req.setParameter("param", "callback");
wc.getResponse(req);

HttpSession session = (HttpSession) servlet.getServletContext().getAttribute("session");
Long lastAccess = (Long) session.getAttribute("lastAccessTime");
final HttpSession session = (HttpSession) servlet.getServletContext().getAttribute("session");
final Long lastAccess = (Long) session.getAttribute("lastAccessTime");

assertTrue(
"Last access time not set correctly: " + lastAccess + " not <= "
Expand Down
Loading

0 comments on commit cb5990c

Please sign in to comment.