-
Notifications
You must be signed in to change notification settings - Fork 242
/
DocumentIdBaseIT.java
199 lines (185 loc) · 6.06 KB
/
DocumentIdBaseIT.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
189
190
191
192
193
194
195
196
197
198
199
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.integrationtest.mapper.pojo.mapping.definition;
import java.lang.invoke.MethodHandles;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.DocumentId;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.IdentifierBridgeBeanReference;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.IdentifierBridgeBuilderBeanReference;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
import org.hibernate.search.integrationtest.mapper.pojo.test.util.rule.JavaBeanMappingSetupHelper;
import org.hibernate.search.util.SearchException;
import org.hibernate.search.util.impl.integrationtest.common.FailureReportUtils;
import org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock;
import org.hibernate.search.util.impl.test.SubTest;
import org.junit.Rule;
import org.junit.Test;
/**
* Test common use cases of the {@code @DocumentId} annotation.
*
* Does not test default bridges, which are tested in {@link DocumentIdDefaultBridgeIT}.
*/
public class DocumentIdBaseIT {
@Rule
public BackendMock backendMock = new BackendMock( "stubBackend" );
@Rule
public JavaBeanMappingSetupHelper setupHelper = new JavaBeanMappingSetupHelper( MethodHandles.lookup() );
@Test
public void error_unableToResolveDefaultIdentifierBridgeFromSourceType() {
@Indexed
class IndexedEntity {
Object id;
@DocumentId
public Object getId() {
return id;
}
}
SubTest.expectException(
() -> setupHelper.withBackendMock( backendMock ).setup( IndexedEntity.class )
)
.assertThrown()
.isInstanceOf( SearchException.class )
.hasMessageMatching( FailureReportUtils.buildSingleContextFailureReportPattern()
.typeContext( IndexedEntity.class.getName() )
.pathContext( ".id" )
.failure(
"Unable to find a default identifier bridge implementation for type '"
+ Object.class.getName() + "'"
)
.build()
);
}
@Test
public void error_unableToResolveDefaultIdentifierBridgeFromSourceType_enumSuperClassRaw() {
@Indexed
class IndexedEntity {
Enum id;
@DocumentId
public Enum getId() {
return id;
}
}
SubTest.expectException(
() -> setupHelper.withBackendMock( backendMock ).setup( IndexedEntity.class )
)
.assertThrown()
.isInstanceOf( SearchException.class )
.hasMessageMatching( FailureReportUtils.buildSingleContextFailureReportPattern()
.typeContext( IndexedEntity.class.getName() )
.pathContext( ".id" )
.failure(
"Unable to find a default identifier bridge implementation for type 'java.lang.Enum'"
)
.build()
);
}
@Test
public void error_unableToResolveDefaultIdentifierBridgeFromSourceType_enumSuperClassWithWildcard() {
@Indexed
class IndexedEntity {
Enum<?> id;
@DocumentId
public Enum<?> getId() {
return id;
}
}
SubTest.expectException(
() -> setupHelper.withBackendMock( backendMock ).setup( IndexedEntity.class )
)
.assertThrown()
.isInstanceOf( SearchException.class )
.hasMessageMatching( FailureReportUtils.buildSingleContextFailureReportPattern()
.typeContext( IndexedEntity.class.getName() )
.pathContext( ".id" )
.failure(
"Unable to find a default identifier bridge implementation for type 'java.lang.Enum<?>'"
)
.build()
);
}
@Test
public void error_unableToResolveDefaultIdentifierBridgeFromSourceType_enumSuperClassWithParameters() {
@Indexed
class IndexedEntity {
Enum<EnumForEnumSuperClassTest> id;
@DocumentId
public Enum<EnumForEnumSuperClassTest> getId() {
return id;
}
}
SubTest.expectException(
() -> setupHelper.withBackendMock( backendMock ).setup( IndexedEntity.class )
)
.assertThrown()
.isInstanceOf( SearchException.class )
.hasMessageMatching( FailureReportUtils.buildSingleContextFailureReportPattern()
.typeContext( IndexedEntity.class.getName() )
.pathContext( ".id" )
.failure(
"Unable to find a default identifier bridge implementation for type 'java.lang.Enum<"
+ EnumForEnumSuperClassTest.class.getName() + ">'"
)
.build()
);
}
enum EnumForEnumSuperClassTest {
VALUE1,
VALUE2
}
@Test
public void error_definingBothBridgeReferenceAndBridgeBuilderReference() {
@Indexed
class IndexedEntity {
Object id;
@DocumentId(
identifierBridge = @IdentifierBridgeBeanReference(name = "foo"),
identifierBridgeBuilder = @IdentifierBridgeBuilderBeanReference(name = "bar")
)
public Object getId() {
return id;
}
}
SubTest.expectException(
() -> setupHelper.withBackendMock( backendMock ).setup( IndexedEntity.class )
)
.assertThrown()
.isInstanceOf( SearchException.class )
.hasMessageMatching( FailureReportUtils.buildSingleContextFailureReportPattern()
.typeContext( IndexedEntity.class.getName() )
.pathContext( ".id" )
.annotationContextAnyParameters( DocumentId.class )
.failure(
"Annotation @DocumentId on property 'id' defines both identifierBridge and identifierBridgeBuilder."
+ " Only one of those can be defined, not both."
)
.build()
);
}
@Test
public void error_missing() {
@Indexed
class IndexedEntity {
Object id;
public Object getId() {
return id;
}
}
SubTest.expectException(
() -> setupHelper.withBackendMock( backendMock ).setup( IndexedEntity.class )
)
.assertThrown()
.isInstanceOf( SearchException.class )
.hasMessageMatching( FailureReportUtils.buildSingleContextFailureReportPattern()
.typeContext( IndexedEntity.class.getName() )
.failure(
"There isn't any explicit document ID mapping for indexed type '"
+ IndexedEntity.class.getName() + "',"
+ " and the entity ID cannot be used as a default because it is unknown"
)
.build()
);
}
}