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.2KB

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