package redis.clients.jedis.tests.commands;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import redis.clients.jedis.JedisException;

public class StringValuesCommandsTest extends JedisCommandTestBase {
    @Test
    public void setAndGet() {
	String status = jedis.set("foo", "bar");
	assertEquals("OK", status);

	String value = jedis.get("foo");
	assertEquals("bar", value);

	assertEquals(null, jedis.get("bar"));
    }

    @Test
    public void getSet() {
	String value = jedis.getSet("foo", "bar");
	assertEquals(null, value);
	value = jedis.get("foo");
	assertEquals("bar", value);
    }

    @Test
    public void mget() {
	List<String> values = jedis.mget("foo", "bar");
	List<String> expected = new ArrayList<String>();
	expected.add(null);
	expected.add(null);

	assertEquals(expected, values);

	jedis.set("foo", "bar");

	expected = new ArrayList<String>();
	expected.add("bar");
	expected.add(null);
	values = jedis.mget("foo", "bar");

	assertEquals(expected, values);

	jedis.set("bar", "foo");

	expected = new ArrayList<String>();
	expected.add("bar");
	expected.add("foo");
	values = jedis.mget("foo", "bar");

	assertEquals(expected, values);
    }

    @Test
    public void setnx() {
	long status = jedis.setnx("foo", "bar");
	assertEquals(1, status);
	assertEquals("bar", jedis.get("foo"));

	status = jedis.setnx("foo", "bar2");
	assertEquals(0, status);
	assertEquals("bar", jedis.get("foo"));
    }

    @Test
    public void setex() {
	String status = jedis.setex("foo", 20, "bar");
	assertEquals("OK", status);
	long ttl = jedis.ttl("foo");
	assertTrue(ttl > 0 && ttl <= 20);
    }

    @Test
    public void mset() {
	String status = jedis.mset("foo", "bar", "bar", "foo");
	assertEquals("OK", status);
	assertEquals("bar", jedis.get("foo"));
	assertEquals("foo", jedis.get("bar"));
    }

    @Test
    public void msetnx() {
	long status = jedis.msetnx("foo", "bar", "bar", "foo");
	assertEquals(1, status);
	assertEquals("bar", jedis.get("foo"));
	assertEquals("foo", jedis.get("bar"));

	status = jedis.msetnx("foo", "bar1", "bar2", "foo2");
	assertEquals(0, status);
	assertEquals("bar", jedis.get("foo"));
	assertEquals("foo", jedis.get("bar"));
    }

    @Test(expected = JedisException.class)
    public void incrWrongValue() {
	jedis.set("foo", "bar");
	jedis.incr("foo");
    }

    @Test
    public void incr() {
	long value = jedis.incr("foo");
	assertEquals(1, value);
	value = jedis.incr("foo");
	assertEquals(2, value);
    }

    @Test(expected = JedisException.class)
    public void incrByWrongValue() {
	jedis.set("foo", "bar");
	jedis.incrBy("foo", 2);
    }

    @Test
    public void incrBy() {
	long value = jedis.incrBy("foo", 2);
	assertEquals(2, value);
	value = jedis.incrBy("foo", 2);
	assertEquals(4, value);
    }

    @Test(expected = JedisException.class)
    public void decrWrongValue() {
	jedis.set("foo", "bar");
	jedis.decr("foo");
    }

    @Test
    public void decr() {
	long value = jedis.decr("foo");
	assertEquals(-1, value);
	value = jedis.decr("foo");
	assertEquals(-2, value);
    }

    @Test(expected = JedisException.class)
    public void decrByWrongValue() {
	jedis.set("foo", "bar");
	jedis.decrBy("foo", 2);
    }

    @Test
    public void decrBy() {
	long value = jedis.decrBy("foo", 2);
	assertEquals(-2, value);
	value = jedis.decrBy("foo", 2);
	assertEquals(-4, value);
    }

    @Test
    public void append() {
	long value = jedis.append("foo", "bar");
	assertEquals(3, value);
	assertEquals("bar", jedis.get("foo"));
	value = jedis.append("foo", "bar");
	assertEquals(6, value);
	assertEquals("barbar", jedis.get("foo"));
    }

    @Test
    public void substr() {
	jedis.set("s", "This is a string");
	assertEquals("This", jedis.substr("s", 0, 3));
	assertEquals("ing", jedis.substr("s", -3, -1));
	assertEquals("This is a string", jedis.substr("s", 0, -1));
	assertEquals(" string", jedis.substr("s", 9, 100000));
    }

    @Test
    public void strlen() {
	jedis.set("s", "This is a string");
	assertEquals("This is a string".length(), jedis.strlen("s").intValue());
    }
}
@@ -4096,9 +4096,378 @@
 ;%0A    %7D%0A
+%09%0A%09@Test%0A%09public void incrLargeNumbers() %7B%0A%09%09long value = jedis.incr(%22foo%22);%0A%09%09assertEquals(1, value);%0A%09%09assertEquals(1L + Integer.MAX_VALUE, (long) jedis.incrBy(%22foo%22, Integer.MAX_VALUE));%0A%09%7D%0A%09%0A%09@Test%0A%09public void incrReallyLargeNumbers() %7B%0A%09%09jedis.set(%22foo%22, Long.toString(Long.MAX_VALUE));%0A%09%09long value = jedis.incr(%22foo%22);%0A%09%09assertEquals(Long.MIN_VALUE, value);%0A%09%7D%0A
 %7D

Verify incr behaviour in rollover conditions.