1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.component.util

File ReflectionUtilsTest.java

 

Code metrics

0
32
12
10
205
147
13
0.41
2.67
1.2
1.08

Classes

Class Line # Actions
ReflectionUtilsTest 36 32 0% 13 1
0.9772727597.7%
ReflectionUtilsTest.TestInterfaceSimple 38 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestInterface 43 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestInterface2 48 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestClass 53 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestClass2 58 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestClass3 63 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestClass4 68 0 - 0 0
-1.0 -
ReflectionUtilsTest.AbstractTestFieldClass 73 0 - 0 0
-1.0 -
ReflectionUtilsTest.TestFieldClass 79 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.Assert;
28    import org.junit.Test;
29    import org.xwiki.component.descriptor.ComponentRole;
30   
31    /**
32    * Unit tests for {@link ReflectionUtils}.
33    *
34    * @version $Id: a5229bc409cbb433b2c841eac70bd90bf0856ca6 $
35    */
 
36    public class ReflectionUtilsTest
37    {
 
38    private static interface TestInterfaceSimple
39    {
40   
41    }
42   
 
43    private static interface TestInterface<A, B>
44    {
45   
46    }
47   
 
48    private static interface TestInterface2<A, B> extends TestInterface<A, B>
49    {
50   
51    }
52   
 
53    private static class TestClass<A, B> implements TestInterface2<A, B>
54    {
55   
56    }
57   
 
58    private static class TestClass2<A> extends TestClass<A, Integer>
59    {
60   
61    }
62   
 
63    private static class TestClass3 extends TestClass2<List<String>>
64    {
65   
66    }
67   
 
68    private static class TestClass4<T> extends TestClass2<T> implements TestInterfaceSimple
69    {
70   
71    }
72   
 
73    private class AbstractTestFieldClass
74    {
75    @SuppressWarnings("unused")
76    private Object superField;
77    }
78   
 
79    private class TestFieldClass extends AbstractTestFieldClass
80    {
81    @SuppressWarnings("unused")
82    private Object field;
83    }
84   
 
85  1 toggle @Test
86    public void testGetField() throws Exception
87    {
88  1 Field field = ReflectionUtils.getField(TestFieldClass.class, "field");
89  1 Assert.assertNotNull(field);
90  1 Assert.assertEquals("field", field.getName());
91    }
92   
 
93  1 toggle @Test
94    public void testGetFieldFromSuperClass() throws Exception
95    {
96  1 Field field = ReflectionUtils.getField(TestFieldClass.class, "superField");
97  1 Assert.assertNotNull(field);
98  1 Assert.assertEquals("superField", field.getName());
99    }
100   
 
101  1 toggle @Test
102    public void testGetFieldWhenDoesntExist()
103    {
104  1 try {
105  1 ReflectionUtils.getField(TestFieldClass.class, "doesntexist");
106  0 Assert.fail();
107    } catch (NoSuchFieldException expected) {
108  1 Assert.assertEquals(
109    "No field named [doesntexist] in class [" + TestFieldClass.class.getName() + "] or superclasses",
110    expected.getMessage());
111    }
112    }
113   
 
114  1 toggle @Test
115    public void testUnserializeType() throws Exception
116    {
117  1 Type simpleType = ComponentRole.class;
118  1 Assert.assertEquals(simpleType, ReflectionUtils.unserializeType("org.xwiki.component.descriptor.ComponentRole",
119    Thread.currentThread().getContextClassLoader()));
120    }
121   
 
122  1 toggle @Test
123    public void testUnserializeTypeWithGenerics() throws Exception
124    {
125  1 Type genericsType = new DefaultParameterizedType(null, ComponentRole.class, String.class);
126  1 Assert.assertEquals(genericsType,
127    ReflectionUtils.unserializeType("org.xwiki.component.descriptor.ComponentRole<java.lang.String>",
128    Thread.currentThread().getContextClassLoader()));
129    }
130   
 
131  1 toggle @Test
132    public void testUnserializeListType() throws Exception
133    {
134  1 Type listType = new DefaultParameterizedType(null, java.util.List.class, ComponentRole.class);
135  1 Assert.assertEquals(listType,
136    ReflectionUtils.unserializeType("java.util.List<org.xwiki.component.descriptor.ComponentRole>",
137    Thread.currentThread().getContextClassLoader()));
138    }
139   
 
140  1 toggle @Test
141    public void testUnserializeMapType() throws Exception
142    {
143  1 Type mapType = new DefaultParameterizedType(null, java.util.Map.class, String.class, ComponentRole.class);
144  1 Assert.assertEquals(mapType,
145    ReflectionUtils.unserializeType(
146    "java.util.Map<java.lang.String, " + "org.xwiki.component.descriptor.ComponentRole>",
147    Thread.currentThread().getContextClassLoader()));
148    }
149   
 
150  1 toggle @Test
151    public void testUnserializeMapTypeWithGenerics() throws Exception
152    {
153  1 Type annotatedType = new DefaultParameterizedType(null, ComponentRole.class, String.class);
154  1 Type mapType = new DefaultParameterizedType(null, java.util.Map.class, String.class, annotatedType);
155  1 Assert.assertEquals(mapType,
156    ReflectionUtils.unserializeType(
157    "java.util.Map<java.lang.String, org.xwiki.component.descriptor.ComponentRole<java.lang.String>>",
158    Thread.currentThread().getContextClassLoader()));
159    }
160   
 
161  1 toggle @Test
162    public void testUnserializeMapInMapWithTypeWithGenerics() throws Exception
163    {
164  1 Type annotatedType = new DefaultParameterizedType(null, ComponentRole.class, String.class);
165  1 Type mapType1 = new DefaultParameterizedType(null, java.util.Map.class, String.class, annotatedType);
166  1 Type mapType2 = new DefaultParameterizedType(null, java.util.Map.class, String.class, mapType1);
167  1 Assert.assertEquals(mapType2,
168    ReflectionUtils.unserializeType(
169    "java.util.Map<java.lang.String, java.util.Map<java.lang.String, "
170    + "org.xwiki.component.descriptor.ComponentRole<java.lang.String>>>",
171    Thread.currentThread().getContextClassLoader()));
172    }
173   
 
174  1 toggle @Test
175    public void testGetAllFields()
176    {
177  1 Collection<Field> fields = ReflectionUtils.getAllFields(TestFieldClass.class);
178   
179  1 Assert.assertEquals(2, fields.size());
180    }
181   
 
182  1 toggle @Test
183    public void testGetTypeClass()
184    {
185  1 Assert.assertSame(TestFieldClass.class, ReflectionUtils.getTypeClass(TestFieldClass.class));
186  1 Assert.assertSame(TestFieldClass.class, ReflectionUtils
187    .getTypeClass(new DefaultParameterizedType(ReflectionUtilsTest.class, TestFieldClass.class)));
188    // TODO: Missing test on GenericArrayType
189    }
190   
 
191  1 toggle @Test
192    public void testResolveType()
193    {
194  1 Assert.assertEquals(
195    new DefaultParameterizedType(ReflectionUtilsTest.class, TestInterface.class,
196    new DefaultParameterizedType(null, List.class, String.class), Integer.class),
197    ReflectionUtils.resolveType(TestInterface.class, TestClass3.class));
198   
199  1 Assert.assertEquals(TestInterfaceSimple.class,
200    ReflectionUtils.resolveType(TestInterfaceSimple.class, TestClass4.class));
201   
202  1 Assert.assertEquals(TestInterfaceSimple.class, ReflectionUtils.resolveType(TestInterfaceSimple.class,
203    new DefaultParameterizedType(ReflectionUtilsTest.class, TestClass4.class, String.class)));
204    }
205    }