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.

TagCommandTest.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.GitAPIException;
  49. import org.eclipse.jgit.api.errors.InvalidTagNameException;
  50. import org.eclipse.jgit.api.errors.JGitInternalException;
  51. import org.eclipse.jgit.lib.Ref;
  52. import org.eclipse.jgit.lib.Repository;
  53. import org.eclipse.jgit.lib.RepositoryTestCase;
  54. import org.eclipse.jgit.revwalk.RevCommit;
  55. import org.eclipse.jgit.revwalk.RevWalk;
  56. import org.junit.Test;
  57. public class TagCommandTest extends RepositoryTestCase {
  58. @Test
  59. public void testTaggingOnHead() throws GitAPIException, IOException {
  60. Git git = new Git(db);
  61. RevCommit commit = git.commit().setMessage("initial commit").call();
  62. Ref tagRef = git.tag().setName("tag").call();
  63. assertEquals(commit.getId(), db.peel(tagRef).getPeeledObjectId());
  64. RevWalk walk = new RevWalk(db);
  65. assertEquals("tag", walk.parseTag(tagRef.getObjectId()).getTagName());
  66. }
  67. @Test
  68. public void testTagging() throws GitAPIException, JGitInternalException {
  69. Git git = new Git(db);
  70. git.commit().setMessage("initial commit").call();
  71. RevCommit commit = git.commit().setMessage("second commit").call();
  72. git.commit().setMessage("third commit").call();
  73. Ref tagRef = git.tag().setObjectId(commit).setName("tag").call();
  74. assertEquals(commit.getId(), db.peel(tagRef).getPeeledObjectId());
  75. }
  76. @Test
  77. public void testEmptyTagName() throws GitAPIException {
  78. Git git = new Git(db);
  79. git.commit().setMessage("initial commit").call();
  80. try {
  81. // forget to tag name
  82. git.tag().setMessage("some message").call();
  83. fail("We should have failed without a tag name");
  84. } catch (InvalidTagNameException e) {
  85. // should hit here
  86. }
  87. }
  88. @Test
  89. public void testInvalidTagName() throws GitAPIException {
  90. Git git = new Git(db);
  91. git.commit().setMessage("initial commit").call();
  92. try {
  93. git.tag().setName("bad~tag~name").setMessage("some message").call();
  94. fail("We should have failed due to a bad tag name");
  95. } catch (InvalidTagNameException e) {
  96. // should hit here
  97. }
  98. }
  99. @Test
  100. public void testFailureOnSignedTags() throws GitAPIException {
  101. Git git = new Git(db);
  102. git.commit().setMessage("initial commit").call();
  103. try {
  104. git.tag().setSigned(true).setName("tag").call();
  105. fail("We should have failed with an UnsupportedOperationException due to signed tag");
  106. } catch (UnsupportedOperationException e) {
  107. // should hit here
  108. }
  109. }
  110. @Test
  111. public void testDelete() throws Exception {
  112. Git git = new Git(db);
  113. git.commit().setMessage("initial commit").call();
  114. Ref tagRef = git.tag().setName("tag").call();
  115. assertEquals(1, db.getTags().size());
  116. List<String> deleted = git.tagDelete().setTags(tagRef.getName())
  117. .call();
  118. assertEquals(1, deleted.size());
  119. assertEquals(tagRef.getName(), deleted.get(0));
  120. assertEquals(0, db.getTags().size());
  121. Ref tagRef1 = git.tag().setName("tag1").call();
  122. Ref tagRef2 = git.tag().setName("tag2").call();
  123. assertEquals(2, db.getTags().size());
  124. deleted = git.tagDelete().setTags(tagRef1.getName(), tagRef2.getName())
  125. .call();
  126. assertEquals(2, deleted.size());
  127. assertEquals(0, db.getTags().size());
  128. }
  129. @Test
  130. public void testDeleteFullName() throws Exception {
  131. Git git = new Git(db);
  132. git.commit().setMessage("initial commit").call();
  133. Ref tagRef = git.tag().setName("tag").call();
  134. assertEquals(1, db.getTags().size());
  135. List<String> deleted = git.tagDelete()
  136. .setTags(Repository.shortenRefName(tagRef.getName())).call();
  137. assertEquals(1, deleted.size());
  138. assertEquals(tagRef.getName(), deleted.get(0));
  139. assertEquals(0, db.getTags().size());
  140. }
  141. @Test
  142. public void testDeleteEmptyTagNames() throws Exception {
  143. Git git = new Git(db);
  144. git.commit().setMessage("initial commit").call();
  145. List<String> deleted = git.tagDelete().setTags().call();
  146. assertEquals(0, deleted.size());
  147. }
  148. @Test
  149. public void testDeleteNonExisting() throws Exception {
  150. Git git = new Git(db);
  151. git.commit().setMessage("initial commit").call();
  152. List<String> deleted = git.tagDelete().setTags("tag").call();
  153. assertEquals(0, deleted.size());
  154. }
  155. @Test
  156. public void testDeleteBadName() throws Exception {
  157. Git git = new Git(db);
  158. git.commit().setMessage("initial commit").call();
  159. List<String> deleted = git.tagDelete().setTags("bad~tag~name")
  160. .call();
  161. assertEquals(0, deleted.size());
  162. }
  163. @Test
  164. public void testShouldNotBlowUpIfThereAreNoTagsInRepository()
  165. throws Exception {
  166. Git git = new Git(db);
  167. git.add().addFilepattern("*").call();
  168. git.commit().setMessage("initial commit").call();
  169. List<Ref> list = git.tagList().call();
  170. assertEquals(0, list.size());
  171. }
  172. @Test
  173. public void testShouldNotBlowUpIfThereAreNoCommitsInRepository()
  174. throws Exception {
  175. Git git = new Git(db);
  176. List<Ref> list = git.tagList().call();
  177. assertEquals(0, list.size());
  178. }
  179. @Test
  180. public void testListAllTagsInRepositoryInOrder() throws Exception {
  181. Git git = new Git(db);
  182. git.add().addFilepattern("*").call();
  183. git.commit().setMessage("initial commit").call();
  184. git.tag().setName("v3").call();
  185. git.tag().setName("v2").call();
  186. git.tag().setName("v10").call();
  187. List<Ref> list = git.tagList().call();
  188. assertEquals(3, list.size());
  189. assertEquals("refs/tags/v10", list.get(0).getName());
  190. assertEquals("refs/tags/v2", list.get(1).getName());
  191. assertEquals("refs/tags/v3", list.get(2).getName());
  192. }
  193. }