Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public void testCSVFileWithHeaderColumnOverride() throws Exception

Assert.assertEquals(1, segments.size());

Assert.assertEquals(Arrays.asList("dim"), segments.get(0).getDimensions());
Assert.assertEquals(Arrays.asList("d"), segments.get(0).getDimensions());
Assert.assertEquals(Arrays.asList("val"), segments.get(0).getMetrics());
Assert.assertEquals(new Interval("2014/P1D"), segments.get(0).getInterval());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,12 @@ public String getListDelimiter()
@Override
public void startFileFromBeginning()
{
supportSkipHeaderRows = true;
if (hasHeaderRow) {
fieldNames = null;
}
hasParsedHeader = false;
skippedHeaderRows = 0;
supportSkipHeaderRows = true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ public String getListDelimiter()
@Override
public void startFileFromBeginning()
{
supportSkipHeaderRows = true;
if (hasHeaderRow) {
fieldNames = null;
}
hasParsedHeader = false;
skippedHeaderRows = 0;
supportSkipHeaderRows = true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.Map;

public class CSVParserTest
{
@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test
public void testValidHeader()
Expand Down Expand Up @@ -117,19 +121,81 @@ public void testCSVParserWithSkipHeaderRows()
);
}

@Test(expected = UnsupportedOperationException.class)
@Test
public void testCSVParserWithHeaderRow()
{
final Parser<String, Object> csvParser = new CSVParser(
Optional.absent(),
true,
0
);
csvParser.startFileFromBeginning();
final String[] body = new String[] {
"time,value1,value2",
"hello,world,foo"
};
Assert.assertNull(csvParser.parse(body[0]));
final Map<String, Object> jsonMap = csvParser.parse(body[1]);
Assert.assertEquals(
"jsonMap",
ImmutableMap.of("time", "hello", "value1", "world", "value2", "foo"),
jsonMap
);
}

@Test
public void testCSVParserWithDifferentHeaderRows()
{
final Parser<String, Object> csvParser = new CSVParser(
Optional.absent(),
true,
0
);
csvParser.startFileFromBeginning();
final String[] body = new String[] {
"time,value1,value2",
"hello,world,foo"
};
Assert.assertNull(csvParser.parse(body[0]));
Map<String, Object> jsonMap = csvParser.parse(body[1]);
Assert.assertEquals(
"jsonMap",
ImmutableMap.of("time", "hello", "value1", "world", "value2", "foo"),
jsonMap
);

csvParser.startFileFromBeginning();
final String[] body2 = new String[] {
"time,value1,value2,value3",
"hello,world,foo,bar"
};
Assert.assertNull(csvParser.parse(body2[0]));
jsonMap = csvParser.parse(body2[1]);
Assert.assertEquals(
"jsonMap",
ImmutableMap.of("time", "hello", "value1", "world", "value2", "foo", "value3", "bar"),
jsonMap
);
}

@Test
public void testCSVParserWithoutStartFileFromBeginning()
{
expectedException.expect(UnsupportedOperationException.class);
expectedException.expectMessage(
"hasHeaderRow or maxSkipHeaderRows is not supported. Please check the indexTask supports these options."
);

final int skipHeaderRows = 2;
final Parser<String, Object> csvParser = new CSVParser(
Optional.absent(),
false,
skipHeaderRows
);
final String[] body = new String[] {
"header\tline\t1",
"header\tline\t2",
"hello\tworld\tfoo"
"header,line,1",
"header,line,2",
"hello,world,foo"
};
csvParser.parse(body[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.Map;

public class DelimitedParserTest
{
@Rule
public ExpectedException expectedException = ExpectedException.none();

@Test
public void testValidHeader()
Expand Down Expand Up @@ -127,9 +131,73 @@ public void testTSVParserWithSkipHeaderRows()
);
}

@Test(expected = UnsupportedOperationException.class)
@Test
public void testTSVParserWithHeaderRow()
{
final Parser<String, Object> parser = new DelimitedParser(
Optional.of("\t"),
Optional.absent(),
true,
0
);
parser.startFileFromBeginning();
final String[] body = new String[] {
"time\tvalue1\tvalue2",
"hello\tworld\tfoo"
};
Assert.assertNull(parser.parse(body[0]));
final Map<String, Object> jsonMap = parser.parse(body[1]);
Assert.assertEquals(
"jsonMap",
ImmutableMap.of("time", "hello", "value1", "world", "value2", "foo"),
jsonMap
);
}

@Test
public void testTSVParserWithDifferentHeaderRows()
{
final Parser<String, Object> csvParser = new DelimitedParser(
Optional.of("\t"),
Optional.absent(),
true,
0
);
csvParser.startFileFromBeginning();
final String[] body = new String[] {
"time\tvalue1\tvalue2",
"hello\tworld\tfoo"
};
Assert.assertNull(csvParser.parse(body[0]));
Map<String, Object> jsonMap = csvParser.parse(body[1]);
Assert.assertEquals(
"jsonMap",
ImmutableMap.of("time", "hello", "value1", "world", "value2", "foo"),
jsonMap
);

csvParser.startFileFromBeginning();
final String[] body2 = new String[] {
"time\tvalue1\tvalue2\tvalue3",
"hello\tworld\tfoo\tbar"
};
Assert.assertNull(csvParser.parse(body2[0]));
jsonMap = csvParser.parse(body2[1]);
Assert.assertEquals(
"jsonMap",
ImmutableMap.of("time", "hello", "value1", "world", "value2", "foo", "value3", "bar"),
jsonMap
);
}

@Test
public void testTSVParserWithoutStartFileFromBeginning()
{
expectedException.expect(UnsupportedOperationException.class);
expectedException.expectMessage(
"hasHeaderRow or maxSkipHeaderRows is not supported. Please check the indexTask supports these options."
);

final int skipHeaderRows = 2;
final Parser<String, Object> delimitedParser = new DelimitedParser(
Optional.of("\t"),
Expand Down