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.Arrays;
30import java.util.Collection;
31import java.util.Collections;
32import java.util.Iterator;
3334import org.apache.giraph.conf.GiraphConfiguration;
35import org.apache.giraph.graph.BasicComputation;
36import org.apache.giraph.graph.Vertex;
37import org.apache.giraph.io.formats.IdWithValueTextOutputFormat;
38import org.apache.giraph.utils.InternalVertexRunner;
39import org.apache.hadoop.io.DoubleWritable;
40import org.apache.hadoop.io.FloatWritable;
41import org.apache.hadoop.io.LongWritable;
42import org.junit.Test;
43import org.junit.Assert;
4445/**46 * Test class for Gora vertex input/output formats.47 */48publicclassTestGoraVertexInputFormat {
4950 @Test
51publicvoid getEmptyDb() throws Exception {
52 Iterable<String> results;
53 Iterator<String> result;
54 GiraphConfiguration conf = new GiraphConfiguration();
55 GIRAPH_GORA_DATASTORE_CLASS.
56 set(conf, "org.apache.gora.memory.store.MemStore");
57 GIRAPH_GORA_KEYS_FACTORY_CLASS.
58 set(conf,"org.apache.giraph.io.gora.utils.DefaultKeyFactory");
59 GIRAPH_GORA_KEY_CLASS.set(conf,"java.lang.String");
60 GIRAPH_GORA_PERSISTENT_CLASS.
61 set(conf,"org.apache.giraph.io.gora.generated.GVertex");
62 GIRAPH_GORA_START_KEY.set(conf,"1");
63 GIRAPH_GORA_END_KEY.set(conf,"10");
64 conf.set("io.serializations",
65"org.apache.hadoop.io.serializer.WritableSerialization," +
66"org.apache.hadoop.io.serializer.JavaSerialization");
67 conf.setComputationClass(EmptyComputation.class);
68 conf.setVertexInputFormatClass(GoraTestVertexInputFormat.class);
69 results = InternalVertexRunner.run(conf, new String[0], new String[0]);
70 Assert.assertNotNull(results);
71 result = results.iterator();
72 Assert.assertFalse(result.hasNext());
73 }
7475 @Test
76publicvoid getTestDb() throws Exception {
77 Iterable<String> results;
78 GiraphConfiguration conf = new GiraphConfiguration();
79 GIRAPH_GORA_DATASTORE_CLASS.
80 set(conf, "org.apache.gora.memory.store.MemStore");
81 GIRAPH_GORA_KEYS_FACTORY_CLASS.
82 set(conf,"org.apache.giraph.io.gora.utils.DefaultKeyFactory");
83 GIRAPH_GORA_KEY_CLASS.set(conf,"java.lang.String");
84 GIRAPH_GORA_PERSISTENT_CLASS.
85 set(conf,"org.apache.giraph.io.gora.generated.GVertex");
86 GIRAPH_GORA_START_KEY.set(conf,"1");
87 GIRAPH_GORA_END_KEY.set(conf,"100");
88 conf.set("io.serializations",
89"org.apache.hadoop.io.serializer.WritableSerialization," +
90"org.apache.hadoop.io.serializer.JavaSerialization");
91 conf.setComputationClass(EmptyComputation.class);
92 conf.setVertexInputFormatClass(GoraTestVertexInputFormat.class);
93 conf.setVertexOutputFormatClass(IdWithValueTextOutputFormat.class);
94 results = InternalVertexRunner.run(conf, new String[0], new String[0]);
95 Assert.assertNotNull(results);
96 Assert.assertEquals(3, ((ArrayList<?>)results).size());
97 Collections.sort((ArrayList)results);
98 String[] correct = new String[] {"1\t0.0", "10\t0.0", "100\t0.0"};
99 Arrays.sort(correct);
100for (int i = 0; i < correct.length; i++) {
101 Assert.assertEquals(correct[i],
102 ((ArrayList<?>) results).get(i).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 vertex.voteToHalt();
119 }
120 }
121 }