2 * This is a common dao with basic CRUD operations and is not limited to any
3 * persistent layer implementation
5 * Copyright (C) 2008 Imran M Yousuf (imyousuf@smartitengineering.com)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 package com
.smartitengineering
.dao
.impl
.hibernate
;
21 import com
.smartitengineering
.dao
.common
.queryparam
.FetchMode
;
22 import com
.smartitengineering
.dao
.common
.queryparam
.MatchMode
;
23 import com
.smartitengineering
.dao
.common
.queryparam
.Order
;
24 import com
.smartitengineering
.dao
.common
.queryparam
.QueryParameter
;
25 import com
.smartitengineering
.dao
.common
.queryparam
.QueryParameterFactory
;
26 import com
.smartitengineering
.dao
.common
.queryparam
.QueryParameterWithPropertyName
;
27 import com
.smartitengineering
.dao
.impl
.hibernate
.domain
.Author
;
28 import com
.smartitengineering
.dao
.impl
.hibernate
.domain
.Book
;
29 import com
.smartitengineering
.dao
.impl
.hibernate
.domain
.Publisher
;
30 import com
.smartitengineering
.domain
.PersistentDTO
;
31 import java
.util
.ArrayList
;
32 import java
.util
.Arrays
;
33 import java
.util
.Calendar
;
34 import java
.util
.Collections
;
35 import java
.util
.Date
;
36 import java
.util
.HashMap
;
37 import java
.util
.HashSet
;
38 import java
.util
.Hashtable
;
39 import java
.util
.LinkedHashSet
;
40 import java
.util
.List
;
42 import java
.util
.Random
;
44 import junit
.framework
.AssertionFailedError
;
45 import junit
.framework
.TestCase
;
46 import org
.springframework
.beans
.BeansException
;
47 import org
.springframework
.context
.ApplicationContext
;
48 import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
54 public class AbstractDAOTest
57 private static ApplicationContext context
;
59 public AbstractDAOTest(String testName
) {
64 protected void setUp()
67 if (context
== null) {
68 context
= new ClassPathXmlApplicationContext("app-context.xml");
73 protected void tearDown()
79 * Test of createEntity method, of class AbstractDAO.
81 public void testCreateEntity() {
82 System
.out
.println("createEntity");
83 AbstractDAO
<Book
> bookInstance
= getDaoInstance();
84 AbstractDAO
<Author
> authorInstance
= getDaoInstance();
85 AbstractDAO
<Publisher
> publisherInstance
= getDaoInstance();
87 * Test null object creation, should throw IllegalArgumentException
90 bookInstance
.createEntity((Book
) null);
91 fail("Should not proceed with saving NULL entity!");
93 catch (IllegalArgumentException argumentException
) {
95 catch (Exception exception
) {
96 fail(exception
.getMessage());
99 * Create proper test data.
100 * This set will contain single of everything - author, publisher, book
102 Publisher shebaProkashani
= getShebaProkashani();
104 publisherInstance
.createEntity(shebaProkashani
);
105 System
.out
.println(shebaProkashani
.getId());
107 catch (Exception ex
) {
108 fail(ex
.getMessage());
110 Author kaziAnowarHossain
= getKaziAnowarHossain();
112 authorInstance
.createEntity(kaziAnowarHossain
);
113 System
.out
.println(kaziAnowarHossain
.getId());
115 catch (Exception ex
) {
116 fail(ex
.getMessage());
118 Book book
= getAgniSopoth(kaziAnowarHossain
, shebaProkashani
);
120 bookInstance
.createEntity(book
);
121 System
.out
.println(book
.getId());
123 catch (Exception ex
) {
124 fail(ex
.getMessage());
127 * Create multiple publications at once.
128 * One of the publications should have more than one book
130 Publisher oReilly
= getOReilly();
131 Publisher annoProkash
= getAnnoProkash();
133 publisherInstance
.createEntity(oReilly
, annoProkash
);
134 System
.out
.println(oReilly
.getId() + ", " + annoProkash
.getId());
136 catch (Exception ex
) {
137 fail(ex
.getMessage());
140 * Create multiple authors at once.
141 * There should be at least one book with multiple authors.
142 * A single author (set) should have multiple books.
144 Author brettMcLaugblin
= getBrett();
145 Author davidLane
= getDavidLane();
146 Author hughWilliams
= getHughWilliams();
147 Author humayunAhmed
= getHumayunAhmed();
149 authorInstance
.createEntity(brettMcLaugblin
, davidLane
, humayunAhmed
,
151 System
.out
.println(brettMcLaugblin
.getId() + ", " +
152 davidLane
.getId() + ", " + humayunAhmed
.getId() + ", " +
153 hughWilliams
.getId());
155 catch (Exception ex
) {
156 fail(ex
.getMessage());
158 Book kothaoKeoNei
= getKothaoKeoNei(annoProkash
, humayunAhmed
);
159 Book agunerPoroshMoni
= getAgunerPoroshMoni(annoProkash
, humayunAhmed
);
160 Book webDbApp
= getWebDbApp(oReilly
, davidLane
, hughWilliams
);
161 Book javaAndXml
= getJavaAndXml(oReilly
, brettMcLaugblin
);
163 bookInstance
.createEntity(kothaoKeoNei
, agunerPoroshMoni
, webDbApp
,
165 System
.out
.println(kothaoKeoNei
.getId() + ", " +
166 agunerPoroshMoni
.getId() + ", " + webDbApp
.getId() + ", " +
169 catch (Exception ex
) {
170 fail(ex
.getMessage());
174 public void testGetAll() {
175 System
.out
.println("getAll");
177 * Add test for read all
179 AbstractDAO
<Book
> bookInstance
= getDaoInstance();
180 Set
<Book
> allBooks
= getAll(bookInstance
, Book
.class);
181 assertTrue(5 <= allBooks
.size());
182 AbstractDAO
<Author
> authorInstance
= getDaoInstance();
183 Set
<Author
> allAuthors
= getAll(authorInstance
, Author
.class);
184 assertTrue(5 <= allAuthors
.size());
185 AbstractDAO
<Publisher
> publisherInstance
= getDaoInstance();
186 Set
<Publisher
> allPublishers
=
187 getAll(publisherInstance
, Publisher
.class);
188 assertTrue(3 <= allPublishers
.size());
189 Map
<String
, Integer
> bookNameToIdMap
= new HashMap
<String
, Integer
>();
190 Map
<String
, Integer
> publisherNameToIdMap
=
191 new HashMap
<String
, Integer
>();
192 Map
<String
, Integer
> authorNameToIdMap
= new HashMap
<String
, Integer
>();
193 makeNameToIdMap(bookNameToIdMap
, bookInstance
, authorNameToIdMap
,
194 authorInstance
, publisherNameToIdMap
, publisherInstance
);
196 * Update one book at a time
199 QueryParameter
<Integer
> idInParam
= getIdInParam(
201 List
<Book
> books
= new ArrayList
<Book
>(new HashSet
<Book
>(
202 bookInstance
.readList(Book
.class, idInParam
)));
203 List
<String
> names
= new ArrayList
<String
>(bookNameToIdMap
.keySet());
204 for (Book book
: books
) {
205 names
.remove(book
.getName());
210 * Update all publisher at once
213 QueryParameter
<Integer
> idInParam
= getIdInParam(
214 publisherNameToIdMap
);
215 List
<Publisher
> publishers
= new ArrayList
<Publisher
>(
216 new HashSet
<Publisher
>(publisherInstance
.readList(
217 Publisher
.class, idInParam
)));
218 List
<String
> names
= new ArrayList
<String
>(publisherNameToIdMap
.
220 for (Publisher publisher
: publishers
) {
221 names
.remove(publisher
.getName());
226 * Update all author at once
229 QueryParameter
<Integer
> idInParam
= getIdInParam(
231 List
<Author
> authors
= new ArrayList
<Author
>(new HashSet
<Author
>(
232 authorInstance
.readList(Author
.class, idInParam
)));
233 List
<String
> names
= new ArrayList
<String
>(
234 authorNameToIdMap
.keySet());
235 for (Author author
: authors
) {
236 names
.remove(author
.getName());
243 * Test of readSingle method, of class AbstractDAO.
245 public void testReadSingle_Class_QueryParameterArr() {
246 System
.out
.println("readSingleArgs");
247 performTestReadSingle(MethodInvocationType
.VAR_ARGS
);
251 * Test of readSingle method, of class AbstractDAO.
253 public void testReadSingle_Class_Hashtable() {
254 System
.out
.println("readSingleHashTable");
255 performTestReadSingle(MethodInvocationType
.HASH_TABLE
);
259 * Test of readSingle method, of class AbstractDAO.
261 public void testReadSingle_Class_List() {
262 System
.out
.println("readSingleList");
263 performTestReadSingle(MethodInvocationType
.LIST
);
267 * Test of readOther method, of class AbstractDAO.
269 public void testReadOther_Class_QueryParameterArr() {
270 System
.out
.println("readOtherVarArgs");
271 performTestReadOtherSingle(MethodInvocationType
.VAR_ARGS
);
275 * Test of readOther method, of class AbstractDAO.
277 public void testReadOther_Class_Hashtable() {
278 System
.out
.println("readOtherHashtable");
279 performTestReadOtherSingle(MethodInvocationType
.HASH_TABLE
);
283 * Test of readOther method, of class AbstractDAO.
285 public void testReadOther_Class_List() {
286 System
.out
.println("readOther_List");
287 performTestReadOtherSingle(MethodInvocationType
.LIST
);
291 * Test of readOtherList method, of class AbstractDAO.
293 public void testReadOtherList_Class_QueryParameterArr() {
294 System
.out
.println("readOtherListVarArgs");
295 performTestReadOtherList(MethodInvocationType
.VAR_ARGS
);
299 * Test of readOtherList method, of class AbstractDAO.
301 public void testReadOtherList_Class_Hashtable() {
302 System
.out
.println("readOtherListHashtable");
303 performTestReadOtherList(MethodInvocationType
.HASH_TABLE
);
307 * Test of readOtherList method, of class AbstractDAO.
309 public void testReadOtherList_Class_List() {
310 System
.out
.println("readOtherList_List");
311 performTestReadOtherList(MethodInvocationType
.LIST
);
315 * Test of updateEntity method, of class AbstractDAO.
317 public void testUpdateEntity() {
318 System
.out
.println("updateEntity");
319 AbstractDAO
<Book
> bookInstance
= getDaoInstance();
320 AbstractDAO
<Author
> authorInstance
= getDaoInstance();
321 AbstractDAO
<Publisher
> publisherInstance
= getDaoInstance();
322 Map
<String
, Integer
> bookNameToIdMap
= new HashMap
<String
, Integer
>();
323 Map
<String
, Integer
> publisherNameToIdMap
=
324 new HashMap
<String
, Integer
>();
325 Map
<String
, Integer
> authorNameToIdMap
= new HashMap
<String
, Integer
>();
326 makeNameToIdMap(bookNameToIdMap
, bookInstance
, authorNameToIdMap
,
327 authorInstance
, publisherNameToIdMap
, publisherInstance
);
329 * Update one book at a time
332 QueryParameter
<Integer
> idInParam
= getIdInParam(
334 List
<Book
> books
= new ArrayList
<Book
>(new HashSet
<Book
>(
335 bookInstance
.readList(Book
.class, idInParam
)));
336 Collections
.sort(books
);
337 Calendar calendar
= getBookFirstPubCal();
338 for (Book book
: books
) {
339 book
.setPublishDate(calendar
.getTime());
340 bookInstance
.updateEntity(book
);
341 QueryParameter parameter
= getIdQueryParam(book
.getId());
343 bookInstance
.readSingle(Book
.class, parameter
);
344 assertEquals(book
.getPublishDate(), updatedBook
.getPublishDate());
345 calendar
.add(Calendar
.MONTH
, 1);
349 * Update all publisher at once
352 QueryParameter
<Integer
> idInParam
= getIdInParam(
353 publisherNameToIdMap
);
354 List
<Publisher
> publishers
= new ArrayList
<Publisher
>(
355 new HashSet
<Publisher
>(publisherInstance
.readList(
356 Publisher
.class, idInParam
)));
357 Collections
.sort(publishers
);
358 Calendar calendar
= getPubFirstEstCal();
359 for (Publisher publisher
: publishers
) {
360 publisher
.setEstablishedDate(calendar
.getTime());
361 calendar
.add(Calendar
.MONTH
, 1);
363 publisherInstance
.updateEntity(publishers
.toArray(new Publisher
[]{}));
364 for (Publisher publisher
: publishers
) {
365 QueryParameter parameter
= getIdQueryParam(publisher
.getId());
366 Publisher updatedPublisher
= publisherInstance
.readSingle(
367 Publisher
.class, parameter
);
368 assertEquals(publisher
.getEstablishedDate(), updatedPublisher
.
369 getEstablishedDate());
373 * Update all author at once
376 QueryParameter
<Integer
> idInParam
= getIdInParam(
378 List
<Author
> authors
= new ArrayList
<Author
>(new HashSet
<Author
>(
379 authorInstance
.readList(Author
.class, idInParam
)));
380 Collections
.sort(authors
);
381 Calendar calendar
= getAuthorFirstBirthCal();
382 for (Author author
: authors
) {
383 author
.setBirthDate(calendar
.getTime());
384 calendar
.add(Calendar
.MONTH
, 1);
386 authorInstance
.updateEntity(authors
.toArray(new Author
[]{}));
387 for (Author author
: authors
) {
388 QueryParameter parameter
= getIdQueryParam(author
.getId());
389 Author updatedAuthor
= authorInstance
.readSingle(Author
.class,
391 assertEquals(author
.getBirthDate(), updatedAuthor
.getBirthDate());
397 * Test of readList method, of class AbstractDAO.
399 public void testReadList_Class_QueryParameterArr() {
400 System
.out
.println("readList");
401 performTestReadList(MethodInvocationType
.VAR_ARGS
);
405 * Test of readList method, of class AbstractDAO.
407 public void testReadList_Class_Hashtable() {
408 System
.out
.println("readList");
409 performTestReadList(MethodInvocationType
.HASH_TABLE
);
413 * Test of readList method, of class AbstractDAO.
415 public void testReadList_Class_List() {
416 System
.out
.println("readList");
417 performTestReadList(MethodInvocationType
.LIST
);
421 * Test of deleteEntity method, of class AbstractDAO.
423 public void testDeleteEntity() {
424 System
.out
.println("deleteEntity");
425 AbstractDAO
<Book
> bookInstance
= getDaoInstance();
426 AbstractDAO
<Author
> authorInstance
= getDaoInstance();
427 AbstractDAO
<Publisher
> publisherInstance
= getDaoInstance();
428 Map
<String
, Integer
> bookNameToIdMap
= new HashMap
<String
, Integer
>();
429 Map
<String
, Integer
> publisherNameToIdMap
=
430 new HashMap
<String
, Integer
>();
431 Map
<String
, Integer
> authorNameToIdMap
= new HashMap
<String
, Integer
>();
432 makeNameToIdMap(bookNameToIdMap
, bookInstance
, authorNameToIdMap
,
433 authorInstance
, publisherNameToIdMap
, publisherInstance
);
435 * Delete one book at a time
438 QueryParameter
<Integer
> idInParam
= getIdInParam(
440 Set
<Book
> books
= new HashSet
<Book
>(
441 bookInstance
.readList(Book
.class, idInParam
));
442 for (Book book
: books
) {
443 bookInstance
.deleteEntity(book
);
445 assertEmpty(bookInstance
.readList(Book
.class, idInParam
));
448 * Delete all publisher at once
451 QueryParameter
<Integer
> idInParam
= getIdInParam(
452 publisherNameToIdMap
);
453 Set
<Publisher
> publishers
= new HashSet
<Publisher
>(
454 publisherInstance
.readList(Publisher
.class, idInParam
));
455 publisherInstance
.deleteEntity(publishers
.toArray(new Publisher
[]{}));
456 assertEmpty(publisherInstance
.readList(Publisher
.class, idInParam
));
459 * Delete all author at once
462 QueryParameter
<Integer
> idInParam
= getIdInParam(
464 Set
<Author
> authors
= new HashSet
<Author
>(
465 authorInstance
.readList(Author
.class, idInParam
));
466 authorInstance
.deleteEntity(authors
.toArray(new Author
[]{}));
467 assertEmpty(authorInstance
.readList(Author
.class, idInParam
));
471 private void assertEmpty(List readList
) {
472 if (readList
!= null && !readList
.isEmpty()) {
473 throw new AssertionFailedError();
477 private Calendar
getAuthorFirstBirthCal() {
478 Calendar calendar
= Calendar
.getInstance();
479 calendar
.setTimeInMillis(0);
480 calendar
.add(Calendar
.YEAR
, 10);
484 private Calendar
getBookFirstPubCal() {
485 Calendar calendar
= Calendar
.getInstance();
486 calendar
.setTimeInMillis(0);
487 calendar
.add(Calendar
.YEAR
, 30);
491 private QueryParameter
<Date
> getDateBetweenParam(final String propName
,
492 final Date startDate
,
493 final Date endDate
) {
494 return QueryParameterFactory
.<Date
>getBetweenPropertyParam(propName
,
498 private QueryParameter
<Date
> getDateGreaterThanParam(final String propName
,
500 return QueryParameterFactory
.<Date
>getGreaterThanPropertyParam(propName
,
504 private QueryParameter
<Date
> getDateLesserThanParam(final String propName
,
506 return QueryParameterFactory
.<Date
>getLesserThanPropertyParam(propName
,
510 private QueryParameter
<Void
> getDisjunctionalParam(
511 QueryParameter
... parameters
) {
512 return QueryParameterFactory
.getDisjunctionParam(parameters
);
515 private QueryParameter
<Integer
> getFirstResultParam(int startIndex
) {
516 return QueryParameterFactory
.getFirstResultParam(startIndex
);
519 private QueryParameter
<Integer
> getGreaterThanEqualIntParam(String idProp
,
521 return QueryParameterFactory
.<Integer
>getGreaterThanEqualToPropertyParam(
525 private QueryParameter
<Integer
> getIdInParam(
526 Map
<String
, Integer
> bookNameToIdMap
) {
527 return QueryParameterFactory
.<Integer
>getIsInPropertyParam("id", bookNameToIdMap
.values().
528 toArray(new Integer
[0]));
531 private QueryParameter
<Integer
> getIdNotInParam(
533 return QueryParameterFactory
.<Integer
>getIsNotInPropertyParam("id", ids
.
534 toArray(new Integer
[0]));
537 private QueryParameter
<Void
> getIsNotNullParam(String propertyName
) {
538 return QueryParameterFactory
.getIsNotNullPropertyParam(propertyName
);
541 private QueryParameter
<Void
> getIsNullParam(String propertyName
) {
542 return QueryParameterFactory
.getIsNullPropertyParam(propertyName
);
545 private QueryParameter
<Void
> getIsNotEmptyParam(String propertyName
) {
546 return QueryParameterFactory
.getIsNotEmptyCollectionPropertyParam(
550 private QueryParameter
<Void
> getIsEmptyParam(String propertyName
) {
551 return QueryParameterFactory
.getIsEmptyCollectionPropertyParam(
555 private QueryParameter
<Integer
> getLesserThanEqualIntParam(String idProp
,
557 return QueryParameterFactory
.<Integer
>getLesserThanEqualToPropertyParam(
561 private QueryParameter
<Integer
> getMaxResultParam(int max
) {
562 return QueryParameterFactory
.getMaxResultsParam(max
);
565 private QueryParameter
<Integer
> getNotEqualIdParam(int id
) {
566 return QueryParameterFactory
.<Integer
>getNotEqualPropertyParam("id", id
);
569 private Calendar
getPubFirstEstCal() {
570 Calendar calendar
= Calendar
.getInstance();
571 calendar
.setTimeInMillis(0);
572 calendar
.add(Calendar
.YEAR
, 0);
576 private void performTestReadList(MethodInvocationType type
) {
577 AbstractDAO
<Book
> bookInstance
= getDaoInstance();
578 AbstractDAO
<Author
> authorInstance
= getDaoInstance();
579 AbstractDAO
<Publisher
> publisherInstance
= getDaoInstance();
580 Map
<String
, Integer
> bookNameToIdMap
= new HashMap
<String
, Integer
>();
581 Map
<String
, Integer
> publisherNameToIdMap
=
582 new HashMap
<String
, Integer
>();
583 Map
<String
, Integer
> authorNameToIdMap
= new HashMap
<String
, Integer
>();
584 makeNameToIdMap(bookNameToIdMap
, bookInstance
, authorNameToIdMap
,
585 authorInstance
, publisherNameToIdMap
, publisherInstance
);
586 QueryParameter
<Integer
> idInParam
= getIdInParam(
587 publisherNameToIdMap
);
588 List
<Publisher
> allPublishers
= new ArrayList
<Publisher
>(
589 new HashSet
<Publisher
>(publisherInstance
.readList(Publisher
.class,
591 Collections
.sort(allPublishers
);
592 idInParam
= getIdInParam(authorNameToIdMap
);
593 List
<Author
> allAuthors
= new ArrayList
<Author
>(new HashSet
<Author
>(
594 authorInstance
.readList(Author
.class, idInParam
)));
595 Collections
.sort(allAuthors
);
596 idInParam
= getIdInParam(bookNameToIdMap
);
597 List
<Book
> allBooks
= new ArrayList
<Book
>(new HashSet
<Book
>(
598 bookInstance
.readList(Book
.class, idInParam
)));
599 Collections
.sort(allBooks
);
604 Calendar calendar
= getBookFirstPubCal();
605 Date startDate
= calendar
.getTime();
606 calendar
.add(Calendar
.MONTH
, 2);
607 Date endDate
= calendar
.getTime();
608 final String propName
= "publishDate";
609 QueryParameter
<Date
> dateBetweenParam
=
610 getDateBetweenParam(propName
, startDate
, endDate
);
611 QueryParameter
<Order
> orderParam
= getOrderByIdParam(Order
.DESC
);
615 result
= new ArrayList
<Book
>(new LinkedHashSet
<Book
>(
616 bookInstance
.readList(Book
.class,
617 getQueryParamHashtable(dateBetweenParam
, orderParam
))));
621 result
= new ArrayList
<Book
>(new LinkedHashSet
<Book
>(
622 bookInstance
.readList(Book
.class, getQueryParamList(
623 dateBetweenParam
, orderParam
))));
628 result
= new ArrayList
<Book
>(new LinkedHashSet
<Book
>(
629 bookInstance
.readList(Book
.class, dateBetweenParam
,
634 List
<Book
> expectedResult
= new ArrayList
<Book
>();
635 for (Book book
: allBooks
) {
636 if (book
.getPublishDate().compareTo(startDate
) > -1 && book
.
637 getPublishDate().compareTo(endDate
) < 1) {
638 expectedResult
.add(book
);
641 assertTrue(Arrays
.equals(expectedResult
.toArray(), result
.toArray()));
647 Calendar calendar
= getAuthorFirstBirthCal();
648 calendar
.add(Calendar
.MONTH
, 1);
649 Date endDate
= calendar
.getTime();
650 calendar
= getAuthorFirstBirthCal();
651 calendar
.add(Calendar
.MONTH
, allAuthors
.size() - 2);
652 Date startDate
= calendar
.getTime();
653 final String propName
= "birthDate";
654 QueryParameter
<Date
> gtDateParam
= getDateGreaterThanParam(propName
,
656 QueryParameter
<Date
> ltDateParam
= getDateLesserThanParam(propName
,
658 QueryParameter
<Void
> disjunctionalParam
= getDisjunctionalParam(
659 gtDateParam
, ltDateParam
);
660 QueryParameter
<Order
> orderParam
= getOrderByIdParam(Order
.DESC
);
664 result
= authorInstance
.readList(Author
.class,
665 getQueryParamHashtable(disjunctionalParam
, orderParam
));
669 result
= authorInstance
.readList(Author
.class,
670 getQueryParamList(disjunctionalParam
, orderParam
));
675 result
= authorInstance
.readList(Author
.class,
676 disjunctionalParam
, orderParam
);
680 result
= new ArrayList
<Author
>(new LinkedHashSet
<Author
>(result
));
681 List
<Author
> expectedAuthors
= new ArrayList
<Author
>();
682 expectedAuthors
.add(allAuthors
.get(0));
683 expectedAuthors
.add(allAuthors
.get(allAuthors
.size() - 1));
685 Arrays
.equals(expectedAuthors
.toArray(), result
.toArray()));
691 List
<Integer
> ids
= Collections
.singletonList(allPublishers
.get(0).
693 QueryParameter
<Integer
> idNotInParam
= getIdNotInParam(
695 QueryParameter
<Order
> orderParam
= getOrderByIdParam(Order
.DESC
);
696 List
<Publisher
> result
;
699 result
= publisherInstance
.readList(Publisher
.class,
700 getQueryParamHashtable(idNotInParam
, orderParam
));
704 result
= publisherInstance
.readList(Publisher
.class,
705 getQueryParamList(idNotInParam
, orderParam
));
710 result
= publisherInstance
.readList(Publisher
.class,
711 idNotInParam
, orderParam
);
715 List
<Publisher
> expectedResult
= allPublishers
.subList(1,
716 allPublishers
.size());
717 assertTrue(Arrays
.equals(expectedResult
.toArray(), result
.toArray()));
720 * Test first result & max result
724 * There is a bug in Derby JDBC Driver or Hibernate Dialect for it,
725 * as startIndex 1 and 2 returns the same result which is absurd.
728 Math
.abs(new Random().nextInt()) % (allBooks
.size() - 1) + 2;
730 QueryParameter
<Order
> orderParam
= getOrderByIdParam(Order
.DESC
);
731 QueryParameter
<Integer
> firstResult
=
732 getFirstResultParam(startIndex
);
733 QueryParameter
<Integer
> maxResult
= getMaxResultParam(max
);
737 result
= bookInstance
.readList(Book
.class,
738 getQueryParamHashtable(firstResult
, maxResult
,
743 result
= bookInstance
.readList(Book
.class,
744 getQueryParamList(firstResult
, maxResult
, orderParam
));
749 result
= bookInstance
.readList(Book
.class, firstResult
,
750 maxResult
, orderParam
);
754 List
<Book
> expectedResult
= allBooks
.subList(startIndex
- 1,
755 startIndex
+ max
- 1);
756 assertEquals(max
, result
.size());
757 assertTrue(Arrays
.equals(expectedResult
.toArray(), result
.toArray()));
763 QueryParameter
<Order
> orderParam
= getOrderByIdParam(Order
.DESC
);
764 int id
= allAuthors
.get(0).getId();
765 QueryParameter
<Integer
> notEqualIdParam
= getNotEqualIdParam(id
);
769 result
= authorInstance
.readList(Author
.class,
770 getQueryParamHashtable(notEqualIdParam
, orderParam
));
774 result
= authorInstance
.readList(Author
.class,
775 getQueryParamList(notEqualIdParam
, orderParam
));
780 result
= authorInstance
.readList(Author
.class,
781 notEqualIdParam
, orderParam
);
785 List
<Author
> expectedResult
= allAuthors
.subList(1,
787 assertTrue(Arrays
.equals(expectedResult
.toArray(), result
.toArray()));
790 * Test less-than-equal, greater-than-equal
793 int authorIndex
= Math
.abs(new Random().nextInt()) %
795 int authorId
= allAuthors
.get(authorIndex
).getId();
796 String idProp
= "id";
797 QueryParameter
<Integer
> lteParameter
= getLesserThanEqualIntParam(
799 QueryParameter
<Integer
> gteParameter
= getGreaterThanEqualIntParam(
804 result
= authorInstance
.readList(Author
.class,
805 getQueryParamHashtable(lteParameter
, gteParameter
));
809 result
= authorInstance
.readList(Author
.class,
810 getQueryParamList(lteParameter
, gteParameter
));
815 result
= authorInstance
.readList(Author
.class, lteParameter
,
820 List
<Author
> expectedResult
= Collections
.singletonList(allAuthors
.
822 assertTrue(Arrays
.equals(expectedResult
.toArray(), result
.toArray()));
828 String propertyName
= "establishedDate";
829 QueryParameter
<Void
> nullParam
= getIsNullParam(propertyName
);
830 List
<Publisher
> result
;
833 result
= publisherInstance
.readList(Publisher
.class,
834 getQueryParamHashtable(nullParam
));
838 result
= publisherInstance
.readList(Publisher
.class,
839 getQueryParamList(nullParam
));
844 result
= publisherInstance
.readList(Publisher
.class,
855 String propertyName
= "establishedDate";
856 QueryParameter
<Void
> notNullParam
=
857 getIsNotNullParam(propertyName
);
858 QueryParameter
<Order
> orderParam
= getOrderByIdParam(Order
.DESC
);
859 List
<Publisher
> result
;
862 result
= publisherInstance
.readList(Publisher
.class,
863 getQueryParamHashtable(notNullParam
, orderParam
));
867 result
= publisherInstance
.readList(Publisher
.class,
868 getQueryParamList(notNullParam
, orderParam
));
873 result
= publisherInstance
.readList(Publisher
.class,
874 notNullParam
, orderParam
);
878 assertTrue(Arrays
.equals(allPublishers
.toArray(), result
.toArray()));
884 String propertyName
= "authors";
885 QueryParameter
<Void
> emptyParam
= getIsEmptyParam(propertyName
);
889 result
= bookInstance
.readList(Book
.class,
890 getQueryParamHashtable(emptyParam
));
894 result
= bookInstance
.readList(Book
.class,
895 getQueryParamList(emptyParam
));
900 result
= bookInstance
.readList(Book
.class, emptyParam
);
910 String propertyName
= "authors";
911 QueryParameter
<Void
> notEmptyParam
=
912 getIsNotEmptyParam(propertyName
);
913 QueryParameter
<Order
> orderParam
= getOrderByIdParam(Order
.DESC
);
917 result
= bookInstance
.readList(Book
.class,
918 getQueryParamHashtable(notEmptyParam
, orderParam
));
922 result
= bookInstance
.readList(Book
.class,
923 getQueryParamList(notEmptyParam
, orderParam
));
928 result
= bookInstance
.readList(Book
.class,
929 notEmptyParam
, orderParam
);
933 result
= new ArrayList
<Book
>(new LinkedHashSet
<Book
>(result
));
934 assertTrue(Arrays
.equals(allBooks
.toArray(), result
.toArray()));
938 private void performTestReadOtherSingle(MethodInvocationType type
) {
939 AbstractDAO
<Publisher
> publisherInstance
= getDaoInstance();
940 QueryParameter
<Void
> param
;
945 param
= getAvgEmployeesParam();
949 average
= (Double
) publisherInstance
.readOther(
950 Publisher
.class, getQueryParamHashtable(param
));
954 average
= (Double
) publisherInstance
.readOther(
955 Publisher
.class, getQueryParamList(param
));
960 average
= (Double
) publisherInstance
.readOther(
961 Publisher
.class, param
);
965 int expectedAverage
= getTotalNumOfEmployeesFromPubs() /
966 getAllPublishers().length
;
967 assertEquals(expectedAverage
, average
.intValue());
973 param
= getCountIdParam();
977 count
= (Integer
) publisherInstance
.readOther(
978 Publisher
.class, getQueryParamHashtable(param
));
982 count
= (Integer
) publisherInstance
.readOther(
983 Publisher
.class, getQueryParamList(param
));
988 count
= (Integer
) publisherInstance
.readOther(
989 Publisher
.class, param
);
993 int expectedCount
= getAllPublishers().length
;
994 assertEquals(expectedCount
, count
.intValue());
997 * Test count distinct
1000 param
= getCountDistinctNumOfEmployeeParam();
1001 Integer distinctCount
;
1004 distinctCount
= (Integer
) publisherInstance
.readOther(
1005 Publisher
.class, getQueryParamHashtable(param
));
1009 distinctCount
= (Integer
) publisherInstance
.readOther(
1010 Publisher
.class, getQueryParamList(param
));
1015 distinctCount
= (Integer
) publisherInstance
.readOther(
1016 Publisher
.class, param
);
1020 int expectedDistinctCount
= getDistinctNumOfEmployeeNum();
1021 assertEquals(expectedDistinctCount
, distinctCount
.intValue());
1027 param
= getMaxNumOfEmployeeParam();
1028 Integer max
= (Integer
) publisherInstance
.readOther(Publisher
.class,
1032 max
= (Integer
) publisherInstance
.readOther(Publisher
.class,
1033 getQueryParamHashtable(param
));
1037 max
= (Integer
) publisherInstance
.readOther(Publisher
.class,
1038 getQueryParamList(param
));
1043 max
= (Integer
) publisherInstance
.readOther(Publisher
.class,
1048 int expectedMax
= getMaxEmployeesFromPubs();
1049 assertEquals(expectedMax
, max
.intValue());
1055 param
= getMinNumOfEmployeeParam();
1059 min
= (Integer
) publisherInstance
.readOther(Publisher
.class,
1060 getQueryParamHashtable(param
));
1064 min
= (Integer
) publisherInstance
.readOther(Publisher
.class,
1065 getQueryParamList(param
));
1070 min
= (Integer
) publisherInstance
.readOther(Publisher
.class,
1075 int expectedMin
= getMinEmployeesFromPubs();
1076 assertEquals(expectedMin
, min
.intValue());
1082 param
= getTotalNumOfEmployeesParam();
1086 sum
= (Integer
) publisherInstance
.readOther(Publisher
.class,
1087 getQueryParamHashtable(param
));
1091 sum
= (Integer
) publisherInstance
.readOther(Publisher
.class,
1092 getQueryParamList(param
));
1097 sum
= (Integer
) publisherInstance
.readOther(Publisher
.class,
1102 int expectedSum
= getTotalNumOfEmployeesFromPubs();
1103 assertEquals(expectedSum
, sum
.intValue());
1107 private void performTestReadSingle(MethodInvocationType type
) {
1108 AbstractDAO
<Book
> bookInstance
= getDaoInstance();
1109 AbstractDAO
<Author
> authorInstance
= getDaoInstance();
1110 AbstractDAO
<Publisher
> publisherInstance
= getDaoInstance();
1111 Map
<String
, Integer
> bookNameToIdMap
= new HashMap
<String
, Integer
>();
1112 Map
<String
, Integer
> publisherNameToIdMap
=
1113 new HashMap
<String
, Integer
>();
1114 Map
<String
, Integer
> authorNameToIdMap
= new HashMap
<String
, Integer
>();
1115 makeNameToIdMap(bookNameToIdMap
, bookInstance
, authorNameToIdMap
,
1116 authorInstance
, publisherNameToIdMap
, publisherInstance
);
1118 * Test non single request
1121 bookInstance
.readSingle(Book
.class);
1122 fail("Should not succeed retrieving 1 Book!");
1124 catch (IllegalArgumentException argumentException
) {
1126 catch (Exception exception
) {
1127 fail(exception
.getMessage());
1129 QueryParameter
<Integer
> param
;
1131 * Try to load a book with non-existing id
1134 param
= getIdQueryParam(-1);
1135 Book nonExistingBook
;
1138 nonExistingBook
= bookInstance
.readSingle(Book
.class,
1139 getQueryParamHashtable(param
));
1143 nonExistingBook
= bookInstance
.readSingle(Book
.class,
1144 getQueryParamList(param
));
1149 nonExistingBook
= bookInstance
.readSingle(Book
.class, param
);
1153 assertNull(nonExistingBook
);
1156 * Test a random single book with id
1159 Book kothaoKeoNei
= getKothaoKeoNei(null, null);
1160 int bookId
= bookNameToIdMap
.get(kothaoKeoNei
.getName());
1161 param
= getIdQueryParam(bookId
);
1162 Book kothaoKeoNeiFromDao
;
1165 kothaoKeoNeiFromDao
= bookInstance
.readSingle(Book
.class,
1166 getQueryParamHashtable(param
));
1170 kothaoKeoNeiFromDao
= bookInstance
.readSingle(Book
.class,
1171 getQueryParamList(param
));
1176 kothaoKeoNeiFromDao
= bookInstance
.readSingle(Book
.class,
1181 assertBook(kothaoKeoNei
, kothaoKeoNeiFromDao
, bookNameToIdMap
, 1);
1184 * Test a random single publisher with id
1187 Publisher annoProkash
= getAnnoProkash();
1188 int publisherId
= publisherNameToIdMap
.get(annoProkash
.getName());
1189 param
= getIdQueryParam(publisherId
);
1190 Publisher annoProkashFromDao
;
1193 annoProkashFromDao
= publisherInstance
.readSingle(
1194 Publisher
.class, getQueryParamHashtable(param
));
1198 annoProkashFromDao
= publisherInstance
.readSingle(
1199 Publisher
.class, getQueryParamList(param
));
1204 annoProkashFromDao
= publisherInstance
.readSingle(
1205 Publisher
.class, param
);
1209 assertPublisher(annoProkash
, annoProkashFromDao
,
1210 publisherNameToIdMap
);
1213 * Test a random single author with id
1216 Author humayunAhmed
= getHumayunAhmed();
1217 int authorId
= authorNameToIdMap
.get(humayunAhmed
.getName());
1218 param
= getIdQueryParam(authorId
);
1219 Author humayunAhmedFromDao
= authorInstance
.readSingle(Author
.class,
1223 humayunAhmedFromDao
=
1224 authorInstance
.readSingle(Author
.class,
1225 getQueryParamHashtable(param
));
1229 humayunAhmedFromDao
=
1230 authorInstance
.readSingle(Author
.class,
1231 getQueryParamList(param
));
1236 humayunAhmedFromDao
=
1237 authorInstance
.readSingle(Author
.class, param
);
1241 assertAuthor(humayunAhmed
, humayunAhmedFromDao
, authorNameToIdMap
);
1243 QueryParameter
<String
> strParam
;
1245 * Test different match modes
1248 * Test a random single book with exact name
1251 Book webDbApp
= getWebDbApp(null, null, null);
1252 String bookName
= webDbApp
.getName();
1253 strParam
= getNameQueryParam(bookName
, MatchMode
.EXACT
);
1254 Book webDbAppFromDao
;
1255 final int numOfAuthors
= 2;
1258 webDbAppFromDao
= bookInstance
.readSingle(Book
.class,
1259 getQueryParamHashtable(strParam
));
1263 webDbAppFromDao
= bookInstance
.readSingle(Book
.class,
1264 getQueryParamList(strParam
));
1269 webDbAppFromDao
= bookInstance
.readSingle(Book
.class,
1274 assertBook(webDbApp
, webDbAppFromDao
, bookNameToIdMap
, numOfAuthors
);
1277 * Test a random single author with start and end match of name
1280 Author brett
= getBrett();
1281 strParam
= getNameQueryParam(brett
.getName().substring(0, 4),
1283 Author brettFromDao
;
1286 brettFromDao
= authorInstance
.readSingle(Author
.class,
1287 getQueryParamHashtable(strParam
));
1291 brettFromDao
= authorInstance
.readSingle(Author
.class,
1292 getQueryParamList(strParam
));
1297 brettFromDao
= authorInstance
.readSingle(Author
.class,
1302 assertAuthor(brett
, brettFromDao
, authorNameToIdMap
);
1303 Author anwar
= getKaziAnowarHossain();
1304 strParam
= getNameQueryParam(anwar
.getName().substring(4),
1306 Author anwarFromDao
;
1309 anwarFromDao
= authorInstance
.readSingle(Author
.class,
1310 getQueryParamHashtable(strParam
));
1314 anwarFromDao
= authorInstance
.readSingle(Author
.class,
1315 getQueryParamList(strParam
));
1320 anwarFromDao
= authorInstance
.readSingle(Author
.class,
1325 assertAuthor(anwar
, anwarFromDao
, authorNameToIdMap
);
1328 * Test a random single book with anywhere name
1331 Publisher sheba
= getAnnoProkash();
1332 strParam
= getNameQueryParam(sheba
.getName().substring(1, sheba
.
1334 length() - 2), MatchMode
.ANYWHERE
);
1335 Publisher shebaFromDao
;
1338 shebaFromDao
= publisherInstance
.readSingle(Publisher
.class,
1339 getQueryParamHashtable(strParam
));
1343 shebaFromDao
= publisherInstance
.readSingle(Publisher
.class,
1344 getQueryParamList(strParam
));
1349 shebaFromDao
= publisherInstance
.readSingle(Publisher
.class,
1354 assertPublisher(sheba
, shebaFromDao
,
1355 publisherNameToIdMap
);
1358 * Test a random single book with author's name
1361 Book webDbApp
= getWebDbApp(null, null, null);
1362 Author hughWilliams
= getHughWilliams();
1364 getNameQueryParam(hughWilliams
.getName(), MatchMode
.EXACT
);
1365 QueryParameter
<Void
> authorParam
= getAuthorNestedParam(strParam
);
1366 Book webDbAppFromDao
;
1369 webDbAppFromDao
= bookInstance
.readSingle(Book
.class,
1370 getQueryParamHashtable(authorParam
));
1374 webDbAppFromDao
= bookInstance
.readSingle(Book
.class,
1375 getQueryParamList(authorParam
));
1380 webDbAppFromDao
= bookInstance
.readSingle(Book
.class,
1385 final int numOfAuthors
= 2;
1386 assertBook(webDbApp
, webDbAppFromDao
, bookNameToIdMap
, numOfAuthors
);
1390 private void performTestReadOtherList(MethodInvocationType type
) {
1391 AbstractDAO
<Book
> bookInstance
= getDaoInstance();
1392 AbstractDAO
<Author
> authorInstance
= getDaoInstance();
1393 AbstractDAO
<Publisher
> publisherInstance
= getDaoInstance();
1394 Map
<String
, Integer
> bookNameToIdMap
= new HashMap
<String
, Integer
>();
1395 Map
<String
, Integer
> publisherNameToIdMap
=
1396 new HashMap
<String
, Integer
>();
1397 Map
<String
, Integer
> authorNameToIdMap
= new HashMap
<String
, Integer
>();
1398 makeNameToIdMap(bookNameToIdMap
, bookInstance
, authorNameToIdMap
,
1399 authorInstance
, publisherNameToIdMap
, publisherInstance
);
1400 QueryParameter
<Void
> param
;
1402 * Test group by and order by
1405 param
= getGroupByPubParam();
1406 QueryParameter
<Void
> countParam
= getCountIdParam();
1407 QueryParameter
<Order
> orderByParam
=
1408 getDescOrderByPubParam();
1412 others
= bookInstance
.readOtherList(Book
.class,
1413 getQueryParamHashtable(param
, countParam
, orderByParam
));
1417 others
= bookInstance
.readOtherList(Book
.class,
1418 getQueryParamList(param
, countParam
, orderByParam
));
1423 others
= bookInstance
.readOtherList(Book
.class, param
,
1424 countParam
, orderByParam
);
1428 int[] pubId
= new int[others
.size()];
1429 Map
<Integer
, Integer
> bookCount
= new HashMap
<Integer
, Integer
>(
1432 for (Object other
: others
) {
1433 assertTrue(other
instanceof Object
[]);
1434 Object
[] values
= (Object
[]) other
;
1435 pubId
[i
] = Integer
.parseInt(values
[0].toString());
1436 bookCount
.put(pubId
[i
++], Integer
.parseInt(values
[1].toString()));
1438 for (i
= 1; i
< pubId
.length
; ++i
) {
1439 assertTrue(pubId
[i
- 1] > pubId
[i
]);
1441 Map
<Integer
, Integer
> expectedBookCount
= getExpectedBookCount();
1442 for (Integer publisherId
: getExpectedBookCount().keySet()) {
1443 final Integer bookCountForPub
= bookCount
.get(publisherId
);
1444 assertNotNull(bookCountForPub
);
1445 assertEquals(bookCountForPub
.intValue(), expectedBookCount
.get(
1446 publisherId
).intValue());
1450 * Test multiple property projection
1453 QueryParameter
<Void
> idProjectionParam
= getIdProjectionParam();
1454 QueryParameter
<Void
> nameProjectionParam
=
1455 getNameProjectionParam();
1459 others
= authorInstance
.readOtherList(Author
.class,
1460 getQueryParamHashtable(nameProjectionParam
,
1461 idProjectionParam
));
1466 authorInstance
.readOtherList(Author
.class,
1467 getQueryParamList(nameProjectionParam
, idProjectionParam
));
1472 others
= authorInstance
.readOtherList(Author
.class,
1473 nameProjectionParam
, idProjectionParam
);
1477 assertNameIdPair(others
, authorNameToIdMap
);
1478 List
<QueryParameter
<Void
>> propsParam
= getNameIdPropsParam();
1481 others
= authorInstance
.readOtherList(Author
.class,
1482 getQueryParamHashtable(propsParam
.toArray(
1483 new QueryParameter
[0])));
1487 others
= authorInstance
.readOtherList(Author
.class,
1488 getQueryParamList(propsParam
.toArray(
1489 new QueryParameter
[0])));
1493 others
= authorInstance
.readOtherList(Author
.class,
1494 propsParam
.toArray(new QueryParameter
[0]));
1499 assertNameIdPair(others
, authorNameToIdMap
);
1502 * Test distinct property projection
1505 QueryParameter
<Void
> distinctNumOfEmployeeProjectionParam
=
1506 getDistinctNumOfEmployeeParam();
1510 nums
= (List
<Integer
>) publisherInstance
.readOtherList(
1511 Publisher
.class, getQueryParamHashtable(
1512 distinctNumOfEmployeeProjectionParam
));
1516 nums
= publisherInstance
.<Integer
>readOtherList(
1517 Publisher
.class, getQueryParamList(
1518 distinctNumOfEmployeeProjectionParam
));
1523 nums
= publisherInstance
.<Integer
>readOtherList(
1524 Publisher
.class, distinctNumOfEmployeeProjectionParam
);
1528 Set
<Integer
> expectedNums
= new HashSet
<Integer
>();
1529 Publisher
[] publishers
= getAllPublishers();
1530 for (Publisher publisher
: publishers
) {
1531 expectedNums
.add(publisher
.getNumOfEmployees());
1533 assertEquals(expectedNums
.size(), nums
.size());
1534 for (Integer num
: nums
) {
1535 assertTrue(expectedNums
.contains(num
));
1540 private void assertAuthor(final Author author
,
1541 final Author authorFromDao
,
1542 final Map
<String
, Integer
> authorNameToIdMap
) {
1543 assertNotNull(authorFromDao
);
1544 assertEquals(authorNameToIdMap
.get(author
.getName()).intValue(),
1545 authorFromDao
.getId().intValue());
1546 assertEquals(author
.getName(), authorFromDao
.getName());
1549 private void assertBook(final Book book
,
1550 final Book bookFromDao
,
1551 final Map
<String
, Integer
> bookNameToIdMap
,
1552 final int numOfAuthors
) {
1553 assertNotNull(bookFromDao
);
1554 assertEquals(bookNameToIdMap
.get(book
.getName()).intValue(),
1555 bookFromDao
.getId().intValue());
1556 assertEquals(book
.getName(), bookFromDao
.getName());
1557 assertEquals(book
.getIsbn(), bookFromDao
.getIsbn());
1558 assertEquals(book
.getPrice().doubleValue(), bookFromDao
.getPrice().
1560 assertEquals(book
.getQuantityInStock().intValue(), bookFromDao
.
1561 getQuantityInStock().intValue());
1562 if (numOfAuthors
> -1) {
1563 assertEquals(numOfAuthors
, bookFromDao
.getAuthors().size());
1567 private void assertNameIdPair(List others
,
1568 Map
<String
, Integer
> authorNameToIdMap
)
1569 throws NumberFormatException
{
1570 Map
<String
, Integer
> nameIds
=
1571 new HashMap
<String
, Integer
>(others
.size());
1572 for (Object other
: others
) {
1573 assertTrue(other
instanceof Object
[]);
1574 Object
[] values
= (Object
[]) other
;
1575 nameIds
.put(values
[0].toString(),
1576 Integer
.parseInt(values
[1].toString()));
1578 for (String name
: authorNameToIdMap
.keySet()) {
1579 assertEquals(authorNameToIdMap
.get(name
).intValue(),
1580 nameIds
.get(name
).intValue());
1584 private void assertPublisher(final Publisher publisher
,
1585 final Publisher publisherFromDao
,
1586 final Map
<String
, Integer
> publisherNameToIdMap
) {
1587 assertNotNull(publisherFromDao
);
1588 assertEquals(publisherNameToIdMap
.get(publisher
.getName()).intValue(),
1589 publisherFromDao
.getId().intValue());
1590 assertEquals(publisher
.getName(), publisherFromDao
.getName());
1591 assertEquals(publisher
.getNumOfEmployees(),
1592 publisherFromDao
.getNumOfEmployees());
1595 private void enterBookToIndex(List
<Book
> bookList
,
1597 Map
<String
, Integer
> bookNameToIdMap
) {
1598 int index
= getBookIndex(bookList
, searchedBook
);
1599 assertTrue(index
> -1);
1600 bookNameToIdMap
.put(searchedBook
.getName(), bookList
.get(index
).getId());
1601 bookList
.remove(index
);
1604 private void enterAuthorToIndex(List
<Author
> authorList
,
1605 Author searchedAuthor
,
1606 Map
<String
, Integer
> authorNameToIdMap
) {
1607 int index
= getAuthorIndex(authorList
, searchedAuthor
);
1608 assertTrue(index
> -1);
1609 authorNameToIdMap
.put(searchedAuthor
.getName(), authorList
.get(index
).
1611 authorList
.remove(index
);
1614 private void enterPublisherToIndex(List
<Publisher
> publisherList
,
1615 Publisher searchedPublisher
,
1616 Map
<String
, Integer
> publisherNameToIdMap
) {
1617 int index
= getPublisherIndex(publisherList
, searchedPublisher
);
1618 assertTrue(index
> -1);
1619 publisherNameToIdMap
.put(searchedPublisher
.getName(), publisherList
.get(
1621 publisherList
.remove(index
);
1624 private Book
getAgniSopoth(Author author
,
1625 Publisher publisher
) {
1626 final String bookName
= "Agni Sopoth";
1627 final Date publishDate
= new Date();
1628 final String isbn
= "123ABC";
1629 return getBook(publisher
, bookName
, publishDate
, isbn
, 40, 400.0, author
);
1632 private Book
getAgunerPoroshMoni(Publisher annoProkash
,
1633 Author humayunAhmed
) {
1634 return getBook(annoProkash
, "Aguner Poroshmoni", new Date(), "222VFEE3",
1635 10, 100.0, humayunAhmed
);
1638 private <Template
extends PersistentDTO
<Template
>> Set
<Template
> getAll(
1639 final AbstractDAO
<Template
> bookInstance
,
1640 final Class
<Template
> templateClass
) {
1641 return new LinkedHashSet
<Template
>(bookInstance
.readList(templateClass
));
1644 private Publisher
[] getAllPublishers() {
1645 return new Publisher
[]{getAnnoProkash(), getOReilly(),
1646 getShebaProkashani()
1650 private Publisher
getAnnoProkash() {
1651 return getPublisher(new Date(), 50, "Anno Prokash");
1654 private QueryParameter
<Void
> getAuthorNestedParam(
1655 QueryParameter
... parameters
) {
1656 return QueryParameterFactory
.getNestedParametersParam("authors",
1657 FetchMode
.DEFAULT
, parameters
);
1660 private QueryParameter
<Void
> getAvgEmployeesParam() {
1661 return QueryParameterFactory
.getElementAvgParam("numOfEmployees");
1664 private int getBookIndex(final List
<Book
> bookList
,
1665 final Book searchedBook
) {
1667 for (index
= 0; index
< bookList
.size();
1669 Book book
= bookList
.get(index
);
1670 if (searchedBook
.getName().equals(book
.getName())) {
1677 private QueryParameter
<Void
> getCountDistinctNumOfEmployeeParam() {
1678 return QueryParameterFactory
.getDistinctElementCountParam(
1682 private QueryParameter
<Void
> getCountIdParam() {
1683 return QueryParameterFactory
.getElementCountParam("id");
1686 private int getDistinctNumOfEmployeeNum() {
1687 HashSet
<Integer
> numbers
= new HashSet
<Integer
>();
1688 for (Publisher publisher
: getAllPublishers()) {
1689 numbers
.add(publisher
.getNumOfEmployees());
1691 return numbers
.size();
1694 private QueryParameter
<Void
> getDistinctNumOfEmployeeParam() {
1695 return QueryParameterFactory
.getDistinctPropProjectionParam(
1699 private HashMap
<Integer
, Integer
> getExpectedBookCount() {
1700 final HashMap
<Integer
, Integer
> result
=
1701 new HashMap
<Integer
, Integer
>();
1702 AbstractDAO
<Book
> bookInstance
= getDaoInstance();
1703 AbstractDAO
<Author
> authorInstance
= getDaoInstance();
1704 AbstractDAO
<Publisher
> publisherInstance
= getDaoInstance();
1705 Map
<String
, Integer
> bookNameToIdMap
= new HashMap
<String
, Integer
>();
1706 Map
<String
, Integer
> publisherNameToIdMap
=
1707 new HashMap
<String
, Integer
>();
1708 Map
<String
, Integer
> authorNameToIdMap
= new HashMap
<String
, Integer
>();
1709 makeNameToIdMap(bookNameToIdMap
, bookInstance
, authorNameToIdMap
,
1710 authorInstance
, publisherNameToIdMap
, publisherInstance
);
1711 result
.put(publisherNameToIdMap
.get(getOReilly().getName()), 2);
1712 result
.put(publisherNameToIdMap
.get(getAnnoProkash().getName()), 2);
1713 result
.put(publisherNameToIdMap
.get(getShebaProkashani().getName()), 1);
1717 private QueryParameter
<Void
> getIdProjectionParam() {
1718 return QueryParameterFactory
.getPropProjectionParam("id");
1721 private QueryParameter
<Void
> getMaxNumOfEmployeeParam() {
1722 return QueryParameterFactory
.getElementMaxParam("numOfEmployees");
1725 private QueryParameter
<Void
> getMinNumOfEmployeeParam() {
1726 return QueryParameterFactory
.getElementMinParam("numOfEmployees");
1729 private QueryParameter
<Void
> getGroupByPubParam() {
1730 return QueryParameterFactory
.getGroupByPropParam("publisher.id");
1733 private QueryParameter
<Order
> getDescOrderByPubParam() {
1734 return QueryParameterFactory
.getOrderByParam("publisher.id", Order
.DESC
);
1737 private QueryParameter
<Order
> getOrderByIdParam(final Order order
) {
1738 return QueryParameterFactory
.getOrderByParam("id", order
);
1741 private List
<QueryParameter
<Void
>> getNameIdPropsParam() {
1742 return QueryParameterFactory
.getMultiPropProjectionParam("name", "id");
1745 private QueryParameter
<Void
> getNameProjectionParam() {
1746 return QueryParameterFactory
.getPropProjectionParam("name");
1749 private Hashtable
<String
, QueryParameter
> getQueryParamHashtable(
1750 QueryParameter
... params
) {
1751 Hashtable
<String
, QueryParameter
> table
=
1752 new Hashtable
<String
, QueryParameter
>();
1753 for (QueryParameter parameter
: params
) {
1754 String paramName
= getPropertyName(parameter
);
1755 if (table
.containsKey(paramName
)) {
1757 while (table
.containsKey(new StringBuilder(paramName
).append(
1761 paramName
= new StringBuilder(paramName
).append(i
).toString();
1763 table
.put(paramName
, parameter
);
1768 private List
<QueryParameter
> getQueryParamList(QueryParameter
... params
) {
1769 ArrayList
<QueryParameter
> result
= new ArrayList
<QueryParameter
>();
1770 Collections
.addAll(result
, params
);
1774 private QueryParameter
<Integer
> getIdQueryParam(int id
) {
1775 return QueryParameterFactory
.<Integer
>getEqualPropertyParam("id", id
);
1778 private QueryParameter
<String
> getNameQueryParam(String name
,
1780 return QueryParameterFactory
.<String
>getStringLikePropertyParam("name",
1784 private int getPublisherIndex(final List
<Publisher
> publisherList
,
1785 final Publisher searchedBook
) {
1787 for (index
= 0; index
< publisherList
.size();
1789 Publisher publisher
= publisherList
.get(index
);
1790 if (searchedBook
.getName().equals(publisher
.getName())) {
1797 private int getAuthorIndex(final List
<Author
> authorList
,
1798 final Author searchedBook
) {
1800 for (index
= 0; index
< authorList
.size();
1802 Author author
= authorList
.get(index
);
1803 if (searchedBook
.getName().equals(author
.getName())) {
1810 private Author
getBrett() {
1811 return getAuthor("Brett McLaugblin", new Date());
1814 private Author
getDavidLane() {
1815 return getAuthor("David Lane", new Date());
1818 private Author
getHughWilliams() {
1819 return getAuthor("Hugh E. Williams", new Date());
1822 private Author
getHumayunAhmed() {
1823 return getAuthor("Humayun Ahmed", new Date());
1826 private Book
getJavaAndXml(Publisher oReilly
,
1827 Author brettMcLaugblin
) {
1828 return getBook(oReilly
, "Java & XML", new Date(), "555UIP66", 20, 200.0,
1832 private Author
getKaziAnowarHossain() {
1833 final String name
= "Kazi Anowar Hossain";
1834 final Date birthDate
= new Date();
1835 return getAuthor(name
, birthDate
);
1838 private Book
getKothaoKeoNei(Publisher annoProkash
,
1839 Author humayunAhmed
) {
1840 return getBook(annoProkash
, "Kothao Keo Nei", new Date(), "11134BCE", 30,
1841 300.0, humayunAhmed
);
1844 private Publisher
getOReilly() {
1845 return getPublisher(new Date(), 100, "O\'Reilly");
1848 private Publisher
getShebaProkashani() {
1849 final String name
= "Sheba Prokashoni";
1850 final Date establishDate
= new Date();
1851 final int numOfEmployees
= 100;
1852 return getPublisher(establishDate
, numOfEmployees
, name
);
1855 private Publisher
getPublisher(final Date establishDate
,
1856 final int numOfEmployees
,
1857 final String name
) {
1858 Publisher publisher
=
1860 publisher
.setEstablishedDate(establishDate
);
1861 publisher
.setNumOfEmployees(numOfEmployees
);
1862 publisher
.setName(name
);
1866 private Author
getAuthor(final String name
,
1867 final Date birthDate
) {
1870 author
.setName(name
);
1871 author
.setBirthDate(birthDate
);
1875 private Book
getBook(final Publisher publisher
,
1876 final String bookName
,
1877 final Date publishDate
,
1879 final Integer quantityInStock
,
1881 final Author
... authors
) {
1882 Book book
= new Book();
1883 Set
<Author
> authorSet
=
1884 new HashSet
<Author
>();
1885 for (Author author
: authors
) {
1886 authorSet
.add(author
);
1888 book
.setAuthors(authorSet
);
1889 book
.setPublisher(publisher
);
1890 book
.setName(bookName
);
1891 book
.setPublishDate(publishDate
);
1893 book
.setPrice(price
);
1894 book
.setQuantityInStock(quantityInStock
);
1898 private <T
extends PersistentDTO
<T
>> AbstractDAO
<T
> getDaoInstance()
1899 throws BeansException
{
1900 AbstractDAO
<T
> instance
=
1901 (AbstractDAO
<T
>) context
.getBean("testDao");
1902 assertNotNull(instance
);
1906 private int getTotalNumOfEmployeesFromPubs() {
1907 Publisher
[] publishers
= getAllPublishers();
1909 for (Publisher publisher
: publishers
) {
1910 total
+= publisher
.getNumOfEmployees();
1915 private QueryParameter
<Void
> getTotalNumOfEmployeesParam() {
1916 return QueryParameterFactory
.getElementSumParam("numOfEmployees");
1919 private Book
getWebDbApp(Publisher oReilly
,
1921 Author hughWilliams
) {
1922 return getBook(oReilly
, "Web Database Applications", new Date(),
1923 "444ERT6", 50, 500.0, davidLane
, hughWilliams
);
1926 private void makeNameToIdMap(Map
<String
, Integer
> bookNameToIdMap
,
1927 AbstractDAO
<Book
> bookInstance
,
1928 Map
<String
, Integer
> authorNameToIdMap
,
1929 AbstractDAO
<Author
> authorInstance
,
1930 Map
<String
, Integer
> publisherNameToIdMap
,
1931 AbstractDAO
<Publisher
> publisherInstance
) {
1935 Set
<Book
> books
= getAll(bookInstance
, Book
.class);
1936 List
<Book
> bookList
= new ArrayList
<Book
>(books
);
1937 Book kothaoKeoNei
= getKothaoKeoNei(null, null);
1938 enterBookToIndex(bookList
, kothaoKeoNei
, bookNameToIdMap
);
1939 Book agunerPoroshMoni
= getAgunerPoroshMoni(null, null);
1940 enterBookToIndex(bookList
, agunerPoroshMoni
, bookNameToIdMap
);
1941 Book webDbApp
= getWebDbApp(null, null, null);
1942 enterBookToIndex(bookList
, webDbApp
, bookNameToIdMap
);
1943 Book javaAndXml
= getJavaAndXml(null, null);
1944 enterBookToIndex(bookList
, javaAndXml
, bookNameToIdMap
);
1945 Book agniSopoth
= getAgniSopoth(null, null);
1946 enterBookToIndex(bookList
, agniSopoth
, bookNameToIdMap
);
1950 Set
<Publisher
> publishers
= getAll(publisherInstance
, Publisher
.class);
1951 List
<Publisher
> publisherList
= new ArrayList
<Publisher
>(publishers
);
1952 Publisher shebaProkashani
= getShebaProkashani();
1953 enterPublisherToIndex(publisherList
, shebaProkashani
,
1954 publisherNameToIdMap
);
1955 Publisher oReilly
= getOReilly();
1956 enterPublisherToIndex(publisherList
, oReilly
, publisherNameToIdMap
);
1957 Publisher annoProkash
= getAnnoProkash();
1958 enterPublisherToIndex(publisherList
, annoProkash
, publisherNameToIdMap
);
1962 Set
<Author
> authors
= getAll(authorInstance
, Author
.class);
1963 List
<Author
> authorList
= new ArrayList
<Author
>(authors
);
1964 Author kaziAnowarHossain
= getKaziAnowarHossain();
1965 enterAuthorToIndex(authorList
, kaziAnowarHossain
, authorNameToIdMap
);
1966 Author brettMcLaugblin
= getBrett();
1967 enterAuthorToIndex(authorList
, brettMcLaugblin
, authorNameToIdMap
);
1968 Author davidLane
= getDavidLane();
1969 enterAuthorToIndex(authorList
, davidLane
, authorNameToIdMap
);
1970 Author hughWilliams
= getHughWilliams();
1971 enterAuthorToIndex(authorList
, hughWilliams
, authorNameToIdMap
);
1972 Author humayunAhmed
= getHumayunAhmed();
1973 enterAuthorToIndex(authorList
, humayunAhmed
, authorNameToIdMap
);
1977 private int getMaxEmployeesFromPubs() {
1978 int max
= Integer
.MIN_VALUE
;
1979 Publisher
[] publishers
= getAllPublishers();
1980 for (Publisher publisher
: publishers
) {
1981 max
= Math
.max(max
, publisher
.getNumOfEmployees());
1986 private int getMinEmployeesFromPubs() {
1987 int min
= Integer
.MAX_VALUE
;
1988 Publisher
[] publishers
= getAllPublishers();
1989 for (Publisher publisher
: publishers
) {
1990 min
= Math
.min(min
, publisher
.getNumOfEmployees());
1995 private String
getPropertyName(
1996 QueryParameter param
) {
1997 String propertyName
;
1999 if (param
instanceof QueryParameterWithPropertyName
) {
2001 ((QueryParameterWithPropertyName
) param
).getPropertyName();
2004 propertyName
= null;
2006 if (propertyName
== null) {
2010 return propertyName
;
2013 private enum MethodInvocationType
{
2015 VAR_ARGS
, HASH_TABLE
, LIST
;