-
Notifications
You must be signed in to change notification settings - Fork 242
/
BridgeConversionErrorTest.java
227 lines (198 loc) · 6.25 KB
/
BridgeConversionErrorTest.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* 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.test.bridge;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.apache.lucene.document.Document;
import org.hibernate.Transaction;
import org.hibernate.search.annotations.ClassBridge;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.bridge.BridgeException;
import org.hibernate.search.bridge.FieldBridge;
import org.hibernate.search.bridge.LuceneOptions;
import org.hibernate.search.test.SearchTestBase;
import org.hibernate.search.testsupport.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Hardy Ferentschik
*/
@TestForIssue(jiraKey = "1045")
public class BridgeConversionErrorTest extends SearchTestBase {
@Test
public void testClassBridgeError() throws Exception {
ClassBridged classBridged = new ClassBridged();
org.hibernate.Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist( classBridged );
try {
tx.commit();
fail();
}
catch (Exception e) {
Throwable cause = e.getCause();
assertTrue( cause instanceof BridgeException );
String expectedErrorMessage = "Exception while calling bridge#set\n" +
"\tclass: org.hibernate.search.test.bridge.BridgeConversionErrorTest$ClassBridged";
assertEquals( "Wrong error message", expectedErrorMessage, cause.getMessage() );
}
}
@Test
public void testFieldBridgeError() throws Exception {
SimpleEntity entity = new SimpleEntity( "foo" );
org.hibernate.Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist( entity );
try {
tx.commit();
fail();
}
catch (Exception e) {
Throwable cause = e.getCause();
assertTrue( cause instanceof BridgeException );
String expectedErrorMessage = "Exception while calling bridge#set\n" +
"\tclass: org.hibernate.search.test.bridge.BridgeConversionErrorTest$SimpleEntity\n" +
"\tpath: name";
assertEquals( "Wrong error message", expectedErrorMessage, cause.getMessage() );
}
}
@Test
public void testEmbeddedBridgeError() throws Exception {
SimpleEntity entity = new SimpleEntity( null ); // null won't throw an exception
EmbeddedEntity embedded = new EmbeddedEntity( "foo" );
entity.setEmbedded( embedded );
org.hibernate.Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist( entity );
try {
tx.commit();
fail();
}
catch (Exception e) {
Throwable cause = e.getCause();
assertTrue( cause instanceof BridgeException );
String expectedErrorMessage = "Exception while calling bridge#set\n" +
"\tclass: org.hibernate.search.test.bridge.BridgeConversionErrorTest$SimpleEntity\n" +
"\tpath: embedded.name";
assertEquals( "Wrong error message", expectedErrorMessage, cause.getMessage() );
}
}
@Test
public void testEmbeddedEmbeddedBridgeError() throws Exception {
SimpleEntity entity = new SimpleEntity( null ); // null won't throw an exception
EmbeddedEntity embedded = new EmbeddedEntity( null ); // null won't throw an exception
entity.setEmbedded( embedded );
embedded.setEmbeddedEmbedded( new EmbeddedEmbeddedEntity( "foo" ) );
org.hibernate.Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist( entity );
try {
tx.commit();
fail();
}
catch (Exception e) {
Throwable cause = e.getCause();
assertTrue( cause instanceof BridgeException );
String expectedErrorMessage = "Exception while calling bridge#set\n" +
"\tclass: org.hibernate.search.test.bridge.BridgeConversionErrorTest$SimpleEntity\n" +
"\tpath: embedded.embeddedEmbedded.name";
assertEquals( "Wrong error message", expectedErrorMessage, cause.getMessage() );
}
}
@Override
public Class<?>[] getAnnotatedClasses() {
return new Class[] {
ClassBridged.class,
SimpleEntity.class,
EmbeddedEntity.class,
EmbeddedEmbeddedEntity.class
};
}
public static class ExceptionThrowingBridge implements FieldBridge {
@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
if ( value != null ) {
throw new RuntimeException( "boom" );
}
}
}
@Entity
@Indexed
@Table(name = "CLASSBRIDGED")
@ClassBridge(impl = ExceptionThrowingBridge.class, name = "test")
public static class ClassBridged {
@Id
@GeneratedValue
private long id;
}
@Entity
@Indexed
@Table(name = "SIMPLEENTITY")
public static class SimpleEntity {
@Id
@GeneratedValue
private long id;
@Field
@org.hibernate.search.annotations.FieldBridge(impl = ExceptionThrowingBridge.class)
private String name;
@IndexedEmbedded
@OneToOne(cascade = CascadeType.ALL)
private EmbeddedEntity embedded;
public SimpleEntity(String name) {
this.name = name;
}
public SimpleEntity() {
}
public void setEmbedded(EmbeddedEntity embedded) {
this.embedded = embedded;
}
}
@Entity
@Table(name = "EMBEDDEDENTITY")
public static class EmbeddedEntity {
@Id
@GeneratedValue
private long id;
@Field
@org.hibernate.search.annotations.FieldBridge(impl = ExceptionThrowingBridge.class)
private String name;
public EmbeddedEntity(String name) {
this.name = name;
}
public EmbeddedEntity() {
}
@IndexedEmbedded
@OneToOne(cascade = CascadeType.ALL)
private EmbeddedEmbeddedEntity embeddedEmbedded;
public void setEmbeddedEmbedded(EmbeddedEmbeddedEntity embedded) {
this.embeddedEmbedded = embedded;
}
}
@Entity
@Table(name = "EMBEDDEDTWICEENTITY")
public static class EmbeddedEmbeddedEntity {
@Id
@GeneratedValue
private long id;
@Field
@org.hibernate.search.annotations.FieldBridge(impl = ExceptionThrowingBridge.class)
private String name;
public EmbeddedEmbeddedEntity(String name) {
this.name = name;
}
public EmbeddedEmbeddedEntity() {
}
}
}