1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.giraph.io.gora;
1920importstatic org.apache.giraph.io.gora.constants.GiraphGoraConstants.GIRAPH_GORA_DATASTORE_CLASS;
21importstatic org.apache.giraph.io.gora.constants.GiraphGoraConstants.GIRAPH_GORA_END_KEY;
22importstatic org.apache.giraph.io.gora.constants.GiraphGoraConstants.GIRAPH_GORA_KEYS_FACTORY_CLASS;
23importstatic org.apache.giraph.io.gora.constants.GiraphGoraConstants.GIRAPH_GORA_KEY_CLASS;
24importstatic org.apache.giraph.io.gora.constants.GiraphGoraConstants.GIRAPH_GORA_PERSISTENT_CLASS;
25importstatic org.apache.giraph.io.gora.constants.GiraphGoraConstants.GIRAPH_GORA_START_KEY;
2627import java.io.IOException;
28import java.util.ArrayList;
29import java.util.Collection;
30import java.util.Iterator;
3132import org.apache.giraph.conf.GiraphConfiguration;
33import org.apache.giraph.graph.BasicComputation;
34import org.apache.giraph.graph.Vertex;
35import org.apache.giraph.io.formats.IdWithValueTextOutputFormat;
36import org.apache.giraph.utils.InternalVertexRunner;
37import org.apache.hadoop.io.DoubleWritable;
38import org.apache.hadoop.io.FloatWritable;
39import org.apache.hadoop.io.LongWritable;
40import org.junit.Test;
41import org.junit.Assert;
4243/**44 * Test class for Gora vertex input/output formats.45 */46publicclassTestGoraEdgeInputFormat {
4748 @Test
49publicvoid getEmptyDb() throws Exception {
50 Iterable<String> results;
51 Iterator<String> result;
52 GiraphConfiguration conf = new GiraphConfiguration();
53 GIRAPH_GORA_DATASTORE_CLASS.
54 set(conf, "org.apache.gora.memory.store.MemStore");
55 GIRAPH_GORA_KEYS_FACTORY_CLASS.
56 set(conf,"org.apache.giraph.io.gora.utils.DefaultKeyFactory");
57 GIRAPH_GORA_KEY_CLASS.set(conf,"java.lang.String");
58 GIRAPH_GORA_PERSISTENT_CLASS.
59 set(conf,"org.apache.giraph.io.gora.generated.GEdge");
60 GIRAPH_GORA_START_KEY.set(conf,"1");
61 GIRAPH_GORA_END_KEY.set(conf,"3");
62 conf.set("io.serializations",
63"org.apache.hadoop.io.serializer.JavaSerialization," +
64"org.apache.hadoop.io.serializer.WritableSerialization");
65 conf.setComputationClass(EmptyComputation.class);
66 conf.setEdgeInputFormatClass(GoraGEdgeEdgeInputFormat.class);
67 results = InternalVertexRunner.run(conf, new String[0], new String[0]);
68 Assert.assertNotNull(results);
69 result = results.iterator();
70 Assert.assertFalse(result.hasNext());
71 }
7273 @Test
74publicvoid getTestDb() throws Exception {
75 Iterable<String> results;
76 GiraphConfiguration conf = new GiraphConfiguration();
77 GIRAPH_GORA_DATASTORE_CLASS.
78 set(conf, "org.apache.gora.memory.store.MemStore");
79 GIRAPH_GORA_KEYS_FACTORY_CLASS.
80 set(conf,"org.apache.giraph.io.gora.utils.DefaultKeyFactory");
81 GIRAPH_GORA_KEY_CLASS.set(conf,"java.lang.String");
82 GIRAPH_GORA_PERSISTENT_CLASS.
83 set(conf,"org.apache.giraph.io.gora.generated.GEdge");
84 GIRAPH_GORA_START_KEY.set(conf,"1");
85 GIRAPH_GORA_END_KEY.set(conf,"4");
86 conf.set("io.serializations",
87"org.apache.hadoop.io.serializer.WritableSerialization," +
88"org.apache.hadoop.io.serializer.JavaSerialization");
89 conf.setComputationClass(EmptyComputation.class);
90 conf.setEdgeInputFormatClass(GoraTestEdgeInputFormat.class);
91 conf.setVertexOutputFormatClass(IdWithValueTextOutputFormat.class);
92 results = InternalVertexRunner.run(conf, new String[0], new String[0]);
93 Assert.assertNotNull(results);
94 Assert.assertEquals(3, ((ArrayList<?>)results).size());
95if (results instanceof Collection<?>
96 & (((Collection<?>)results).size() == 2)) {
97 Assert.assertEquals("33\t0.0",
98 ((ArrayList<?>)results).get(0).toString());
99 Assert.assertEquals("22\t0.0",
100 ((ArrayList<?>)results).get(1).toString());
101 Assert.assertEquals("11\t0.0",
102 ((ArrayList<?>)results).get(2).toString());
103 }
104 }
105106/*107 Test compute method that sends each edge a notification of its parents.108 The test set only has a 1-1 parent-to-child ratio for this unit test.109 */110publicstaticclassEmptyComputation111extends BasicComputation<LongWritable, DoubleWritable,
112 FloatWritable, LongWritable> {
113114 @Override
115publicvoid compute(
116 Vertex<LongWritable, DoubleWritable, FloatWritable> vertex,
117 Iterable<LongWritable> messages) throws IOException {
118 Assert.assertNotNull(vertex);
119 vertex.voteToHalt();
120 }
121 }
122 }