This project has retired. For details please refer to its
Attic page.
TestIdWithValueTextOutputFormat xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.giraph.io;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23
24 import org.apache.giraph.conf.GiraphConfiguration;
25 import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
26 import org.apache.giraph.graph.Vertex;
27 import org.apache.giraph.io.formats.IdWithValueTextOutputFormat;
28 import org.apache.giraph.utils.NoOpComputation;
29 import org.apache.hadoop.io.DoubleWritable;
30 import org.apache.hadoop.io.Text;
31 import org.apache.hadoop.io.Writable;
32 import org.apache.hadoop.mapreduce.RecordWriter;
33 import org.apache.hadoop.mapreduce.TaskAttemptContext;
34 import org.junit.Before;
35 import org.junit.Test;
36
37 import static org.mockito.Mockito.*;
38
39 public class TestIdWithValueTextOutputFormat extends
40 IdWithValueTextOutputFormat<Text, DoubleWritable, Writable> {
41
42 private ImmutableClassesGiraphConfiguration<
43 Text, DoubleWritable, Writable> conf;
44
45
46
47 public static class DummyComputation extends NoOpComputation<Text,
48 DoubleWritable, DoubleWritable, DoubleWritable> { }
49
50 @Before
51 public void setUp() {
52 GiraphConfiguration giraphConfiguration = new GiraphConfiguration();
53 giraphConfiguration.setComputationClass(DummyComputation.class);
54 conf = new ImmutableClassesGiraphConfiguration<Text, DoubleWritable,
55 Writable>(giraphConfiguration);
56 }
57
58 @Test
59 public void testHappyPath() throws IOException, InterruptedException {
60 Text expected = new Text("Four Tops\t4.0");
61
62 IdWithValueTestWorker(expected);
63 }
64
65 @Test
66 public void testReverseIdAndValue() throws IOException, InterruptedException {
67 conf.setBoolean(REVERSE_ID_AND_VALUE, true);
68 Text expected = new Text("4.0\tFour Tops");
69
70 IdWithValueTestWorker(expected);
71 }
72
73 @Test
74 public void testWithDifferentDelimiter() throws IOException,
75 InterruptedException {
76 conf.set(LINE_TOKENIZE_VALUE, "blah");
77 Text expected = new Text("Four Topsblah4.0");
78
79 IdWithValueTestWorker(expected);
80 }
81
82 private void IdWithValueTestWorker(Text expected)
83 throws IOException, InterruptedException {
84 TaskAttemptContext tac = mock(TaskAttemptContext.class);
85 when(tac.getConfiguration()).thenReturn(conf);
86
87 Vertex vertex = mock(Vertex.class);
88 when(vertex.getId()).thenReturn(new Text("Four Tops"));
89 when(vertex.getValue()).thenReturn(new DoubleWritable(4d));
90
91
92 when(vertex.getEdges()).thenReturn(new ArrayList<Text>());
93
94 final RecordWriter<Text, Text> tw = mock(RecordWriter.class);
95 IdWithValueVertexWriter writer = new IdWithValueVertexWriter() {
96 @Override
97 protected RecordWriter<Text, Text> createLineRecordWriter(
98 TaskAttemptContext context) throws IOException, InterruptedException {
99 return tw;
100 }
101 };
102 writer.setConf(conf);
103 writer.initialize(tac);
104 writer.writeVertex(vertex);
105
106 verify(tw).write(expected, null);
107 verify(vertex, times(0)).getEdges();
108 }
109 }