Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  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.lib;
  44. import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertNotNull;
  48. import static org.junit.Assert.assertNotSame;
  49. import static org.junit.Assert.assertSame;
  50. import static org.junit.Assert.assertTrue;
  51. import static org.junit.Assert.fail;
  52. import java.io.ByteArrayInputStream;
  53. import java.io.ByteArrayOutputStream;
  54. import java.io.IOException;
  55. import java.util.Arrays;
  56. import org.eclipse.jgit.errors.LargeObjectException;
  57. import org.eclipse.jgit.errors.MissingObjectException;
  58. import org.eclipse.jgit.junit.JGitTestUtil;
  59. import org.eclipse.jgit.junit.TestRng;
  60. import org.junit.Test;
  61. public class ObjectLoaderTest {
  62. private TestRng rng;
  63. private TestRng getRng() {
  64. if (rng == null)
  65. rng = new TestRng(JGitTestUtil.getName());
  66. return rng;
  67. }
  68. @Test
  69. public void testSmallObjectLoader() throws MissingObjectException,
  70. IOException {
  71. final byte[] act = getRng().nextBytes(512);
  72. final ObjectLoader ldr = new ObjectLoader.SmallObject(OBJ_BLOB, act);
  73. assertEquals(OBJ_BLOB, ldr.getType());
  74. assertEquals(act.length, ldr.getSize());
  75. assertFalse("not is large", ldr.isLarge());
  76. assertSame(act, ldr.getCachedBytes());
  77. assertSame(act, ldr.getCachedBytes(1));
  78. assertSame(act, ldr.getCachedBytes(Integer.MAX_VALUE));
  79. byte[] copy = ldr.getBytes();
  80. assertNotSame(act, copy);
  81. assertTrue("same content", Arrays.equals(act, copy));
  82. copy = ldr.getBytes(1);
  83. assertNotSame(act, copy);
  84. assertTrue("same content", Arrays.equals(act, copy));
  85. copy = ldr.getBytes(Integer.MAX_VALUE);
  86. assertNotSame(act, copy);
  87. assertTrue("same content", Arrays.equals(act, copy));
  88. ObjectStream in = ldr.openStream();
  89. assertNotNull("has stream", in);
  90. assertTrue("is small stream", in instanceof ObjectStream.SmallStream);
  91. assertEquals(OBJ_BLOB, in.getType());
  92. assertEquals(act.length, in.getSize());
  93. assertEquals(act.length, in.available());
  94. assertTrue("mark supported", in.markSupported());
  95. copy = new byte[act.length];
  96. assertEquals(act.length, in.read(copy));
  97. assertEquals(0, in.available());
  98. assertEquals(-1, in.read());
  99. assertTrue("same content", Arrays.equals(act, copy));
  100. ByteArrayOutputStream tmp = new ByteArrayOutputStream();
  101. ldr.copyTo(tmp);
  102. assertTrue("same content", Arrays.equals(act, tmp.toByteArray()));
  103. }
  104. @Test
  105. public void testLargeObjectLoader() throws MissingObjectException,
  106. IOException {
  107. final byte[] act = getRng().nextBytes(512);
  108. final ObjectLoader ldr = new ObjectLoader() {
  109. @Override
  110. public byte[] getCachedBytes() throws LargeObjectException {
  111. throw new LargeObjectException();
  112. }
  113. @Override
  114. public long getSize() {
  115. return act.length;
  116. }
  117. @Override
  118. public int getType() {
  119. return OBJ_BLOB;
  120. }
  121. @Override
  122. public ObjectStream openStream() throws MissingObjectException,
  123. IOException {
  124. return new ObjectStream.Filter(getType(), act.length,
  125. new ByteArrayInputStream(act));
  126. }
  127. };
  128. assertEquals(OBJ_BLOB, ldr.getType());
  129. assertEquals(act.length, ldr.getSize());
  130. assertTrue("is large", ldr.isLarge());
  131. try {
  132. ldr.getCachedBytes();
  133. fail("did not throw on getCachedBytes()");
  134. } catch (LargeObjectException tooBig) {
  135. // expected
  136. }
  137. try {
  138. ldr.getBytes();
  139. fail("did not throw on getBytes()");
  140. } catch (LargeObjectException tooBig) {
  141. // expected
  142. }
  143. try {
  144. ldr.getCachedBytes(64);
  145. fail("did not throw on getCachedBytes(64)");
  146. } catch (LargeObjectException tooBig) {
  147. // expected
  148. }
  149. byte[] copy = ldr.getCachedBytes(1024);
  150. assertNotSame(act, copy);
  151. assertTrue("same content", Arrays.equals(act, copy));
  152. ObjectStream in = ldr.openStream();
  153. assertNotNull("has stream", in);
  154. assertEquals(OBJ_BLOB, in.getType());
  155. assertEquals(act.length, in.getSize());
  156. assertEquals(act.length, in.available());
  157. assertTrue("mark supported", in.markSupported());
  158. copy = new byte[act.length];
  159. assertEquals(act.length, in.read(copy));
  160. assertEquals(0, in.available());
  161. assertEquals(-1, in.read());
  162. assertTrue("same content", Arrays.equals(act, copy));
  163. ByteArrayOutputStream tmp = new ByteArrayOutputStream();
  164. ldr.copyTo(tmp);
  165. assertTrue("same content", Arrays.equals(act, tmp.toByteArray()));
  166. }
  167. @Test
  168. public void testLimitedGetCachedBytes() throws LargeObjectException,
  169. MissingObjectException, IOException {
  170. byte[] act = getRng().nextBytes(512);
  171. ObjectLoader ldr = new ObjectLoader.SmallObject(OBJ_BLOB, act) {
  172. @Override
  173. public boolean isLarge() {
  174. return true;
  175. }
  176. };
  177. assertTrue("is large", ldr.isLarge());
  178. try {
  179. ldr.getCachedBytes(10);
  180. fail("Did not throw LargeObjectException");
  181. } catch (LargeObjectException tooBig) {
  182. // Expected result.
  183. }
  184. byte[] copy = ldr.getCachedBytes(512);
  185. assertNotSame(act, copy);
  186. assertTrue("same content", Arrays.equals(act, copy));
  187. copy = ldr.getCachedBytes(1024);
  188. assertNotSame(act, copy);
  189. assertTrue("same content", Arrays.equals(act, copy));
  190. }
  191. @Test
  192. public void testLimitedGetCachedBytesExceedsJavaLimits()
  193. throws LargeObjectException, MissingObjectException, IOException {
  194. ObjectLoader ldr = new ObjectLoader() {
  195. @Override
  196. public boolean isLarge() {
  197. return true;
  198. }
  199. @Override
  200. public byte[] getCachedBytes() throws LargeObjectException {
  201. throw new LargeObjectException();
  202. }
  203. @Override
  204. public long getSize() {
  205. return Long.MAX_VALUE;
  206. }
  207. @Override
  208. public int getType() {
  209. return OBJ_BLOB;
  210. }
  211. @Override
  212. public ObjectStream openStream() throws MissingObjectException,
  213. IOException {
  214. return new ObjectStream() {
  215. @Override
  216. public long getSize() {
  217. return Long.MAX_VALUE;
  218. }
  219. @Override
  220. public int getType() {
  221. return OBJ_BLOB;
  222. }
  223. @Override
  224. public int read() throws IOException {
  225. fail("never should have reached read");
  226. return -1;
  227. }
  228. @Override
  229. public int read(byte b[], int off, int len) {
  230. fail("never should have reached read");
  231. return -1;
  232. }
  233. };
  234. }
  235. };
  236. assertTrue("is large", ldr.isLarge());
  237. try {
  238. ldr.getCachedBytes(10);
  239. fail("Did not throw LargeObjectException");
  240. } catch (LargeObjectException tooBig) {
  241. // Expected result.
  242. }
  243. try {
  244. ldr.getCachedBytes(Integer.MAX_VALUE);
  245. fail("Did not throw LargeObjectException");
  246. } catch (LargeObjectException tooBig) {
  247. // Expected result.
  248. }
  249. }
  250. }