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.

ObjectLoaderTest.java 7.7KB

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