package com.splicemachine.derby.impl.sql.execute;

import com.splicemachine.derby.impl.sql.execute.serial.StringDVDSerializer;
import org.apache.derby.iapi.types.SQLVarchar;
import org.junit.Assert;
import org.junit.Test;

public class LazyDataValueDescriptorTest {

    private byte[] justBytes(String value) throws Exception {
        LazyDataValueDescriptor ldvd = new LazyDataValueDescriptor(new SQLVarchar(value), new StringDVDSerializer());
        return ldvd.getBytes();
    }

    @Test
    public void testLazyDeserialization() throws Exception{

        byte[] stringBytes = justBytes("foo");

        LazyDataValueDescriptor ldvd = new LazyDataValueDescriptor(new SQLVarchar(), new StringDVDSerializer());
        ldvd.initForDeserialization(stringBytes);

        Assert.assertTrue(ldvd.getDvd().isNull());

        Assert.assertEquals("foo", ldvd.getString());

        Assert.assertFalse(ldvd.getDvd().isNull());

    }

    @Test
    public void testValuesOnlyDeserializeOnce() throws Exception{

        byte[] stringBytes = justBytes("foo");

        LazyDataValueDescriptor ldvd = new LazyDataValueDescriptor(new SQLVarchar(), new StringDVDSerializer());
        ldvd.initForDeserialization(stringBytes);

        Assert.assertFalse(ldvd.isDeserialized());
        Assert.assertTrue(ldvd.getDvd().isNull());

        Assert.assertEquals("foo", ldvd.getString());

        Assert.assertTrue(ldvd.getString() == ldvd.getString());

        Assert.assertFalse(ldvd.getDvd().isNull());
    }

    @Test
    public void testLazySerialization() throws Exception{

        LazyDataValueDescriptor ldvd = new LazyDataValueDescriptor(new SQLVarchar("foo"), new StringDVDSerializer());

        Assert.assertFalse(ldvd.isSerialized());

        ldvd.getBytes();
        Assert.assertTrue(ldvd.isSerialized());

        LazyDataValueDescriptor ldvd2 = new LazyDataValueDescriptor(new SQLVarchar(), new StringDVDSerializer());
        ldvd2.initForDeserialization(ldvd.getBytes());

        Assert.assertEquals("foo", ldvd2.getString());

    }

}

@@ -131,46 +131,143 @@
 ort 
-org.apache.derby.iapi.types.SQLVarchar
+com.splicemachine.homeless.SerializationUtils;%0Aimport org.apache.derby.iapi.types.SQLVarchar;%0Aimport org.apache.hadoop.hbase.util.Bytes
 ;%0Aim
@@ -2143,10 +2143,1704 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testReusingDVDForDeserialization() throws Exception%7B%0A        byte%5B%5D fooBytes = justBytes(%22foo%22);%0A        byte%5B%5D barBytes = justBytes(%22bar%22);%0A%0A        LazyDataValueDescriptor ldvd = new LazyDataValueDescriptor(new SQLVarchar(), new StringDVDSerializer());%0A        ldvd.initForDeserialization(fooBytes);%0A%0A        Assert.assertTrue(ldvd.getDvd().isNull());%0A        Assert.assertEquals(%22foo%22, ldvd.getString());%0A        Assert.assertEquals(%22Bytes still present after forcing deserialization%22, fooBytes, ldvd.getBytes());%0A%0A        ldvd.initForDeserialization(barBytes);%0A        Assert.assertEquals(%22LDVD should now have bytes representing %5C%22bar%5C%22%22, barBytes, ldvd.getBytes());%0A        Assert.assertEquals(%22bar%22, ldvd.getString());%0A%0A    %7D%0A%0A    @Test%0A    public void testSerializingWholeDescriptor() throws Exception%7B%0A%0A        byte%5B%5D fooBytes = justBytes(%22foo%22);%0A%0A        LazyDataValueDescriptor ldvd = new LazyDataValueDescriptor(new SQLVarchar(%22foo%22), new StringDVDSerializer());%0A%0A        Assert.assertFalse(ldvd.isSerialized());%0A        Assert.assertEquals(%22foo%22, ldvd.getString());%0A        Assert.assertTrue(%22Foo bytes match%22, Bytes.equals(fooBytes, ldvd.getBytes()));%0A        Assert.assertTrue(ldvd.isSerialized());%0A%0A        byte%5B%5D  wholeDvdBytes = SerializationUtils.serializeToBytes(ldvd);%0A%0A        LazyDataValueDescriptor ldvdFromBytes = (LazyDataValueDescriptor) SerializationUtils.deserializeObject(wholeDvdBytes);%0A%0A        Assert.assertTrue(ldvdFromBytes.isSerialized());%0A        Assert.assertEquals(%22foo%22, ldvdFromBytes.getString());%0A        Assert.assertTrue(%22Foo bytes match after deserialization of whole DVD%22, Bytes.equals(fooBytes, ldvd.getBytes()));%0A    %7D%0A%0A
 %7D%0A

Add test covering reusing DVD for deserialization and serializing the whole DVD object