package redis.clients.jedis.tests;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.Assert;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.tests.commands.JedisCommandTestBase;
import redis.clients.util.RedisOutputStream;

public class JedisTest extends JedisCommandTestBase {
    @Test
    public void useWithoutConnecting() {
	Jedis jedis = new Jedis("localhost");
	jedis.auth("foobared");
	jedis.dbSize();
    }

    @Test
    public void checkBinaryData() {
	byte[] bigdata = new byte[1777];
	for (int b = 0; b < bigdata.length; b++) {
	    bigdata[b] = (byte) ((byte) b % 255);
	}
	Map<String, String> hash = new HashMap<String, String>();
	hash.put("data", new String(bigdata, Protocol.UTF8));

	String status = jedis.hmset("foo", hash);
	assertEquals("OK", status);
	assertEquals(hash, jedis.hgetAll("foo"));
    }

    @Test
    public void connectWithShardInfo() {
	JedisShardInfo shardInfo = new JedisShardInfo("localhost",
		Protocol.DEFAULT_PORT);
	shardInfo.setPassword("foobared");
	Jedis jedis = new Jedis(shardInfo);
	jedis.get("foo");
    }
    
    @SuppressWarnings("rawtypes")
	public static void compareList(List expected, List result) {
    	assertEquals(expected.size(), result.size());
    	
    	final Iterator expectedit = expected.iterator();
    	final Iterator responseit = result.iterator();
    	while(expectedit.hasNext()) {
    		final Object exp = expectedit.next();
    		final Object resp = responseit.next();
    		if(exp instanceof byte[]) {
    			final byte[] bexp = (byte[]) exp;
    			final byte[] bresp = (byte[]) resp;
    			Assert.assertArrayEquals(bexp, bresp);
    		} else if (exp instanceof List) {
    			final List subexp = (List) exp;
    			final List subresp = (List) resp;
    			compareList(subexp, subresp);
    		} else {
    			assertEquals(exp, resp);
    		}
    	}
    }

}

@@ -1443,24 +1443,107 @@
 iterator();%0A
+    %09while(expectedit.hasNext()) %7B%0A    %09%09final Object exp = expectedit.next();%0A    
     %09final I
@@ -1574,32 +1574,68 @@
 ult.iterator();%0A
+        %09boolean found = false;%0A    
     %09while(expec
@@ -1625,32 +1625,32 @@
      %09while(
-expected
+response
 it.hasNext()
@@ -1653,56 +1653,23 @@
 xt()
-) %7B%0A    %09%09final Object exp = expectedit.next();%0A
+ && !found) %7B%0A%09
     
@@ -1705,24 +1705,25 @@
 eit.next();%0A
+%09
     %09%09if(exp
@@ -1740,24 +1740,25 @@
 f byte%5B%5D) %7B%0A
+%09
     %09%09%09final
@@ -1782,24 +1782,25 @@
 yte%5B%5D) exp;%0A
+%09
     %09%09%09final
@@ -1826,24 +1826,103 @@
 te%5B%5D) resp;%0A
+%09    %09%09%09if(arraysAreEquals(bexp, bresp)) %7B%0A%09    %09%09%09%09found = true;%0A%09    %09%09%09%7D%0A//%09
     %09%09%09Asser
@@ -1947,32 +1947,33 @@
 s(bexp, bresp);%0A
+%09
     %09%09%7D else if 
@@ -1992,24 +1992,25 @@
 eof List) %7B%0A
+%09
     %09%09%09final
@@ -2036,16 +2036,17 @@
 t) exp;%0A
+%09
     %09%09%09f
@@ -2078,16 +2078,17 @@
 ) resp;%0A
+%09
     %09%09%09c
@@ -2112,24 +2112,25 @@
 , subresp);%0A
+%09
     %09%09%7D else
@@ -2128,24 +2128,25 @@
  %09%09%7D else %7B%0A
+%09
     %09%09%09asser
@@ -2165,16 +2165,17 @@
  resp);%0A
+%09
     %09%09%7D%0A
@@ -2182,16 +2182,433 @@
     
+ 
+   
 %09%7D%0A    
+    %09if(!found)%7B%0A        %09%09fail(%22Result doesn't contain %22 + exp.toString());%0A        %09%7D%0A    %09%7D%0A    %7D%0A%0A    public static boolean arraysAreEquals(final byte%5B%5D expected, final byte%5B%5D result) %7B%0A    %09if(expected.length != result.length) %7B%0A    %09%09return false;%0A    %09%7D%0A    %09%0A    %09for(int i=0; i %3C expected.length; i++) %7B%0A    %09%09if(expected%5Bi%5D != result%5Bi%5D) %7B%0A    %09%09%09return false;%0A    %09%09%7D%0A    %09%7D%0A    %09%0A    %09return true;%0A    
 %7D
-%0A
 %0A%7D%0A

Fix some more U tests ...