This project has retired. For details please refer to its
Attic page.
TestTextDoubleDoubleAdjacencyListVertexInputFormat xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.giraph.io;
19
20 import org.apache.giraph.conf.GiraphConfiguration;
21 import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
22 import org.apache.giraph.edge.Edge;
23 import org.apache.giraph.edge.EdgeFactory;
24 import org.apache.giraph.graph.Vertex;
25 import org.apache.giraph.io.formats.AdjacencyListTextVertexInputFormat;
26 import org.apache.giraph.io.formats.TextDoubleDoubleAdjacencyListVertexInputFormat;
27 import org.apache.giraph.utils.EdgeIterables;
28 import org.apache.giraph.utils.NoOpComputation;
29 import org.apache.hadoop.io.BooleanWritable;
30 import org.apache.hadoop.io.DoubleWritable;
31 import org.apache.hadoop.io.LongWritable;
32 import org.apache.hadoop.io.Text;
33 import org.apache.hadoop.io.Writable;
34 import org.apache.hadoop.io.WritableComparable;
35 import org.apache.hadoop.mapreduce.InputSplit;
36 import org.apache.hadoop.mapreduce.RecordReader;
37 import org.apache.hadoop.mapreduce.TaskAttemptContext;
38 import org.junit.Before;
39 import org.junit.Test;
40
41 import java.io.IOException;
42 import java.util.Arrays;
43
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertTrue;
46 import static org.junit.Assert.fail;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.when;
49
50 public class TestTextDoubleDoubleAdjacencyListVertexInputFormat extends TextDoubleDoubleAdjacencyListVertexInputFormat {
51
52 private RecordReader<LongWritable, Text> rr;
53 private ImmutableClassesGiraphConfiguration<Text, DoubleWritable,
54 DoubleWritable> conf;
55 private TaskAttemptContext tac;
56
57 @Before
58 public void setUp() throws IOException, InterruptedException {
59 rr = mock(RecordReader.class);
60 when(rr.nextKeyValue()).thenReturn(true).thenReturn(false);
61 GiraphConfiguration giraphConf = new GiraphConfiguration();
62 giraphConf.setComputationClass(DummyComputation.class);
63 conf = new ImmutableClassesGiraphConfiguration<Text, DoubleWritable,
64 DoubleWritable>(giraphConf);
65 tac = mock(TaskAttemptContext.class);
66 when(tac.getConfiguration()).thenReturn(conf);
67 }
68
69 protected TextVertexReader createVertexReader(
70 RecordReader<LongWritable, Text> rr) {
71 return createVertexReader(rr, null);
72 }
73
74 protected TextVertexReader createVertexReader(
75 final RecordReader<LongWritable, Text> rr, LineSanitizer lineSanitizer) {
76 return new TextDoubleDoubleAdjacencyListVertexReader(lineSanitizer) {
77 @Override
78 protected RecordReader<LongWritable, Text> createLineRecordReader(
79 InputSplit inputSplit, TaskAttemptContext context)
80 throws IOException, InterruptedException {
81 return rr;
82 }
83 };
84 }
85
86 @Test
87 public void testIndexMustHaveValue() throws IOException, InterruptedException {
88 String input = "hi";
89
90 when(rr.getCurrentValue()).thenReturn(new Text(input));
91 TextVertexReader vr = createVertexReader(rr);
92 vr.setConf(conf);
93 vr.initialize(null, tac);
94
95 try {
96 vr.nextVertex();
97 vr.getCurrentVertex();
98 fail("Should have thrown an IllegalArgumentException");
99 } catch (IllegalArgumentException iae) {
100 assertTrue(iae.getMessage().startsWith("Line did not split correctly: "));
101 }
102 }
103
104 @Test
105 public void testEdgesMustHaveValues() throws IOException, InterruptedException {
106 String input = "index\t55.66\tindex2";
107
108 when(rr.getCurrentValue()).thenReturn(new Text(input));
109 TextVertexReader vr = createVertexReader(rr);
110 vr.setConf(conf);
111 vr.initialize(null, tac);
112 try {
113 vr.nextVertex();
114 vr.getCurrentVertex();
115 fail("Should have thrown an IllegalArgumentException");
116 } catch (IllegalArgumentException iae) {
117 assertTrue(iae.getMessage().startsWith("Line did not split correctly: "));
118 }
119 }
120
121 public static <I extends WritableComparable, V extends Writable,
122 E extends WritableComparable> void assertValidVertex(
123 ImmutableClassesGiraphConfiguration<I, V, E> conf,
124 Vertex<I, V, E> actual,
125 I expectedId,
126 V expectedValue,
127 Edge<I, E>... edges) throws Exception {
128 Vertex<I, V, E> expected = conf.createVertex();
129 expected.initialize(expectedId, expectedValue, Arrays.asList(edges));
130 assertValid(expected, actual);
131 }
132
133 public static <I extends WritableComparable, V extends Writable,
134 E extends WritableComparable> void assertValid(
135 Vertex<I, V, E> expected, Vertex<I, V, E> actual) {
136 assertEquals(expected.getId(), actual.getId());
137 assertEquals(expected.getValue(), actual.getValue());
138 assertTrue(EdgeIterables.equals(expected.getEdges(), actual.getEdges()));
139 }
140
141 @Test
142 public void testHappyPath() throws Exception {
143 String input = "Hi\t0\tCiao\t1.123\tBomdia\t2.234\tOla\t3.345";
144
145 when(rr.getCurrentValue()).thenReturn(new Text(input));
146 TextVertexReader vr = createVertexReader(rr);
147 vr.setConf(conf);
148 vr.initialize(null, tac);
149 assertTrue("Should have been able to add a vertex", vr.nextVertex());
150 Vertex<Text, DoubleWritable, DoubleWritable> vertex = vr.getCurrentVertex();
151 assertValidVertex(conf, vertex,
152 new Text("Hi"), new DoubleWritable(0),
153 EdgeFactory.create(new Text("Ciao"), new DoubleWritable(1.123d)),
154 EdgeFactory.create(new Text("Bomdia"), new DoubleWritable(2.234d)),
155 EdgeFactory.create(new Text("Ola"), new DoubleWritable(3.345d)));
156 assertEquals(vertex.getNumEdges(), 3);
157 }
158
159 @Test
160 public void testLineSanitizer() throws Exception {
161 String input = "Bye\t0.01\tCiao\t1.001\tTchau\t2.0001\tAdios\t3.00001";
162
163 AdjacencyListTextVertexInputFormat.LineSanitizer toUpper =
164 new AdjacencyListTextVertexInputFormat.LineSanitizer() {
165 @Override
166 public String sanitize(String s) {
167 return s.toUpperCase();
168 }
169 };
170
171 when(rr.getCurrentValue()).thenReturn(new Text(input));
172 TextVertexReader vr = createVertexReader(rr, toUpper);
173 vr.setConf(conf);
174 vr.initialize(null, tac);
175 assertTrue("Should have been able to read vertex", vr.nextVertex());
176 Vertex<Text, DoubleWritable, DoubleWritable> vertex = vr.getCurrentVertex();
177 assertValidVertex(conf, vertex,
178 new Text("BYE"), new DoubleWritable(0.01d),
179 EdgeFactory.create(new Text("CIAO"), new DoubleWritable(1.001d)),
180 EdgeFactory.create(new Text("TCHAU"), new DoubleWritable(2.0001d)),
181 EdgeFactory.create(new Text("ADIOS"), new DoubleWritable(3.00001d)));
182
183 assertEquals(vertex.getNumEdges(), 3);
184 }
185
186 @Test
187 public void testDifferentSeparators() throws Exception {
188 String input = "alpha:42:beta:99";
189
190 when(rr.getCurrentValue()).thenReturn(new Text(input));
191 conf.set(AdjacencyListTextVertexInputFormat.LINE_TOKENIZE_VALUE, ":");
192 TextVertexReader vr = createVertexReader(rr);
193 vr.setConf(conf);
194 vr.initialize(null, tac);
195 assertTrue("Should have been able to read vertex", vr.nextVertex());
196 Vertex<Text, DoubleWritable, DoubleWritable> vertex = vr.getCurrentVertex();
197 assertValidVertex(conf, vertex,
198 new Text("alpha"), new DoubleWritable(42d),
199 EdgeFactory.create(new Text("beta"), new DoubleWritable(99d)));
200 assertEquals(vertex.getNumEdges(), 1);
201 }
202
203 public static class DummyComputation extends NoOpComputation<Text,
204 DoubleWritable, DoubleWritable, BooleanWritable> { }
205 }