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.sql.Date;
import java.util.UUID;

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(JdbcAscii.instance.decompose("string"));
        JdbcBoolean.instance.compose(JdbcBoolean.instance.decompose(true));
        JdbcBytes.instance.compose(JdbcBytes.instance.decompose(ByteBuffer.wrap("string".getBytes())));
        JdbcDate.instance.compose(JdbcDate.instance.decompose(new Date(System.currentTimeMillis())));
        JdbcDecimal.instance.compose(JdbcDecimal.instance.decompose(new BigDecimal(1)));
        JdbcDouble.instance.compose(JdbcDouble.instance.decompose(new Double(1.0d)));
        JdbcFloat.instance.compose(JdbcFloat.instance.decompose(new Float(1.0f)));
        JdbcInt32.instance.compose(JdbcInt32.instance.decompose(1));
        JdbcInteger.instance.compose(JdbcInteger.instance.decompose(new BigInteger("1")));
        JdbcLong.instance.compose(JdbcLong.instance.decompose(1L));
        JdbcUTF8.instance.compose(JdbcUTF8.instance.decompose("string"));

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

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

@@ -202,10 +202,11 @@
 ava.
-sq
+uti
 l.Da
@@ -233,16 +233,66 @@
 .UUID;%0A%0A
+import org.apache.cassandra.utils.ByteBufferUtil;%0A
 import o
@@ -574,36 +574,10 @@
 ose(
-JdbcAscii.instance.decompose
+wr
 (%22st
@@ -626,43 +626,18 @@
 ose(
-JdbcBoolean.instance.decompose(true
+wr(%22false%22
 ));%0A
@@ -675,73 +675,19 @@
 ose(
-JdbcBytes.instance.decompose(ByteBuffer.wrap(%22string%22.getBytes())
+wr(%22string%22
 ));%0A
@@ -724,35 +724,29 @@
 ose(
-JdbcDate.instance.decompose
+ByteBufferUtil.bytes(
 (new
@@ -779,16 +779,27 @@
 llis()))
+.getTime())
 );%0A     
@@ -834,38 +834,27 @@
 ose(
-JdbcDecimal.instance.decompose
+decomposeBigDecimal
 (new
@@ -911,56 +911,35 @@
 ose(
-JdbcDouble.instance.decompose(new Double
+ByteBufferUtil.bytes
 (1.0d))
-)
 ;%0A  
@@ -975,54 +975,35 @@
 ose(
-JdbcFloat.instance.decompose(new Float
+ByteBufferUtil.bytes
 (1.0f))
-)
 ;%0A  
@@ -1039,36 +1039,28 @@
 ose(
-JdbcInt32.instance.decompose
+ByteBufferUtil.bytes
 (1))
@@ -1102,38 +1102,24 @@
 ose(
-JdbcInteger.instance.decompose
+ByteBuffer.wrap(
 (new
@@ -1135,16 +1135,31 @@
 er(%221%22))
+.toByteArray())
 );%0A     
@@ -1191,35 +1191,28 @@
 ose(
-JdbcLong.instance.decompose
+ByteBufferUtil.bytes
 (1L)
@@ -1252,35 +1252,10 @@
 ose(
-JdbcUTF8.instance.decompose
+wr
 (%22st
@@ -1370,28 +1370,24 @@
         Jdbc
-Time
 UUID.instanc
@@ -1400,29 +1400,31 @@
 ose(
-JdbcTimeUUID.instance
+ByteBuffer.wrap(UUIDGen
 .dec
@@ -1428,32 +1428,33 @@
 decompose(uuid))
+)
 ;%0A        JdbcUU
@@ -1443,32 +1443,36 @@
 ));%0A        Jdbc
+Time
 UUID.instance.co
@@ -1477,33 +1477,39 @@
 compose(
-JdbcUUID.instance
+ByteBuffer.wrap(UUIDGen
 .decompo
@@ -1517,16 +1517,17 @@
 e(uuid))
+)
 ;%0A      
@@ -1565,32 +1565,31 @@
 ose(
-JdbcLexicalUUID.instance
+ByteBuffer.wrap(UUIDGen
 .dec
@@ -1601,16 +1601,17 @@
 e(uuid))
+)
 ;%0A%0A     
@@ -1845,11 +1845,737 @@
  %7D%0A    %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

fix #3665 breakage (1.0 classes can't decompose())