Home | History | Annotate | Download | only in reflection
      1 /*
      2  * Copyright (c) 2007 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 package org.mockito.internal.util.reflection;
      6 
      7 import org.junit.Test;
      8 
      9 import java.lang.reflect.Field;
     10 import java.lang.reflect.Type;
     11 import java.util.*;
     12 
     13 import static org.junit.Assert.assertEquals;
     14 
     15 public class GenericMasterTest {
     16 
     17     GenericMaster m = new GenericMaster();
     18 
     19     List<String> one;
     20     Set<Integer> two;
     21     Map<Double, String> map;
     22     String nonGeneric;
     23     List<Set<String>> nested;
     24     List<Set<Collection<String>>> multiNested;
     25 
     26     public interface ListSet extends List<Set<?>> {}
     27     public interface MapNumberString extends Map<Number, String> {}
     28     public class HashMapNumberString<K extends Number> extends HashMap<K, String> {}
     29 
     30     public List<Number> numberList() { return null; }
     31     public Comparable<Number> numberComparable() { return null; }
     32     @SuppressWarnings("rawtypes")
     33     public List rawList() { return null; }
     34     public List<? extends Type> typeList() { return null; }
     35 
     36 
     37 
     38     @Test
     39     public void should_find_generic_class() throws Exception {
     40         assertEquals(String.class, m.getGenericType(field("one")));
     41         assertEquals(Integer.class, m.getGenericType(field("two")));
     42         assertEquals(Double.class, m.getGenericType(field("map")));
     43     }
     44 
     45     @Test
     46     public void should_get_object_for_non_generic() throws Exception {
     47         assertEquals(Object.class, m.getGenericType(field("nonGeneric")));
     48     }
     49 
     50     @Test
     51     public void should_deal_with_nested_generics() throws Exception {
     52         assertEquals(Set.class, m.getGenericType(field("nested")));
     53         assertEquals(Set.class, m.getGenericType(field("multiNested")));
     54     }
     55 
     56     private Field field(String fieldName) throws SecurityException, NoSuchFieldException {
     57         return this.getClass().getDeclaredField(fieldName);
     58     }
     59 
     60 }
     61