This project has retired. For details please refer to its Attic page.
TestSrcIdDstIdEdgeValueTextOutputFormat 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  
19  package org.apache.giraph.io;
20  
21  import static org.apache.giraph.conf.GiraphConstants.GIRAPH_TEXT_OUTPUT_FORMAT_REVERSE;
22  import static org.apache.giraph.conf.GiraphConstants.GIRAPH_TEXT_OUTPUT_FORMAT_SEPARATOR;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.verify;
25  import static org.mockito.Mockito.when;
26  
27  import java.io.IOException;
28  
29  import org.apache.giraph.conf.GiraphConfiguration;
30  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
31  import org.apache.giraph.edge.Edge;
32  import org.apache.giraph.io.formats.SrcIdDstIdEdgeValueTextOutputFormat;
33  import org.apache.giraph.utils.NoOpComputation;
34  import org.apache.hadoop.io.DoubleWritable;
35  import org.apache.hadoop.io.LongWritable;
36  import org.apache.hadoop.io.Text;
37  import org.apache.hadoop.mapreduce.RecordWriter;
38  import org.apache.hadoop.mapreduce.TaskAttemptContext;
39  import org.junit.Before;
40  import org.junit.Test;
41  
42  public class TestSrcIdDstIdEdgeValueTextOutputFormat
43    extends SrcIdDstIdEdgeValueTextOutputFormat<LongWritable,
44            LongWritable, LongWritable> {
45    /** Test configuration */
46    private ImmutableClassesGiraphConfiguration<
47        LongWritable, LongWritable, LongWritable> conf;
48    /**
49     * Dummy class to allow ImmutableClassesGiraphConfiguration to be created.
50     */
51    public static class DummyComputation extends NoOpComputation<Text,
52        DoubleWritable, DoubleWritable, DoubleWritable> { }
53  
54    @Before
55    public void setUp() {
56      GiraphConfiguration giraphConfiguration = new GiraphConfiguration();
57      giraphConfiguration.setComputationClass(DummyComputation.class);
58      conf =
59        new ImmutableClassesGiraphConfiguration<LongWritable, LongWritable,
60          LongWritable>(giraphConfiguration);
61    }
62  
63    @Test
64    public void testHappyPath() throws IOException, InterruptedException {
65      Text expected = new Text("0\t1\t5");
66  
67      checkSrcIdDstIdEdgeValueWorker(expected);
68    }
69  
70    @Test
71    public void testReverseIdAndValue() throws IOException, InterruptedException {
72      GIRAPH_TEXT_OUTPUT_FORMAT_REVERSE.set(this.conf, true);
73      Text expected = new Text("5\t1\t0");
74  
75      checkSrcIdDstIdEdgeValueWorker(expected);
76    }
77  
78    @Test
79    public void testWithDifferentDelimiter()  throws IOException,
80        InterruptedException {
81      GIRAPH_TEXT_OUTPUT_FORMAT_SEPARATOR.set(this.conf, "->");
82      Text expected = new Text("0->1->5");
83  
84      checkSrcIdDstIdEdgeValueWorker(expected);
85    }
86  
87    private void checkSrcIdDstIdEdgeValueWorker(Text expected)
88      throws IOException, InterruptedException {
89  
90      TaskAttemptContext tac = mock(TaskAttemptContext.class);
91      when(tac.getConfiguration()).thenReturn(conf);
92  
93      Edge edge = mock(Edge.class);
94  
95      when(edge.getTargetVertexId()).thenReturn(new LongWritable(1));
96      when(edge.getValue()).thenReturn(new LongWritable(5));
97  
98      final RecordWriter<Text, Text> tw = mock(RecordWriter.class);
99      SrcIdDstIdEdgeValueEdgeWriter writer = new SrcIdDstIdEdgeValueEdgeWriter() {
100       @Override
101       protected RecordWriter<Text, Text> createLineRecordWriter(
102         TaskAttemptContext context) throws IOException, InterruptedException {
103 
104         return tw;
105       }
106     };
107 
108     writer.setConf(conf);
109     writer.initialize(tac);
110     writer.writeEdge(new LongWritable(0), new LongWritable(0), edge);
111 
112     verify(tw).write(expected, null);
113   }
114 }