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