Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TagCommandTest.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (C) 2010, Chris Aniszczyk <caniszczyk@gmail.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.fail;
  46. import java.io.IOException;
  47. import java.util.List;
  48. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  49. import org.eclipse.jgit.api.errors.InvalidTagNameException;
  50. import org.eclipse.jgit.api.errors.JGitInternalException;
  51. import org.eclipse.jgit.api.errors.NoHeadException;
  52. import org.eclipse.jgit.api.errors.NoMessageException;
  53. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  54. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  55. import org.eclipse.jgit.errors.MissingObjectException;
  56. import org.eclipse.jgit.errors.UnmergedPathException;
  57. import org.eclipse.jgit.lib.Ref;
  58. import org.eclipse.jgit.lib.Repository;
  59. import org.eclipse.jgit.lib.RepositoryTestCase;
  60. import org.eclipse.jgit.revwalk.RevCommit;
  61. import org.eclipse.jgit.revwalk.RevWalk;
  62. import org.junit.Test;
  63. public class TagCommandTest extends RepositoryTestCase {
  64. @Test
  65. public void testTaggingOnHead() throws NoHeadException, NoMessageException,
  66. ConcurrentRefUpdateException, JGitInternalException,
  67. WrongRepositoryStateException, InvalidTagNameException,
  68. MissingObjectException, IncorrectObjectTypeException, IOException {
  69. Git git = new Git(db);
  70. RevCommit commit = git.commit().setMessage("initial commit").call();
  71. Ref tagRef = git.tag().setName("tag").call();
  72. assertEquals(commit.getId(), db.peel(tagRef).getPeeledObjectId());
  73. RevWalk walk = new RevWalk(db);
  74. assertEquals("tag", walk.parseTag(tagRef.getObjectId()).getTagName());
  75. }
  76. @Test
  77. public void testTagging() throws NoHeadException, NoMessageException,
  78. UnmergedPathException, ConcurrentRefUpdateException,
  79. JGitInternalException, WrongRepositoryStateException,
  80. InvalidTagNameException {
  81. Git git = new Git(db);
  82. git.commit().setMessage("initial commit").call();
  83. RevCommit commit = git.commit().setMessage("second commit").call();
  84. git.commit().setMessage("third commit").call();
  85. Ref tagRef = git.tag().setObjectId(commit).setName("tag").call();
  86. assertEquals(commit.getId(), db.peel(tagRef).getPeeledObjectId());
  87. }
  88. @Test
  89. public void testEmptyTagName() throws NoHeadException, NoMessageException,
  90. UnmergedPathException, ConcurrentRefUpdateException,
  91. JGitInternalException, WrongRepositoryStateException {
  92. Git git = new Git(db);
  93. git.commit().setMessage("initial commit").call();
  94. try {
  95. // forget to tag name
  96. git.tag().setMessage("some message").call();
  97. fail("We should have failed without a tag name");
  98. } catch (InvalidTagNameException e) {
  99. // should hit here
  100. }
  101. }
  102. @Test
  103. public void testInvalidTagName() throws NoHeadException,
  104. NoMessageException, UnmergedPathException,
  105. ConcurrentRefUpdateException, JGitInternalException,
  106. WrongRepositoryStateException {
  107. Git git = new Git(db);
  108. git.commit().setMessage("initial commit").call();
  109. try {
  110. git.tag().setName("bad~tag~name").setMessage("some message").call();
  111. fail("We should have failed due to a bad tag name");
  112. } catch (InvalidTagNameException e) {
  113. // should hit here
  114. }
  115. }
  116. @Test
  117. public void testFailureOnSignedTags() throws NoHeadException,
  118. NoMessageException, UnmergedPathException,
  119. ConcurrentRefUpdateException, JGitInternalException,
  120. WrongRepositoryStateException, InvalidTagNameException {
  121. Git git = new Git(db);
  122. git.commit().setMessage("initial commit").call();
  123. try {
  124. git.tag().setSigned(true).setName("tag").call();
  125. fail("We should have failed with an UnsupportedOperationException due to signed tag");
  126. } catch (UnsupportedOperationException e) {
  127. // should hit here
  128. }
  129. }
  130. @Test
  131. public void testDelete() throws Exception {
  132. Git git = new Git(db);
  133. git.commit().setMessage("initial commit").call();
  134. Ref tagRef = git.tag().setName("tag").call();
  135. assertEquals(1, db.getTags().size());
  136. List<String> deleted = git.tagDelete().setTags(tagRef.getName())
  137. .call();
  138. assertEquals(1, deleted.size());
  139. assertEquals(tagRef.getName(), deleted.get(0));
  140. assertEquals(0, db.getTags().size());
  141. Ref tagRef1 = git.tag().setName("tag1").call();
  142. Ref tagRef2 = git.tag().setName("tag2").call();
  143. assertEquals(2, db.getTags().size());
  144. deleted = git.tagDelete().setTags(tagRef1.getName(), tagRef2.getName())
  145. .call();
  146. assertEquals(2, deleted.size());
  147. assertEquals(0, db.getTags().size());
  148. }
  149. @Test
  150. public void testDeleteFullName() throws Exception {
  151. Git git = new Git(db);
  152. git.commit().setMessage("initial commit").call();
  153. Ref tagRef = git.tag().setName("tag").call();
  154. assertEquals(1, db.getTags().size());
  155. List<String> deleted = git.tagDelete()
  156. .setTags(Repository.shortenRefName(tagRef.getName())).call();
  157. assertEquals(1, deleted.size());
  158. assertEquals(tagRef.getName(), deleted.get(0));
  159. assertEquals(0, db.getTags().size());
  160. }
  161. @Test
  162. public void testDeleteEmptyTagNames() throws Exception {
  163. Git git = new Git(db);
  164. git.commit().setMessage("initial commit").call();
  165. List<String> deleted = git.tagDelete().setTags().call();
  166. assertEquals(0, deleted.size());
  167. }
  168. @Test
  169. public void testDeleteNonExisting() throws Exception {
  170. Git git = new Git(db);
  171. git.commit().setMessage("initial commit").call();
  172. List<String> deleted = git.tagDelete().setTags("tag").call();
  173. assertEquals(0, deleted.size());
  174. }
  175. @Test
  176. public void testDeleteBadName() throws Exception {
  177. Git git = new Git(db);
  178. git.commit().setMessage("initial commit").call();
  179. List<String> deleted = git.tagDelete().setTags("bad~tag~name")
  180. .call();
  181. assertEquals(0, deleted.size());
  182. }
  183. @Test
  184. public void testShouldNotBlowUpIfThereAreNoTagsInRepository()
  185. throws Exception {
  186. Git git = new Git(db);
  187. git.add().addFilepattern("*").call();
  188. git.commit().setMessage("initial commit").call();
  189. List<Ref> list = git.tagList().call();
  190. assertEquals(0, list.size());
  191. }
  192. @Test
  193. public void testShouldNotBlowUpIfThereAreNoCommitsInRepository()
  194. throws Exception {
  195. Git git = new Git(db);
  196. List<Ref> list = git.tagList().call();
  197. assertEquals(0, list.size());
  198. }
  199. @Test
  200. public void testListAllTagsInRepositoryInOrder() throws Exception {
  201. Git git = new Git(db);
  202. git.add().addFilepattern("*").call();
  203. git.commit().setMessage("initial commit").call();
  204. git.tag().setName("v3").call();
  205. git.tag().setName("v2").call();
  206. git.tag().setName("v10").call();
  207. List<Ref> list = git.tagList().call();
  208. assertEquals(3, list.size());
  209. assertEquals("refs/tags/v10", list.get(0).getName());
  210. assertEquals("refs/tags/v2", list.get(1).getName());
  211. assertEquals("refs/tags/v3", list.get(2).getName());
  212. }
  213. }