Home | History | Annotate | Download | only in reflect
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package org.apache.harmony.tests.java.lang.reflect;
     18 
     19 import java.lang.reflect.Method;
     20 import java.lang.reflect.TypeVariable;
     21 
     22 import junit.framework.TestCase;
     23 
     24 public class GenericReflectionTestsBase extends TestCase{
     25 
     26     /**
     27      * Returns the type parameter of the declaring method.
     28      *
     29      * @param method
     30      *            the declaring method
     31      * @return the type parameter of the method
     32      */
     33     public TypeVariable<Method> getTypeParameter(Method method) {
     34         TypeVariable<Method>[] typeParameters = method.getTypeParameters();
     35         assertLenghtOne(typeParameters);
     36         TypeVariable<Method> typeParameter = typeParameters[0];
     37         return typeParameter;
     38     }
     39 
     40     /**
     41      * Returns the type parameter of the declaring class.
     42      *
     43      * @param method
     44      *            the declaring method.
     45      * @return the type parameter of the method.
     46      */
     47     @SuppressWarnings("unchecked")
     48     public TypeVariable<Class> getTypeParameter(Class<?> clazz) {
     49         TypeVariable[] typeParameters = clazz.getTypeParameters();
     50         assertLenghtOne(typeParameters);
     51         TypeVariable<Class> typeVariable = typeParameters[0];
     52         assertEquals(clazz, typeVariable.getGenericDeclaration());
     53         assertEquals("T", typeVariable.getName());
     54         return typeVariable;
     55     }
     56 
     57     public static void assertLenghtOne(Object[] array) {
     58         TestCase.assertEquals("Array does NOT contain exactly one element.", 1, array.length);
     59     }
     60 
     61     public static void assertLenghtZero(Object[] array) {
     62         TestCase.assertEquals("Array has more than zero elements.", 0, array.length);
     63     }
     64 
     65     public static void assertInstanceOf(Class<?> expectedClass, Object actual) {
     66         TestCase.assertTrue(actual.getClass().getName() + " is not instance of :" + expectedClass.getName(), expectedClass
     67                 .isInstance(actual));
     68     }
     69 
     70     public static void assertNotEquals(Object expected, Object actual) {
     71         TestCase.assertFalse(actual.toString() + " has not to be equal to " + expected.toString(), expected.equals(actual));
     72     }
     73 
     74 }
     75