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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 org.eclipse.jgit.api.errors.GitAPIException;
  47. import org.eclipse.jgit.junit.RepositoryTestCase;
  48. import org.eclipse.jgit.lib.FileMode;
  49. import org.eclipse.jgit.lib.ObjectLoader;
  50. import org.eclipse.jgit.revwalk.RevCommit;
  51. import org.junit.After;
  52. import org.junit.Before;
  53. import org.junit.Test;
  54. import java.io.IOException;
  55. import java.io.OutputStream;
  56. import java.util.*;
  57. public class ArchiveCommandTest extends RepositoryTestCase {
  58. private static final String UNEXPECTED_ARCHIVE_SIZE = "Unexpected archive size";
  59. private static final String UNEXPECTED_FILE_CONTENTS = "Unexpected file contents";
  60. private static final String UNEXPECTED_TREE_CONTENTS = "Unexpected tree contents";
  61. private MockFormat format = null;
  62. @Before
  63. public void setup() {
  64. format = new MockFormat();
  65. ArchiveCommand.registerFormat(format.SUFFIXES.get(0), format);
  66. }
  67. @After
  68. public void tearDown() {
  69. ArchiveCommand.unregisterFormat(format.SUFFIXES.get(0));
  70. }
  71. @Test
  72. public void archiveHeadAllFiles() throws IOException, GitAPIException {
  73. Git git = new Git(db);
  74. writeTrashFile("file_1.txt", "content_1_1");
  75. git.add().addFilepattern("file_1.txt").call();
  76. git.commit().setMessage("create file").call();
  77. writeTrashFile("file_1.txt", "content_1_2");
  78. writeTrashFile("file_2.txt", "content_2_2");
  79. git.add().addFilepattern(".").call();
  80. git.commit().setMessage("updated file").call();
  81. git.archive().setOutputStream(new MockOutputStream())
  82. .setFormat(format.SUFFIXES.get(0))
  83. .setTree(git.getRepository().resolve("HEAD")).call();
  84. assertEquals(UNEXPECTED_ARCHIVE_SIZE, 2, format.size());
  85. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_2", format.getByPath("file_1.txt"));
  86. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath("file_2.txt"));
  87. }
  88. @Test
  89. public void archiveHeadSpecificPath() throws IOException, GitAPIException {
  90. Git git = new Git(db);
  91. writeTrashFile("file_1.txt", "content_1_1");
  92. git.add().addFilepattern("file_1.txt").call();
  93. git.commit().setMessage("create file").call();
  94. writeTrashFile("file_1.txt", "content_1_2");
  95. String expectedFilePath = "some_directory/file_2.txt";
  96. writeTrashFile(expectedFilePath, "content_2_2");
  97. git.add().addFilepattern(".").call();
  98. git.commit().setMessage("updated file").call();
  99. git.archive().setOutputStream(new MockOutputStream())
  100. .setFormat(format.SUFFIXES.get(0))
  101. .setTree(git.getRepository().resolve("HEAD"))
  102. .setPaths(expectedFilePath).call();
  103. assertEquals(UNEXPECTED_ARCHIVE_SIZE, 2, format.size());
  104. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath(expectedFilePath));
  105. assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory"));
  106. }
  107. @Test
  108. public void archiveByIdSpecificFile() throws IOException, GitAPIException {
  109. Git git = new Git(db);
  110. writeTrashFile("file_1.txt", "content_1_1");
  111. git.add().addFilepattern("file_1.txt").call();
  112. RevCommit first = git.commit().setMessage("create file").call();
  113. writeTrashFile("file_1.txt", "content_1_2");
  114. String expectedFilePath = "some_directory/file_2.txt";
  115. writeTrashFile(expectedFilePath, "content_2_2");
  116. git.add().addFilepattern(".").call();
  117. git.commit().setMessage("updated file").call();
  118. git.archive().setOutputStream(new MockOutputStream())
  119. .setFormat(format.SUFFIXES.get(0)).setTree(first)
  120. .setPaths("file_1.txt").call();
  121. assertEquals(UNEXPECTED_ARCHIVE_SIZE, 1, format.size());
  122. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_1", format.getByPath("file_1.txt"));
  123. }
  124. @Test
  125. public void archiveByDirectoryPath() throws GitAPIException, IOException {
  126. Git git = new Git(db);
  127. writeTrashFile("file_0.txt", "content_0_1");
  128. git.add().addFilepattern("file_0.txt").call();
  129. git.commit().setMessage("commit_1").call();
  130. writeTrashFile("file_0.txt", "content_0_2");
  131. String expectedFilePath1 = "some_directory/file_1.txt";
  132. writeTrashFile(expectedFilePath1, "content_1_2");
  133. String expectedFilePath2 = "some_directory/file_2.txt";
  134. writeTrashFile(expectedFilePath2, "content_2_2");
  135. String expectedFilePath3 = "some_directory/nested_directory/file_3.txt";
  136. writeTrashFile(expectedFilePath3, "content_3_2");
  137. git.add().addFilepattern(".").call();
  138. git.commit().setMessage("commit_2").call();
  139. git.archive().setOutputStream(new MockOutputStream())
  140. .setFormat(format.SUFFIXES.get(0))
  141. .setTree(git.getRepository().resolve("HEAD"))
  142. .setPaths("some_directory/").call();
  143. assertEquals(UNEXPECTED_ARCHIVE_SIZE, 5, format.size());
  144. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_1_2", format.getByPath(expectedFilePath1));
  145. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_2_2", format.getByPath(expectedFilePath2));
  146. assertEquals(UNEXPECTED_FILE_CONTENTS, "content_3_2", format.getByPath(expectedFilePath3));
  147. assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory"));
  148. assertNull(UNEXPECTED_TREE_CONTENTS, format.getByPath("some_directory/nested_directory"));
  149. }
  150. private class MockFormat implements ArchiveCommand.Format<MockOutputStream> {
  151. private Map<String, String> entries = new HashMap<String, String>();
  152. private int size() {
  153. return entries.size();
  154. }
  155. private String getByPath(String path) {
  156. return entries.get(path);
  157. }
  158. private final List<String> SUFFIXES = Collections
  159. .unmodifiableList(Arrays.asList(".mck"));
  160. public MockOutputStream createArchiveOutputStream(OutputStream s)
  161. throws IOException {
  162. return new MockOutputStream();
  163. }
  164. public void putEntry(MockOutputStream out, String path, FileMode mode, ObjectLoader loader) {
  165. String content = mode != FileMode.TREE ? new String(loader.getBytes()) : null;
  166. entries.put(path, content);
  167. }
  168. public Iterable<String> suffixes() {
  169. return SUFFIXES;
  170. }
  171. }
  172. private class MockOutputStream extends OutputStream {
  173. @Override
  174. public void write(int b) throws IOException {
  175. // Do nothing. for testing purposes.
  176. }
  177. }
  178. }