This project has retired. For details please refer to its Attic page.
TestAdjacencyListTextVertexOutputFormat xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.giraph.io;
19  
20  import com.google.common.collect.Lists;
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.giraph.conf.GiraphConfiguration;
27  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
28  import org.apache.giraph.edge.Edge;
29  import org.apache.giraph.edge.EdgeFactory;
30  import org.apache.giraph.graph.Vertex;
31  import org.apache.giraph.io.formats.AdjacencyListTextVertexOutputFormat;
32  import org.apache.giraph.utils.NoOpComputation;
33  import org.apache.hadoop.io.DoubleWritable;
34  import org.apache.hadoop.io.Text;
35  import org.apache.hadoop.mapreduce.RecordWriter;
36  import org.apache.hadoop.mapreduce.TaskAttemptContext;
37  import org.junit.Before;
38  import org.junit.Test;
39  
40  import static org.mockito.Mockito.*;
41  
42  public class TestAdjacencyListTextVertexOutputFormat extends AdjacencyListTextVertexOutputFormat<Text, DoubleWritable, DoubleWritable> {
43    /** Test configuration */
44    private ImmutableClassesGiraphConfiguration<Text,
45        DoubleWritable, DoubleWritable> conf;
46  
47    /**
48     * Dummy class to allow ImmutableClassesGiraphConfiguration to be created.
49     */
50    public static class DummyComputation extends NoOpComputation<Text,
51        DoubleWritable, DoubleWritable, DoubleWritable> { }
52  
53    @Before
54    public void setUp() {
55      GiraphConfiguration giraphConfiguration = new GiraphConfiguration();
56      giraphConfiguration.setComputationClass(DummyComputation.class);
57      conf = new ImmutableClassesGiraphConfiguration<Text,
58          DoubleWritable, DoubleWritable>(giraphConfiguration);
59    }
60  
61    protected AdjacencyListTextVertexWriter createVertexWriter(
62        final RecordWriter<Text, Text> tw) {
63      AdjacencyListTextVertexWriter writer = new AdjacencyListTextVertexWriter() {
64        @Override
65        protected RecordWriter<Text, Text> createLineRecordWriter(
66            TaskAttemptContext context) throws IOException, InterruptedException {
67          return tw;
68        }
69      };
70      return writer;
71    }
72  
73    @Test
74    public void testVertexWithNoEdges() throws IOException, InterruptedException {
75      TaskAttemptContext tac = mock(TaskAttemptContext.class);
76      when(tac.getConfiguration()).thenReturn(conf);
77  
78      Vertex vertex = mock(Vertex.class);
79      when(vertex.getId()).thenReturn(new Text("The Beautiful South"));
80      when(vertex.getValue()).thenReturn(new DoubleWritable(32.2d));
81      // Create empty iterable == no edges
82      when(vertex.getEdges()).thenReturn(new ArrayList<Text>());
83  
84      RecordWriter<Text, Text> tw = mock(RecordWriter.class);
85      AdjacencyListTextVertexWriter writer = createVertexWriter(tw);
86      writer.setConf(conf);
87      writer.initialize(tac);
88      writer.writeVertex(vertex);
89  
90      Text expected = new Text("The Beautiful South\t32.2");
91      verify(tw).write(expected, null);
92      verify(vertex, times(1)).getEdges();
93    }
94  
95    @Test
96    public void testVertexWithEdges() throws IOException, InterruptedException {
97      TaskAttemptContext tac = mock(TaskAttemptContext.class);
98      when(tac.getConfiguration()).thenReturn(conf);
99  
100     Vertex vertex = mock(Vertex.class);
101     when(vertex.getId()).thenReturn(new Text("San Francisco"));
102     when(vertex.getValue()).thenReturn(new DoubleWritable(0d));
103     List<Edge<Text, DoubleWritable>> cities = Lists.newArrayList();
104     Collections.addAll(cities,
105         EdgeFactory.create(new Text("Los Angeles"), new DoubleWritable(347.16)),
106         EdgeFactory.create(new Text("Phoenix"), new DoubleWritable(652.48)));
107 
108     when(vertex.getEdges()).thenReturn(cities);
109 
110     RecordWriter<Text, Text> tw = mock(RecordWriter.class);
111     AdjacencyListTextVertexWriter writer = createVertexWriter(tw);
112     writer.setConf(conf);
113     writer.initialize(tac);
114     writer.writeVertex(vertex);
115 
116     Text expected = new Text("San Francisco\t0.0\tLos Angeles\t347.16\t" +
117             "Phoenix\t652.48");
118     verify(tw).write(expected, null);
119     verify(vertex, times(1)).getEdges();
120   }
121 
122   @Test
123   public void testWithDifferentDelimiter() throws IOException, InterruptedException {
124     conf.set(AdjacencyListTextVertexOutputFormat.LINE_TOKENIZE_VALUE, ":::");
125     TaskAttemptContext tac = mock(TaskAttemptContext.class);
126     when(tac.getConfiguration()).thenReturn(conf);
127 
128     Vertex vertex = mock(Vertex.class);
129     when(vertex.getId()).thenReturn(new Text("San Francisco"));
130     when(vertex.getValue()).thenReturn(new DoubleWritable(0d));
131     List<Edge<Text, DoubleWritable>> cities = Lists.newArrayList();
132     Collections.addAll(cities,
133         EdgeFactory.create(new Text("Los Angeles"), new DoubleWritable(347.16)),
134         EdgeFactory.create(new Text("Phoenix"), new DoubleWritable(652.48)));
135 
136     when(vertex.getEdges()).thenReturn(cities);
137 
138     RecordWriter<Text, Text> tw = mock(RecordWriter.class);
139     AdjacencyListTextVertexWriter writer = createVertexWriter(tw);
140     writer.setConf(conf);
141     writer.initialize(tac);
142     writer.writeVertex(vertex);
143 
144     Text expected = new Text("San Francisco:::0.0:::Los Angeles:::347.16:::" +
145             "Phoenix:::652.48");
146     verify(tw).write(expected, null);
147     verify(vertex, times(1)).getEdges();
148   }
149 
150 }