This project has retired. For details please refer to its Attic page.
TestIdWithValueTextOutputFormat 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 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    /** Test configuration */
42    private ImmutableClassesGiraphConfiguration<
43        Text, DoubleWritable, Writable> conf;
44    /**
45     * Dummy class to allow ImmutableClassesGiraphConfiguration to be created.
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      // Create empty iterator == no edges
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 }