/hazelcast-hibernate/src/test/java/com/hazelcast/hibernate/CustomPropertiesTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 180 lines · 150 code · 15 blank · 15 comment · 1 complexity · 3a58e9d83de14bc6aa6cbb46d00c57c0 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.hibernate;
  17. import com.hazelcast.client.ClientConfig;
  18. import com.hazelcast.client.HazelcastClient;
  19. import com.hazelcast.config.ClasspathXmlConfig;
  20. import com.hazelcast.config.Config;
  21. import com.hazelcast.config.MapConfig;
  22. import com.hazelcast.core.Hazelcast;
  23. import com.hazelcast.core.HazelcastInstance;
  24. import com.hazelcast.hibernate.entity.DummyEntity;
  25. import com.hazelcast.hibernate.instance.HazelcastAccessor;
  26. import com.hazelcast.hibernate.provider.HazelcastCacheProvider;
  27. import org.hibernate.*;
  28. import org.hibernate.cache.CacheException;
  29. import org.hibernate.cache.CacheKey;
  30. import org.hibernate.cfg.Environment;
  31. import org.hibernate.engine.SessionFactoryImplementor;
  32. import org.junit.*;
  33. import org.junit.runner.RunWith;
  34. import java.util.Date;
  35. import java.util.Properties;
  36. import static org.junit.Assert.*;
  37. @RunWith(TestBlockJUnit4ClassRunner.class)
  38. public class CustomPropertiesTest extends HibernateTestSupport {
  39. @BeforeClass
  40. public static void init() throws Exception {
  41. Hazelcast.shutdownAll();
  42. }
  43. @Before
  44. @After
  45. public void start() {
  46. Hazelcast.shutdownAll();
  47. }
  48. @Test
  49. public void test() {
  50. Properties props = getDefaultProperties();
  51. props.put(CacheEnvironment.SHUTDOWN_ON_STOP, "false");
  52. SessionFactory sf = createSessionFactory(props);
  53. HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf);
  54. assertNotSame(Hazelcast.getDefaultInstance(), hz);
  55. assertEquals(1, hz.getCluster().getMembers().size());
  56. MapConfig cfg = hz.getConfig().getMapConfig("com.hazelcast.hibernate.entity.*");
  57. assertNotNull(cfg);
  58. assertEquals(30, cfg.getTimeToLiveSeconds());
  59. assertEquals(50, cfg.getMaxSizeConfig().getSize());
  60. Hazelcast.getDefaultInstance().getLifecycleService().shutdown();
  61. sf.close();
  62. assertTrue(hz.getLifecycleService().isRunning());
  63. hz.getLifecycleService().shutdown();
  64. }
  65. @Test
  66. public void testLiteMember() throws Exception {
  67. HazelcastInstance main = Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast-custom.xml"));
  68. Properties props = getDefaultProperties();
  69. props.setProperty(CacheEnvironment.USE_LITE_MEMBER, "true");
  70. SessionFactory sf = createSessionFactory(props);
  71. HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf);
  72. assertTrue(hz.getCluster().getLocalMember().isLiteMember());
  73. assertEquals(2, main.getCluster().getMembers().size());
  74. sf.close();
  75. main.getLifecycleService().shutdown();
  76. }
  77. @Test
  78. public void testNativeClient() throws Exception {
  79. HazelcastInstance main = Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast-custom.xml"));
  80. Properties props = getDefaultProperties();
  81. props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName());
  82. props.setProperty(CacheEnvironment.USE_NATIVE_CLIENT, "true");
  83. props.setProperty(CacheEnvironment.NATIVE_CLIENT_GROUP, "dev-custom");
  84. props.setProperty(CacheEnvironment.NATIVE_CLIENT_PASSWORD, "dev-pass");
  85. props.setProperty(CacheEnvironment.NATIVE_CLIENT_ADDRESS, "localhost");
  86. SessionFactory sf = createSessionFactory(props);
  87. HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf);
  88. assertTrue(hz instanceof HazelcastClient);
  89. assertEquals(1, main.getCluster().getMembers().size());
  90. HazelcastClient client = (HazelcastClient) hz;
  91. ClientConfig clientConfig = client.getClientConfig();
  92. assertEquals("dev-custom", clientConfig.getGroupConfig().getName());
  93. assertEquals("dev-pass", clientConfig.getGroupConfig().getPassword());
  94. Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast-custom.xml"));
  95. assertEquals(2, hz.getCluster().getMembers().size());
  96. main.getLifecycleService().shutdown();
  97. Thread.sleep(1000 * 2); // let client to reconnect
  98. assertEquals(1, hz.getCluster().getMembers().size());
  99. Session session = sf.openSession();
  100. Transaction tx = session.beginTransaction();
  101. session.save(new DummyEntity(1L, "dummy", 0, new Date()));
  102. tx.commit();
  103. session.close();
  104. sf.close();
  105. Hazelcast.shutdownAll();
  106. }
  107. @Test
  108. public void testNamedInstance() {
  109. Config config = new Config();
  110. config.setInstanceName("hibernate");
  111. HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
  112. Properties props = getDefaultProperties();
  113. props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName());
  114. props.put(CacheEnvironment.HAZELCAST_INSTANCE_NAME, "hibernate");
  115. props.put(CacheEnvironment.SHUTDOWN_ON_STOP, "false");
  116. final SessionFactory sf = createSessionFactory(props);
  117. assertTrue(hz == HazelcastAccessor.getHazelcastInstance(sf));
  118. sf.close();
  119. assertTrue(hz.getLifecycleService().isRunning());
  120. hz.getLifecycleService().shutdown();
  121. }
  122. @Ignore
  123. @Test(expected = CacheException.class)
  124. public void testTimeout() throws InterruptedException {
  125. Properties props = getDefaultProperties();
  126. props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName());
  127. props.put(CacheEnvironment.LOCK_TIMEOUT_SECONDS, "3");
  128. final SessionFactory sf = createSessionFactory(props);
  129. assertEquals(3000, CacheEnvironment.getLockTimeoutInMillis(props));
  130. final HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf);
  131. final Long id = new Long(1L);
  132. DummyEntity e = new DummyEntity(id, "", 0, null);
  133. Session session = sf.openSession();
  134. Transaction tx = session.beginTransaction();
  135. session.save(e);
  136. tx.commit();
  137. session.close();
  138. new Thread() {
  139. public void run() {
  140. final SessionFactoryImplementor sfi = (SessionFactoryImplementor) sf;
  141. final CacheKey key = new CacheKey(id, Hibernate.LONG,
  142. DummyEntity.class.getName(), EntityMode.POJO, sfi);
  143. assertTrue(hz.getMap(DummyEntity.class.getName()).tryLock(key));
  144. }
  145. }.start();
  146. Thread.sleep(1000);
  147. session = sf.openSession();
  148. try {
  149. e = (DummyEntity) session.get(DummyEntity.class, id);
  150. e.setName("test");
  151. tx = session.beginTransaction();
  152. session.update(e);
  153. tx.commit();
  154. } finally {
  155. session.close();
  156. sf.close();
  157. }
  158. }
  159. private Properties getDefaultProperties() {
  160. Properties props = new Properties();
  161. props.setProperty(Environment.CACHE_PROVIDER, HazelcastCacheProvider.class.getName());
  162. props.setProperty(CacheEnvironment.CONFIG_FILE_PATH_LEGACY, "hazelcast-custom.xml");
  163. return props;
  164. }
  165. }