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