package net.rubyeye.xmemcached.test.unittest;

import java.util.concurrent.TimeoutException;

import net.rubyeye.xmemcached.KeyProvider;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.MemcachedClientCallable;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.command.BinaryCommandFactory;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;
import net.rubyeye.xmemcached.utils.ByteUtils;

public class XMemcachedClientWithKeyProviderIT extends XMemcachedClientIT{
	
	private KeyProvider keyProvider;
	
	@Override
	public void setUp() throws Exception {
		super.setUp();
		keyProvider = new KeyProvider() {
			
			public String process(String key) {
				// 现实中是基于某种规则进行字符串转换, 为了简单, 我直接用hashCode
				return String.valueOf(key.hashCode());
			}
		};
	}
	
	@Override
	public MemcachedClientBuilder createBuilder() throws Exception {

		MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil
				.getAddresses(this.properties
						.getProperty("test.memcached.servers")));
		builder.setCommandFactory(new BinaryCommandFactory());
		ByteUtils.testing = true;
		return builder;
	}
	
	public void testKeyProvider(){
		String process = keyProvider.process("namespace:a");
		assertEquals("790852098", process);
	}
	
	public void testWithNamespaceAndKeyProvider() throws Exception{
		memcachedClient.setKeyProvider(keyProvider);
		memcachedClient.withNamespace("a", new MemcachedClientCallable<Void>() {

			public Void call(MemcachedClient client) throws MemcachedException, InterruptedException, TimeoutException {
				client.set("name", 0, "Mike Liu");
				return null;
			}
		});
		
		memcachedClient.invalidateNamespace("a");
		
		Object result = memcachedClient.withNamespace("a", new MemcachedClientCallable<Object>() {
			public Object call(MemcachedClient client) throws MemcachedException, InterruptedException, TimeoutException {
				return memcachedClient.get("name");
			}
		});
		
		assertNull(result);
	}
	
}

@@ -38,24 +38,84 @@
 nittest;%0D%0A%0D%0A
+import java.net.InetSocketAddress;%0D%0Aimport java.util.List;%0D%0A
 import java.
@@ -149,16 +149,16 @@
 ption;%0D%0A
-
 %0D%0Aimport
@@ -532,16 +532,83 @@
 ption;%0D%0A
+import net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator;%0D%0A
 import n
@@ -1419,20 +1419,573 @@
 ilder;%0D%0A
-
 %09%7D%0D%0A
+%09%0D%0A%09@Override%0D%0A%09public MemcachedClientBuilder createWeightedBuilder() throws Exception %7B%0D%0A%09%09List%3CInetSocketAddress%3E addressList = AddrUtil%0D%0A%09%09%09%09.getAddresses(this.properties%0D%0A%09%09%09%09%09%09.getProperty(%22test.memcached.servers%22));%0D%0A%09%09int%5B%5D weights = new int%5BaddressList.size()%5D;%0D%0A%09%09for (int i = 0; i %3C weights.length; i++) %7B%0D%0A%09%09%09weights%5Bi%5D = i + 1;%0D%0A%09%09%7D%0D%0A%0D%0A%09%09MemcachedClientBuilder builder = new XMemcachedClientBuilder(%0D%0A%09%09%09%09addressList, weights);%0D%0A%09%09builder.setCommandFactory(new BinaryCommandFactory());%0D%0A%09%09ByteUtils.testing = true;%0D%0A%09%09return builder;%0D%0A%09%7D%0D%0A%0D%0A
 %09%0D%0A%09publ

Fix XMemcachedClientWithKeyProviderIT::testWeightedServers by implementing a missing factory method