Package com.hazelcast.client.standalone

Source Code of com.hazelcast.client.standalone.ClientMapStandaloneTest

/*
* 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.client.standalone;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.standalone.model.MyElement;
import com.hazelcast.client.standalone.model.MyKey;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.annotation.QuickTest;
import com.hazelcast.util.FilteringClassLoader;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import static com.hazelcast.test.HazelcastTestSupport.randomString;
import static org.junit.Assert.assertEquals;

@RunWith(HazelcastSerialClassRunner.class)
@Category(QuickTest.class)
public class ClientMapStandaloneTest {

    private static final ClassLoader FILTERING_CLASS_LOADER;

    static HazelcastInstance client;

    static {
        List<String> excludes = Arrays.asList(new String[]{"com.hazelcast.client.standalone.model"});
        FILTERING_CLASS_LOADER = new FilteringClassLoader(excludes, "com.hazelcast");
    }

    @BeforeClass
    public static void init()
            throws Exception {

        Thread thread = Thread.currentThread();
        ClassLoader tccl = thread.getContextClassLoader();
        thread.setContextClassLoader(FILTERING_CLASS_LOADER);

        try {
            Class<?> configClazz = FILTERING_CLASS_LOADER.loadClass("com.hazelcast.config.Config");
            Object config = configClazz.newInstance();
            Method setClassLoader = configClazz.getDeclaredMethod("setClassLoader", ClassLoader.class);

            setClassLoader.invoke(config, FILTERING_CLASS_LOADER);

            Class<?> hazelcastClazz = FILTERING_CLASS_LOADER.loadClass("com.hazelcast.core.Hazelcast");
            Method newHazelcastInstance = hazelcastClazz.getDeclaredMethod("newHazelcastInstance", configClazz);

            newHazelcastInstance.invoke(hazelcastClazz, config);
        } finally {
            thread.setContextClassLoader(tccl);
        }
        client = HazelcastClient.newHazelcastClient(null);
    }

    public IMap createMap() {
        return client.getMap(randomString());
    }

    @AfterClass
    public static void destroy()
            throws Exception {

        client.shutdown();

        Class<?> hazelcastClazz = FILTERING_CLASS_LOADER.loadClass("com.hazelcast.core.Hazelcast");
        Method shutdownAll = hazelcastClazz.getDeclaredMethod("shutdownAll");
        shutdownAll.invoke(hazelcastClazz);
    }

    @Test
    public void testPut()
            throws Exception {

        MyKey key = new MyKey();
        MyElement element = new MyElement(randomString());

        Thread thread = Thread.currentThread();
        ClassLoader tccl = thread.getContextClassLoader();
        thread.setContextClassLoader(FILTERING_CLASS_LOADER);

        try {
            IMap<MyKey, MyElement> map = createMap();
            map.put(key, element);
        } finally {
            thread.setContextClassLoader(tccl);
        }
    }

    @Test
    public void testGet()
            throws Exception {

        IMap<MyKey, MyElement> map = createMap();

        MyKey key = new MyKey();
        MyElement element = new MyElement(randomString());

        Thread thread = Thread.currentThread();
        ClassLoader tccl = thread.getContextClassLoader();
        thread.setContextClassLoader(FILTERING_CLASS_LOADER);

        try {
            map.put(key, element);
            MyElement result = map.get(key);
            assertEquals(element, result);
        } finally {
            thread.setContextClassLoader(tccl);
        }

    }

    @Test
    public void testRemove()
            throws Exception {

        IMap<MyKey, MyElement> map = createMap();

        MyKey key = new MyKey();
        MyElement element = new MyElement(randomString());

        Thread thread = Thread.currentThread();
        ClassLoader tccl = thread.getContextClassLoader();
        thread.setContextClassLoader(FILTERING_CLASS_LOADER);

        try {
            map.put(key, element);
            MyElement result = map.remove(key);
            assertEquals(element, result);
        } finally {
            thread.setContextClassLoader(tccl);
        }
    }

    @Test
    public void testClear()
            throws Exception {

        IMap<MyKey, MyElement> map = createMap();

        Thread thread = Thread.currentThread();
        ClassLoader tccl = thread.getContextClassLoader();
        thread.setContextClassLoader(FILTERING_CLASS_LOADER);

        try {
            MyKey key = new MyKey();
            MyElement element = new MyElement(randomString());
            map.put(key, element);
            map.clear();
        } finally {
            thread.setContextClassLoader(tccl);
        }
    }
}
TOP

Related Classes of com.hazelcast.client.standalone.ClientMapStandaloneTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.