// Copyright (C) 2011 - Will Glozer.  All rights reserved.

package com.lambdaworks.redis;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.List;

import org.junit.Test;

import com.lambdaworks.redis.codec.RedisCodec;

public class CustomCodecTest extends AbstractCommandTest {
    @Test
    public void test() throws Exception {
        RedisConnection<String, Object> connection = client.connect(new SerializedObjectCodec());
        List<String> list = list("one", "two");
        connection.set(key, list);
        assertThat(connection.get(key)).isEqualTo(list);
    }

    public class SerializedObjectCodec extends RedisCodec<String, Object> {
        private Charset charset = Charset.forName("UTF-8");

        @Override
        public String decodeKey(ByteBuffer bytes) {
            return charset.decode(bytes).toString();
        }

        @Override
        public Object decodeValue(ByteBuffer bytes) {
            try {
                byte[] array = new byte[bytes.remaining()];
                bytes.get(array);
                ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(array));
                return is.readObject();
            } catch (Exception e) {
                return null;
            }
        }

        @Override
        public byte[] encodeKey(String key) {
            return charset.encode(key).array();
        }

        @Override
        public byte[] encodeValue(Object value) {
            try {
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                ObjectOutputStream os = new ObjectOutputStream(bytes);
                os.writeObject(value);
                return bytes.toByteArray();
            } catch (IOException e) {
                return null;
            }
        }
    }
}

@@ -397,24 +397,75 @@
 util.List;%0A%0A
+import com.lambdaworks.redis.codec.ByteArrayCodec;%0A
 import org.j
@@ -876,24 +876,467 @@
 st);%0A    %7D%0A%0A
+    @Test%0A    public void testByteCodec() throws Exception %7B%0A        RedisConnection%3Cbyte%5B%5D, byte%5B%5D%3E connection = client.connect(new ByteArrayCodec());%0A        String value = %22%C3%BC%C3%B6%C3%A4%C3%BC+#%22;%0A        connection.set(key.getBytes(), value.getBytes());%0A        assertThat(connection.get(key.getBytes())).isEqualTo(value.getBytes());%0A%0A        List%3Cbyte%5B%5D%3E keys = connection.keys(key.getBytes());%0A        assertThat(keys).contains(key.getBytes());%0A    %7D%0A%0A
     public c

Add test for byte array codec #70