PageRenderTime 18ms CodeModel.GetById 7ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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