-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
UnsupportedPropertiesTest.java
226 lines (188 loc) · 8.72 KB
/
UnsupportedPropertiesTest.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
package io.quarkus.hibernate.orm.config.unsupportedproperties;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Formatter;
import java.util.logging.Level;
import javax.inject.Inject;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.hibernate.BaseSessionEventListener;
import org.hibernate.Session;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.cfg.AvailableSettings;
import org.jboss.logmanager.formatters.PatternFormatter;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import io.quarkus.hibernate.orm.runtime.FastBootHibernatePersistenceProvider;
import io.quarkus.narayana.jta.QuarkusTransaction;
import io.quarkus.test.QuarkusUnitTest;
public class UnsupportedPropertiesTest {
private static final Formatter LOG_FORMATTER = new PatternFormatter("%s");
@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClass(ParentEntity.class)
.addClass(ChildEntity.class)
.addClass(SpyingIdentifierGeneratorEntity.class)
.addClass(SettingsSpyingIdentifierGenerator.class))
.withConfigurationResource("application.properties")
.overrideConfigKey("quarkus.hibernate-orm.jdbc.statement-batch-size", "10")
// This should be taken into account by Hibernate ORM
.overrideConfigKey("quarkus.hibernate-orm.unsupported-properties.\"" + AvailableSettings.ORDER_INSERTS + "\"",
"true")
// This is just to test a property set at build time
.overrideConfigKey("quarkus.hibernate-orm.unsupported-properties.\"hibernate.some.unknown.key.static-and-runtime\"",
"some-value-1")
// This is just to test a property set at runtime, which which would not be available during the build
// (or even during static init with native applications).
.overrideRuntimeConfigKey(
"quarkus.hibernate-orm.unsupported-properties.\"hibernate.some.unknown.key.runtime-only\"",
"some-value-2")
// This should be ignored with a warning
.overrideConfigKey(
"quarkus.hibernate-orm.unsupported-properties.\"" + AvailableSettings.HBM2DDL_DATABASE_ACTION + "\"",
"drop-and-create")
// Expect warnings on startup
.setLogRecordPredicate(record -> FastBootHibernatePersistenceProvider.class.getName().equals(record.getLoggerName())
&& record.getLevel().intValue() >= Level.WARNING.intValue())
.assertLogRecords(records -> {
var assertion = assertThat(records)
.as("Warnings on startup")
.hasSize(2);
assertion.element(0).satisfies(record -> assertThat(LOG_FORMATTER.formatMessage(record))
.contains("Persistence-unit [<default>] sets unsupported properties",
"These properties may not work correctly",
"may change when upgrading to a newer version of Quarkus (even just a micro/patch version)",
"Consider using a supported configuration property",
"make sure to file a feature request so that a supported configuration property can be added to Quarkus")
.contains(AvailableSettings.ORDER_INSERTS, AvailableSettings.HBM2DDL_DATABASE_ACTION,
"hibernate.some.unknown.key.static-and-runtime", "hibernate.some.unknown.key.runtime-only")
// We should not log property values, that could be a security breach for some properties.
.doesNotContain("some-value"));
assertion.element(1).satisfies(record -> assertThat(LOG_FORMATTER.formatMessage(record))
.contains(
"Persistence-unit [<default>] sets property '" + AvailableSettings.HBM2DDL_DATABASE_ACTION
+ "' to a custom value through 'quarkus.hibernate-orm.unsupported-properties.\""
+ AvailableSettings.HBM2DDL_DATABASE_ACTION + "\"'",
"Quarkus already set that property independently",
"The custom value will be ignored"));
});
@Inject
EntityManagerFactory emf;
@Inject
EntityManager em;
@Test
public void testPropertiesPropagatedToStaticInit() {
assertThat(SettingsSpyingIdentifierGenerator.collectedSettings).hasSize(1);
Map<String, Object> settings = SettingsSpyingIdentifierGenerator.collectedSettings.get(0);
assertThat(settings)
.containsEntry("hibernate.some.unknown.key.static-and-runtime", "some-value-1")
.doesNotContainKey("hibernate.some.unknown.key.runtime-only");
}
@Test
public void testPropertiesPropagatedToRuntimeInit() {
assertThat(emf.getProperties())
.contains(entry("hibernate.order_inserts", "true"),
// Also test a property that Quarkus cannot possibly know about
entry("hibernate.some.unknown.key.static-and-runtime", "some-value-1"),
entry("hibernate.some.unknown.key.runtime-only", "some-value-2"));
}
// This tests a particular feature that can (currently) only be enabled with unsupported properties
@Test
public void testInsertsOrdered() {
var listener = new BatchCountSpyingEventListener();
QuarkusTransaction.run(() -> {
em.unwrap(Session.class).addEventListeners(listener);
ParentEntity parent1 = new ParentEntity(1);
parent1.getChildren().add(new ChildEntity(2));
parent1.getChildren().add(new ChildEntity(3));
ParentEntity parent2 = new ParentEntity(4);
parent2.getChildren().add(new ChildEntity(5));
parent2.getChildren().add(new ChildEntity(6));
em.persist(parent1);
em.persist(parent2);
});
// Thanks to insert ordering, we expect only 2 JDBC statement batches
// (instead of 4 if insert ordering was disabled)
assertThat(listener.batchCount).isEqualTo(2);
}
private static class BatchCountSpyingEventListener extends BaseSessionEventListener {
private long batchCount = 0;
@Override
public void jdbcExecuteBatchStart() {
++batchCount;
}
}
@Entity
public static class ParentEntity {
@Id
private Long id;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
private List<ChildEntity> children = new ArrayList<>();
public ParentEntity() {
}
public ParentEntity(long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<ChildEntity> getChildren() {
return children;
}
public void setChildren(List<ChildEntity> children) {
this.children = children;
}
}
@Entity
public static class ChildEntity {
@Id
private Long id;
@ManyToOne
private ParentEntity parent;
public ChildEntity() {
}
public ChildEntity(long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public ParentEntity getParent() {
return parent;
}
public void setParent(ParentEntity parent) {
this.parent = parent;
}
}
@Entity
public static class SpyingIdentifierGeneratorEntity {
@Id
@GeneratedValue(generator = "spying-generator")
@GenericGenerator(name = "spying-generator", strategy = "io.quarkus.hibernate.orm.config.unsupportedproperties.SettingsSpyingIdentifierGenerator")
private Long id;
public SpyingIdentifierGeneratorEntity() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
}