-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
363 lines (268 loc) · 15 KB
/
index.html
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<p>Persistence of model objects is a part of many Java projects and a part which deserves, and often gets, high test coverage as one of the key layer integration points in the code. However, I've often felt the testing paradigms for this can be cumbersome, often involving a large amount of setup with an equivalent amount of validation. This can be tedious to both create and maintain. As a solution to this I've been testing persistence with a different pattern; by combining both the <a href="https://github.com/eXparity/exparity-stub">exparity-stub</a> and the <a href="https://github.com/eXparity/hamcrest-bean">hamcrest-bean</a> library you can thoroughly test model persistence in a few lines of test code as per the snippet below; </p>
<pre class="brush:java">..
User user = aRandomInstanceOf(User.class);
User saved = dao.save(user);
assertThat(dao.getUserById(saved.getId()), theSameBeanAs(saved));
..
</pre>
<p>The test snippet above is small but in those few lines will thoroughly test that all fields in a graph can be persisted and retrieved without loss, that any JPA or other mapping is valid, and that your queries are valid. For a complete example we'll work through testing a simple DAO for storing and retrieving User objects using the in-memory H2 database for simplicity. The same example will work for any persistence mechanism. Before we get started with an example lets briefly outline what the libraries are and what they do.</p>
<h3>The exparity-stub library</h3>
<p>The exparity-stub libraries provides a set of static methods for creating stubs of model objects, object graphs, collections, types, and primitive types. For our example we'll be creating random stubs because we want to completely fill the graph with junk data and check it can be written down. exparity-stub offers two approaches to this, the RandomBuilder or the BeanBuilder. The RandomBuilder provides a terser notation to create random objects with less code. For example:</p>
<pre>User user = RandomBuilder.aRandomInstanceOf(User.class);
List<User> users = RandomBuilder.aRandomListOf(User.class);
String anyString = RandomBuilder.aRandomString();
</pre>
<p>Whereas the BeanBuilder provides a fluent interface with finer control for building individual objects and graphs, for example;</p>
<pre>User user = BeanBuilder.aRandomInstanceOf(User.class).excludeProperty("Id").build();
</pre>
<p>For this example i'm going to use the BeanBuilder so I can exclude the <em>User.Id</em> property from being populated by the random builder.</p>
<h3>The hamcrest-bean library</h3>
<p>The hamcrest-bean library is an extension library to the <a href="http://github.com/hamcrest/JavaHamcrest">Java Hamcrest</a> library. The hamcrest-bean library provides a set of matchers specifically for testing Java objects and object graphs and performs deep inspections of those objects. It supports exclusions and overrides to allow fine control, if required, of how matching of any property, path, or type is handled, for example: </p>
<pre>User expected = new User("Jane", "Doe");
assertThat(new User("John", "Doe"), BeanMatchers.theSameAs(expected).excludeProperty("FirstName"));
</pre>
<h2>A sample project</h2>
<p>The sample project I'll work through is persistence of a simple User object with a child list of UserComment objects. This simple graph will be persisted to a H2 database with hibernate handling the Object-Relational Mapping (ORM) mapping, and Java Persistence Annotation (JPA) used to mark-up the model.</p>
<h3>The Model</h3>
<p>Below are the two model classes; first the User class.</p>
<pre class="brush:java">package org.exparity.hamcrest.bean.sample.dao;
import java.util.*;
import javax.persistence.*;
@Entity
@Table
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private Date createTs;
private String username, firstName, surname;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List comments = new ArrayList<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getCreateTs() {
return createTs;
}
public void setCreateTs(Date createTs) {
this.createTs = createTs;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public List getComments() {
return comments;
}
public void setComments(List comments) {
this.comments = comments;
}
}
</pre>
<p>Followed by the UserComment class.</p>
<pre class="brush:java">package org.exparity.hamcrest.bean.sample.dao;
import java.util.Date;
import javax.persistence.*;
@Table
@Entity
public class UserComment {
private Long id;
private Date timestamp;
@Transient
private String text;
private String title;
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
</pre>
<h3>The Data Access Object (DAO)</h3>
<p>Next up we write our DAO layer. I've excluded the UserDAO interface from this post but it is available in the sample project on <a href="https://github.com/exparity/hamcrest-bean-dao-example">github</a> .The full, if somewhat crude, implementation of the UserDAO is below. </p>
<pre class="brush:java">package org.exparity.hamcrest.bean.sample.dao;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.*;
public class UserDAOHibernateImpl implements UserDAO {
private final SessionFactory factory;
public UserDAOHibernateImpl(final String resourceFile) {
this.factory = new Configuration()
.addAnnotatedClass(User.class)
.addAnnotatedClass(UserComment.class)
.buildSessionFactory(
new StandardServiceRegistryBuilder()
.loadProperties(resourceFile)
.build());
}
@Override
public User save(final User user) {
Session session = factory.getCurrentSession();
Transaction txn = session.beginTransaction();
try {
session.save(user);
txn.commit();
} catch (final Exception e) {
txn.rollback();
}
return user;
}
@Override
public User getUserById(Long userId) {
Session session = factory.getCurrentSession();
Transaction txn = session.beginTransaction();
try {
return (User) session.get(User.class, userId);
} finally {
txn.rollback();
}
}
}
</pre>
<h3>Integration Test</h3>
<p>And finally, onto our integration test. The hibernate.properties will create an instance of an in-memory database and create the necessary tables on instantiation of the DAO.</p>
<pre class="brush:java">hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.connection.driver_class=org.h2.Driver
hibernate.connection.url=jdbc:h2:mem:test
hibernate.current_session_context_class=thread
hibernate.cache.provider_class=org.hibernate.cache.internal.NoCacheProvider
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
</pre>
<p>The integration test is below.</p>
<pre class="brush:java">package org.exparity.hamcrest.bean.sample.dao;
import static org.exparity.hamcrest.BeanMatchers.theSameBeanAs;
import static org.exparity.stub.bean.BeanBuilder.aRandomInstanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import org.junit.Test;
public class UserDAOHibernateImplTest {
@Test
public void canSaveAUser() {
User user = aRandomInstanceOf(User.class).excludeProperty("Id").build();
UserDAOHibernateImpl dao = new UserDAOHibernateImpl("hibernate.properties");
User saved = dao.save(user);
User loaded = dao.getUserById(saved.getId());
assertThat(loaded, not(sameInstance(user)));
assertThat(loaded, theSameBeanAs(user));
}
}
</pre>
<p>Let's break the test down step by step to see what each step is doing and why the test is put together this way.</p>
<h4>1) Model Setup</h4>
<pre class="brush:java">User user = aRandomInstanceOf(User.class).excludeProperty("Id").build();
</pre>
<p>Create a random instance of the User class and it's associates using exparity-stub. The instance will be populated with random data with the exception of the Id property. I've excluded the Id property so that is left null to test that the id is being generated in the database.</p>
<h4>2) DAO Setup</h4>
<pre class="brush:java">UserDAOHibernateImpl dao = new UserDAOHibernateImpl("hibernate.properties")
</pre>
<p>Instantiate the DAO ready to be tested, passing in the property file to use for the test. The hibernate properties used will configure an in-memory instance of H2 and create the schema automatically.</p>
<h4>3) Exercise the DAO</h4>
<pre class="brush:java">User saved = dao.save(user);
User loaded = dao.getUserById(saved.getId());
</pre>
<p>Save the random instance of the model set up in step (1) and then query the object back out again.</p>
<h4>4) Verify the results</h4>
<pre class="brush:java">assertThat(loaded, not(sameInstance(user)));
assertThat(loaded, theSameBeanAs(user));
</pre>
<p>The first line verifies that the loaded User instance is not the same instance as the originally saved User. This prevents false positive results when the loaded instance is returned directly from a cache. The second line uses hamcrest-bean to perform a deep comparison of the loaded User instance against the original user instance.</p>
<h3>Running the test</h3>
<p>The first run of the test yields an error; specifically a hibernate warning because a @Id annotation has been missed on UserComment.</p>
<pre class="brush:java">org.hibernate.AnnotationException: No identifier specified for entity: org.exparity.hamcrest.bean.sample.dao.UserComment
at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:277)
at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:224)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:775)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3845)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3799)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1412)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
at org.exparity.hamcrest.bean.sample.dao.UserDAOHibernateImpl.(UserDAOHibernateImpl.java:15)
at org.exparity.hamcrest.bean.sample.dao.UserDAOHibernateImplTest.canSaveAUser(UserDAOHibernateImplTest.java:18)
</pre>
<p>A fix to the UserComment object and we can run the test again.</p>
<pre class="brush:java">@Table
@Entity
public class UserComment {
<strong>@Id</strong>
<strong>@GeneratedValue(strategy = GenerationType.SEQUENCE)</strong>
private Long id;
private Date timestamp;
@Transient
private String text;
private String title;
...
</pre>
<p>After running the test again we get another failure. The presence of the @Transient annotation on the UserComment.text property is preventing the value being persisted</p>
<pre>java.lang.AssertionError:
Expected: the same as
but: User.Comments[0].Text is null instead of "mDAWDJXbheIHbbHLR1NNVJqAki49RvaVwQtKD38r79u0y3MTDD"
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
at org.exparity.hamcrest.bean.sample.dao.UserDAOHibernateImplTest.canSaveAUser(UserDAOHibernateImplTest.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
</pre>
<p>Another change to the UserComment object to remove the @Transient annotation and we can run the test again.</p>
<pre class="brush:java">@Table
@Entity
public class UserComment {
<strong>@Id</strong>
<strong>@GeneratedValue(strategy = GenerationType.SEQUENCE)</strong>
private Long id;
private Date timestamp;
private String text;
private String title;
...
</pre>
<p>After running the test again it all passes.</p>
<h2>Try it out</h2>
<p>To try hamcrest-bean and exparity-stub out for yourself include the dependency in your maven pom or other dependency manager.</p>
<pre class="brush:xml"> <dependency>
<groupId>org.exparity</groupId>
<artifactId>hamcrest-bean</artifactId>
<version>1.0.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.exparity</groupId>
<artifactId>exparity-stub</artifactId>
<version>1.1.5</version>
<scope>test</scope>
</dependency>
</pre>