Java Reference
In-Depth Information
such as Eclipse, which incrementally compiles files when you save them. Some
JDO
vendors provide an Eclipse plug-in that automatically enhances class files, but
some only do so during a full rebuild. As a result, a test will sometimes fail because
it attempts to persist an object whose class has not been enhanced. You must then
manually run the enhancer, which can be frustrating. However, I believe that this
is a small price to pay for objects that support
instanceof
and downcasting.
Now that we have looked at some of the issues with using
JDO
, let's see how to
use it to persist a domain model class and implement repositories.
5.2 Persisting a domain model class with JDO
You now should have a good understanding of the issues you will encounter and the
decisions you must make when using
JDO
to persist to a domain model. In this sec-
tion, we'll use
JDO
to persist the
PendingOrder
class. You will see examples of the
tests that you will need to write when using a test-driven approach to persisting your
own domain model with
JDO
. We'll describe the minor changes, such as adding an
ID
field, that you must make to some classes in order to be able to persist them, and
we'll show the
JDO
object/relational mapping for the
PendingOrder
class.
5.2.1
Writing JDO persistence tests with ORMUnit
The first step in the process of making a class persistent is to write some tests that
verify the correctness of the object/relational mapping and make sure that
instances of the class can be saved, updated, and deleted. Ironically, writing these
tests usually takes longer than making the classes persistent. There are also lots of
details to discuss, so bear with me through this discussion.
To make writing
JDO
persistence tests easier,
ORMU
nit provides three base
classes:
JDOMappingTests
,
JDOSchemaTests
, and
JDOPersistenceTests
. As
figure 5.2 shows, each class extends
JU
nit
TestCase
. You use
JDOMappingTests
and
JDOSchemaTests
to write tests for the object/relational mapping and
JDO-
PersistenceTests
to write tests that create, find, update, and delete persistent
JDO
objects.
JDOMappingTests
and
JDOSchemaTests
delegate to a strategy class that
implements the
JDOMappingStrategy
interface and uses the vendor-specific
API
s
to test the object/relational mapping.
JDOPersistenceTests
is implemented
using the standard
JDO
API
s. Let's first look at these classes and then see how to
use them.
Search WWH ::
Custom Search