/hazelcast-spring/src/test/java/com/hazelcast/spring/cache/TestCacheManager.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 96 lines · 65 code · 16 blank · 15 comment · 3 complexity · 58e6598f43a21e2316a7aff9331945be 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.cache;
  17. import com.hazelcast.core.Hazelcast;
  18. import com.hazelcast.core.HazelcastInstance;
  19. import com.hazelcast.impl.GroupProperties;
  20. import com.hazelcast.spring.CustomSpringJUnit4ClassRunner;
  21. import org.junit.AfterClass;
  22. import org.junit.Assert;
  23. import org.junit.BeforeClass;
  24. import org.junit.Test;
  25. import org.junit.runner.RunWith;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.cache.annotation.Cacheable;
  28. import org.springframework.test.context.ContextConfiguration;
  29. import javax.annotation.Resource;
  30. import java.util.concurrent.atomic.AtomicBoolean;
  31. @RunWith(CustomSpringJUnit4ClassRunner.class)
  32. @ContextConfiguration(locations = {"cacheManager-applicationContext-hazelcast.xml"})
  33. public class TestCacheManager {
  34. static {
  35. System.setProperty(GroupProperties.PROP_VERSION_CHECK_ENABLED, "false");
  36. }
  37. @Resource(name = "instance")
  38. private HazelcastInstance instance;
  39. @Autowired
  40. private IDummyBean bean;
  41. @BeforeClass
  42. @AfterClass
  43. public static void start() {
  44. Hazelcast.shutdownAll();
  45. }
  46. @Test
  47. public void test() {
  48. for (int i = 0; i < 100; i++) {
  49. Assert.assertEquals("name:" + i, bean.getName(i));
  50. Assert.assertEquals("city:" + i, bean.getCity(i));
  51. }
  52. }
  53. @Test
  54. public void testNull() {
  55. for (int i = 0; i < 100; i++) {
  56. Assert.assertNull(bean.getNull());
  57. }
  58. }
  59. public static class DummyBean implements IDummyBean {
  60. @Cacheable("name")
  61. public String getName(int k) {
  62. Assert.fail("should not call this method!");
  63. return null;
  64. }
  65. @Cacheable("city")
  66. public String getCity(int k) {
  67. Assert.fail("should not call this method!");
  68. return null;
  69. }
  70. final AtomicBoolean nullCall = new AtomicBoolean(false);
  71. @Cacheable("null-map")
  72. public Object getNull() {
  73. if (nullCall.compareAndSet(false, true)) {
  74. return null;
  75. }
  76. Assert.fail("should not call this method!");
  77. return null;
  78. }
  79. }
  80. }