/src/test/java/com/google/ie/business/dao/impl/TagDaoImplTest.java

http://thoughtsite.googlecode.com/ · Java · 136 lines · 93 code · 26 blank · 17 comment · 2 complexity · 9d121712bed4be87b58ebc64558989a7 MD5 · raw file

  1. // Copyright 2009 Google Inc. All Rights Reserved.
  2. /**
  3. *
  4. */
  5. package com.google.ie.business.dao.impl;
  6. import static org.junit.Assert.assertEquals;
  7. import static org.junit.Assert.assertNotNull;
  8. import static org.junit.Assert.assertTrue;
  9. import com.google.ie.business.domain.Tag;
  10. import com.google.ie.dto.RetrievalInfo;
  11. import com.google.ie.test.DatastoreTest;
  12. import org.junit.Before;
  13. import org.junit.Test;
  14. import java.util.ArrayList;
  15. import java.util.Collection;
  16. import java.util.List;
  17. /**
  18. * Test cases for TagDaoImpl class.
  19. *
  20. * @author Charanjeet singh
  21. *
  22. */
  23. public class TagDaoImplTest extends DatastoreTest {
  24. TagDaoImpl tagDaoImpl = null;
  25. /**
  26. */
  27. @Before
  28. public void setUp() {
  29. super.setUp();
  30. if (tagDaoImpl == null) {
  31. tagDaoImpl = new TagDaoImpl();
  32. tagDaoImpl.setPersistenceManagerFactory(pmf);
  33. }
  34. }
  35. /**
  36. * Test method for
  37. * {@link com.google.ie.business.dao.impl.TagDaoImpl#saveTag(com.google.ie.business.domain.Tag)}
  38. * .
  39. */
  40. @Test
  41. public final void saveTag() {
  42. Tag expactedTag = new Tag();
  43. expactedTag.setTitle("TestTag");
  44. Tag actualTag = tagDaoImpl.saveTag(expactedTag);
  45. assertNotNull(actualTag);
  46. assertEquals(expactedTag.getTitle(), actualTag.getTitle());
  47. }
  48. @Test
  49. public final void getAllTags() {
  50. Tag expectedTag1 = new Tag();
  51. expectedTag1.setTitle("TestTag1");
  52. tagDaoImpl.saveTag(expectedTag1);
  53. Tag expectedTag2 = new Tag();
  54. expectedTag2.setTitle("TestTag2");
  55. tagDaoImpl.saveTag(expectedTag2);
  56. assertEquals(2, tagDaoImpl.getAllTags().size());
  57. }
  58. @Test
  59. public final void getTagsByKeys() {
  60. Tag tag = new Tag();
  61. tag.setTitle("testTag");
  62. Tag tag1 = new Tag();
  63. tag1.setTitle("testTag1");
  64. tag = tagDaoImpl.saveTag(tag);
  65. tag1 = tagDaoImpl.saveTag(tag1);
  66. Collection<String> keys = new ArrayList<String>();
  67. keys.add(tag.getKey());
  68. keys.add(tag1.getKey());
  69. assertNotNull(tagDaoImpl.getTagsByKeys(keys));
  70. assertEquals(tag.getKey(),
  71. tagDaoImpl.getTagsByKeys(keys).get(0).getKey());
  72. assertEquals(2,
  73. tagDaoImpl.getTagsByKeys(keys).size());
  74. }
  75. @Test
  76. public void getTagsForTagCloud() {
  77. Tag tag = new Tag();
  78. tag.setTitle("finance");
  79. tagDaoImpl.saveTag(tag);
  80. RetrievalInfo info = new RetrievalInfo();
  81. info.setNoOfRecords(10);
  82. List<Tag> tagList = tagDaoImpl.getTagsForTagCloud(info);
  83. assertNotNull(tagList);
  84. }
  85. @Test
  86. public void getTagsWithSpecificStartString() {
  87. String expectedString = "ab";
  88. Tag tag = new Tag();
  89. tag.setTitle("abstract");
  90. tagDaoImpl.saveTag(tag);
  91. Tag tag2 = new Tag();
  92. tag2.setTitle("bargain");
  93. tagDaoImpl.saveTag(tag2);
  94. Tag tag3 = new Tag();
  95. tag3.setTitle("science");
  96. tagDaoImpl.saveTag(tag3);
  97. Tag tag4 = new Tag();
  98. tag4.setTitle("agriculture");
  99. tagDaoImpl.saveTag(tag4);
  100. RetrievalInfo info = new RetrievalInfo();
  101. info.setOrderType("asc");
  102. info.setOrderBy("title");
  103. info.setNoOfRecords(10);
  104. List<Tag> listOfTags = tagDaoImpl.getTagsWithSpecificStartString(expectedString, info
  105. );
  106. assertNotNull(listOfTags);
  107. String actualString = listOfTags.get(0).getTitle();
  108. assertTrue(actualString.startsWith(expectedString));
  109. }
  110. }