1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hadoop.util;
import com.yahoo.vespa.hadoop.mapreduce.util.TupleTools;
import org.apache.pig.data.DataType;
import org.apache.pig.data.Tuple;
import org.apache.pig.data.TupleFactory;
import org.apache.pig.impl.logicalLayer.schema.Schema;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
public class TupleToolsTest {
@Test
public void requireThatTupleToStringHandlesSimpleTypes() throws IOException {
Schema schema = new Schema();
Tuple tuple = TupleFactory.getInstance().newTuple();
addToTuple("id", DataType.CHARARRAY, "123", schema, tuple);
addToTuple("rank", DataType.INTEGER, 1, schema, tuple);
String template = "Id is <id> and rank is <rank>";
String result = TupleTools.toString(schema, tuple, template);
assertEquals("Id is 123 and rank is 1", result);
}
private void addToTuple(String alias, byte type, Object value, Schema schema, Tuple tuple) {
schema.add(new Schema.FieldSchema(alias, type));
tuple.append(value);
}
@Test
public void requireThatTupleToStringHandlesStringCharacters() throws IOException {
Schema schema = new Schema();
Tuple tuple = TupleFactory.getInstance().newTuple();
addToTuple("id", DataType.CHARARRAY, "_!@#$%^&*()", schema, tuple);
addToTuple("rank", DataType.INTEGER, 1, schema, tuple);
String template = "Id is <id> and rank is <rank>";
String result = TupleTools.toString(schema, tuple, template);
assertEquals("Id is _!@#$%^&*() and rank is 1", result);
}
}
|