Add test cases for default annotation config scanning
[smart-dao.git] / smart-hibernate-abstract-dao / src / test / java / com / smartitengineering / dao / impl / hibernate / AbstractDAOTest.java
blobf169d2899de6ecf8ea17df7a8afbcb73294b54e3
1 /*
2 * This is a common dao with basic CRUD operations and is not limited to any
3 * persistent layer implementation
4 *
5 * Copyright (C) 2008 Imran M Yousuf (imyousuf@smartitengineering.com)
6 *
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;
41 import java.util.Map;
42 import java.util.Random;
43 import java.util.Set;
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;
50 /**
52 * @author imyousuf
54 public class AbstractDAOTest
55 extends TestCase {
57 private static ApplicationContext context;
59 public AbstractDAOTest(String testName) {
60 super(testName);
63 @Override
64 protected void setUp()
65 throws Exception {
66 super.setUp();
67 if (context == null) {
68 context = new ClassPathXmlApplicationContext("app-context.xml");
72 @Override
73 protected void tearDown()
74 throws Exception {
75 super.tearDown();
78 /**
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();
86 /**
87 * Test null object creation, should throw IllegalArgumentException
89 try {
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());
98 /**
99 * Create proper test data.
100 * This set will contain single of everything - author, publisher, book
102 Publisher shebaProkashani = getShebaProkashani();
103 try {
104 publisherInstance.createEntity(shebaProkashani);
105 System.out.println(shebaProkashani.getId());
107 catch (Exception ex) {
108 fail(ex.getMessage());
110 Author kaziAnowarHossain = getKaziAnowarHossain();
111 try {
112 authorInstance.createEntity(kaziAnowarHossain);
113 System.out.println(kaziAnowarHossain.getId());
115 catch (Exception ex) {
116 fail(ex.getMessage());
118 Book book = getAgniSopoth(kaziAnowarHossain, shebaProkashani);
119 try {
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();
132 try {
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();
148 try {
149 authorInstance.createEntity(brettMcLaugblin, davidLane, humayunAhmed,
150 hughWilliams);
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);
162 try {
163 bookInstance.createEntity(kothaoKeoNei, agunerPoroshMoni, webDbApp,
164 javaAndXml);
165 System.out.println(kothaoKeoNei.getId() + ", " +
166 agunerPoroshMoni.getId() + ", " + webDbApp.getId() + ", " +
167 javaAndXml.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(
200 bookNameToIdMap);
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());
207 assertEmpty(names);
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.
219 keySet());
220 for (Publisher publisher : publishers) {
221 names.remove(publisher.getName());
223 assertEmpty(names);
226 * Update all author at once
229 QueryParameter<Integer> idInParam = getIdInParam(
230 authorNameToIdMap);
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());
238 assertEmpty(names);
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(
333 bookNameToIdMap);
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());
342 Book updatedBook =
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(
377 authorNameToIdMap);
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,
390 parameter);
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(
439 bookNameToIdMap);
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(
463 authorNameToIdMap);
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);
481 return calendar;
484 private Calendar getBookFirstPubCal() {
485 Calendar calendar = Calendar.getInstance();
486 calendar.setTimeInMillis(0);
487 calendar.add(Calendar.YEAR, 30);
488 return calendar;
491 private QueryParameter<Date> getDateBetweenParam(final String propName,
492 final Date startDate,
493 final Date endDate) {
494 return QueryParameterFactory.<Date>getBetweenPropertyParam(propName,
495 startDate, endDate);
498 private QueryParameter<Date> getDateGreaterThanParam(final String propName,
499 Date startDate) {
500 return QueryParameterFactory.<Date>getGreaterThanPropertyParam(propName,
501 startDate);
504 private QueryParameter<Date> getDateLesserThanParam(final String propName,
505 Date endDate) {
506 return QueryParameterFactory.<Date>getLesserThanPropertyParam(propName,
507 endDate);
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,
520 int authorId) {
521 return QueryParameterFactory.<Integer>getGreaterThanEqualToPropertyParam(
522 idProp, authorId);
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(
532 List<Integer> ids) {
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(
547 propertyName);
550 private QueryParameter<Void> getIsEmptyParam(String propertyName) {
551 return QueryParameterFactory.getIsEmptyCollectionPropertyParam(
552 propertyName);
555 private QueryParameter<Integer> getLesserThanEqualIntParam(String idProp,
556 int authorId) {
557 return QueryParameterFactory.<Integer>getLesserThanEqualToPropertyParam(
558 idProp, authorId);
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);
573 return calendar;
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,
590 idInParam)));
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);
601 * Test between
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);
612 List<Book> result;
613 switch (type) {
614 case HASH_TABLE: {
615 result = new ArrayList<Book>(new LinkedHashSet<Book>(
616 bookInstance.readList(Book.class,
617 getQueryParamHashtable(dateBetweenParam, orderParam))));
618 break;
620 case LIST: {
621 result = new ArrayList<Book>(new LinkedHashSet<Book>(
622 bookInstance.readList(Book.class, getQueryParamList(
623 dateBetweenParam, orderParam))));
624 break;
626 case VAR_ARGS:
627 default: {
628 result = new ArrayList<Book>(new LinkedHashSet<Book>(
629 bookInstance.readList(Book.class, dateBetweenParam,
630 orderParam)));
631 break;
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()));
644 * Test disjunction
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,
655 startDate);
656 QueryParameter<Date> ltDateParam = getDateLesserThanParam(propName,
657 endDate);
658 QueryParameter<Void> disjunctionalParam = getDisjunctionalParam(
659 gtDateParam, ltDateParam);
660 QueryParameter<Order> orderParam = getOrderByIdParam(Order.DESC);
661 List<Author> result;
662 switch (type) {
663 case HASH_TABLE: {
664 result = authorInstance.readList(Author.class,
665 getQueryParamHashtable(disjunctionalParam, orderParam));
666 break;
668 case LIST: {
669 result = authorInstance.readList(Author.class,
670 getQueryParamList(disjunctionalParam, orderParam));
671 break;
673 case VAR_ARGS:
674 default: {
675 result = authorInstance.readList(Author.class,
676 disjunctionalParam, orderParam);
677 break;
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));
684 assertTrue(
685 Arrays.equals(expectedAuthors.toArray(), result.toArray()));
688 * Test not in
691 List<Integer> ids = Collections.singletonList(allPublishers.get(0).
692 getId());
693 QueryParameter<Integer> idNotInParam = getIdNotInParam(
694 ids);
695 QueryParameter<Order> orderParam = getOrderByIdParam(Order.DESC);
696 List<Publisher> result;
697 switch (type) {
698 case HASH_TABLE: {
699 result = publisherInstance.readList(Publisher.class,
700 getQueryParamHashtable(idNotInParam, orderParam));
701 break;
703 case LIST: {
704 result = publisherInstance.readList(Publisher.class,
705 getQueryParamList(idNotInParam, orderParam));
706 break;
708 case VAR_ARGS:
709 default: {
710 result = publisherInstance.readList(Publisher.class,
711 idNotInParam, orderParam);
712 break;
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.
727 int startIndex =
728 Math.abs(new Random().nextInt()) % (allBooks.size() - 1) + 2;
729 int max = 1;
730 QueryParameter<Order> orderParam = getOrderByIdParam(Order.DESC);
731 QueryParameter<Integer> firstResult =
732 getFirstResultParam(startIndex);
733 QueryParameter<Integer> maxResult = getMaxResultParam(max);
734 List<Book> result;
735 switch (type) {
736 case HASH_TABLE: {
737 result = bookInstance.readList(Book.class,
738 getQueryParamHashtable(firstResult, maxResult,
739 orderParam));
740 break;
742 case LIST: {
743 result = bookInstance.readList(Book.class,
744 getQueryParamList(firstResult, maxResult, orderParam));
745 break;
747 case VAR_ARGS:
748 default: {
749 result = bookInstance.readList(Book.class, firstResult,
750 maxResult, orderParam);
751 break;
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()));
760 * Test not equal
763 QueryParameter<Order> orderParam = getOrderByIdParam(Order.DESC);
764 int id = allAuthors.get(0).getId();
765 QueryParameter<Integer> notEqualIdParam = getNotEqualIdParam(id);
766 List<Author> result;
767 switch (type) {
768 case HASH_TABLE: {
769 result = authorInstance.readList(Author.class,
770 getQueryParamHashtable(notEqualIdParam, orderParam));
771 break;
773 case LIST: {
774 result = authorInstance.readList(Author.class,
775 getQueryParamList(notEqualIdParam, orderParam));
776 break;
778 case VAR_ARGS:
779 default: {
780 result = authorInstance.readList(Author.class,
781 notEqualIdParam, orderParam);
782 break;
785 List<Author> expectedResult = allAuthors.subList(1,
786 allAuthors.size());
787 assertTrue(Arrays.equals(expectedResult.toArray(), result.toArray()));
790 * Test less-than-equal, greater-than-equal
793 int authorIndex = Math.abs(new Random().nextInt()) %
794 allAuthors.size();
795 int authorId = allAuthors.get(authorIndex).getId();
796 String idProp = "id";
797 QueryParameter<Integer> lteParameter = getLesserThanEqualIntParam(
798 idProp, authorId);
799 QueryParameter<Integer> gteParameter = getGreaterThanEqualIntParam(
800 idProp, authorId);
801 List<Author> result;
802 switch (type) {
803 case HASH_TABLE: {
804 result = authorInstance.readList(Author.class,
805 getQueryParamHashtable(lteParameter, gteParameter));
806 break;
808 case LIST: {
809 result = authorInstance.readList(Author.class,
810 getQueryParamList(lteParameter, gteParameter));
811 break;
813 case VAR_ARGS:
814 default: {
815 result = authorInstance.readList(Author.class, lteParameter,
816 gteParameter);
817 break;
820 List<Author> expectedResult = Collections.singletonList(allAuthors.
821 get(authorIndex));
822 assertTrue(Arrays.equals(expectedResult.toArray(), result.toArray()));
825 * Test null
828 String propertyName = "establishedDate";
829 QueryParameter<Void> nullParam = getIsNullParam(propertyName);
830 List<Publisher> result;
831 switch (type) {
832 case HASH_TABLE: {
833 result = publisherInstance.readList(Publisher.class,
834 getQueryParamHashtable(nullParam));
835 break;
837 case LIST: {
838 result = publisherInstance.readList(Publisher.class,
839 getQueryParamList(nullParam));
840 break;
842 case VAR_ARGS:
843 default: {
844 result = publisherInstance.readList(Publisher.class,
845 nullParam);
846 break;
849 assertEmpty(result);
852 * Test not null
855 String propertyName = "establishedDate";
856 QueryParameter<Void> notNullParam =
857 getIsNotNullParam(propertyName);
858 QueryParameter<Order> orderParam = getOrderByIdParam(Order.DESC);
859 List<Publisher> result;
860 switch (type) {
861 case HASH_TABLE: {
862 result = publisherInstance.readList(Publisher.class,
863 getQueryParamHashtable(notNullParam, orderParam));
864 break;
866 case LIST: {
867 result = publisherInstance.readList(Publisher.class,
868 getQueryParamList(notNullParam, orderParam));
869 break;
871 case VAR_ARGS:
872 default: {
873 result = publisherInstance.readList(Publisher.class,
874 notNullParam, orderParam);
875 break;
878 assertTrue(Arrays.equals(allPublishers.toArray(), result.toArray()));
881 * Test empty
884 String propertyName = "authors";
885 QueryParameter<Void> emptyParam = getIsEmptyParam(propertyName);
886 List<Book> result;
887 switch (type) {
888 case HASH_TABLE: {
889 result = bookInstance.readList(Book.class,
890 getQueryParamHashtable(emptyParam));
891 break;
893 case LIST: {
894 result = bookInstance.readList(Book.class,
895 getQueryParamList(emptyParam));
896 break;
898 case VAR_ARGS:
899 default: {
900 result = bookInstance.readList(Book.class, emptyParam);
901 break;
904 assertEmpty(result);
907 * Test not empty
910 String propertyName = "authors";
911 QueryParameter<Void> notEmptyParam =
912 getIsNotEmptyParam(propertyName);
913 QueryParameter<Order> orderParam = getOrderByIdParam(Order.DESC);
914 List<Book> result;
915 switch (type) {
916 case HASH_TABLE: {
917 result = bookInstance.readList(Book.class,
918 getQueryParamHashtable(notEmptyParam, orderParam));
919 break;
921 case LIST: {
922 result = bookInstance.readList(Book.class,
923 getQueryParamList(notEmptyParam, orderParam));
924 break;
926 case VAR_ARGS:
927 default: {
928 result = bookInstance.readList(Book.class,
929 notEmptyParam, orderParam);
930 break;
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;
942 * Test average
945 param = getAvgEmployeesParam();
946 Double average;
947 switch (type) {
948 case HASH_TABLE: {
949 average = (Double) publisherInstance.readOther(
950 Publisher.class, getQueryParamHashtable(param));
951 break;
953 case LIST: {
954 average = (Double) publisherInstance.readOther(
955 Publisher.class, getQueryParamList(param));
956 break;
958 case VAR_ARGS:
959 default: {
960 average = (Double) publisherInstance.readOther(
961 Publisher.class, param);
962 break;
965 int expectedAverage = getTotalNumOfEmployeesFromPubs() /
966 getAllPublishers().length;
967 assertEquals(expectedAverage, average.intValue());
970 * Test count
973 param = getCountIdParam();
974 Integer count;
975 switch (type) {
976 case HASH_TABLE: {
977 count = (Integer) publisherInstance.readOther(
978 Publisher.class, getQueryParamHashtable(param));
979 break;
981 case LIST: {
982 count = (Integer) publisherInstance.readOther(
983 Publisher.class, getQueryParamList(param));
984 break;
986 case VAR_ARGS:
987 default: {
988 count = (Integer) publisherInstance.readOther(
989 Publisher.class, param);
990 break;
993 int expectedCount = getAllPublishers().length;
994 assertEquals(expectedCount, count.intValue());
997 * Test count distinct
1000 param = getCountDistinctNumOfEmployeeParam();
1001 Integer distinctCount;
1002 switch (type) {
1003 case HASH_TABLE: {
1004 distinctCount = (Integer) publisherInstance.readOther(
1005 Publisher.class, getQueryParamHashtable(param));
1006 break;
1008 case LIST: {
1009 distinctCount = (Integer) publisherInstance.readOther(
1010 Publisher.class, getQueryParamList(param));
1011 break;
1013 case VAR_ARGS:
1014 default: {
1015 distinctCount = (Integer) publisherInstance.readOther(
1016 Publisher.class, param);
1017 break;
1020 int expectedDistinctCount = getDistinctNumOfEmployeeNum();
1021 assertEquals(expectedDistinctCount, distinctCount.intValue());
1024 * Test Max
1027 param = getMaxNumOfEmployeeParam();
1028 Integer max = (Integer) publisherInstance.readOther(Publisher.class,
1029 param);
1030 switch (type) {
1031 case HASH_TABLE: {
1032 max = (Integer) publisherInstance.readOther(Publisher.class,
1033 getQueryParamHashtable(param));
1034 break;
1036 case LIST: {
1037 max = (Integer) publisherInstance.readOther(Publisher.class,
1038 getQueryParamList(param));
1039 break;
1041 case VAR_ARGS:
1042 default: {
1043 max = (Integer) publisherInstance.readOther(Publisher.class,
1044 param);
1045 break;
1048 int expectedMax = getMaxEmployeesFromPubs();
1049 assertEquals(expectedMax, max.intValue());
1052 * Test Min
1055 param = getMinNumOfEmployeeParam();
1056 Integer min;
1057 switch (type) {
1058 case HASH_TABLE: {
1059 min = (Integer) publisherInstance.readOther(Publisher.class,
1060 getQueryParamHashtable(param));
1061 break;
1063 case LIST: {
1064 min = (Integer) publisherInstance.readOther(Publisher.class,
1065 getQueryParamList(param));
1066 break;
1068 case VAR_ARGS:
1069 default: {
1070 min = (Integer) publisherInstance.readOther(Publisher.class,
1071 param);
1072 break;
1075 int expectedMin = getMinEmployeesFromPubs();
1076 assertEquals(expectedMin, min.intValue());
1079 * Test Sum
1082 param = getTotalNumOfEmployeesParam();
1083 Integer sum;
1084 switch (type) {
1085 case HASH_TABLE: {
1086 sum = (Integer) publisherInstance.readOther(Publisher.class,
1087 getQueryParamHashtable(param));
1088 break;
1090 case LIST: {
1091 sum = (Integer) publisherInstance.readOther(Publisher.class,
1092 getQueryParamList(param));
1093 break;
1095 case VAR_ARGS:
1096 default: {
1097 sum = (Integer) publisherInstance.readOther(Publisher.class,
1098 param);
1099 break;
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
1120 try {
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;
1136 switch (type) {
1137 case HASH_TABLE: {
1138 nonExistingBook = bookInstance.readSingle(Book.class,
1139 getQueryParamHashtable(param));
1140 break;
1142 case LIST: {
1143 nonExistingBook = bookInstance.readSingle(Book.class,
1144 getQueryParamList(param));
1145 break;
1147 case VAR_ARGS:
1148 default: {
1149 nonExistingBook = bookInstance.readSingle(Book.class, param);
1150 break;
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;
1163 switch (type) {
1164 case HASH_TABLE: {
1165 kothaoKeoNeiFromDao = bookInstance.readSingle(Book.class,
1166 getQueryParamHashtable(param));
1167 break;
1169 case LIST: {
1170 kothaoKeoNeiFromDao = bookInstance.readSingle(Book.class,
1171 getQueryParamList(param));
1172 break;
1174 case VAR_ARGS:
1175 default: {
1176 kothaoKeoNeiFromDao = bookInstance.readSingle(Book.class,
1177 param);
1178 break;
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;
1191 switch (type) {
1192 case HASH_TABLE: {
1193 annoProkashFromDao = publisherInstance.readSingle(
1194 Publisher.class, getQueryParamHashtable(param));
1195 break;
1197 case LIST: {
1198 annoProkashFromDao = publisherInstance.readSingle(
1199 Publisher.class, getQueryParamList(param));
1200 break;
1202 case VAR_ARGS:
1203 default: {
1204 annoProkashFromDao = publisherInstance.readSingle(
1205 Publisher.class, param);
1206 break;
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,
1220 param);
1221 switch (type) {
1222 case HASH_TABLE: {
1223 humayunAhmedFromDao =
1224 authorInstance.readSingle(Author.class,
1225 getQueryParamHashtable(param));
1226 break;
1228 case LIST: {
1229 humayunAhmedFromDao =
1230 authorInstance.readSingle(Author.class,
1231 getQueryParamList(param));
1232 break;
1234 case VAR_ARGS:
1235 default: {
1236 humayunAhmedFromDao =
1237 authorInstance.readSingle(Author.class, param);
1238 break;
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;
1256 switch (type) {
1257 case HASH_TABLE: {
1258 webDbAppFromDao = bookInstance.readSingle(Book.class,
1259 getQueryParamHashtable(strParam));
1260 break;
1262 case LIST: {
1263 webDbAppFromDao = bookInstance.readSingle(Book.class,
1264 getQueryParamList(strParam));
1265 break;
1267 case VAR_ARGS:
1268 default: {
1269 webDbAppFromDao = bookInstance.readSingle(Book.class,
1270 strParam);
1271 break;
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),
1282 MatchMode.START);
1283 Author brettFromDao;
1284 switch (type) {
1285 case HASH_TABLE: {
1286 brettFromDao = authorInstance.readSingle(Author.class,
1287 getQueryParamHashtable(strParam));
1288 break;
1290 case LIST: {
1291 brettFromDao = authorInstance.readSingle(Author.class,
1292 getQueryParamList(strParam));
1293 break;
1295 case VAR_ARGS:
1296 default: {
1297 brettFromDao = authorInstance.readSingle(Author.class,
1298 strParam);
1299 break;
1302 assertAuthor(brett, brettFromDao, authorNameToIdMap);
1303 Author anwar = getKaziAnowarHossain();
1304 strParam = getNameQueryParam(anwar.getName().substring(4),
1305 MatchMode.END);
1306 Author anwarFromDao;
1307 switch (type) {
1308 case HASH_TABLE: {
1309 anwarFromDao = authorInstance.readSingle(Author.class,
1310 getQueryParamHashtable(strParam));
1311 break;
1313 case LIST: {
1314 anwarFromDao = authorInstance.readSingle(Author.class,
1315 getQueryParamList(strParam));
1316 break;
1318 case VAR_ARGS:
1319 default: {
1320 anwarFromDao = authorInstance.readSingle(Author.class,
1321 strParam);
1322 break;
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.
1333 getName().
1334 length() - 2), MatchMode.ANYWHERE);
1335 Publisher shebaFromDao;
1336 switch (type) {
1337 case HASH_TABLE: {
1338 shebaFromDao = publisherInstance.readSingle(Publisher.class,
1339 getQueryParamHashtable(strParam));
1340 break;
1342 case LIST: {
1343 shebaFromDao = publisherInstance.readSingle(Publisher.class,
1344 getQueryParamList(strParam));
1345 break;
1347 case VAR_ARGS:
1348 default: {
1349 shebaFromDao = publisherInstance.readSingle(Publisher.class,
1350 strParam);
1351 break;
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();
1363 strParam =
1364 getNameQueryParam(hughWilliams.getName(), MatchMode.EXACT);
1365 QueryParameter<Void> authorParam = getAuthorNestedParam(strParam);
1366 Book webDbAppFromDao;
1367 switch (type) {
1368 case HASH_TABLE: {
1369 webDbAppFromDao = bookInstance.readSingle(Book.class,
1370 getQueryParamHashtable(authorParam));
1371 break;
1373 case LIST: {
1374 webDbAppFromDao = bookInstance.readSingle(Book.class,
1375 getQueryParamList(authorParam));
1376 break;
1378 case VAR_ARGS:
1379 default: {
1380 webDbAppFromDao = bookInstance.readSingle(Book.class,
1381 authorParam);
1382 break;
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();
1409 List others;
1410 switch (type) {
1411 case HASH_TABLE: {
1412 others = bookInstance.readOtherList(Book.class,
1413 getQueryParamHashtable(param, countParam, orderByParam));
1414 break;
1416 case LIST: {
1417 others = bookInstance.readOtherList(Book.class,
1418 getQueryParamList(param, countParam, orderByParam));
1419 break;
1421 case VAR_ARGS:
1422 default: {
1423 others = bookInstance.readOtherList(Book.class, param,
1424 countParam, orderByParam);
1425 break;
1428 int[] pubId = new int[others.size()];
1429 Map<Integer, Integer> bookCount = new HashMap<Integer, Integer>(
1430 others.size());
1431 int i = 0;
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();
1456 List others;
1457 switch (type) {
1458 case HASH_TABLE: {
1459 others = authorInstance.readOtherList(Author.class,
1460 getQueryParamHashtable(nameProjectionParam,
1461 idProjectionParam));
1462 break;
1464 case LIST: {
1465 others =
1466 authorInstance.readOtherList(Author.class,
1467 getQueryParamList(nameProjectionParam, idProjectionParam));
1468 break;
1470 case VAR_ARGS:
1471 default: {
1472 others = authorInstance.readOtherList(Author.class,
1473 nameProjectionParam, idProjectionParam);
1474 break;
1477 assertNameIdPair(others, authorNameToIdMap);
1478 List<QueryParameter<Void>> propsParam = getNameIdPropsParam();
1479 switch (type) {
1480 case HASH_TABLE: {
1481 others = authorInstance.readOtherList(Author.class,
1482 getQueryParamHashtable(propsParam.toArray(
1483 new QueryParameter[0])));
1484 break;
1486 case LIST: {
1487 others = authorInstance.readOtherList(Author.class,
1488 getQueryParamList(propsParam.toArray(
1489 new QueryParameter[0])));
1490 break;
1492 case VAR_ARGS:
1493 others = authorInstance.readOtherList(Author.class,
1494 propsParam.toArray(new QueryParameter[0]));
1495 default: {
1496 break;
1499 assertNameIdPair(others, authorNameToIdMap);
1502 * Test distinct property projection
1505 QueryParameter<Void> distinctNumOfEmployeeProjectionParam =
1506 getDistinctNumOfEmployeeParam();
1507 List<Integer> nums;
1508 switch (type) {
1509 case HASH_TABLE: {
1510 nums = (List<Integer>) publisherInstance.readOtherList(
1511 Publisher.class, getQueryParamHashtable(
1512 distinctNumOfEmployeeProjectionParam));
1513 break;
1515 case LIST: {
1516 nums = publisherInstance.<Integer>readOtherList(
1517 Publisher.class, getQueryParamList(
1518 distinctNumOfEmployeeProjectionParam));
1519 break;
1521 case VAR_ARGS:
1522 default: {
1523 nums = publisherInstance.<Integer>readOtherList(
1524 Publisher.class, distinctNumOfEmployeeProjectionParam);
1525 break;
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().
1559 doubleValue());
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,
1596 Book searchedBook,
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).
1610 getId());
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(
1620 index).getId());
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) {
1666 int index;
1667 for (index = 0; index < bookList.size();
1668 ++index) {
1669 Book book = bookList.get(index);
1670 if (searchedBook.getName().equals(book.getName())) {
1671 break;
1674 return index;
1677 private QueryParameter<Void> getCountDistinctNumOfEmployeeParam() {
1678 return QueryParameterFactory.getDistinctElementCountParam(
1679 "numOfEmployees");
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(
1696 "numOfEmployees");
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);
1714 return result;
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)) {
1756 int i = 1;
1757 while (table.containsKey(new StringBuilder(paramName).append(
1758 i).toString())) {
1759 i++;
1761 paramName = new StringBuilder(paramName).append(i).toString();
1763 table.put(paramName, parameter);
1765 return table;
1768 private List<QueryParameter> getQueryParamList(QueryParameter... params) {
1769 ArrayList<QueryParameter> result = new ArrayList<QueryParameter>();
1770 Collections.addAll(result, params);
1771 return result;
1774 private QueryParameter<Integer> getIdQueryParam(int id) {
1775 return QueryParameterFactory.<Integer>getEqualPropertyParam("id", id);
1778 private QueryParameter<String> getNameQueryParam(String name,
1779 MatchMode mode) {
1780 return QueryParameterFactory.<String>getStringLikePropertyParam("name",
1781 name, mode);
1784 private int getPublisherIndex(final List<Publisher> publisherList,
1785 final Publisher searchedBook) {
1786 int index;
1787 for (index = 0; index < publisherList.size();
1788 ++index) {
1789 Publisher publisher = publisherList.get(index);
1790 if (searchedBook.getName().equals(publisher.getName())) {
1791 break;
1794 return index;
1797 private int getAuthorIndex(final List<Author> authorList,
1798 final Author searchedBook) {
1799 int index;
1800 for (index = 0; index < authorList.size();
1801 ++index) {
1802 Author author = authorList.get(index);
1803 if (searchedBook.getName().equals(author.getName())) {
1804 break;
1807 return index;
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,
1829 brettMcLaugblin);
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 =
1859 new Publisher();
1860 publisher.setEstablishedDate(establishDate);
1861 publisher.setNumOfEmployees(numOfEmployees);
1862 publisher.setName(name);
1863 return publisher;
1866 private Author getAuthor(final String name,
1867 final Date birthDate) {
1868 Author author =
1869 new Author();
1870 author.setName(name);
1871 author.setBirthDate(birthDate);
1872 return author;
1875 private Book getBook(final Publisher publisher,
1876 final String bookName,
1877 final Date publishDate,
1878 final String isbn,
1879 final Integer quantityInStock,
1880 final Double price,
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);
1892 book.setIsbn(isbn);
1893 book.setPrice(price);
1894 book.setQuantityInStock(quantityInStock);
1895 return book;
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);
1903 return instance;
1906 private int getTotalNumOfEmployeesFromPubs() {
1907 Publisher[] publishers = getAllPublishers();
1908 int total = 0;
1909 for (Publisher publisher : publishers) {
1910 total += publisher.getNumOfEmployees();
1912 return total;
1915 private QueryParameter<Void> getTotalNumOfEmployeesParam() {
1916 return QueryParameterFactory.getElementSumParam("numOfEmployees");
1919 private Book getWebDbApp(Publisher oReilly,
1920 Author davidLane,
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) {
1933 * Map Books
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);
1948 * Map Publishers
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);
1960 * Map Authors
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());
1983 return max;
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());
1992 return min;
1995 private String getPropertyName(
1996 QueryParameter param) {
1997 String propertyName;
1999 if (param instanceof QueryParameterWithPropertyName) {
2000 propertyName =
2001 ((QueryParameterWithPropertyName) param).getPropertyName();
2003 else {
2004 propertyName = null;
2006 if (propertyName == null) {
2007 propertyName = "";
2010 return propertyName;
2013 private enum MethodInvocationType {
2015 VAR_ARGS, HASH_TABLE, LIST;