1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.component.util

File ReflectionUtilsTest.java

 

Code metrics

0
31
12
10
199
147
12
0.39
2.58
1.2
1

Classes

Class Line # Actions
ReflectionUtilsTest 40 31 0% 12 0
1.0100%
ReflectionUtilsTest.TestInterfaceSimple 42 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestInterface 46 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestInterface2 50 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestClass 54 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestClass2 58 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestClass3 62 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestClass4 66 0 - 0 0
-1.0 -
ReflectionUtilsTest.AbstractTestFieldClass 70 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestFieldClass 76 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 12 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.component.util;
21   
22    import java.lang.reflect.Field;
23    import java.lang.reflect.Type;
24    import java.util.Collection;
25    import java.util.List;
26   
27    import org.junit.jupiter.api.Test;
28    import org.xwiki.component.descriptor.ComponentRole;
29   
30    import static org.junit.jupiter.api.Assertions.assertEquals;
31    import static org.junit.jupiter.api.Assertions.assertNotNull;
32    import static org.junit.jupiter.api.Assertions.assertSame;
33    import static org.junit.jupiter.api.Assertions.assertThrows;
34   
35    /**
36    * Unit tests for {@link ReflectionUtils}.
37    *
38    * @version $Id: e4d16fee0426a0aa04d212479db02e6c0aa6703c $
39    */
 
