1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.util.io;
11
12 import static org.junit.Assert.assertArrayEquals;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.ByteArrayOutputStream;
17 import java.io.InputStream;
18 import java.util.zip.InflaterInputStream;
19
20 import org.junit.Test;
21
22
23
24
25
26 public class BinaryDeltaInputStreamTest {
27
28 private InputStream getBinaryHunk(String name) {
29 return this.getClass().getResourceAsStream(name);
30 }
31
32 @Test
33 public void testBinaryDelta() throws Exception {
34
35 byte[] data = new byte[8192];
36 for (int i = 0; i < data.length; i++) {
37 data[i] = (byte) (255 - (i % 256));
38 }
39
40 int middle = data.length / 2;
41 byte[] newData = new byte[data.length + 5];
42 System.arraycopy(data, 0, newData, 0, middle);
43 for (int i = 0; i < 5; i++) {
44 newData[middle + i] = 'x';
45 }
46 System.arraycopy(data, middle, newData, middle + 5, middle);
47
48
49
50
51
52
53
54
55
56
57
58
59 try (ByteArrayOutputStream out = new ByteArrayOutputStream();
60 BinaryDeltaInputStream input = new BinaryDeltaInputStream(data,
61 new InflaterInputStream(new BinaryHunkInputStream(
62 getBinaryHunk("delta1.forward"))))) {
63 byte[] buf = new byte[1024];
64 int n;
65 while ((n = input.read(buf)) >= 0) {
66 out.write(buf, 0, n);
67 }
68 assertArrayEquals(newData, out.toByteArray());
69 assertTrue(input.isFullyConsumed());
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 try (ByteArrayOutputStream out = new ByteArrayOutputStream();
88 BinaryDeltaInputStream input = new BinaryDeltaInputStream(
89 newData,
90 new InflaterInputStream(new BinaryHunkInputStream(
91 getBinaryHunk("delta1.reverse"))))) {
92 long expectedSize = input.getExpectedResultSize();
93 assertEquals(data.length, expectedSize);
94 byte[] buf = new byte[1024];
95 int n;
96 while ((n = input.read(buf)) >= 0) {
97 out.write(buf, 0, n);
98 }
99 assertArrayEquals(data, out.toByteArray());
100 assertTrue(input.isFullyConsumed());
101 }
102 }
103 }