This project has retired. For details please refer to its Attic page.
TestReflectionUtils 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.utils;
19  
20  import static org.apache.giraph.utils.ReflectionUtils.getTypeArguments;
21  import static org.junit.Assert.assertEquals;
22  
23  import java.io.IOException;
24  
25  import org.apache.giraph.conf.TypesHolder;
26  import org.apache.giraph.edge.ByteArrayEdges;
27  import org.apache.giraph.edge.OutEdges;
28  import org.apache.giraph.factories.DefaultEdgeValueFactory;
29  import org.apache.giraph.factories.DefaultMessageValueFactory;
30  import org.apache.giraph.factories.DefaultVertexIdFactory;
31  import org.apache.giraph.factories.DefaultVertexValueFactory;
32  import org.apache.giraph.factories.EdgeValueFactory;
33  import org.apache.giraph.factories.MessageValueFactory;
34  import org.apache.giraph.factories.VertexIdFactory;
35  import org.apache.giraph.factories.VertexValueFactory;
36  import org.apache.giraph.graph.AbstractComputation;
37  import org.apache.giraph.graph.BasicComputation;
38  import org.apache.giraph.graph.Computation;
39  import org.apache.giraph.graph.DefaultVertexResolver;
40  import org.apache.giraph.graph.Vertex;
41  import org.apache.giraph.graph.VertexResolver;
42  import org.apache.hadoop.io.IntWritable;
43  import org.apache.hadoop.io.Writable;
44  import org.apache.hadoop.io.WritableComparable;
45  import org.junit.Test;
46  
47  public class TestReflectionUtils {
48    @Test
49    public void testPackagePath() {
50      assertEquals("org/apache/giraph/utils",
51          ReflectionUtils.getPackagePath(TestReflectionUtils.class));
52      assertEquals("org/apache/giraph/utils",
53          ReflectionUtils.getPackagePath(getClass()));
54      assertEquals("org/apache/giraph/utils",
55          ReflectionUtils.getPackagePath(this));
56    }
57  
58    private static class IntTypes implements TypesHolder<IntWritable,
59        IntWritable, IntWritable, IntWritable, IntWritable> { }
60  
61    private static class IntComputation extends AbstractComputation<IntWritable,
62            IntWritable, IntWritable, IntWritable, IntWritable> {
63      @Override
64      public void compute(Vertex<IntWritable, IntWritable, IntWritable> vertex,
65          Iterable<IntWritable> messages) throws IOException {
66      }
67    }
68  
69    private static class IntBasicComputation extends BasicComputation<IntWritable,
70        IntWritable, IntWritable, IntWritable> {
71      @Override
72      public void compute(Vertex<IntWritable, IntWritable, IntWritable> vertex,
73          Iterable<IntWritable> messages) throws IOException {
74      }
75    }
76  
77    @Test
78    public void testInferWithGenerics() {
79      Class<?>[] classes = getTypeArguments(VertexResolver.class,
80          DefaultVertexResolver.class);
81      assertEquals(3, classes.length);
82      assertEquals(WritableComparable.class, classes[0]);
83      assertEquals(Writable.class, classes[1]);
84      assertEquals(Writable.class, classes[2]);
85  
86      classes = getTypeArguments(VertexIdFactory.class,
87          DefaultVertexIdFactory.class);
88      assertEquals(1, classes.length);
89      assertEquals(WritableComparable.class, classes[0]);
90  
91      classes = getTypeArguments(VertexValueFactory.class,
92          DefaultVertexValueFactory.class);
93      assertEquals(1, classes.length);
94      assertEquals(Writable.class, classes[0]);
95  
96      classes = getTypeArguments(EdgeValueFactory.class,
97          DefaultEdgeValueFactory.class);
98      assertEquals(1, classes.length);
99      assertEquals(Writable.class, classes[0]);
100 
101     classes = getTypeArguments(MessageValueFactory.class,
102         DefaultMessageValueFactory.class);
103     assertEquals(1, classes.length);
104     assertEquals(Writable.class, classes[0]);
105 
106     classes = getTypeArguments(OutEdges.class, ByteArrayEdges.class);
107     assertEquals(2, classes.length);
108     assertEquals(WritableComparable.class, classes[0]);
109     assertEquals(Writable.class, classes[1]);
110   }
111 
112   @Test
113   public void testInferTypeParams() {
114     checkTypes(TypesHolder.class, IntTypes.class, 5);
115     checkTypes(TypesHolder.class, IntComputation.class, 5);
116     checkTypes(Computation.class, IntComputation.class, 5);
117     checkTypes(TypesHolder.class, IntBasicComputation.class, 5);
118     checkTypes(Computation.class, IntBasicComputation.class, 5);
119     checkTypes(BasicComputation.class, IntBasicComputation.class, 4);
120   }
121 
122   private <T> void checkTypes(Class<T> baseClass,
123     Class<? extends T> childClass, int numArgs) {
124     Class<?>[] classes = getTypeArguments(baseClass, childClass);
125     assertEquals(numArgs, classes.length);
126     for (Class<?> klass : classes) {
127       assertEquals(IntWritable.class, klass);
128     }
129   }
130 }