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