<commit_before>/*
 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.spring.cache;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spring.CustomSpringJUnit4ClassRunner;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.test.context.ContextConfiguration;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

@RunWith(CustomSpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"cacheManager-applicationContext-hazelcast.xml"})
@Category(QuickTest.class)
public class TestCacheManager {

    @Resource(name = "instance")
    private HazelcastInstance instance;

    @Autowired
    private IDummyBean bean;

    @Autowired
    private CacheManager cacheManager;

    @BeforeClass
    @AfterClass
    public static void start() {
        Hazelcast.shutdownAll();
    }

    @Test
    public void test() {
        for (int i = 0; i < 100; i++) {
            assertEquals("name:" + i, bean.getName(i));
            assertEquals("city:" + i, bean.getCity(i));
        }
    }

    @Test
    public void testNull() {
        for (int i = 0; i < 100; i++) {
            assertNull(bean.getNull());
        }
    }

    @Test
    public void testTTL() throws InterruptedException {
        final String name = bean.getNameWithTTL();
        assertEquals("ali", name);
        final String nameFromCache = bean.getNameWithTTL();
        assertEquals("ali", nameFromCache);

        Thread.sleep(3000);

        final String nameFromCacheAfterTTL = bean.getNameWithTTL();
        assertNull(nameFromCacheAfterTTL);

    }

    @Test
    public void testCacheNames() throws InterruptedException {

        //Create a test instance, to reproduce the behaviour stated in the
        //github issue: https://github.com/hazelcast/hazelcast/issues/492
        String testMap = "test-map";
        HazelcastInstance testInstance = Hazelcast.newHazelcastInstance();
        testInstance.getMap(testMap);

        //Be sure that test-map is distrubuted
        Thread.sleep(3000);
<commit_msg>Removed Thread.sleep from test case testCacheNames<commit_after>/*
 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.spring.cache;

import com.hazelcast.core.*;
import com.hazelcast.spring.CustomSpringJUnit4ClassRunner;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.test.context.ContextConfiguration;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

@RunWith(CustomSpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"cacheManager-applicationContext-hazelcast.xml"})
@Category(QuickTest.class)
public class TestCacheManager {

    @Resource(name = "instance")
    private HazelcastInstance instance;

    @Autowired
    private IDummyBean bean;

    @Autowired
    private CacheManager cacheManager;

    @BeforeClass
    @AfterClass
    public static void start() {
        Hazelcast.shutdownAll();
    }

    @Test
    public void test() {
        for (int i = 0; i < 100; i++) {
            assertEquals("name:" + i, bean.getName(i));
            assertEquals("city:" + i, bean.getCity(i));
        }
    }

    @Test
    public void testNull() {
        for (int i = 0; i < 100; i++) {
            assertNull(bean.getNull());
        }
    }

    @Test
    public void testTTL() throws InterruptedException {
        final String name = bean.getNameWithTTL();
        assertEquals("ali", name);
        final String nameFromCache = bean.getNameWithTTL();
        assertEquals("ali", nameFromCache);

        Thread.sleep(3000);

        final String nameFromCacheAfterTTL = bean.getNameWithTTL();
        assertNull(nameFromCacheAfterTTL);

    }

    @Test
    public void testCacheNames() throws InterruptedException {

        //Create a test instance, to reproduce the behaviour stated in the
        //github issue: https://github.com/hazelcast/hazelcast/issues/492
        final String testMap = "test-map";

        final CountDownLatch distributionSignal = new CountDownLatch(1);
        instance.addDistributedObjectListener(new DistributedObjectListener() {

            @Override
            public void distributedObjectCreated(DistributedObjectEvent event) {
                DistributedObject distributedObject = event.getDistributedObject();
                if(distributedObject instanceof IMap) {
                    IMap<?, ?> map = (IMap) distributedObject;
                    if(testMap.equals(map.getName())) {
                        distributionSignal.countDown();
                    }
                }
            }

            @Override
            public void distributedObjectDestroyed(DistributedObjectEvent event) {

            }
        });

        HazelcastInstance testInstance = Hazelcast.newHazelcastInstance();
        testInstance.getMap(testMap);
        //Be sure that test-map is distrubuted
        distributionSignal.await();

{'fraction_non_alphanumeric': 0.0944506726457399, 'fraction_numerical': 0.00882847533632287, 'mean_word_length': 3.243162901307967, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4743544', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1904, 'n_words': 521}