PageRenderTime 40ms CodeModel.GetById 1ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 1ms

/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
 17package com.hazelcast.hibernate;
 18
 19import com.hazelcast.client.ClientConfig;
 20import com.hazelcast.client.HazelcastClient;
 21import com.hazelcast.config.ClasspathXmlConfig;
 22import com.hazelcast.config.Config;
 23import com.hazelcast.config.MapConfig;
 24import com.hazelcast.core.Hazelcast;
 25import com.hazelcast.core.HazelcastInstance;
 26import com.hazelcast.hibernate.entity.DummyEntity;
 27import com.hazelcast.hibernate.instance.HazelcastAccessor;
 28import com.hazelcast.hibernate.provider.HazelcastCacheProvider;
 29import org.hibernate.*;
 30import org.hibernate.cache.CacheException;
 31import org.hibernate.cache.CacheKey;
 32import org.hibernate.cfg.Environment;
 33import org.hibernate.engine.SessionFactoryImplementor;
 34import org.junit.*;
 35import org.junit.runner.RunWith;
 36
 37import java.util.Date;
 38import java.util.Properties;
 39
 40import static org.junit.Assert.*;
 41
 42@RunWith(TestBlockJUnit4ClassRunner.class)
 43public class CustomPropertiesTest extends HibernateTestSupport {
 44
 45    @BeforeClass
 46    public static void init() throws Exception {
 47        Hazelcast.shutdownAll();
 48    }
 49
 50    @Before
 51    @After
 52    public void start() {
 53        Hazelcast.shutdownAll();
 54    }
 55
 56    @Test
 57    public void test() {
 58        Properties props = getDefaultProperties();
 59        props.put(CacheEnvironment.SHUTDOWN_ON_STOP, "false");
 60        SessionFactory sf = createSessionFactory(props);
 61        HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf);
 62        assertNotSame(Hazelcast.getDefaultInstance(), hz);
 63        assertEquals(1, hz.getCluster().getMembers().size());
 64        MapConfig cfg = hz.getConfig().getMapConfig("com.hazelcast.hibernate.entity.*");
 65        assertNotNull(cfg);
 66        assertEquals(30, cfg.getTimeToLiveSeconds());
 67        assertEquals(50, cfg.getMaxSizeConfig().getSize());
 68        Hazelcast.getDefaultInstance().getLifecycleService().shutdown();
 69        sf.close();
 70        assertTrue(hz.getLifecycleService().isRunning());
 71        hz.getLifecycleService().shutdown();
 72    }
 73
 74    @Test
 75    public void testLiteMember() throws Exception {
 76        HazelcastInstance main = Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast-custom.xml"));
 77        Properties props = getDefaultProperties();
 78        props.setProperty(CacheEnvironment.USE_LITE_MEMBER, "true");
 79        SessionFactory sf = createSessionFactory(props);
 80        HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf);
 81        assertTrue(hz.getCluster().getLocalMember().isLiteMember());
 82        assertEquals(2, main.getCluster().getMembers().size());
 83        sf.close();
 84        main.getLifecycleService().shutdown();
 85    }
 86
 87    @Test
 88    public void testNativeClient() throws Exception {
 89        HazelcastInstance main = Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast-custom.xml"));
 90        Properties props = getDefaultProperties();
 91        props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName());
 92        props.setProperty(CacheEnvironment.USE_NATIVE_CLIENT, "true");
 93        props.setProperty(CacheEnvironment.NATIVE_CLIENT_GROUP, "dev-custom");
 94        props.setProperty(CacheEnvironment.NATIVE_CLIENT_PASSWORD, "dev-pass");
 95        props.setProperty(CacheEnvironment.NATIVE_CLIENT_ADDRESS, "localhost");
 96        SessionFactory sf = createSessionFactory(props);
 97        HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf);
 98        assertTrue(hz instanceof HazelcastClient);
 99        assertEquals(1, main.getCluster().getMembers().size());
100        HazelcastClient client = (HazelcastClient) hz;
101        ClientConfig clientConfig = client.getClientConfig();
102        assertEquals("dev-custom", clientConfig.getGroupConfig().getName());
103        assertEquals("dev-pass", clientConfig.getGroupConfig().getPassword());
104        Hazelcast.newHazelcastInstance(new ClasspathXmlConfig("hazelcast-custom.xml"));
105        assertEquals(2, hz.getCluster().getMembers().size());
106        main.getLifecycleService().shutdown();
107        Thread.sleep(1000 * 2); // let client to reconnect
108        assertEquals(1, hz.getCluster().getMembers().size());
109
110        Session session = sf.openSession();
111        Transaction tx = session.beginTransaction();
112        session.save(new DummyEntity(1L, "dummy", 0, new Date()));
113        tx.commit();
114        session.close();
115
116        sf.close();
117        Hazelcast.shutdownAll();
118    }
119
120    @Test
121    public void testNamedInstance() {
122        Config config = new Config();
123        config.setInstanceName("hibernate");
124        HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
125        Properties props = getDefaultProperties();
126        props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName());
127        props.put(CacheEnvironment.HAZELCAST_INSTANCE_NAME, "hibernate");
128        props.put(CacheEnvironment.SHUTDOWN_ON_STOP, "false");
129        final SessionFactory sf = createSessionFactory(props);
130        assertTrue(hz == HazelcastAccessor.getHazelcastInstance(sf));
131        sf.close();
132        assertTrue(hz.getLifecycleService().isRunning());
133        hz.getLifecycleService().shutdown();
134    }
135
136    @Ignore
137    @Test(expected = CacheException.class)
138    public void testTimeout() throws InterruptedException {
139        Properties props = getDefaultProperties();
140        props.setProperty(Environment.CACHE_REGION_FACTORY, HazelcastCacheRegionFactory.class.getName());
141        props.put(CacheEnvironment.LOCK_TIMEOUT_SECONDS, "3");
142        final SessionFactory sf = createSessionFactory(props);
143        assertEquals(3000, CacheEnvironment.getLockTimeoutInMillis(props));
144        final HazelcastInstance hz = HazelcastAccessor.getHazelcastInstance(sf);
145        final Long id = new Long(1L);
146        DummyEntity e = new DummyEntity(id, "", 0, null);
147        Session session = sf.openSession();
148        Transaction tx = session.beginTransaction();
149        session.save(e);
150        tx.commit();
151        session.close();
152        new Thread() {
153            public void run() {
154                final SessionFactoryImplementor sfi = (SessionFactoryImplementor) sf;
155                final CacheKey key = new CacheKey(id, Hibernate.LONG,
156                        DummyEntity.class.getName(), EntityMode.POJO, sfi);
157                assertTrue(hz.getMap(DummyEntity.class.getName()).tryLock(key));
158            }
159        }.start();
160        Thread.sleep(1000);
161        session = sf.openSession();
162        try {
163            e = (DummyEntity) session.get(DummyEntity.class, id);
164            e.setName("test");
165            tx = session.beginTransaction();
166            session.update(e);
167            tx.commit();
168        } finally {
169            session.close();
170            sf.close();
171        }
172    }
173
174    private Properties getDefaultProperties() {
175        Properties props = new Properties();
176        props.setProperty(Environment.CACHE_PROVIDER, HazelcastCacheProvider.class.getName());
177        props.setProperty(CacheEnvironment.CONFIG_FILE_PATH_LEGACY, "hazelcast-custom.xml");
178        return props;
179    }
180}