Skip to content

Commit

Permalink
added BasicWebFilterTests
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalyasar committed Aug 26, 2014
1 parent 277642f commit 1b7c5d2
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

public abstract class AbstractWebFilterTest extends HazelcastTestSupport{

public static boolean isBasicTest,isSetup;
protected enum RequestType {

GET_REQUEST,
Expand Down Expand Up @@ -79,11 +80,11 @@ protected enum RequestType {
protected String serverXml1;
protected String serverXml2;

protected int serverPort1;
protected int serverPort2;
protected static int serverPort1;
protected static int serverPort2;
protected ServletContainer server1;
protected ServletContainer server2;
protected HazelcastInstance hz;
protected static HazelcastInstance hz;

protected AbstractWebFilterTest(String serverXml1) {
this.serverXml1 = serverXml1;
Expand All @@ -96,6 +97,9 @@ protected AbstractWebFilterTest(String serverXml1, String serverXml2) {

@Before
public void setup() throws Exception {
if(isBasicTest == true && isSetup == true){
return;
}
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";
Expand All @@ -107,18 +111,22 @@ public void setup() throws Exception {
serverPort2 = availablePort();
server2 = getServletContainer(serverPort2, sourceDir, serverXml2);
}
isSetup = true;
}

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

private int availablePort() throws IOException {
protected int availablePort() throws IOException {
while (true) {
int port = (int) (65536 * Math.random());
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* 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;

import com.hazelcast.core.IMap;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.annotation.QuickTest;
import org.apache.http.client.CookieStore;
import org.apache.http.impl.client.BasicCookieStore;
import org.junit.After;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

/**
* Tests to basic session methods. getAttribute,setAttribute,isNew,getAttributeNames etc.
* <p/>
* This test is classified as "quick" because we start jetty server only once.
*
* @since 3.3
*/
@RunWith(HazelcastSerialClassRunner.class)
@Category(QuickTest.class)
public class WebFilterBasicTest extends AbstractWebFilterTest {

public WebFilterBasicTest() {
super("node1-node.xml", "node2-node.xml");
isBasicTest = true;
}

@Test(timeout = 20000)
public void test_setAttribute() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
executeRequest("write", serverPort1, cookieStore);

assertEquals("value", executeRequest("read", serverPort2, cookieStore));
}

@Test(timeout = 20000)
public void test_getAttribute() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
executeRequest("write", serverPort1, cookieStore);

assertEquals("value", executeRequest("readIfExist", serverPort2, cookieStore));
}

@Test(timeout = 20000)
public void test_getAttributeNames_WhenSessionEmpty() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
assertEquals("", executeRequest("names", serverPort1, cookieStore));
}

@Test(timeout = 20000)
public void test_getAttributeNames_WhenSessionNotEmpty() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
executeRequest("write", serverPort1, cookieStore);

assertEquals("key", executeRequest("names", serverPort1, cookieStore));
}

@Test(timeout = 20000)
public void test_removeAttribute() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
executeRequest("write", serverPort1, cookieStore);
executeRequest("remove", serverPort2, cookieStore);

assertEquals("null", executeRequest("read", serverPort1, cookieStore));
}

@Test(timeout = 20000)
public void test_clusterMapSize() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
IMap<String, Object> map = hz.getMap("default");
executeRequest("write", serverPort1, cookieStore);

assertEquals(2, map.size());
}

@Test(timeout = 20000)
public void test_clusterMapSizeAfterRemove() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
IMap<String, Object> map = hz.getMap("default");

executeRequest("write", serverPort1, cookieStore);
executeRequest("remove", serverPort2, cookieStore);

assertEquals(1, map.size());
}

@Test(timeout = 20000)
public void test_updateAttribute() throws Exception {
IMap<String, Object> map = hz.getMap("default");
CookieStore cookieStore = new BasicCookieStore();

executeRequest("write", serverPort1, cookieStore);
executeRequest("update", serverPort2, cookieStore);

assertEquals("value-updated", executeRequest("read", serverPort1, cookieStore));
assertSizeEventually(2,map);
}

@Test(timeout = 20000)
public void test_invalidateSession() throws Exception {
IMap<String, Object> map = hz.getMap("default");
CookieStore cookieStore = new BasicCookieStore();

executeRequest("write", serverPort1, cookieStore);
executeRequest("invalidate", serverPort2, cookieStore);

assertSizeEventually(0,map);
}

@Test(timeout = 20000)
public void test_isNew() throws Exception {
CookieStore cookieStore = new BasicCookieStore();

assertEquals("true", executeRequest("isNew", serverPort1, cookieStore));
assertEquals("false", executeRequest("isNew", serverPort1, cookieStore));
}

@Test(timeout = 20000)
public void test_sessionTimeout() throws Exception {
CookieStore cookieStore = new BasicCookieStore();
IMap<String, Object> map = hz.getMap("default");

executeRequest("write",serverPort1,cookieStore);
executeRequest("timeout",serverPort1,cookieStore);
assertSizeEventually(0,map);
}

@Override
protected ServletContainer getServletContainer(int port, String sourceDir, String serverXml) throws Exception {
return new JettyServer(port, sourceDir, serverXml);
}

@After
public void clearMap() {
IMap<String, Object> map = hz.getMap("default");
map.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protected WebFilterSlowTests(String serverXml1) {

protected WebFilterSlowTests(String serverXml1, String serverXml2) {
super(serverXml1, serverXml2);
isBasicTest = false;
}

@Test
Expand Down

0 comments on commit 1b7c5d2

Please sign in to comment.