package org.apache.cassandra.cql.jdbc;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.UUID;

import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.UUIDGen;
import org.junit.Test;

public class ClientUtilsTest
{
    /** Exercises the classes in the clientutil jar to expose missing dependencies. */
    @Test
    public void test() throws UnknownHostException
    {
        JdbcAscii.instance.compose(wr("string"));
        JdbcBoolean.instance.compose(wr("false"));
        JdbcBytes.instance.compose(wr("string"));
        JdbcDate.instance.compose(ByteBufferUtil.bytes((new Date(System.currentTimeMillis())).getTime()));
        JdbcDecimal.instance.compose(decomposeBigDecimal(new BigDecimal(1)));
        JdbcDouble.instance.compose(ByteBufferUtil.bytes(1.0d));
        JdbcFloat.instance.compose(ByteBufferUtil.bytes(1.0f));
        JdbcInt32.instance.compose(ByteBufferUtil.bytes(1));
        JdbcInteger.instance.compose(ByteBuffer.wrap((new BigInteger("1")).toByteArray()));
        JdbcLong.instance.compose(ByteBufferUtil.bytes(1L));
        JdbcUTF8.instance.compose(wr("string"));

        // UUIDGen
        UUID uuid = UUIDGen.makeType1UUIDFromHost(InetAddress.getLocalHost());
        JdbcUUID.instance.compose(ByteBuffer.wrap(UUIDGen.decompose(uuid)));
        JdbcTimeUUID.instance.compose(ByteBuffer.wrap(UUIDGen.decompose(uuid)));
        JdbcLexicalUUID.instance.compose(ByteBuffer.wrap(UUIDGen.decompose(uuid)));

        // Raise a MarshalException
        try
        {
            JdbcLexicalUUID.instance.getString(ByteBuffer.wrap("notauuid".getBytes()));
        }
        catch (MarshalException me)
        {
            // Success
        }
    }

    /* Copypasta from DecimalType */
    private static ByteBuffer decomposeBigDecimal(BigDecimal value)
    {
        if (value == null) return ByteBufferUtil.EMPTY_BYTE_BUFFER;

        BigInteger bi = value.unscaledValue();
        Integer scale = value.scale();
        byte[] bibytes = bi.toByteArray();
        byte[] sbytes = ByteBufferUtil.bytes(scale).array();
        byte[] bytes = new byte[bi.toByteArray().length+4];

        for (int i = 0 ; i < 4 ; i++) bytes[i] = sbytes[i];
        for (int i = 4 ; i < bibytes.length+4 ; i++) bytes[i] = bibytes[i-4];

        return ByteBuffer.wrap(bytes);
    }

    private static ByteBuffer wr(String value)
    {
        return ByteBuffer.wrap(value.getBytes());
    }
}

@@ -198,19 +198,18 @@
 rt java.
-uti
+sq
 l.Date;%0A
@@ -236,58 +236,8 @@
 D;%0A%0A
-import org.apache.cassandra.utils.ByteBufferUtil;%0A
 impo
@@ -511,34 +511,60 @@
 nstance.compose(
-wr
+JdbcAscii.instance.decompose
 (%22string%22));%0A   
@@ -601,18 +601,43 @@
 ose(
-wr(%22false%22
+JdbcBoolean.instance.decompose(true
 ));%0A
@@ -667,35 +667,89 @@
 nce.compose(
-wr(%22string%22
+JdbcBytes.instance.decompose(ByteBuffer.wrap(%22string%22.getBytes())
 ));%0A        
@@ -774,37 +774,43 @@
 compose(
-ByteBufferUtil.bytes(
+JdbcDate.instance.decompose
 (new Dat
@@ -843,19 +843,8 @@
 ()))
-.getTime())
 );%0A 
@@ -879,35 +879,46 @@
 compose(
-decomposeBigDecimal
+JdbcDecimal.instance.decompose
 (new Big
@@ -971,35 +971,56 @@
 ose(
-ByteBufferUtil.bytes
+JdbcDouble.instance.decompose(new Double
 (1.0d))
+)
 ;%0A  
@@ -1056,35 +1056,54 @@
 ose(
-ByteBufferUtil.bytes
+JdbcFloat.instance.decompose(new Float
 (1.0f))
+)
 ;%0A  
@@ -1135,36 +1135,44 @@
 compose(
-ByteBufferUtil.bytes
+JdbcInt32.instance.decompose
 (1));%0A  
@@ -1206,32 +1206,46 @@
 compose(
-ByteBuffer.wrap(
+JdbcInteger.instance.decompose
 (new Big
@@ -1261,23 +1261,8 @@
 1%22))
-.toByteArray())
 );%0A 
@@ -1294,36 +1294,43 @@
 compose(
-ByteBufferUtil.bytes
+JdbcLong.instance.decompose
 (1L));%0A 
@@ -1362,18 +1362,43 @@
 compose(
-wr
+JdbcUTF8.instance.decompose
 (%22string
@@ -1513,16 +1513,20 @@
     Jdbc
+Time
 UUID.ins
@@ -1539,39 +1539,37 @@
 compose(
-ByteBuffer.wrap(UUIDGen
+JdbcTimeUUID.instance
 .decompo
@@ -1569,33 +1569,32 @@
 decompose(uuid))
-)
 ;%0A        JdbcTi
@@ -1583,36 +1583,32 @@
 ));%0A        Jdbc
-Time
 UUID.instance.co
@@ -1613,39 +1613,33 @@
 compose(
-ByteBuffer.wrap(UUIDGen
+JdbcUUID.instance
 .decompo
@@ -1647,17 +1647,16 @@
 e(uuid))
-)
 ;%0A      
@@ -1694,31 +1694,32 @@
 ose(
-ByteBuffer.wrap(UUIDGen
+JdbcLexicalUUID.instance
 .dec
@@ -1731,17 +1731,16 @@
 e(uuid))
-)
 ;%0A%0A     
@@ -1978,733 +1978,7 @@
    %7D
-%0A%0A    /* Copypasta from DecimalType */%0A    private static ByteBuffer decomposeBigDecimal(BigDecimal value)%0A    %7B%0A        if (value == null) return ByteBufferUtil.EMPTY_BYTE_BUFFER;%0A%0A        BigInteger bi = value.unscaledValue();%0A        Integer scale = value.scale();%0A        byte%5B%5D bibytes = bi.toByteArray();%0A        byte%5B%5D sbytes = ByteBufferUtil.bytes(scale).array();%0A        byte%5B%5D bytes = new byte%5Bbi.toByteArray().length+4%5D;%0A%0A        for (int i = 0 ; i %3C 4 ; i++) bytes%5Bi%5D = sbytes%5Bi%5D;%0A        for (int i = 4 ; i %3C bibytes.length+4 ; i++) bytes%5Bi%5D = bibytes%5Bi-4%5D;%0A%0A        return ByteBuffer.wrap(bytes);%0A    %7D%0A%0A    private static ByteBuffer wr(String value)%0A    %7B%0A        return ByteBuffer.wrap(value.getBytes());%0A    %7D
 %0A%7D%0A

use decompose() for tests