You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IssuesTest.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright 2012 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.tests;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNotNull;
  20. import static org.junit.Assert.assertTrue;
  21. import java.util.List;
  22. import org.bouncycastle.util.Arrays;
  23. import org.eclipse.jgit.lib.Repository;
  24. import org.junit.Test;
  25. import com.gitblit.LuceneExecutor;
  26. import com.gitblit.models.IssueModel;
  27. import com.gitblit.models.IssueModel.Attachment;
  28. import com.gitblit.models.IssueModel.Change;
  29. import com.gitblit.models.IssueModel.Field;
  30. import com.gitblit.models.IssueModel.Priority;
  31. import com.gitblit.models.IssueModel.Status;
  32. import com.gitblit.models.SearchResult;
  33. import com.gitblit.utils.FileUtils;
  34. import com.gitblit.utils.IssueUtils;
  35. import com.gitblit.utils.IssueUtils.IssueFilter;
  36. /**
  37. * Tests the mechanics of distributed issue management on the gb-issues branch.
  38. *
  39. * @author James Moger
  40. *
  41. */
  42. public class IssuesTest {
  43. @Test
  44. public void testLifecycle() throws Exception {
  45. Repository repository = GitBlitSuite.getIssuesTestRepository();
  46. String name = FileUtils.getRelativePath(GitBlitSuite.REPOSITORIES, repository.getDirectory());
  47. // create and insert an issue
  48. Change c1 = newChange("testCreation() " + Long.toHexString(System.currentTimeMillis()));
  49. IssueModel issue = IssueUtils.createIssue(repository, c1);
  50. assertNotNull(issue.id);
  51. // retrieve issue and compare
  52. IssueModel constructed = IssueUtils.getIssue(repository, issue.id);
  53. compare(issue, constructed);
  54. assertEquals(1, constructed.changes.size());
  55. // C1: create the issue
  56. c1 = newChange("testUpdates() " + Long.toHexString(System.currentTimeMillis()));
  57. issue = IssueUtils.createIssue(repository, c1);
  58. assertNotNull(issue.id);
  59. constructed = IssueUtils.getIssue(repository, issue.id);
  60. compare(issue, constructed);
  61. assertEquals(1, constructed.changes.size());
  62. // C2: set owner
  63. Change c2 = new Change("C2");
  64. c2.comment("I'll fix this");
  65. c2.setField(Field.Owner, c2.author);
  66. assertTrue(IssueUtils.updateIssue(repository, issue.id, c2));
  67. constructed = IssueUtils.getIssue(repository, issue.id);
  68. assertEquals(2, constructed.changes.size());
  69. assertEquals(c2.author, constructed.owner);
  70. // C3: add a note
  71. Change c3 = new Change("C3");
  72. c3.comment("yeah, this is working");
  73. assertTrue(IssueUtils.updateIssue(repository, issue.id, c3));
  74. constructed = IssueUtils.getIssue(repository, issue.id);
  75. assertEquals(3, constructed.changes.size());
  76. // C4: add attachment
  77. Change c4 = new Change("C4");
  78. Attachment a = newAttachment();
  79. c4.addAttachment(a);
  80. assertTrue(IssueUtils.updateIssue(repository, issue.id, c4));
  81. Attachment a1 = IssueUtils.getIssueAttachment(repository, issue.id, a.name);
  82. assertEquals(a.content.length, a1.content.length);
  83. assertTrue(Arrays.areEqual(a.content, a1.content));
  84. // C5: close the issue
  85. Change c5 = new Change("C5");
  86. c5.comment("closing issue");
  87. c5.setField(Field.Status, Status.Fixed);
  88. assertTrue(IssueUtils.updateIssue(repository, issue.id, c5));
  89. // retrieve issue again
  90. constructed = IssueUtils.getIssue(repository, issue.id);
  91. assertEquals(5, constructed.changes.size());
  92. assertTrue(constructed.status.isClosed());
  93. List<IssueModel> allIssues = IssueUtils.getIssues(repository, null);
  94. List<IssueModel> openIssues = IssueUtils.getIssues(repository, new IssueFilter() {
  95. @Override
  96. public boolean accept(IssueModel issue) {
  97. return !issue.status.isClosed();
  98. }
  99. });
  100. List<IssueModel> closedIssues = IssueUtils.getIssues(repository, new IssueFilter() {
  101. @Override
  102. public boolean accept(IssueModel issue) {
  103. return issue.status.isClosed();
  104. }
  105. });
  106. assertTrue(allIssues.size() > 0);
  107. assertEquals(1, openIssues.size());
  108. assertEquals(1, closedIssues.size());
  109. // build a new Lucene index
  110. LuceneExecutor lucene = new LuceneExecutor(null, GitBlitSuite.REPOSITORIES);
  111. lucene.deleteIndex(name);
  112. for (IssueModel anIssue : allIssues) {
  113. lucene.index(name, anIssue);
  114. }
  115. List<SearchResult> hits = lucene.search("working", 1, 10, name);
  116. assertTrue(hits.size() == 1);
  117. // reindex an issue
  118. issue = allIssues.get(0);
  119. Change change = new Change("reindex");
  120. change.comment("this is a test of reindexing an issue");
  121. IssueUtils.updateIssue(repository, issue.id, change);
  122. issue = IssueUtils.getIssue(repository, issue.id);
  123. lucene.index(name, issue);
  124. hits = lucene.search("working", 1, 10, name);
  125. assertTrue(hits.size() == 1);
  126. // delete all issues
  127. for (IssueModel anIssue : allIssues) {
  128. assertTrue(IssueUtils.deleteIssue(repository, anIssue.id, "D"));
  129. }
  130. lucene.close();
  131. repository.close();
  132. }
  133. @Test
  134. public void testChangeComment() throws Exception {
  135. Repository repository = GitBlitSuite.getIssuesTestRepository();
  136. // C1: create the issue
  137. Change c1 = newChange("testChangeComment() " + Long.toHexString(System.currentTimeMillis()));
  138. IssueModel issue = IssueUtils.createIssue(repository, c1);
  139. assertNotNull(issue.id);
  140. assertTrue(issue.changes.get(0).hasComment());
  141. assertTrue(IssueUtils.changeComment(repository, issue, c1, "E1", "I changed the comment"));
  142. issue = IssueUtils.getIssue(repository, issue.id);
  143. assertTrue(issue.changes.get(0).hasComment());
  144. assertEquals("I changed the comment", issue.changes.get(0).comment.text);
  145. assertTrue(IssueUtils.deleteIssue(repository, issue.id, "D"));
  146. repository.close();
  147. }
  148. @Test
  149. public void testDeleteComment() throws Exception {
  150. Repository repository = GitBlitSuite.getIssuesTestRepository();
  151. // C1: create the issue
  152. Change c1 = newChange("testDeleteComment() " + Long.toHexString(System.currentTimeMillis()));
  153. IssueModel issue = IssueUtils.createIssue(repository, c1);
  154. assertNotNull(issue.id);
  155. assertTrue(issue.changes.get(0).hasComment());
  156. assertTrue(IssueUtils.deleteComment(repository, issue, c1, "D1"));
  157. issue = IssueUtils.getIssue(repository, issue.id);
  158. assertEquals(1, issue.changes.size());
  159. assertFalse(issue.changes.get(0).hasComment());
  160. issue = IssueUtils.getIssue(repository, issue.id, false);
  161. assertEquals(2, issue.changes.size());
  162. assertTrue(issue.changes.get(0).hasComment());
  163. assertFalse(issue.changes.get(1).hasComment());
  164. assertTrue(IssueUtils.deleteIssue(repository, issue.id, "D"));
  165. repository.close();
  166. }
  167. private Change newChange(String summary) {
  168. Change change = new Change("C1");
  169. change.setField(Field.Summary, summary);
  170. change.setField(Field.Description, "this is my description");
  171. change.setField(Field.Priority, Priority.High);
  172. change.setField(Field.Labels, "helpdesk");
  173. change.comment("my comment");
  174. return change;
  175. }
  176. private Attachment newAttachment() {
  177. Attachment attachment = new Attachment(Long.toHexString(System.currentTimeMillis())
  178. + ".txt");
  179. attachment.content = new byte[] { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  180. 0x4a };
  181. return attachment;
  182. }
  183. private void compare(IssueModel issue, IssueModel constructed) {
  184. assertEquals(issue.id, constructed.id);
  185. assertEquals(issue.reporter, constructed.reporter);
  186. assertEquals(issue.owner, constructed.owner);
  187. assertEquals(issue.summary, constructed.summary);
  188. assertEquals(issue.description, constructed.description);
  189. assertEquals(issue.created, constructed.created);
  190. assertTrue(issue.hasLabel("helpdesk"));
  191. }
  192. }