This project has retired. For details please refer to its Attic page.
TestLongDoubleDoubleAdjacencyListVertexInputFormat 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  
21  import java.io.IOException;
22  
23  import org.apache.giraph.conf.GiraphConfiguration;
24  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
25  import org.apache.giraph.edge.EdgeFactory;
26  import org.apache.giraph.graph.Vertex;
27  import org.apache.giraph.io.formats.AdjacencyListTextVertexInputFormat;
28  import org.apache.giraph.io.formats.LongDoubleDoubleAdjacencyListVertexInputFormat;
29  
30  import org.apache.giraph.utils.NoOpComputation;
31  import org.apache.hadoop.io.BooleanWritable;
32  import org.apache.hadoop.io.DoubleWritable;
33  import org.apache.hadoop.io.LongWritable;
34  import org.apache.hadoop.io.Text;
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 static org.apache.giraph.io.TestTextDoubleDoubleAdjacencyListVertexInputFormat.assertValidVertex;
42  import static org.junit.Assert.*;
43  import static org.mockito.Mockito.mock;
44  import static org.mockito.Mockito.when;
45  
46  public class TestLongDoubleDoubleAdjacencyListVertexInputFormat extends LongDoubleDoubleAdjacencyListVertexInputFormat {
47  
48    private RecordReader<LongWritable, Text> rr;
49    private ImmutableClassesGiraphConfiguration<LongWritable, DoubleWritable,
50        DoubleWritable> conf;
51    private TaskAttemptContext tac;
52  
53    @Before
54    public void setUp() throws IOException, InterruptedException {
55      rr = mock(RecordReader.class);
56      when(rr.nextKeyValue()).thenReturn(true);
57      GiraphConfiguration giraphConf = new GiraphConfiguration();
58      giraphConf.setComputationClass(DummyComputation.class);
59      conf = new ImmutableClassesGiraphConfiguration<LongWritable, DoubleWritable,
60          DoubleWritable>(giraphConf);
61      tac = mock(TaskAttemptContext.class);
62      when(tac.getConfiguration()).thenReturn(conf);
63    }
64  
65    protected TextVertexReader createVertexReader(
66         RecordReader<LongWritable, Text> rr) {
67      return createVertexReader(rr, null);
68    }
69  
70    protected TextVertexReader createVertexReader(
71        final RecordReader<LongWritable, Text> rr, LineSanitizer lineSanitizer) {
72      return new LongDoubleDoubleAdjacencyListVertexReader(lineSanitizer) {
73        @Override
74        protected RecordReader<LongWritable, Text> createLineRecordReader(
75            InputSplit inputSplit, TaskAttemptContext context)
76            throws IOException, InterruptedException {
77          return rr;
78        }
79      };
80    }
81  
82    @Test
83    public void testIndexMustHaveValue() throws IOException, InterruptedException {
84      String input = "123";
85  
86      when(rr.getCurrentValue()).thenReturn(new Text(input));
87      TextVertexReader vr = createVertexReader(rr);
88  
89      vr.setConf(conf);
90      vr.initialize(null, tac);
91  
92      try {
93        vr.nextVertex();
94        vr.getCurrentVertex();
95        fail("Should have thrown an IllegalArgumentException");
96      } catch (IllegalArgumentException iae) {
97        assertTrue(iae.getMessage().startsWith("Line did not split correctly: "));
98      }
99    }
100 
101   @Test
102   public void testEdgesMustHaveValues() throws IOException, InterruptedException {
103     String input = "99\t55.2\t100";
104 
105     when(rr.getCurrentValue()).thenReturn(new Text(input));
106     TextVertexReader vr = createVertexReader(rr);
107 
108     vr.setConf(conf);
109     vr.initialize(null, tac);
110 
111     try {
112       vr.nextVertex();
113       vr.getCurrentVertex();
114       fail("Should have thrown an IllegalArgumentException");
115     } catch (IllegalArgumentException iae) {
116       assertTrue(iae.getMessage().startsWith("Line did not split correctly: "));
117     }
118   }
119 
120   @Test
121   public void testHappyPath() throws Exception {
122     String input = "42\t0.1\t99\t0.2\t2000\t0.3\t4000\t0.4";
123 
124     when(rr.getCurrentValue()).thenReturn(new Text(input));
125     TextVertexReader vr = createVertexReader(rr);
126     vr.setConf(conf);
127     vr.initialize(null, tac);
128 
129     assertTrue("Should have been able to read vertex", vr.nextVertex());
130     Vertex<LongWritable, DoubleWritable, DoubleWritable>
131         vertex = vr.getCurrentVertex();
132     assertValidVertex(conf, vertex,
133         new LongWritable(42), new DoubleWritable(0.1),
134         EdgeFactory.create(new LongWritable(99), new DoubleWritable(0.2)),
135         EdgeFactory.create(new LongWritable(2000), new DoubleWritable(0.3)),
136         EdgeFactory.create(new LongWritable(4000), new DoubleWritable(0.4)));
137     assertEquals(vertex.getNumEdges(), 3);
138   }
139 
140   @Test
141   public void testDifferentSeparators() throws Exception {
142     String input = "12345:42.42:9999999:99.9";
143 
144     when(rr.getCurrentValue()).thenReturn(new Text(input));
145     conf.set(AdjacencyListTextVertexInputFormat.LINE_TOKENIZE_VALUE, ":");
146     TextVertexReader vr = createVertexReader(rr);
147     vr.setConf(conf);
148     vr.initialize(null, tac);
149     assertTrue("Should have been able to read vertex", vr.nextVertex());
150     Vertex<LongWritable, DoubleWritable, DoubleWritable>
151         vertex = vr.getCurrentVertex();
152     assertValidVertex(conf, vertex,
153         new LongWritable(12345), new DoubleWritable(42.42),
154        EdgeFactory.create(new LongWritable(9999999), new DoubleWritable(99.9)));
155     assertEquals(vertex.getNumEdges(), 1);
156   }
157 
158   public static class DummyComputation extends NoOpComputation<LongWritable,
159       DoubleWritable, DoubleWritable, BooleanWritable> { }
160 }