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

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 94 lines · 69 code · 8 blank · 17 comment · 2 complexity · 7e5e9f5a922b67288cddb75900feaed9 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.core.Hazelcast;
  18. import com.hazelcast.core.HazelcastInstance;
  19. import com.hazelcast.hibernate.entity.DummyEntity;
  20. import org.hibernate.Session;
  21. import org.hibernate.Transaction;
  22. import org.hibernate.cfg.Environment;
  23. import org.junit.BeforeClass;
  24. import org.junit.Test;
  25. import org.junit.runner.RunWith;
  26. import java.util.ArrayList;
  27. import java.util.Date;
  28. import java.util.List;
  29. import java.util.Properties;
  30. import static org.junit.Assert.assertEquals;
  31. import static org.junit.Assert.assertNotNull;
  32. @RunWith(TestBlockJUnit4ClassRunner.class)
  33. public class LocalRegionFactoryDefaultTest extends RegionFactoryDefaultTest {
  34. @BeforeClass
  35. public static void init() throws Exception {
  36. Hazelcast.shutdownAll();
  37. }
  38. protected Properties getCacheProperties() {
  39. Properties props = new Properties();
  40. props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastLocalCacheRegionFactory.class.getName());
  41. return props;
  42. }
  43. @Test
  44. public void testEntity() {
  45. final HazelcastInstance hz = getHazelcastInstance();
  46. assertNotNull(hz);
  47. assertEquals(Hazelcast.getDefaultInstance(), hz);
  48. final int count = 100;
  49. final int childCount = 3;
  50. insertDummyEntities(count, childCount);
  51. sleep(1);
  52. List<DummyEntity> list = new ArrayList<DummyEntity>(count);
  53. Session session = sf.openSession();
  54. try {
  55. for (int i = 0; i < count; i++) {
  56. DummyEntity e = (DummyEntity) session.get(DummyEntity.class, new Long(i));
  57. session.evict(e);
  58. list.add(e);
  59. }
  60. } finally {
  61. session.close();
  62. }
  63. session = sf.openSession();
  64. Transaction tx = session.beginTransaction();
  65. try {
  66. for (DummyEntity dummy : list) {
  67. dummy.setDate(new Date());
  68. session.update(dummy);
  69. }
  70. tx.commit();
  71. } catch (Exception e) {
  72. tx.rollback();
  73. e.printStackTrace();
  74. } finally {
  75. session.close();
  76. }
  77. assertEquals((childCount + 1) * count, stats.getEntityInsertCount());
  78. // twice put of entity and properties (on load and update) and once put of collection
  79. assertEquals((childCount + 1) * count * 2 + count, stats.getSecondLevelCachePutCount());
  80. assertEquals(childCount * count, stats.getEntityLoadCount());
  81. assertEquals(count, stats.getSecondLevelCacheHitCount());
  82. // collection cache miss
  83. assertEquals(count, stats.getSecondLevelCacheMissCount());
  84. stats.logSummary();
  85. }
  86. }