diff --git a/spring-session/build.gradle b/spring-session/build.gradle index f608fdb..ff5e57d 100644 --- a/spring-session/build.gradle +++ b/spring-session/build.gradle @@ -24,7 +24,8 @@ dependencies { "org.springframework:spring-websocket:$springVersion" provided "javax.servlet:javax.servlet-api:$servletApiVersion" integrationTestCompile "redis.clients:jedis:2.4.1", - "org.apache.commons:commons-pool2:2.2" + "org.apache.commons:commons-pool2:2.2", + "com.hazelcast:hazelcast-client:$hazelcastVersion" integrationTestRuntime "org.springframework.shell:spring-shell:1.0.0.RELEASE" diff --git a/spring-session/src/integration-test/java/org/springframework/session/hazelcast/HazelcastRepositoryITests.java b/spring-session/src/integration-test/java/org/springframework/session/hazelcast/AbstractHazelcastRepositoryITests.java similarity index 52% rename from spring-session/src/integration-test/java/org/springframework/session/hazelcast/HazelcastRepositoryITests.java rename to spring-session/src/integration-test/java/org/springframework/session/hazelcast/AbstractHazelcastRepositoryITests.java index c6150b5..07dc817 100644 --- a/spring-session/src/integration-test/java/org/springframework/session/hazelcast/HazelcastRepositoryITests.java +++ b/spring-session/src/integration-test/java/org/springframework/session/hazelcast/AbstractHazelcastRepositoryITests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,77 +13,50 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.session.hazelcast; -import static org.assertj.core.api.Assertions.*; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.session.ExpiringSession; -import org.springframework.session.SessionRepository; -import org.springframework.session.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.util.SocketUtils; - -import com.hazelcast.config.Config; -import com.hazelcast.config.NetworkConfig; -import com.hazelcast.core.Hazelcast; import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.IMap; +import org.junit.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.session.ExpiringSession; +import org.springframework.session.SessionRepository; + +import static org.assertj.core.api.Assertions.assertThat; /** - * Integration tests that check the underlying data source - in this case - * Hazelcast. + * Abstract base class for Hazelcast integration tests. * * @author Tommy Ludwig + * @author Vedran Pavic */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration -@WebAppConfiguration -public class HazelcastRepositoryITests { +public abstract class AbstractHazelcastRepositoryITests { @Autowired private HazelcastInstance hazelcast; - + @Autowired private SessionRepository repository; - + @Test public void createAndDestorySession() { S sessionToSave = repository.createSession(); String sessionId = sessionToSave.getId(); - + IMap hazelcastMap = hazelcast.getMap("spring:session:sessions"); - + assertThat(hazelcastMap.size()).isEqualTo(0); - + repository.save(sessionToSave); - + assertThat(hazelcastMap.size()).isEqualTo(1); assertThat(hazelcastMap.get(sessionId)).isEqualTo(sessionToSave); - + repository.delete(sessionId); - + assertThat(hazelcastMap.size()).isEqualTo(0); } - @EnableHazelcastHttpSession - @Configuration - static class HazelcastSessionConfig { - - @Bean - public HazelcastInstance embeddedHazelcast() { - Config hazelcastConfig = new Config(); - NetworkConfig netConfig = new NetworkConfig(); - netConfig.setPort(SocketUtils.findAvailableTcpPort()); - hazelcastConfig.setNetworkConfig(netConfig); - return Hazelcast.newHazelcastInstance(hazelcastConfig); - } - } - } diff --git a/spring-session/src/integration-test/java/org/springframework/session/hazelcast/HazelcastClientRepositoryITests.java b/spring-session/src/integration-test/java/org/springframework/session/hazelcast/HazelcastClientRepositoryITests.java new file mode 100644 index 0000000..58dffc6 --- /dev/null +++ b/spring-session/src/integration-test/java/org/springframework/session/hazelcast/HazelcastClientRepositoryITests.java @@ -0,0 +1,74 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * 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 org.springframework.session.hazelcast; + +import com.hazelcast.client.HazelcastClient; +import com.hazelcast.client.config.ClientConfig; +import com.hazelcast.core.Hazelcast; +import com.hazelcast.core.HazelcastInstance; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.session.ExpiringSession; +import org.springframework.session.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.util.SocketUtils; + +/** + * Integration tests that check the underlying data source - in this case + * Hazelcast Client. + * + * @author Vedran Pavic + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@WebAppConfiguration +public class HazelcastClientRepositoryITests + extends AbstractHazelcastRepositoryITests { + + private static final int PORT = SocketUtils.findAvailableTcpPort(); + + @BeforeClass + public static void setup() { + HazelcastITestUtils.embeddedHazelcastServer(PORT); + } + + @AfterClass + public static void teardown() { + Hazelcast.shutdownAll(); + } + + @Configuration + @EnableHazelcastHttpSession + static class HazelcastSessionConfig { + + @Bean + public HazelcastInstance embeddedHazelcastClient() { + ClientConfig clientConfig = new ClientConfig(); + clientConfig.getNetworkConfig() + .addAddress("127.0.0.1:" + PORT); + return HazelcastClient.newHazelcastClient(clientConfig); + } + + } + +} diff --git a/spring-session/src/integration-test/java/org/springframework/session/hazelcast/HazelcastITestUtils.java b/spring-session/src/integration-test/java/org/springframework/session/hazelcast/HazelcastITestUtils.java new file mode 100644 index 0000000..bd7b819 --- /dev/null +++ b/spring-session/src/integration-test/java/org/springframework/session/hazelcast/HazelcastITestUtils.java @@ -0,0 +1,52 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * 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 org.springframework.session.hazelcast; + +import com.hazelcast.config.Config; +import com.hazelcast.core.Hazelcast; +import com.hazelcast.core.HazelcastInstance; + +import org.springframework.util.SocketUtils; + +/** + * Utility class for Hazelcast integration tests. + * + * @author Vedran Pavic + */ +public class HazelcastITestUtils { + + /** + * Creates {@link HazelcastInstance} for use in integration tests. + * @param port the port for Hazelcast to bind to + * @return the Hazelcast instance + */ + public static HazelcastInstance embeddedHazelcastServer(int port) { + Config config = new Config(); + config.getNetworkConfig() + .setPort(port); + return Hazelcast.newHazelcastInstance(config); + } + + /** + * Creates {@link HazelcastInstance} for use in integration tests. + * @return the Hazelcast instance + */ + public static HazelcastInstance embeddedHazelcastServer() { + return embeddedHazelcastServer(SocketUtils.findAvailableTcpPort()); + } + +} diff --git a/spring-session/src/integration-test/java/org/springframework/session/hazelcast/HazelcastServerRepositoryITests.java b/spring-session/src/integration-test/java/org/springframework/session/hazelcast/HazelcastServerRepositoryITests.java new file mode 100644 index 0000000..5e87114 --- /dev/null +++ b/spring-session/src/integration-test/java/org/springframework/session/hazelcast/HazelcastServerRepositoryITests.java @@ -0,0 +1,54 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * 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 org.springframework.session.hazelcast; + +import com.hazelcast.core.HazelcastInstance; +import org.junit.runner.RunWith; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.session.ExpiringSession; +import org.springframework.session.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +/** + * Integration tests that check the underlying data source - in this case + * Hazelcast Server. + * + * @author Tommy Ludwig + * @author Vedran Pavic + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +@WebAppConfiguration +public class HazelcastServerRepositoryITests + extends AbstractHazelcastRepositoryITests { + + @EnableHazelcastHttpSession + @Configuration + static class HazelcastSessionConfig { + + @Bean + public HazelcastInstance embeddedHazelcastServer() { + return HazelcastITestUtils.embeddedHazelcastServer(); + } + + } + +} diff --git a/spring-session/src/integration-test/java/org/springframework/session/hazelcast/config/annotation/web/http/EnableHazelcastHttpSessionEventsTests.java b/spring-session/src/integration-test/java/org/springframework/session/hazelcast/config/annotation/web/http/EnableHazelcastHttpSessionEventsTests.java index aa88bb1..b4b1aab 100644 --- a/spring-session/src/integration-test/java/org/springframework/session/hazelcast/config/annotation/web/http/EnableHazelcastHttpSessionEventsTests.java +++ b/spring-session/src/integration-test/java/org/springframework/session/hazelcast/config/annotation/web/http/EnableHazelcastHttpSessionEventsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,15 +36,11 @@ import org.springframework.session.data.SessionEventRegistry; import org.springframework.session.events.SessionCreatedEvent; import org.springframework.session.events.SessionDeletedEvent; import org.springframework.session.events.SessionExpiredEvent; -import org.springframework.session.hazelcast.config.annotation.web.http.EnableHazelcastHttpSession; +import org.springframework.session.hazelcast.HazelcastITestUtils; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.util.SocketUtils; -import com.hazelcast.config.Config; -import com.hazelcast.config.NetworkConfig; -import com.hazelcast.core.Hazelcast; import com.hazelcast.core.HazelcastInstance; /** @@ -169,11 +165,7 @@ public class EnableHazelcastHttpSessionEventsTests { @Bean public HazelcastInstance embeddedHazelcast() { - Config hazelcastConfig = new Config(); - NetworkConfig netConfig = new NetworkConfig(); - netConfig.setPort(SocketUtils.findAvailableTcpPort()); - hazelcastConfig.setNetworkConfig(netConfig); - return Hazelcast.newHazelcastInstance(hazelcastConfig); + return HazelcastITestUtils.embeddedHazelcastServer(); } @Bean