PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/test/java/hudson/plugins/tfs/model/ProjectTest.java

https://gitlab.com/vectorci/tfs-plugin
Java | 269 lines | 210 code | 57 blank | 2 comment | 1 complexity | ff02b54fd14be4088dd217e3293d4054 MD5 | raw file
  1. package hudson.plugins.tfs.model;
  2. import static org.junit.Assert.*;
  3. import static org.mockito.Matchers.*;
  4. import static org.mockito.Mockito.*;
  5. import java.io.IOException;
  6. import java.io.Reader;
  7. import java.io.StringReader;
  8. import java.net.URISyntaxException;
  9. import java.util.Arrays;
  10. import java.util.Calendar;
  11. import java.util.Collections;
  12. import java.util.Date;
  13. import java.util.List;
  14. import com.microsoft.tfs.core.clients.versioncontrol.specs.version.ChangesetVersionSpec;
  15. import hudson.model.User;
  16. import hudson.plugins.tfs.IntegrationTestHelper;
  17. import hudson.plugins.tfs.IntegrationTests;
  18. import hudson.plugins.tfs.SwedishLocaleTestCase;
  19. import hudson.plugins.tfs.Util;
  20. import hudson.plugins.tfs.commands.RemoteChangesetVersionCommand;
  21. import hudson.plugins.tfs.model.ChangeSet.Item;
  22. import hudson.plugins.tfs.util.MaskedArgumentListBuilder;
  23. import hudson.tasks.Mailer;
  24. import org.junit.Assert;
  25. import org.junit.Test;
  26. import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Change;
  27. import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.ChangeType;
  28. import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Changeset;
  29. import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.ItemType;
  30. import org.junit.experimental.categories.Category;
  31. public class ProjectTest extends SwedishLocaleTestCase {
  32. private com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Change createServerChange() {
  33. final com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Item serverItem
  34. = new com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Item();
  35. serverItem.setItemType(ItemType.FILE);
  36. serverItem.setServerItem("$/tfsandbox");
  37. final com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Change serverChange
  38. = new Change(serverItem, ChangeType.ADD, null);
  39. return serverChange;
  40. }
  41. @Test
  42. public void assertConvertServerChange() throws Exception {
  43. final com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Change serverChange = createServerChange();
  44. final Item actual = Project.convertServerChange(serverChange);
  45. assertEquals("$/tfsandbox", actual.getPath());
  46. assertEquals("add", actual.getAction());
  47. }
  48. private UserLookup createMockUserLookup(String accountName, String displayName, String emailAddress) {
  49. UserLookup userLookup = mock(UserLookup.class);
  50. User user = mock(User.class);
  51. // this portion stolen from User.get()
  52. final String id = accountName.replace('\\', '_').replace('/', '_').replace('<','_')
  53. .replace('>','_'); // 4 replace() still faster than regex
  54. // end stolen portion
  55. when(user.getId()).thenReturn(id);
  56. when(user.getDisplayName()).thenReturn(displayName);
  57. when(user.getProperty(Mailer.UserProperty.class)).thenReturn(new Mailer.UserProperty(emailAddress));
  58. when(userLookup.find(accountName)).thenReturn(user);
  59. return userLookup;
  60. }
  61. @Test
  62. public void assertConvertServerChangeset() throws Exception {
  63. final com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Change serverChange = createServerChange();
  64. final String comment = "Created team project folder $/tfsandbox via the Team Project Creation Wizard";
  65. final Calendar juneTwentySeventh = Util.getCalendar(2008, 06, 27, 11, 16, 06);
  66. final String userString = "EXAMPLE\\ljenkins";
  67. Changeset serverChangeset = new Changeset(userString, comment, null, null);
  68. serverChangeset.setChangesetID(12472);
  69. serverChangeset.setCommitter(userString);
  70. serverChangeset.setDate(juneTwentySeventh);
  71. final Change[] changes = new Change[] { serverChange };
  72. serverChangeset.setChanges(changes);
  73. final String userDisplayName = "Leeroy Jenkins";
  74. final String userEmailAddress = "leeroy.jenkins@example.com";
  75. final UserLookup userLookup = createMockUserLookup(userString, userDisplayName, userEmailAddress);
  76. hudson.plugins.tfs.model.ChangeSet actual = Project.convertServerChangeset(serverChangeset, userLookup);
  77. final User author = actual.getAuthor();
  78. assertEquals("The version was incorrect", "12472", actual.getVersion());
  79. assertEquals("The author's user ID was incorrect", "EXAMPLE_ljenkins", author.getId());
  80. assertEquals("The author's display name was incorrect", userDisplayName, author.getDisplayName());
  81. final String actualEmailAddress = author.getProperty(Mailer.UserProperty.class).getAddress();
  82. assertEquals("The author's e-mail address was incorrect", userEmailAddress, actualEmailAddress);
  83. assertEquals("The date was incorrect", juneTwentySeventh.getTime(), actual.getDate());
  84. assertEquals("The comment was incorrect", comment, actual.getComment());
  85. Item item = actual.getItems().get(0);
  86. assertEquals("The item path was incorrect", "$/tfsandbox", item.getPath());
  87. assertEquals("The item action was incorrect", "add", item.getAction());
  88. }
  89. @Test
  90. public void findLatestUncloakedChangeset_latestIsUncloaked() {
  91. final List<String> cloakedPaths = Arrays.asList("$/MyProject/A/2", "$/MyProject/B");
  92. final ChangeSet changeSet42 = createChangeSet(42, "$/MyProject/A/foo", "$/MyProject/A/bar");
  93. final ChangeSet changeSet43 = createChangeSet(43, "$/MyProject/A/bar");
  94. final ChangeSet changeSet44 = createChangeSet(44, "$/MyProject/A/foo");
  95. final List<ChangeSet> changeSets = Arrays.asList(changeSet44, changeSet43, changeSet42);
  96. final ChangeSet actual = Project.findLatestUncloakedChangeset(cloakedPaths, changeSets);
  97. Assert.assertEquals("44", actual.getVersion());
  98. }
  99. @Test
  100. public void findLatestUncloakedChangeset_latestIsCloaked() {
  101. final List<String> cloakedPaths = Arrays.asList("$/MyProject/A/2", "$/MyProject/B");
  102. final ChangeSet changeSet42 = createChangeSet(42, "$/MyProject/A/foo", "$/MyProject/A/bar");
  103. final ChangeSet changeSet43 = createChangeSet(43, "$/MyProject/A/bar");
  104. final ChangeSet changeSet44 = createChangeSet(44, "$/MyProject/A/2/foo");
  105. final List<ChangeSet> changeSets = Arrays.asList(changeSet44, changeSet43, changeSet42);
  106. final ChangeSet actual = Project.findLatestUncloakedChangeset(cloakedPaths, changeSets);
  107. Assert.assertEquals("43", actual.getVersion());
  108. }
  109. @Test
  110. public void findLatestUncloakedChangeset_everythingIsCloaked() {
  111. final List<String> cloakedPaths = Arrays.asList("$/MyProject/A/2", "$/MyProject/B");
  112. final ChangeSet changeSet42 = createChangeSet(42, "$/MyProject/A/2/foo", "$/MyProject/B/bar");
  113. final ChangeSet changeSet43 = createChangeSet(43, "$/MyProject/B/bar");
  114. final ChangeSet changeSet44 = createChangeSet(44, "$/MyProject/A/2/foo");
  115. final List<ChangeSet> changeSets = Arrays.asList(changeSet44, changeSet43, changeSet42);
  116. final ChangeSet actual = Project.findLatestUncloakedChangeset(cloakedPaths, changeSets);
  117. Assert.assertEquals(null, actual);
  118. }
  119. private static ChangeSet createChangeSet(final int version, final String... itemPaths) {
  120. final String stringVersion = Integer.toString(version);
  121. final Calendar calendar = Util.getCalendar(2016, 1, 5, 10, version, 0);
  122. final Date date = calendar.getTime();
  123. final ChangeSet result = new ChangeSet(stringVersion, date, "ljenkins", "synthetic for testing");
  124. for (final String itemPath : itemPaths) {
  125. final Item item = new Item(itemPath, "edit");
  126. result.add(item);
  127. }
  128. return result;
  129. }
  130. @Test
  131. public void isChangesetFullyCloaked_nullCloakedPaths() {
  132. final List<String> changesetPaths = Arrays.asList("$/foo", "$/foo/bar.baz");
  133. final boolean actual = Project.isChangesetFullyCloaked(changesetPaths, null);
  134. Assert.assertEquals(false, actual);
  135. }
  136. @Test
  137. public void isChangesetFullyCloaked_independentCloakedPaths() {
  138. final List<String> changesetPaths = Arrays.asList("$/foo", "$/foo/bar.baz");
  139. final List<String> cloakedPaths = Arrays.asList("$/fizz", "$/fizz/FizzBuzz.java");
  140. final boolean actual = Project.isChangesetFullyCloaked(changesetPaths, cloakedPaths);
  141. Assert.assertEquals(false, actual);
  142. }
  143. @Test
  144. public void isChangesetFullyCloaked_caseInsensitiveCloakedPaths() {
  145. final List<String> changesetPaths = Arrays.asList("$/foo/bar/test.baz");
  146. final List<String> cloakedPaths = Arrays.asList("$/fOo/bAr/");
  147. final boolean actual = Project.isChangesetFullyCloaked(changesetPaths, cloakedPaths);
  148. Assert.assertEquals(true, actual);
  149. }
  150. @Test
  151. public void isChangesetFullyCloaked_cloakingChild() {
  152. final List<String> changesetPaths = Arrays.asList("$/foo", "$/foo/bar.baz");
  153. final List<String> cloakedPaths = Collections.singletonList("$/foo/bar.baz");
  154. final boolean actual = Project.isChangesetFullyCloaked(changesetPaths, cloakedPaths);
  155. Assert.assertEquals(false, actual);
  156. }
  157. @Test
  158. public void isChangesetFullyCloaked_partiallyCloakedPaths() {
  159. final List<String> changesetPaths = Arrays.asList("$/foo", "$/bar");
  160. final List<String> cloakedPaths = Collections.singletonList("$/foo");
  161. final boolean actual = Project.isChangesetFullyCloaked(changesetPaths, cloakedPaths);
  162. Assert.assertEquals(false, actual);
  163. }
  164. @Test
  165. public void isChangesetFullyCloaked_fullyCloakedPath() {
  166. final List<String> changesetPaths = Arrays.asList("$/foo", "$/foo/bar.baz");
  167. final List<String> cloakedPaths = Collections.singletonList("$/foo");
  168. final boolean actual = Project.isChangesetFullyCloaked(changesetPaths, cloakedPaths);
  169. Assert.assertEquals(true, actual);
  170. }
  171. @Test
  172. public void isChangesetFullyCloaked_fullyCloakedPaths() {
  173. final List<String> changesetPaths = Collections.singletonList("$/foo/bar.baz");
  174. final List<String> cloakedPaths = Arrays.asList("$/foo", "$/bar");
  175. final boolean actual = Project.isChangesetFullyCloaked(changesetPaths, cloakedPaths);
  176. Assert.assertEquals(true, actual);
  177. }
  178. @Test
  179. public void isChangesetFullyCloaked_manyToMany() {
  180. final List<String> changesetPaths = Arrays.asList("$/foo/bar.baz", "$/bar/foo.baz");
  181. final List<String> cloakedPaths = Arrays.asList("$/foo", "$/bar");
  182. final boolean actual = Project.isChangesetFullyCloaked(changesetPaths, cloakedPaths);
  183. Assert.assertEquals(true, actual);
  184. }
  185. @Category(IntegrationTests.class)
  186. @Test public void getDetailedHistory_singleVersionSpec() throws URISyntaxException, IOException {
  187. final IntegrationTestHelper helper = new IntegrationTestHelper();
  188. final String serverUrl = helper.getServerUrl();
  189. final String userName = helper.getUserName();
  190. final String userPassword = helper.getUserPassword();
  191. final Server server = new Server(null, null, serverUrl, userName, userPassword);
  192. try {
  193. final Project project = new Project(server, "$/FunctionalTests");
  194. final UserLookup userLookup = mock(UserLookup.class);
  195. final User fakeUser = mock(User.class);
  196. when(userLookup.find(isA(String.class))).thenReturn(fakeUser);
  197. project.setUserLookup(userLookup);
  198. final MockableVersionControlClient vcc = server.getVersionControlClient();
  199. final int latestChangesetID = vcc.getLatestChangesetID();
  200. final ChangesetVersionSpec spec = new ChangesetVersionSpec(latestChangesetID);
  201. final String singleVersionSpecString = RemoteChangesetVersionCommand.toString(spec);
  202. final List<ChangeSet> actual = project.getDetailedHistory(singleVersionSpecString);
  203. Assert.assertEquals(1, actual.size());
  204. final ChangeSet changeSet = actual.get(0);
  205. final String latestChangesetString = Integer.toString(latestChangesetID, 10);
  206. Assert.assertEquals(latestChangesetString, changeSet.getVersion());
  207. }
  208. finally {
  209. server.close();
  210. }
  211. }
  212. }