Java Reference
In-Depth Information
5.3.3
Writing the query that finds the restaurants
So far we have implemented the
findAvailableRestaurants()
method and
written a test that verifies that it executes the named query correctly. The final
step is to implement the
JDO
query that it uses to finds the available restaurants.
Because it is a named query, it is defined in the
XML
metadata for the
Restau-
rant
class:
<class name="Restaurant" table="RESTAURANT" …>
…
<query name="Restaurant.findAvailableRestaurants">
<![CDATA[
select
where serviceArea.contains(zipCode)
&& timeRanges.contains(tr)
&& (tr.dayOfWeek == day
&& (tr.openHour < hour
|| (tr.openHour == hour
&& tr.openMinute <= minute))
&& (tr.closeHour > hour
|| (tr.closeHour == hour
&& tr.closeMinute > minute))
)
variables TimeRange tr
parameters String zipCode, int day, int hour, int minute
]]>
</query>
</class>
This query takes a ZIP code, a day of the week, an hour, and a minute as parame-
ters. It finds all restaurants whose
serviceArea
field contains the specified ZIP
code and whose
timeRanges
field contains a
TimeRange
for the specified day of
the week, hour, and minute.
5.3.4
Writing tests for a query
Unless a query is extremely simple, it is usually worthwhile to write tests for it.
Let's look at one way to test the query that finds the available restaurants. This
query's
where
clause contains several relational operators, and so it is important
to test with various combinations of test data. Each of the tests for this query, some
of which are shown in listing 5.7, initializes the database with test data, invokes the
query with a particular set of arguments, and verifies that it returns the expected
results. The test class extends the
ORMU
nit class
JDOPersistenceTests
and uses
the
RestaurantMother
helper class to construct the test restaurant.
Search WWH ::
Custom Search