| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 33 |
|
|
| 34 |
|
@version |
| 35 |
|
|
| |
|
| 97.7% |
Uncovered Elements: 1 (44) |
Complexity: 13 |
Complexity Density: 0.41 |
|
| 36 |
|
public class ReflectionUtilsTest |
| 37 |
|
{ |
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 38 |
|
private static interface TestInterfaceSimple |
| 39 |
|
{ |
| 40 |
|
|
| 41 |
|
} |
| 42 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 43 |
|
private static interface TestInterface<A, B> |
| 44 |
|
{ |
| 45 |
|
|
| 46 |
|
} |
| 47 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 48 |
|
private static interface TestInterface2<A, B> extends TestInterface<A, B> |
| 49 |
|
{ |
| 50 |
|
|
| 51 |
|
} |
| 52 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 53 |
|
private static class TestClass<A, B> implements TestInterface2<A, B> |
| 54 |
|
{ |
| 55 |
|
|
| 56 |
|
} |
| 57 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 58 |
|
private static class TestClass2<A> extends TestClass<A, Integer> |
| 59 |
|
{ |
| 60 |
|
|
| 61 |
|
} |
| 62 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 63 |
|
private static class TestClass3 extends TestClass2<List<String>> |
| 64 |
|
{ |
| 65 |
|
|
| 66 |
|
} |
| 67 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 68 |
|
private static class TestClass4<T> extends TestClass2<T> implements TestInterfaceSimple |
| 69 |
|
{ |
| 70 |
|
|
| 71 |
|
} |
| 72 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 73 |
|
private class AbstractTestFieldClass |
| 74 |
|
{ |
| 75 |
|
@SuppressWarnings("unused") |
| 76 |
|
private Object superField; |
| 77 |
|
} |
| 78 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 79 |
|
private class TestFieldClass extends AbstractTestFieldClass |
| 80 |
|
{ |
| 81 |
|
@SuppressWarnings("unused") |
| 82 |
|
private Object field; |
| 83 |
|
} |
| 84 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 85 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 93 |
1 |
@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 |
|
|
| |
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
| 101 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 114 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 122 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 131 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 140 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 150 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
| 161 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 174 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 182 |
1 |
@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 |
|
|
| 189 |
|
} |
| 190 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 191 |
1 |
@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 |
|
} |