40    public class ReflectionUtilsTest
41    {
 
42    private static interface TestInterfaceSimple
43    {
44    }
45   
 
46    private static interface TestInterface<A, B>
47    {
48    }
49   
 
50    private static interface TestInterface2<A, B> extends TestInterface<A, B>
51    {
52    }
53   
 
54    private static class TestClass<A, B> implements TestInterface2<A, B>
55    {
56    }
57   
 
58    private static class TestClass2<A> extends TestClass<A, Integer>
59    {
60    }
61   
 
62    private static class TestClass3 extends TestClass2<List<String>>
63    {
64    }
65   
 
66    private static class TestClass4<T> extends TestClass2<T> implements TestInterfaceSimple
67    {
68    }
69   
 
70    private class AbstractTestFieldClass
71    {
72    @SuppressWarnings("unused")
73    private Object superField;
74    }
75   
 
76    private class TestFieldClass extends AbstractTestFieldClass
77    {
78    @SuppressWarnings("unused")
79    private Object field;
80    }
81   
 
82  1 toggle @Test
83    public void getField() throws Exception
84    {
85  1 Field field = ReflectionUtils.getField(TestFieldClass.class, "field");
86  1 assertNotNull(field);
87  1 assertEquals("field", field.getName());
88    }
89   
 
90  1 toggle @Test
91    public void getFieldFromSuperClass() throws Exception
92    {
93  1 Field field = ReflectionUtils.getField(TestFieldClass.class, "superField");
94  1 assertNotNull(field);
95  1 assertEquals("superField", field.getName());
96    }
97   
 
98  1 toggle @Test
99    public void getFieldWhenDoesntExist()
100    {
101  1 Throwable exception = assertThrows(NoSuchFieldException.class, () -> {
102  1 ReflectionUtils.getField(TestFieldClass.class, "doesntexist");
103    });
104  1 assertEquals("No field named [doesntexist] in class [" + TestFieldClass.class.getName() + "] or superclasses",
105    exception.getMessage());
106    }
107   
 
108  1 toggle @Test
109    public void unserializeType() throws Exception
110    {
111  1 Type simpleType = ComponentRole.class;
112  1 assertEquals(simpleType, ReflectionUtils.unserializeType("org.xwiki.component.descriptor.ComponentRole",
113    Thread.currentThread().getContextClassLoader()));
114    }
115   
 
116  1 toggle @Test
117    public void unserializeTypeWithGenerics() throws Exception
118    {
119  1 Type genericsType = new DefaultParameterizedType(null, ComponentRole.class, String.class);
120  1 assertEquals(genericsType,
121    ReflectionUtils.unserializeType("org.xwiki.component.descriptor.ComponentRole<java.lang.String>",
122    Thread.currentThread().getContextClassLoader()));
123    }
124   
 
125  1 toggle @Test
126    public void unserializeListType() throws Exception
127    {
128  1 Type listType = new DefaultParameterizedType(null, java.util.List.class, ComponentRole.class);
129  1 assertEquals(listType,
130    ReflectionUtils.unserializeType("java.util.List<org.xwiki.component.descriptor.ComponentRole>",
131    Thread.currentThread().getContextClassLoader()));
132    }
133   
 
134  1 toggle @Test
135    public void unserializeMapType() throws Exception
136    {
137  1 Type mapType = new DefaultParameterizedType(null, java.util.Map.class, String.class, ComponentRole.class);
138  1 assertEquals(mapType,
139    ReflectionUtils.unserializeType(
140    "java.util.Map<java.lang.String, " + "org.xwiki.component.descriptor.ComponentRole>",
141    Thread.currentThread().getContextClassLoader()));
142    }
143   
 
144  1 toggle @Test
145    public void unserializeMapTypeWithGenerics() throws Exception
146    {
147  1 Type annotatedType = new DefaultParameterizedType(null, ComponentRole.class, String.class);
148  1 Type mapType = new DefaultParameterizedType(null, java.util.Map.class, String.class, annotatedType);
149  1 assertEquals(mapType,
150    ReflectionUtils.unserializeType(
151    "java.util.Map<java.lang.String, org.xwiki.component.descriptor.ComponentRole<java.lang.String>>",
152    Thread.currentThread().getContextClassLoader()));
153    }
154   
 
155  1 toggle @Test
156    public void testUnserializeMapInMapWithTypeWithGenerics() throws Exception
157    {
158  1 Type annotatedType = new DefaultParameterizedType(null, ComponentRole.class, String.class);
159  1 Type mapType1 = new DefaultParameterizedType(null, java.util.Map.class, String.class, annotatedType);
160  1 Type mapType2 = new DefaultParameterizedType(null, java.util.Map.class, String.class, mapType1);
161  1 assertEquals(mapType2,
162    ReflectionUtils.unserializeType(
163    "java.util.Map<java.lang.String, java.util.Map<java.lang.String, "
164    + "org.xwiki.component.descriptor.ComponentRole<java.lang.String>>>",
165    Thread.currentThread().getContextClassLoader()));
166    }
167   
 
168  1 toggle @Test
169    public void getAllFields()
170    {
171  1 Collection<Field> fields = ReflectionUtils.getAllFields(TestFieldClass.class);
172   
173  1 assertEquals(2, fields.size());
174    }
175   
 
176  1 toggle @Test
177    public void getTypeClass()
178    {
179  1 assertSame(TestFieldClass.class, ReflectionUtils.getTypeClass(TestFieldClass.class));
180  1 assertSame(TestFieldClass.class, ReflectionUtils
181    .getTypeClass(new DefaultParameterizedType(ReflectionUtilsTest.class, TestFieldClass.class)));
182    // TODO: Missing test on GenericArrayType
183    }
184   
 
185  1 toggle @Test
186    public void resolveType()
187    {
188  1 assertEquals(
189    new DefaultParameterizedType(ReflectionUtilsTest.class, TestInterface.class,
190    new DefaultParameterizedType(null, List.class, String.class), Integer.class),
191    ReflectionUtils.resolveType(TestInterface.class, TestClass3.class));
192   
193  1 assertEquals(TestInterfaceSimple.class,
194    ReflectionUtils.resolveType(TestInterfaceSimple.class, TestClass4.class));
195   
196  1 assertEquals(TestInterfaceSimple.class, ReflectionUtils.resolveType(TestInterfaceSimple.class,
197    new DefaultParameterizedType(ReflectionUtilsTest.class, TestClass4.class, String.class)));
198    }
199    }