PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast-spring/src/test/java/com/hazelcast/spring/context/TestManagedContext.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 114 lines | 78 code | 18 blank | 18 comment | 0 complexity | 1168ef9da9b71d62a441511032c68480 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.spring.context;
 18
 19import com.hazelcast.core.DistributedTask;
 20import com.hazelcast.core.Hazelcast;
 21import com.hazelcast.core.HazelcastInstance;
 22import com.hazelcast.impl.GroupProperties;
 23import com.hazelcast.spring.CustomSpringJUnit4ClassRunner;
 24import org.junit.AfterClass;
 25import org.junit.Assert;
 26import org.junit.BeforeClass;
 27import org.junit.Test;
 28import org.junit.runner.RunWith;
 29import org.springframework.beans.factory.annotation.Autowired;
 30import org.springframework.context.ApplicationContext;
 31import org.springframework.test.context.ContextConfiguration;
 32
 33import javax.annotation.Resource;
 34import java.util.concurrent.ExecutionException;
 35import java.util.concurrent.Future;
 36
 37/**
 38 * @mdogan 4/6/12
 39 */
 40@RunWith(CustomSpringJUnit4ClassRunner.class)
 41@ContextConfiguration(locations = {"managedContext-applicationContext-hazelcast.xml"})
 42public class TestManagedContext {
 43
 44    static {
 45        System.setProperty(GroupProperties.PROP_VERSION_CHECK_ENABLED, "false");
 46    }
 47
 48    @Resource(name = "instance1")
 49    private HazelcastInstance instance1;
 50
 51    @Resource(name = "instance2")
 52    private HazelcastInstance instance2;
 53
 54    @Autowired
 55    private ApplicationContext context;
 56
 57    @Autowired
 58    private DummyTransactionManager transactionManager;
 59
 60    @Autowired
 61    private SomeBean bean;
 62
 63    @BeforeClass
 64    @AfterClass
 65    public static void start() {
 66        Hazelcast.shutdownAll();
 67    }
 68
 69    @Test
 70    public void testSerialization() throws InterruptedException {
 71        instance1.getMap("test").put(1L, new SomeValue());
 72        SomeValue v = (SomeValue) instance1.getMap("test").get(1L);
 73        Assert.assertNotNull(v.context);
 74        Assert.assertNotNull(v.someBean);
 75        Assert.assertEquals(context, v.context);
 76        Assert.assertEquals(bean, v.someBean);
 77        Assert.assertTrue(v.init);
 78    }
 79
 80    @Test
 81    public void testDistributedTask() throws ExecutionException, InterruptedException {
 82        SomeTask task = (SomeTask) context.getBean("someTask");
 83        Future<Long> f = instance1.getExecutorService().submit(task);
 84        Assert.assertEquals(bean.value, f.get().longValue());
 85
 86        Future<Long> f2 = (Future<Long>) instance1.getExecutorService()
 87                .submit(new DistributedTask<Long>(new SomeTask()));
 88        Assert.assertEquals(bean.value, f2.get().longValue());
 89    }
 90
 91    @Test
 92    public void testTransactionalTask() throws ExecutionException, InterruptedException {
 93        Future f = instance1.getExecutorService().submit(new DistributedTask(new SomeTransactionalTask(),
 94                instance2.getCluster().getLocalMember()));
 95        f.get();
 96        Assert.assertTrue("transaction manager could not proxy the submitted task.",
 97                transactionManager.isCommitted());
 98    }
 99
100    @Test
101    public void testRunnableTask() throws ExecutionException, InterruptedException {
102        Future<?> future = instance1.getExecutorService().submit(new SomeRunnableTask());
103        future.get();
104    }
105
106    @Test
107    public void testTransactionalRunnableTask() throws ExecutionException, InterruptedException {
108        Future<?> future = instance1.getExecutorService().submit(new SomeTransactionalRunnableTask());
109        future.get();
110        Assert.assertTrue("transaction manager could not proxy the submitted task.",
111                transactionManager.isCommitted());
112    }
113
114}