package com.hazelcast.client.map;

import com.hazelcast.client.HazelcastClient;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.core.MapStoreAdapter;
import com.hazelcast.map.mapstore.MapStoreTest;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.QuickTest;
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.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@RunWith(HazelcastSerialClassRunner.class)
@Category(QuickTest.class)
public class ClientMapLoaderExceptionHandlingTest extends HazelcastTestSupport {

    private static final String mapName = randomMapName();
    private static HazelcastInstance client;
    private static HazelcastInstance server;

    @BeforeClass
    public static void init() {
        final Config config = createNewConfig(mapName);
        server = Hazelcast.newHazelcastInstance(config);
        client = HazelcastClient.newHazelcastClient();
    }

    @AfterClass
    public static void destroy() {
        HazelcastClient.shutdownAll();
        Hazelcast.shutdownAll();
    }

    @Test(expected = ClassCastException.class)
    public void test_initial_map_load_propagates_exception_to_client() throws Exception {
        final IMap<Integer, Integer> map = client.getMap(mapName);
        map.get(1);
    }

    private static Config createNewConfig(String mapName) {
        final ExceptionalMapStore store = new ExceptionalMapStore();
        return MapStoreTest.newConfig(mapName, store, 0);
    }

    private static class ExceptionalMapStore extends MapStoreAdapter {

        @Override
        public Set loadAllKeys() {
            final HashSet<Integer> integers = new HashSet<Integer>();
            for (int i = 0; i < 1000; i++) {
                integers.add(i);
            }
            return integers;
        }

        @Override
        public Map loadAll(Collection keys) {
            throw new ClassCastException("ExceptionalMapStore.loadAll");
        }
    }

    private static void closeResources(HazelcastInstance... instances) {
        if (instances == null) {
            return;
        }
        for (HazelcastInstance instance : instances) {
            instance.shutdown();
        }
    }
}

@@ -310,24 +310,62 @@
 pStoreTest;%0A
+import com.hazelcast.test.AssertTask;%0A
 import com.h
@@ -402,24 +402,24 @@
 lassRunner;%0A
-
 import com.h
@@ -769,16 +769,108 @@
 l.Set;%0A%0A
+import static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertNotNull;%0A%0A
 @RunWith
@@ -1530,45 +1530,8 @@
 Test
-(expected = ClassCastException.class)
 %0A   
@@ -1688,25 +1688,463 @@
 e);%0A
-        map.get(1
+%0A        assertTrueEventually(new AssertTask() %7B%0A            @Override%0A            public void run() throws Exception %7B%0A                Exception exception = null;%0A                try %7B%0A                    map.get(1);%0A                %7D catch (Exception e) %7B%0A                    exception = e;%0A                %7D%0A                assertNotNull(exception);%0A                assertEquals(ClassCastException.class, exception.getClass());%0A%0A            %7D%0A        %7D
 );%0A 

test fix