package com.bizo.hive.serde.csv;

import java.util.List;
import java.util.Properties;

import org.apache.hadoop.hive.serde.Constants;
import org.apache.hadoop.io.Text;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;


public final class CSVSerdeTest {
  private final CSVSerde csv = new CSVSerde();
  final Properties props = new Properties();  
  
  @Before
  public void setup() throws Exception {
    props.put(Constants.LIST_COLUMNS, "a,b,c");
    props.put(Constants.LIST_COLUMN_TYPES, "string,string,string");
  }
  
  @Test
  public void testDeserialize() throws Exception {
    csv.initialize(null, props);    
    final Text in = new Text("hello,\"yes, okay\",1");
    
    final List<String> row = (List<String>) csv.deserialize(in);

    assertEquals("hello", row.get(0));
    assertEquals("yes, okay", row.get(1));
    assertEquals("1", row.get(2));
  }
  
  
  @Test
  public void testDeserializeCustomSeparators() throws Exception {
    props.put("separatorChar", "\t");
    props.put("quoteChar", "'");
    
    csv.initialize(null, props);
    
    final Text in = new Text("hello\t'yes\tokay'\t1");
    final List<String> row = (List<String>) csv.deserialize(in);
        
    assertEquals("hello", row.get(0));
    assertEquals("yes\tokay", row.get(1));    
    assertEquals("1", row.get(2));
  }
  
  @Test
  public void testDeserializeCustomEscape() throws Exception {
    props.put("quoteChar", "'");
    props.put("escapeChar", "\\");
    
    csv.initialize(null, props);
    
    final Text in = new Text("hello,'yes\\'okay',1");
    final List<String> row = (List<String>) csv.deserialize(in);
        
    assertEquals("hello", row.get(0));
    assertEquals("yes'okay", row.get(1));
    assertEquals("1", row.get(2));
  }  
}

@@ -116,16 +116,21 @@
 e.serde.
+serde
 Constant
@@ -132,16 +132,16 @@
 stants;%0A
-
 import o
@@ -449,32 +449,37 @@
 %7B%0A    props.put(
+serde
 Constants.LIST_C
@@ -492,16 +492,18 @@
 , %22a,b,c
+,d
 %22);%0A    
@@ -512,16 +512,21 @@
 ops.put(
+serde
 Constant
@@ -567,16 +567,23 @@
 g,string
+,string
 %22);%0A  %7D%0A
@@ -732,16 +732,26 @@
 okay%5C%22,1
+,new%5Cnline
 %22);%0A    
@@ -926,32 +926,75 @@
 %22, row.get(2));%0A
+    assertEquals(%22new%5Cnline%22, row.get(3));%0A
   %7D%0A  %0A  %0A  @Tes
@@ -1227,16 +1227,27 @@
 okay'%5Ct1
+%5Ctnew%5Cnline
 %22);%0A    
@@ -1433,24 +1433,67 @@
 ow.get(2));%0A
+    assertEquals(%22new%5Cnline%22, row.get(3));%0A
   %7D%0A  %0A  @Te
@@ -1719,16 +1719,26 @@
 'okay',1
+,new%5Cnline
 %22);%0A    
@@ -1880,32 +1880,32 @@
 %22, row.get(1));%0A
-
     assertEquals
@@ -1923,16 +1923,59 @@
 et(2));%0A
+    assertEquals(%22new%5Cnline%22, row.get(3));%0A
   %7D  %0A%7D%0A

Add tests for CSV files with new lines. Remove deprecated org.apache.hadoop.hive.Constants use.