/* Copyright (c) 2014-2017 Jakub Białek
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.google.code.ssm.providers.elasticache;

import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.List;

import net.spy.memcached.FailureMode;

import org.junit.Before;
import org.junit.Test;

import com.google.code.ssm.providers.CacheClient;
import com.google.code.ssm.providers.CacheConfiguration;
import com.google.code.ssm.providers.elasticache.MemcacheClientFactoryImpl;
import com.google.code.ssm.providers.elasticache.ElastiCacheConfiguration;

/**
 * 
 * @author Jakub Białek
 * 
 */
public class MemcacheClientFactoryImplTest {

    private MemcacheClientFactoryImpl factory;

    private List<InetSocketAddress> addrs;

    @Before
    public void setUp() {
        factory = new MemcacheClientFactoryImpl();
        addrs = Collections.singletonList(new InetSocketAddress(12345));
    }

    @Test
    public void createWithBaseConf() throws IOException {
        CacheConfiguration conf = new CacheConfiguration();
        conf.setConsistentHashing(true);
        conf.setOperationTimeout(1000);
        conf.setUseBinaryProtocol(false);

        try {
            CacheClient client = factory.create(addrs, conf);

            assertNotNull(client);
            client.shutdown();
        } catch (RuntimeException ex) {
            printException(ex);
            throw ex;
        }
    }

    @Test
    public void createWithSpecificConf() throws IOException {
        ElastiCacheConfiguration conf = new ElastiCacheConfiguration();
        conf.setConsistentHashing(true);
        conf.setOperationTimeout(100);
        conf.setUseBinaryProtocol(false);
        conf.setFailureMode(FailureMode.Retry);
        conf.setShouldOptimize(true);
        conf.setMaxReconnectDelay(1000L);
        conf.setTimeoutExceptionThreshold(100);
        conf.setUseNagleAlgorithm(false);

        try {
            CacheClient client = factory.create(addrs, conf);

            assertNotNull(client);
            client.shutdown();
        } catch (RuntimeException ex) {
            printException(ex);
            throw ex;
        }
    }
    
    private void printException(final Exception ex) {
        System.err.println("An exception has occurred");
        ex.printStackTrace();
        Throwable t = ex;
        while (t.getCause() != null) {
            System.err.println("Cause: ");
            t = t.getCause();
            t.printStackTrace();
        }
    }

}

@@ -2263,34 +2263,16 @@
 alse);%0A%0A
-        try %7B%0A    
         
@@ -2322,36 +2322,32 @@
 conf);%0A%0A        
-    
 assertNotNull(cl
@@ -2353,36 +2353,32 @@
 lient);%0A        
-   
- 
 client.shutdown(
@@ -2384,112 +2384,8 @@
 ();%0A
-        %7D catch (RuntimeException ex) %7B%0A            printException(ex);%0A            throw ex;%0A        %7D%0A
     
@@ -2876,26 +2876,8 @@
 );%0A%0A
-        try %7B%0A    
     
@@ -2927,28 +2927,24 @@
 rs, conf);%0A%0A
-    
         asse
@@ -2966,28 +2966,24 @@
 t);%0A        
-    
 client.shutd
@@ -2993,445 +2993,8 @@
 ();%0A
-        %7D catch (RuntimeException ex) %7B%0A            printException(ex);%0A            throw ex;%0A        %7D%0A    %7D%0A    %0A    private void printException(final Exception ex) %7B%0A        System.err.println(%22An exception has occurred%22);%0A        ex.printStackTrace();%0A        Throwable t = ex;%0A        while (t.getCause() != null) %7B%0A            System.err.println(%22Cause: %22);%0A            t = t.getCause();%0A            t.printStackTrace();%0A        %7D%0A
     

Clear test code from debug messages