/*
 * Copyright (c) 2008-2018, 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.starter;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ICompletableFuture;
import com.hazelcast.core.IMap;
import com.hazelcast.test.HazelcastSerialClassRunner;
import com.hazelcast.test.annotation.SlowTest;
import com.hazelcast.test.starter.HazelcastStarter;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.util.concurrent.ExecutionException;

import static org.junit.Assert.assertEquals;

@RunWith(HazelcastSerialClassRunner.class)
@Category(SlowTest.class)
public class HazelcastClientStarterTest {
    @Test
    public void testClientLifecycle() {
        HazelcastInstance member = null;
        try {
            member = HazelcastStarter.newHazelcastInstance("3.7");

            for (int i = 1; i < 6; i++) {
                String version = "3.7." + i;
                System.out.println("Starting client " + version);
                HazelcastInstance instance = HazelcastClientStarter.newHazelcastClient(version, false);
                System.out.println("Stopping client " + version);
                instance.shutdown();
            }

        } finally {
            if (member != null) {
                member.shutdown();
            }
        }
    }

    @Test
    public void testClientMap() {
        HazelcastInstance memberInstance = null;
        HazelcastInstance clientInstance = null;
        try {
            memberInstance = HazelcastStarter.newHazelcastInstance("3.7");
            clientInstance = HazelcastClientStarter.newHazelcastClient("3.7.2", false);

            IMap<Integer, Integer> clientMap = clientInstance.getMap("myMap");
            IMap<Integer, Integer> memberMap = memberInstance.getMap("myMap");

            clientMap.put(1, 2);

            assertEquals(2, (int) memberMap.get(1));
        } finally {
            if (memberInstance != null) {
                memberInstance.shutdown();
            }
            if (clientInstance != null) {
                clientInstance.shutdown();
            }
        }
    }

    @Test
    public void testAdvancedClientMap() {
        HazelcastInstance memberInstance = null;

        try {
            memberInstance = HazelcastStarter.newHazelcastInstance("3.7");
            HazelcastInstance clientInstance = HazelcastClientStarter.newHazelcastClient("3.7.2", false);

            System.out.println("About to terminate the client");
            clientInstance.getLifecycleService().terminate();
            System.out.println("Client terminated");
        } finally {
            if (memberInstance != null) {
                memberInstance.shutdown();
            }
        }
    }

    @Test
    public void testClientMap_async() throws InterruptedException, ExecutionException {
        HazelcastInstance memberInstance = null;
        HazelcastInstance clientInstance = null;
        try {
            memberInstance = HazelcastStarter.newHazelcastInstance("3.7");
            clientInstance = HazelcastClientStarter.newHazelcastClient("3.7.2", false);

            IMap<Integer, Integer> clientMap = clientInstance.getMap("myMap");
            clientMap.put(0, 1);
            ICompletableFuture<Integer> async = clientMap.getAsync(0);
            int value = async.get();

            assertEquals(1, value);
        } finally {
            if (memberInstance != null) {
                memberInstance.shutdown();
            }
            if (clientInstance != null) {
                clientInstance.shutdown();
            }
        }
    }
}

@@ -931,24 +931,48 @@
 astStarter;%0A
+import org.junit.After;%0A
 import org.j
@@ -1277,117 +1277,240 @@
 t %7B%0A
+%0A
     
-@Test%0A    public void testClientLifecycle() %7B%0A        HazelcastInstance member = null;%0A        try %7B%0A    
+private HazelcastInstance memberInstance;%0A%0A    @After%0A    public void tearDown() %7B%0A        if (memberInstance != null) %7B%0A            memberInstance.shutdown();%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testClientLifecycle() %7B%0A
     
@@ -1519,16 +1519,24 @@
   member
+Instance
  = Hazel
@@ -1573,28 +1573,24 @@
 ce(%223.7%22);%0A%0A
-    
         for 
@@ -1627,20 +1627,16 @@
         
-    
 String v
@@ -1656,20 +1656,16 @@
 .%22 + i;%0A
-    
         
@@ -1722,36 +1722,32 @@
 n);%0A            
-  
-  
 HazelcastInstanc
@@ -1822,36 +1822,32 @@
 e);%0A            
-    
 System.out.print
@@ -1892,20 +1892,16 @@
         
-    
 instance
@@ -1917,126 +1917,8 @@
 ();%0A
-            %7D%0A%0A        %7D finally %7B%0A            if (member != null) %7B%0A                member.shutdown();%0A            %7D%0A
     
@@ -1978,57 +1978,8 @@
 ) %7B%0A
-        HazelcastInstance memberInstance = null;%0A
     
@@ -2471,107 +2471,8 @@
 y %7B%0A
-            if (memberInstance != null) %7B%0A                memberInstance.shutdown();%0A            %7D%0A
     
@@ -2639,76 +2639,8 @@
 ) %7B%0A
-        HazelcastInstance memberInstance = null;%0A%0A        try %7B%0A    
     
@@ -2706,36 +2706,32 @@
 %223.7%22);%0A        
-    
 HazelcastInstanc
@@ -2809,36 +2809,32 @@
 alse);%0A%0A        
-   
- 
 System.out.print
@@ -2870,36 +2870,32 @@
 ient%22);%0A        
-    
 clientInstance.g
@@ -2928,36 +2928,32 @@
 nate();%0A        
- 
-   
 System.out.print
@@ -2981,137 +2981,8 @@
 %22);%0A
-        %7D finally %7B%0A            if (memberInstance != null) %7B%0A                memberInstance.shutdown();%0A            %7D%0A        %7D%0A
     
@@ -3086,57 +3086,8 @@
 n %7B%0A
-        HazelcastInstance memberInstance = null;%0A
     
@@ -3590,107 +3590,8 @@
 y %7B%0A
-            if (memberInstance != null) %7B%0A                memberInstance.shutdown();%0A            %7D%0A
     

Address review comments