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

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