-
Notifications
You must be signed in to change notification settings - Fork 2
/
BeanClassProcessorTest.java
188 lines (158 loc) · 7.23 KB
/
BeanClassProcessorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Copyright (C) 2017 Czech Technical University in Prague
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jsonld.common;
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
import cz.cvut.kbss.jopa.model.annotations.Properties;
import cz.cvut.kbss.jsonld.environment.model.Employee;
import cz.cvut.kbss.jsonld.environment.model.Organization;
import cz.cvut.kbss.jsonld.environment.model.Person;
import cz.cvut.kbss.jsonld.exception.BeanProcessingException;
import cz.cvut.kbss.jsonld.exception.TargetTypeException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.*;
public class BeanClassProcessorTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void createNewInstanceCreatesNewClassInstance() {
final Person result = BeanClassProcessor.createInstance(Person.class);
assertNotNull(result);
}
@Test
public void createNewInstanceThrowsBeanProcessingExceptionWhenNoArgConstructorIsMissing() {
thrown.expect(BeanProcessingException.class);
thrown.expectMessage("Class " + ClassWithoutPublicCtor.class + " is missing a public no-arg constructor.");
BeanClassProcessor.createInstance(ClassWithoutPublicCtor.class);
}
private static class ClassWithoutPublicCtor {
private String name;
public ClassWithoutPublicCtor(String name) {
this.name = name;
}
}
@Test
public void testCreateCollectionOfListType() {
final Collection<?> res = BeanClassProcessor.createCollection(CollectionType.LIST);
assertTrue(res instanceof List);
}
@Test
public void testCreateCollectionOfSetType() {
final Collection<?> res = BeanClassProcessor.createCollection(CollectionType.SET);
assertTrue(res instanceof Set);
}
@Test
public void testCreateCollectionFromSetField() throws Exception {
final Collection<?> res = BeanClassProcessor.createCollection(Organization.class.getDeclaredField("employees"));
assertTrue(res instanceof Set);
}
@Test
public void testCreateCollectionFromListField() throws Exception {
final Collection<?> res = BeanClassProcessor
.createCollection(ClassWithListField.class.getDeclaredField("list"));
assertTrue(res instanceof List);
}
private static class ClassWithListField {
@OWLDataProperty(iri = "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/List")
private List<Integer> list;
}
@Test
public void createCollectionFromFieldThrowsIllegalArgumentForNonCollectionField() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(String.class + " is not a supported collection type.");
BeanClassProcessor.createCollection(Person.class.getDeclaredField("firstName"));
}
@Test
public void getCollectionItemTypeReturnsDeclaredCollectionGenericArgument() throws Exception {
final Class<?> res = BeanClassProcessor.getCollectionItemType(Organization.class.getDeclaredField("employees"));
assertEquals(Employee.class, res);
}
@Test
public void getCollectionItemThrowsBeanProcessingExceptionWhenFieldIsNotCollection() throws Exception {
thrown.expect(BeanProcessingException.class);
thrown.expectMessage(
"Field " + Person.class.getDeclaredField("firstName") + " is not of parametrized type.");
BeanClassProcessor.getCollectionItemType(Person.class.getDeclaredField("firstName"));
}
@Test
public void verifyPropertiesFieldTypeThrowsExceptionWhenFieldIsNotMap() throws Exception {
final Field field = ClassWithInvalidProperties.class.getDeclaredField("properties");
thrown.expect(TargetTypeException.class);
thrown.expectMessage("Properties field " + field + " must be a java.util.Map.");
BeanClassProcessor.verifyPropertiesFieldType(field);
}
private static class ClassWithInvalidProperties {
@Properties
private List<String> properties;
}
@Test
public void getMapValueTypeReturnsValueType() throws Exception {
final Field field = Person.class.getDeclaredField("properties");
assertEquals(Set.class, BeanClassProcessor.getMapValueType(field));
}
@Test
public void getMapKeyTypeReturnsKeyType() throws Exception {
final Field field = Person.class.getDeclaredField("properties");
assertEquals(String.class, BeanClassProcessor.getMapKeyType(field));
}
@Test
public void getMapValueTypeThrowsBeanProcessingExceptionWhenFieldIsNotMap() throws Exception {
final Field field = ClassWithInvalidProperties.class.getDeclaredField("properties");
thrown.expect(BeanProcessingException.class);
thrown.expectMessage("Unable to determine declared Map value type of field " + field + ".");
BeanClassProcessor.getMapValueType(field);
}
@Test
public void getMapGenericValueTypeDeterminesCorrectItemTypeOfMapValueWhenItIsCollection() throws Exception {
final Field field = Person.class.getDeclaredField("properties");
assertEquals(String.class, BeanClassProcessor.getMapGenericValueType(field));
}
@Test
public void getMapGenericValueTypeThrowsBeanProcessingExceptionWhenMapValueTypeIsNotGeneric() throws Exception {
final Field field = NonGenericValue.class.getDeclaredField("properties");
thrown.expect(BeanProcessingException.class);
thrown.expectMessage("Expected map value type to be generic. Field: " + field);
BeanClassProcessor.getMapGenericValueType(field);
}
private static class NonGenericValue {
@Properties
private Map<String, String> properties;
}
@Test
public void getMapGenericValueTypeReturnsNullWhenGenericValueTypeIsUnspecified() throws Exception {
final Field field = UndefinedGenericValueType.class.getDeclaredField("properties");
assertNull(BeanClassProcessor.getMapGenericValueType(field));
}
private static class UndefinedGenericValueType {
@Properties
private Map<URI, Set<?>> properties;
}
@Test
public void getMapGenericValueTypeReturnsNullForRawValueType() throws Exception {
final Field field = RawValueType.class.getDeclaredField("properties");
assertNull(BeanClassProcessor.getMapGenericValueType(field));
}
private static class RawValueType {
@Properties
private Map<String, Set> properties;
}
}