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.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 {

    @Test(expected = ClassCastException.class)
    public void test_initial_map_load_propagates_exception_to_client() throws Exception {
        final String mapName = randomMapName();
        final Config config = createNewConfig(mapName);
        final HazelcastInstance server = Hazelcast.newHazelcastInstance(config);
        final HazelcastInstance client = HazelcastClient.newHazelcastClient();
        final IMap<Integer, Integer> map = client.getMap(mapName);
        try {
            map.get(1);
        } finally {
            closeResources(client, server);
        }
    }

    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();
        }
    }
}

@@ -460,24 +460,83 @@
 .QuickTest;%0A
+import org.junit.AfterClass;%0Aimport org.junit.BeforeClass;%0A
 import org.j
@@ -891,188 +891,202 @@
     
-@Test(expected = ClassCastException.class)%0A    public void 
+private static final String mapName = randomMapName();%0A    priva
 te
+ 
 st
-_initial_map_load_propagates_exception_to_client() throws Exception %7B%0A        final String mapName = randomMapName();
+atic HazelcastInstance client;%0A    private static HazelcastInstance server;%0A%0A    @BeforeClass%0A    public static void init() %7B
 %0A   
@@ -1150,32 +1150,8 @@
     
-final HazelcastInstance 
 serv
@@ -1207,32 +1207,8 @@
     
-final HazelcastInstance 
 clie
@@ -1258,182 +1258,364 @@
     
+%7D%0A%0A
     
-final IMap%3CInteger, Integer%3E map = client.getMap(mapName);%0A        try %7B
+@AfterClass%0A    public static void destroy() %7B%0A        HazelcastClient.shutdownAll();%0A        Hazelcast.shutdownAll();
 %0A    
+%7D%0A%0A
     
-    map.get(1);%0A        %7D finally %7B%0A            closeResources(client, server);%0A        %7D
+@Test(expected = ClassCastException.class)%0A    public void test_initial_map_load_propagates_exception_to_client() throws Exception %7B%0A        final IMap%3CInteger, Integer%3E map = client.getMap(mapName);%0A        map.get(1);
 %0A   

test fix