package com.dianping.cat.abtest.spi.internal;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;

import junit.framework.Assert;

import org.junit.Test;
import org.unidal.lookup.ComponentTestCase;

public class ABTestContextManagerTest extends ComponentTestCase {
	@Test
	public void testCodec() throws Exception {
		check("1=ab:A|cd:B&2=ab:A|cd:B", "1=ab:A|cd:B&2=ab:A|cd:B");
		check("1=ab:|cd:B&2=ab:A|cd:B", "1=ab:|cd:B&2=ab:A|cd:B");
		check("1=ab:A|cd:B&2=ab:A|cd:", "1=ab:A|cd:B&2=ab:A|cd:");
		check("", "");

		check("1=ab:A|cd:B&2=ab:A|cd:B", "1=ab:A|cd:B&2=ab:A|cd:B", "1", "2");
		check("1=ab:A|cd:B&2=ab:A|cd:B", "1=ab:A|cd:B", "1");
	}

	private void check(String source, String expected, String... keys) throws Exception {
		ABTestCodec codec = lookup(ABTestCodec.class);
		Map<String, Map<String, String>> map = codec.decode(source,
		      keys.length == 0 ? null : new HashSet<String>(Arrays.asList(keys)));

		Assert.assertEquals(expected, codec.encode(map));
	}
	
	@Test
	public void testCodec2() throws Exception{
		check2("1=ab:A|cd:B&2=ab:A|cd:B", "{1=A, 2=A}");
		check2("1=ab:A&2=ab:A|cd:B", "{1=A, 2=A}");
		check2("1=ab:A|cd:B&2=ab:|cd:B", "{1=A, 2=}");
		check2("1=cd:B&2=ab:A|cd:B", "{2=A}");
		check2("", "{}");
	}
	
	private void check2(String source, String expected) throws Exception {
		ABTestCodec codec = lookup(ABTestCodec.class);
		Map<String, String> map = codec.decode(source);

		Assert.assertEquals(expected, map.toString());
	}
}

@@ -512,24 +512,155 @@
 ab:A%7Ccd:%22);%0A
+%09%09check(%221=ab:%7Ccd:B&2=ab:A%7Ccd:%22, %221=ab:%7Ccd:B&2=ab:A%7Ccd:%22);%0A%09%09check(%221=ab:%7Ccd:%22, %221=ab:%7Ccd:%22);%0A%09%09check(%221=ab:%7Ccd:A%22, %221=ab:%7Ccd:A%22);%0A
 %09%09check(%22%22, 
@@ -1361,32 +1361,130 @@
 d:B%22, %22%7B2=A%7D%22);%0A
+%09%09check2(%2230=l:A%7Cab:A%22, %22%7B30=A%7D%22);%0A%09%09check2(%221=ab:%7Ccd:%22, %22%7B1=%7D%22);%0A%09%09check2(%221=ab:%7Ccd:A%22, %22%7B1=%7D%22);%0A
 %09%09check2(%22%22, %22%7B%7D

add more test cases