Skip to content

Commit

Permalink
unit test for hazelcast#3049 and spring test infrastructure for hazel…
Browse files Browse the repository at this point in the history
…cast-vm
  • Loading branch information
serkanozal committed Aug 6, 2014
1 parent 3c7916e commit ef31223
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 16 deletions.
Expand Up @@ -27,6 +27,8 @@
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.After;
Expand All @@ -41,6 +43,13 @@

public abstract class AbstractWebFilterTest extends HazelcastTestSupport{

protected enum RequestType {

GET_REQUEST,
POST_REQUEST

}

static {
final String logging = "hazelcast.logging.type";
if (System.getProperty(logging) == null) {
Expand All @@ -64,7 +73,9 @@ public abstract class AbstractWebFilterTest extends HazelcastTestSupport{
}

protected static final String HAZELCAST_SESSION_ATTRIBUTE_SEPARATOR = "::hz::";


protected static final RequestType DEFAULT_REQUEST_TYPE = RequestType.GET_REQUEST;

protected String serverXml1;
protected String serverXml2;

Expand All @@ -74,6 +85,10 @@ public abstract class AbstractWebFilterTest extends HazelcastTestSupport{
protected ServletContainer server2;
protected HazelcastInstance hz;

protected AbstractWebFilterTest(String serverXml1) {
this.serverXml1 = serverXml1;
}

protected AbstractWebFilterTest(String serverXml1, String serverXml2) {
this.serverXml1 = serverXml1;
this.serverXml2 = serverXml2;
Expand All @@ -84,17 +99,22 @@ public void setup() throws Exception {
final URL root = new URL(TestServlet.class.getResource("/"), "../test-classes");
final String baseDir = new File(root.getFile().replaceAll("%20", " ")).toString();
final String sourceDir = baseDir + "/../../src/test/webapp";
hz = Hazelcast.newHazelcastInstance(new FileSystemXmlConfig(new File(sourceDir + "/WEB-INF/", "hazelcast.xml")));
hz = Hazelcast.newHazelcastInstance(
new FileSystemXmlConfig(new File(sourceDir + "/WEB-INF/", "hazelcast.xml")));
serverPort1 = availablePort();
serverPort2 = availablePort();
server1 = getServletContainer(serverPort1, sourceDir, serverXml1);
server2 = getServletContainer(serverPort2, sourceDir, serverXml2);
if (serverXml2 != null) {
serverPort2 = availablePort();
server2 = getServletContainer(serverPort2, sourceDir, serverXml2);
}
}

@After
public void teardown() throws Exception {
server1.stop();
server2.stop();
if (server2 != null) {
server2.stop();
}
Hazelcast.shutdownAll();
}

Expand All @@ -120,22 +140,51 @@ protected String findHazelcastSessionId(IMap<String, Object> map) {
return null;
}

protected String executeRequest(String context, int serverPort, CookieStore cookieStore) throws Exception {
HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
HttpGet request = new HttpGet("http://localhost:" + serverPort + "/" + context);
HttpResponse response = client.execute(request);
protected String responseToString(HttpResponse response) throws IOException {
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
}

protected String executeRequest(String context, int serverPort, CookieStore cookieStore) throws Exception {
return responseToString(request(context, serverPort, cookieStore));
}

protected HttpResponse request(String context, int serverPort, CookieStore cookieStore) throws Exception {
return request(DEFAULT_REQUEST_TYPE, context, serverPort, cookieStore);
}

protected String executeRequest(RequestType reqType,
String context,
int serverPort,
CookieStore cookieStore) throws Exception {
return responseToString(request(reqType, context, serverPort, cookieStore));
}

protected HttpResponse request(RequestType reqType,
String context,
int serverPort,
CookieStore cookieStore) throws Exception {
if (reqType == null) {
throw new IllegalArgumentException("Request type paramater cannot be empty !");
}
HttpClient client = HttpClientBuilder.create().disableRedirectHandling().setDefaultCookieStore(cookieStore).build();
HttpGet request = new HttpGet("http://localhost:" + serverPort + "/" + context);
HttpUriRequest request = null;
switch (reqType) {
case GET_REQUEST:
request = new HttpGet("http://localhost:" + serverPort + "/" + context);
break;
case POST_REQUEST:
request = new HttpPost("http://localhost:" + serverPort + "/" + context);
break;
default:
throw new IllegalArgumentException(reqType + " typed request is not supported");
}
HttpResponse response = client.execute(request);
return response;
}

protected abstract ServletContainer getServletContainer(int port, String sourceDir, String serverXml) throws Exception;

protected abstract ServletContainer getServletContainer(int port,
String sourceDir,
String serverXml) throws Exception;

}
Expand Up @@ -29,10 +29,7 @@ public class TestServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


if (req.getRequestURI().endsWith("redirect"))
{
if (req.getRequestURI().endsWith("redirect")) {
//Don't touch session before redirect
resp.sendRedirect("/");
return;
Expand Down
@@ -0,0 +1,25 @@
package com.hazelcast.wm.test.spring;

import org.eclipse.jetty.util.ConcurrentHashSet;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Collections;
import java.util.Set;

public class SpringApplicationContextProvider implements ApplicationContextAware {

private static Set<ApplicationContext> applicationContextSet =
new ConcurrentHashSet<ApplicationContext>();

public static Set<ApplicationContext> getApplicationContextSet() {
return Collections.unmodifiableSet(applicationContextSet);
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
applicationContextSet.add(applicationContext);
}

}
@@ -0,0 +1,122 @@
/*
* Copyright (c) 2008-2014, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hazelcast.wm.test.spring;

import com.hazelcast.wm.test.AbstractWebFilterTest;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.springframework.web.context.support.WebApplicationContextUtils;

public abstract class SpringAwareWebFilterTestSupport extends AbstractWebFilterTest {

protected static final String DEFAULT_SPRING_CONTEXT_FILE_PATH = "spring/hazelcast-spring.xml";

protected static final String SPRING_SECURITY_LOGIN_URL = "j_spring_security_check";
protected static final String SPRING_SECURITY_LOGOUT_URL = "j_spring_security_logout";
protected static final String SPRING_SECURITY_LOGIN_USERNAME_PARAM = "j_username";
protected static final String SPRING_SECURITY_LOGIN_PASSWORD_PARAM = "j_password";
protected static final String SPRING_SECURITY_REMEMBER_ME_PARAM = "_spring_security_remember_me";

protected static final String SPRING_SECURITY_DEFAULT_USERNAME = "user";
protected static final String SPRING_SECURITY_DEFAULT_PASSWORD = "password";

protected static final String SESSION_ID_COOKIE_NAME = "JSESSIONID";
protected static final String HZ_SESSION_ID_COOKIE_NAME = "hazelcast.sessionId";
protected static final String SPRING_SECURITY_COOKIE_NAME = "SPRING_SECURITY_REMEMBER_ME_COOKIE";

public SpringAwareWebFilterTestSupport() {
this(DEFAULT_SPRING_CONTEXT_FILE_PATH, DEFAULT_SPRING_CONTEXT_FILE_PATH);
}

protected SpringAwareWebFilterTestSupport(String serverXml1, String serverXml2) {
super(serverXml1, serverXml2);
}

protected static class SpringSecuritySession {

protected CookieStore cookieStore;
protected HttpResponse lastResponse;

protected SpringSecuritySession() {
this.cookieStore = new BasicCookieStore();
}

protected SpringSecuritySession(CookieStore cookieStore) {
this.cookieStore = cookieStore;
}

protected String getCookie(String cookieName) {
for (Cookie cookie : cookieStore.getCookies()) {
if (cookie.getName().equals(cookieName)) {
return cookie.getValue();
}
}
return null;
}

protected String getSessionId() {
return getCookie(SESSION_ID_COOKIE_NAME);
}

protected String getHazelcastSessionId() {
return getCookie(HZ_SESSION_ID_COOKIE_NAME);
}

protected String getSpringSecurityCookie() {
return getCookie(SPRING_SECURITY_COOKIE_NAME);
}

}

protected SpringSecuritySession login(SpringSecuritySession springSecuritySession) throws Exception {
return login(springSecuritySession, SPRING_SECURITY_DEFAULT_USERNAME, SPRING_SECURITY_DEFAULT_PASSWORD);
}

protected SpringSecuritySession login(SpringSecuritySession springSecuritySession,
String username,
String password) throws Exception {
if (springSecuritySession== null) {
springSecuritySession = new SpringSecuritySession();
}
HttpResponse response =
request(
RequestType.POST_REQUEST,
SPRING_SECURITY_LOGIN_URL + "?" +
SPRING_SECURITY_LOGIN_USERNAME_PARAM + "=" + username + "&" +
SPRING_SECURITY_LOGIN_PASSWORD_PARAM + "=" + password + "&" +
SPRING_SECURITY_REMEMBER_ME_PARAM + "=" + "true",
serverPort1, springSecuritySession.cookieStore);
springSecuritySession.lastResponse = response;
return springSecuritySession;
}

protected SpringSecuritySession logout(SpringSecuritySession springSecuritySession) throws Exception {
if (springSecuritySession == null) {
throw new IllegalArgumentException("SpringSecuritySession cannot be null !");
}
HttpResponse response =
request(
RequestType.POST_REQUEST,
SPRING_SECURITY_LOGOUT_URL,
serverPort1, springSecuritySession.cookieStore);
springSecuritySession.lastResponse = response;
return springSecuritySession;
}

}
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->

<context:annotation-config/>

<context:component-scan base-package="com.hazelcast.wm.test.spring" />

<bean id="springApplicationContextProvider"
class="com.hazelcast.wm.test.spring.SpringApplicationContextProvider" />

</beans>

0 comments on commit ef31223

Please sign in to comment.