1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
155 }
156
157 try {
158 ldr.getBytes();
159 fail("did not throw on getBytes()");
160 } catch (LargeObjectException tooBig) {
161
162 }
163
164 try {
165 ldr.getCachedBytes(64);
166 fail("did not throw on getCachedBytes(64)");
167 } catch (LargeObjectException tooBig) {
168
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
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
279 }
280
281 try {
282 ldr.getCachedBytes(Integer.MAX_VALUE);
283 fail("Did not throw LargeObjectException");
284 } catch (LargeObjectException tooBig) {
285
286 }
287 }
288 }