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.

ArchiveCommandTest.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (C) 2014, Shaul Zorea <shaulzorea@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.assertNull;
  46. import java.beans.Statement;
  47. import java.io.IOException;
  48. import java.io.OutputStream;
  49. import java.util.Arrays;
  50. import java.util.Collections;
  51. import java.util.HashMap;
  52. import java.util.List;
  53. import java.util.Map;
  54. import org.eclipse.jgit.api.errors.GitAPIException;
  55. import org.eclipse.jgit.junit.RepositoryTestCase;
  56. import org.eclipse.jgit.lib.FileMode;
  57. import org.eclipse.jgit.lib.ObjectLoader;
  58. import org.eclipse.jgit.revwalk.RevCommit;
  59. import org.eclipse.jgit.util.StringUtils;
  60. import org.junit.After;
  61. import org.junit.Before;
  62. import org.junit.Test;
  63. public class ArchiveCommandTest extends RepositoryTestCase {
  64. private static final String UNEXPECTED_ARCHIVE_SIZE = "Unexpected archive size";
  65. private static final String UNEXPECTED_FILE_CONTENTS = "Unexpected file contents";
  66. private static final String UNEXPECTED_TREE_CONTENTS = "Unexpected tree contents";
  67. private MockFormat format = null;
  68. @Before
  69. public void setup() {
  70. format = new MockFormat();
  71. ArchiveCommand.registerFormat(format.SUFFIXES.get(0), format);
  72. }
  73. @After
  74. public void tearDown() {
  75. ArchiveCommand.unregisterFormat(format.SUFFIXES.get(0));
  76. }
  77. @Test
  78. public void archiveHeadAllFiles() throws IOException, GitAPIException {
  79. try (Git git = new Git(db)) {
  80. writeTrashFile("file_1.txt", "content_1_1");
  81. git.add().addFilepattern("file_1.txt").call();
  82. git.commit().setMessage("create file").call();
  83. writeTrashFile("file_1.txt", "content_1_2");
  84. writeTrashFile("file_2.txt", "content_2_2");
  85. git.add().addFilepattern(".").call();
  86. git.commit().setMessage("updated file").call();
  87. git.archive().setOutputStream(new MockOutputStream())
  88. .setFormat(format.SUFFIXES.get(0))
  89. .setTree(git.getRepository().resolve("HEAD")).call();
  90. assertEquals(UNEXPECTED_ARCHIVE_SIZE, 2, format.size());
  91. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_2", format.getByPath("file_1.txt"));
  92. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath("file_2.txt"));
  93. }
  94. }
  95. @Test
  96. public void archiveHeadSpecificPath() throws IOException, GitAPIException {
  97. try (Git git = new Git(db)) {
  98. writeTrashFile("file_1.txt", "content_1_1");
  99. git.add().addFilepattern("file_1.txt").call();
  100. git.commit().setMessage("create file").call();
  101. writeTrashFile("file_1.txt", "content_1_2");
  102. String expectedFilePath = "some_directory/file_2.txt";
  103. writeTrashFile(expectedFilePath, "content_2_2");
  104. git.add().addFilepattern(".").call();
  105. git.commit().setMessage("updated file").call();
  106. git.archive().setOutputStream(new MockOutputStream())
  107. .setFormat(format.SUFFIXES.get(0))
  108. .setTree(git.getRepository().resolve("HEAD"))
  109. .setPaths(expectedFilePath).call();
  110. assertEquals(UNEXPECTED_ARCHIVE_SIZE, 2, format.size());
  111. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath(expectedFilePath));
  112. assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory"));
  113. }
  114. }
  115. @Test
  116. public void archiveByIdSpecificFile() throws IOException, GitAPIException {
  117. try (Git git = new Git(db)) {
  118. writeTrashFile("file_1.txt", "content_1_1");
  119. git.add().addFilepattern("file_1.txt").call();
  120. RevCommit first = git.commit().setMessage("create file").call();
  121. writeTrashFile("file_1.txt", "content_1_2");
  122. String expectedFilePath = "some_directory/file_2.txt";
  123. writeTrashFile(expectedFilePath, "content_2_2");
  124. git.add().addFilepattern(".").call();
  125. git.commit().setMessage("updated file").call();
  126. Map<String, Object> options = new HashMap<>();
  127. Integer opt = Integer.valueOf(42);
  128. options.put("foo", opt);
  129. MockOutputStream out = new MockOutputStream();
  130. git.archive().setOutputStream(out)
  131. .setFormat(format.SUFFIXES.get(0))
  132. .setFormatOptions(options)
  133. .setTree(first)
  134. .setPaths("file_1.txt").call();
  135. assertEquals(opt.intValue(), out.getFoo());
  136. assertEquals(UNEXPECTED_ARCHIVE_SIZE, 1, format.size());
  137. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_1", format.getByPath("file_1.txt"));
  138. }
  139. }
  140. @Test
  141. public void archiveByDirectoryPath() throws GitAPIException, IOException {
  142. try (Git git = new Git(db)) {
  143. writeTrashFile("file_0.txt", "content_0_1");
  144. git.add().addFilepattern("file_0.txt").call();
  145. git.commit().setMessage("commit_1").call();
  146. writeTrashFile("file_0.txt", "content_0_2");
  147. String expectedFilePath1 = "some_directory/file_1.txt";
  148. writeTrashFile(expectedFilePath1, "content_1_2");
  149. String expectedFilePath2 = "some_directory/file_2.txt";
  150. writeTrashFile(expectedFilePath2, "content_2_2");
  151. String expectedFilePath3 = "some_directory/nested_directory/file_3.txt";
  152. writeTrashFile(expectedFilePath3, "content_3_2");
  153. git.add().addFilepattern(".").call();
  154. git.commit().setMessage("commit_2").call();
  155. git.archive().setOutputStream(new MockOutputStream())
  156. .setFormat(format.SUFFIXES.get(0))
  157. .setTree(git.getRepository().resolve("HEAD"))
  158. .setPaths("some_directory/").call();
  159. assertEquals(UNEXPECTED_ARCHIVE_SIZE, 5, format.size());
  160. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_2", format.getByPath(expectedFilePath1));
  161. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath(expectedFilePath2));
  162. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_3_2", format.getByPath(expectedFilePath3));
  163. assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory"));
  164. assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory/nested_directory"));
  165. }
  166. }
  167. private class MockFormat implements ArchiveCommand.Format<MockOutputStream> {
  168. private Map<String, String> entries = new HashMap<String, String>();
  169. private int size() {
  170. return entries.size();
  171. }
  172. private String getByPath(String path) {
  173. return entries.get(path);
  174. }
  175. private final List<String> SUFFIXES = Collections
  176. .unmodifiableList(Arrays.asList(".mck"));
  177. public MockOutputStream createArchiveOutputStream(OutputStream s)
  178. throws IOException {
  179. return createArchiveOutputStream(s,
  180. Collections.<String, Object> emptyMap());
  181. }
  182. public MockOutputStream createArchiveOutputStream(OutputStream s,
  183. Map<String, Object> o) throws IOException {
  184. for (Map.Entry<String, Object> p : o.entrySet()) {
  185. try {
  186. String methodName = "set"
  187. + StringUtils.capitalize(p.getKey());
  188. new Statement(s, methodName, new Object[] { p.getValue() })
  189. .execute();
  190. } catch (Exception e) {
  191. throw new IOException("cannot set option: " + p.getKey(), e);
  192. }
  193. }
  194. return new MockOutputStream();
  195. }
  196. public void putEntry(MockOutputStream out, String path, FileMode mode, ObjectLoader loader) {
  197. String content = mode != FileMode.TREE ? new String(loader.getBytes()) : null;
  198. entries.put(path, content);
  199. }
  200. public Iterable<String> suffixes() {
  201. return SUFFIXES;
  202. }
  203. }
  204. public class MockOutputStream extends OutputStream {
  205. private int foo;
  206. public void setFoo(int foo) {
  207. this.foo = foo;
  208. }
  209. public int getFoo() {
  210. return foo;
  211. }
  212. @Override
  213. public void write(int b) throws IOException {
  214. // Do nothing. for testing purposes.
  215. }
  216. }
  217. }