PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tita-issuetracker-integration/src/test/java/at/ac/tuwien/ifs/tita/issuetracker/mantis/base/MantisBaseTest.java

https://github.com/tita/tita
Java | 265 lines | 129 code | 26 blank | 110 comment | 2 complexity | f6e0bd0189bee09a8ae445958eb0b0f3 MD5 | raw file
  1. /**
  2. Copyright 2009 TiTA Project, Vienna University of Technology
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE\-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package at.ac.tuwien.ifs.tita.issuetracker.mantis.base;
  14. import static org.junit.Assert.assertTrue;
  15. import java.net.MalformedURLException;
  16. import java.net.URL;
  17. import org.junit.Before;
  18. import org.mantisbt.connect.AccessLevel;
  19. import org.mantisbt.connect.MCException;
  20. import org.mantisbt.connect.axis.MCSession;
  21. import org.mantisbt.connect.model.IIssue;
  22. import org.mantisbt.connect.model.INote;
  23. import org.mantisbt.connect.model.IProject;
  24. import org.mantisbt.connect.model.Issue;
  25. import org.mantisbt.connect.model.MCAttribute;
  26. import org.mantisbt.connect.model.Note;
  27. import org.mantisbt.connect.model.Project;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import at.ac.tuwien.ifs.tita.entity.IssueTrackerLogin;
  31. import at.ac.tuwien.ifs.tita.entity.conv.IssueTracker;
  32. /**
  33. * Base class for all local mantis tests (connecting, etc.).
  34. *
  35. * @author herbert
  36. *
  37. */
  38. public class MantisBaseTest {
  39. protected MCSession session;
  40. protected final IssueTrackerLogin defaultLogin = new IssueTrackerLogin("administrator",
  41. "root", new IssueTracker(1L, "test-mantis", "http://localhost/mantisbt-1.1.8"),null);
  42. private final String url = this.defaultLogin.getIssueTracker().getUrl() + "/api/soap/mantisconnect.php";
  43. private final String user = this.defaultLogin.getUserName();
  44. private final String pwd = this.defaultLogin.getPassword();
  45. private Long startTime;
  46. private Long endTime;
  47. private String performanceOutput = "";
  48. private final Logger log = LoggerFactory.getLogger(MantisBaseTest.class);
  49. /**
  50. * Connects to Mantis-Server.
  51. */
  52. @Before
  53. public void setUp() {
  54. try {
  55. URL u = new URL(this.url);
  56. this.session = new MCSession(u, this.user, this.pwd);
  57. } catch (MCException e) {
  58. assertTrue(false);
  59. } catch (MalformedURLException e) {
  60. assertTrue(false);
  61. }
  62. }
  63. /**
  64. * Creates a Project on the Mantis-Server.
  65. *
  66. * @param projectName
  67. * - name of the project
  68. * @param description
  69. * - description of the project
  70. * @param enabled
  71. * - status of the project
  72. * @param viewStatePrivate
  73. * - private or public
  74. * @return id of the created project
  75. * @throws MCException
  76. * - if error occurs, when project is added
  77. */
  78. protected Long createTestProject(String projectName, String description,
  79. Boolean enabled, Boolean viewStatePrivate) throws MCException {
  80. IProject newProject = new Project();
  81. newProject.setName(projectName);
  82. newProject.setAccessLevelMin(AccessLevel.DEVELOPER);
  83. newProject.setDesription(description);
  84. newProject.setEnabled(enabled); // ProjectStatus: Open
  85. newProject.setPrivate(viewStatePrivate); // ViewState:Public
  86. Long id = this.session.addProject(newProject);
  87. this.session.flush();
  88. return id;
  89. }
  90. /**
  91. * Creates a task on the Mantis-Server.
  92. *
  93. * @param description
  94. * - description of the project
  95. * @param summary
  96. * - summary of the project
  97. * @param projectName
  98. * - name of the project of the task
  99. * @return id of the created task
  100. * @throws MCException
  101. * - if error occurs, when task is added
  102. */
  103. protected Long createTestTask(String description, String summary,
  104. String projectName) throws MCException {
  105. IIssue newIssue = new Issue();
  106. newIssue.setDescription(description);
  107. // newIssue.setHandler(new Account(100, "test", "test", "test@test"));
  108. newIssue.setPriority(this.session.getDefaultIssuePriority());
  109. newIssue.setSummary(summary);
  110. newIssue.setSeverity(this.session.getDefaultIssueSeverity());
  111. // newIssue.setReporter(new Account(101, "rep1", "rep1", "rep@rep"));
  112. IProject p = this.session.getProject(projectName);
  113. newIssue.setProject(new MCAttribute(p.getId(), p.getName()));
  114. long id = this.session.addIssue(newIssue);
  115. this.session.flush();
  116. return id;
  117. }
  118. /**
  119. * Creates a comment on the Mantis-Server.
  120. *
  121. * @param text
  122. * - text of the comment
  123. * @param isPrivate
  124. * - if true, comment is private, else public
  125. * @param issueId
  126. * - id of the issue, the comment is linked to
  127. * @return id of the created comment
  128. * @throws MCException
  129. * - MCException - if error occurs, when comment is added
  130. */
  131. protected Long createTestComment(String text, boolean isPrivate,
  132. long issueId) throws MCException {
  133. INote newNote = new Note();
  134. newNote.setText(text);
  135. newNote.setPrivate(isPrivate);
  136. Long id = this.session.addNote(issueId, newNote);
  137. this.session.flush();
  138. return id;
  139. }
  140. /**
  141. * Deletes project on the Mantis-Server.
  142. *
  143. * @param projectName
  144. * - name of the project to delete
  145. */
  146. protected void deleteTestProject(String projectName) {
  147. IProject old;
  148. try {
  149. old = this.session.getProject(projectName);
  150. if (old != null) {
  151. this.session.deleteProject(old.getId());
  152. this.session.flush();
  153. }
  154. } catch (MCException e) {
  155. assertTrue(false);
  156. }
  157. }
  158. /**
  159. * Deletes task on the Mantis-Server.
  160. *
  161. * @param taskId
  162. * - id of the task to delete
  163. */
  164. protected void deleteTestTask(long taskId) {
  165. try {
  166. this.session.deleteIssue(taskId);
  167. this.session.flush();
  168. } catch (MCException e) {
  169. assertTrue(false);
  170. }
  171. }
  172. /**
  173. * Deletes comment on the Mantis-Server.
  174. *
  175. * @param commentId
  176. * - id of the comment to delete
  177. */
  178. protected void deleteTestComment(long commentId) {
  179. try {
  180. this.session.deleteNote(commentId);
  181. this.session.flush();
  182. } catch (MCException e) {
  183. assertTrue(false);
  184. }
  185. }
  186. /**
  187. * Starts the timer.
  188. *
  189. * @param description
  190. * - It describes the measured situation.
  191. */
  192. protected void startTimer(String description) {
  193. this.log.debug(description);
  194. this.performanceOutput = "";
  195. this.performanceOutput += description + "\n";
  196. this.startTime = System.currentTimeMillis();
  197. }
  198. /**
  199. * Stops the timer.
  200. *
  201. * @param description
  202. * - It describes the measured situation.
  203. */
  204. protected void stopTimer(String description) {
  205. this.log.debug(description);
  206. this.performanceOutput += description + "\n";
  207. this.endTime = System.currentTimeMillis();
  208. showDuration();
  209. }
  210. /**
  211. * Shows the duration that was measured.
  212. */
  213. private void showDuration() {
  214. // CHECKSTYLE:OFF
  215. this.log.debug("Duration:" + (getEndTime() - getStartTime()) / 1000 + " sec.");
  216. this.performanceOutput += "Duration:" + (getEndTime() - getStartTime()) / 1000 + " sec." + "\n";
  217. // CHECKSTYLE:ON
  218. }
  219. /**
  220. * Returns the startTime.
  221. *
  222. * @return startTime of the measured activity.
  223. */
  224. private Long getStartTime() {
  225. return this.startTime;
  226. }
  227. /**
  228. * Returns the endTime.
  229. *
  230. * @return endTime of the measured activity.
  231. */
  232. private Long getEndTime() {
  233. return this.endTime;
  234. }
  235. protected String getPerformanceOutput() {
  236. return this.performanceOutput;
  237. }
  238. }