PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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