-
Notifications
You must be signed in to change notification settings - Fork 14
/
ConverterLoaderTest.java
98 lines (80 loc) · 3.49 KB
/
ConverterLoaderTest.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
package cz.cvut.kbss.jopa.loaders;
import cz.cvut.kbss.jopa.environment.ZoneOffsetConverter;
import cz.cvut.kbss.jopa.exception.InstantiationException;
import cz.cvut.kbss.jopa.exception.InvalidConverterException;
import cz.cvut.kbss.jopa.model.AttributeConverter;
import cz.cvut.kbss.jopa.model.annotations.Converter;
import cz.cvut.kbss.jopa.oom.converter.ConverterWrapper;
import cz.cvut.kbss.jopa.oom.converter.CustomConverterWrapper;
import org.junit.jupiter.api.Test;
import java.time.Period;
import java.time.ZoneOffset;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class ConverterLoaderTest {
private final ConverterLoader sut = new ConverterLoader();
@Test
void acceptDoesNothingWhenClassHasNoConverterAnnotation() {
assertThat(sut.getConverters().keySet(), empty());
sut.accept(this.getClass());
assertThat(sut.getConverters().keySet(), empty());
}
@Test
void acceptCreatesConverterWrapperForSpecifiedConverterClassInstanceWhenAutoApplyIsTrue() {
sut.accept(AutoAppliedConverter.class);
assertThat(sut.getConverters().keySet(), not(empty()));
assertEquals(1, sut.getConverters().size());
final ConverterWrapper<?, ?> wrapper = sut.getConverters().get(ZoneOffset.class);
assertThat(wrapper, instanceOf(CustomConverterWrapper.class));
assertThat(((CustomConverterWrapper<?, ?>) wrapper).getWrappedConverter(),
instanceOf(AutoAppliedConverter.class));
}
@Converter(autoApply = true)
public static class AutoAppliedConverter implements AttributeConverter<ZoneOffset, String> {
@Override
public String convertToAxiomValue(ZoneOffset value) {
return value.getId();
}
@Override
public ZoneOffset convertToAttribute(String value) {
return ZoneOffset.of(value);
}
}
@Test
void acceptDoesNothingWhenConverterAutoApplyIsFalse() {
assertThat(sut.getConverters().keySet(), empty());
sut.accept(ZoneOffsetConverter.class);
assertThat(sut.getConverters().keySet(), empty());
}
@Test
void acceptThrowsInvalidConverterExceptionWhenAnnotatedClassDoesNotImplementAttributeConverter() {
final InvalidConverterException e =
assertThrows(InvalidConverterException.class, () -> sut.accept(InvalidTypeConverter.class));
assertThat(e.getMessage(), containsString(AttributeConverter.class.getSimpleName()));
}
@Converter(autoApply = true)
public static class InvalidTypeConverter {
}
@Test
void acceptThrowsInvalidConverterExceptionWhenAnnotatedClassDoesNotHavePublicNoArgConstructor() {
final InvalidConverterException e =
assertThrows(InvalidConverterException.class, () -> sut.accept(MissingNoArgConstructorConverter.class));
assertThat(e.getCause(), instanceOf(InstantiationException.class));
}
@Converter(autoApply = true)
public static class MissingNoArgConstructorConverter implements AttributeConverter<Period, String> {
public MissingNoArgConstructorConverter(String argument) {
// Do nothing
}
@Override
public String convertToAxiomValue(Period value) {
return null;
}
@Override
public Period convertToAttribute(String value) {
return null;
}
}
}