-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataTypeTransformerTest.java
78 lines (68 loc) · 3.38 KB
/
DataTypeTransformerTest.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
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* 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.
* <p>
* 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.deserialization.util;
import cz.cvut.kbss.jsonld.environment.Generator;
import cz.cvut.kbss.jsonld.environment.model.Person;
import org.junit.Test;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.Date;
import java.util.Map;
import java.util.function.Function;
import static org.junit.Assert.*;
public class DataTypeTransformerTest {
@SuppressWarnings("unchecked")
@Test
public void registerTransformationRuleAddsRuleToTransformationRules() throws Exception {
final Function<Integer, Double> rule = Integer::doubleValue;
DataTypeTransformer.registerTransformationRule(Integer.class, Double.class, rule);
final Field rulesField = DataTypeTransformer.class.getDeclaredField("rules");
rulesField.setAccessible(true);
final Map<DataTypeTransformer.TransformationRuleIdentifier<?, ?>, Function<?, ?>> rules = (Map<DataTypeTransformer.TransformationRuleIdentifier<?, ?>, Function<?, ?>>) rulesField
.get(null);
assertTrue(
rules.containsKey(new DataTypeTransformer.TransformationRuleIdentifier<>(Integer.class, Double.class)));
}
@Test
public void transformValueTransformsStringToUri() {
final String value = Generator.generateUri().toString();
final Object result = DataTypeTransformer.transformValue(value, URI.class);
assertEquals(URI.create(value), result);
}
@Test
public void transformValueReturnsNullForUnsupportedTransformation() {
final String value = "RandomValue";
assertNull(DataTypeTransformer.transformValue(value, Person.class));
}
@Test
public void testTransformStringToDate() {
// Get rid of millis in Date, they are not expressed in the string form
final Date date = new Date((System.currentTimeMillis() / 1000) * 1000);
assertEquals(date, DataTypeTransformer.transformValue(date.toString(), Date.class));
}
@Test
public void transformationPerformsTypeWideningConversionFromInteger() {
final Integer value = 117;
assertEquals(value.longValue(), DataTypeTransformer.transformValue(value, Long.class));
assertEquals(value.floatValue(), DataTypeTransformer.transformValue(value, Float.class));
assertEquals(value.doubleValue(), DataTypeTransformer.transformValue(value, Double.class));
}
@Test
public void transformationPerformsTypeWideningConversionFromLong() {
final Long value = System.currentTimeMillis();
assertEquals(value.floatValue(), DataTypeTransformer.transformValue(value, Float.class));
assertEquals(value.doubleValue(), DataTypeTransformer.transformValue(value, Double.class));
}
}