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

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