Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5f5e7d8f7ff34c20c9183f3eb8df66522689f547 (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*******************************************************************************
 * Copyright (c) 2007 compeople AG and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * 	compeople AG (Stefan Liebig) - initial API and implementation
 *******************************************************************************/
package ie.wombat.jbdiff.test;

import ie.wombat.jbdiff.JBDiff;
import ie.wombat.jbdiff.JBPatch;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import junit.framework.TestCase;

public class DiffPatchTest extends TestCase {

	public void testNullData() throws Exception {
		bench("null.data", "null.data");
	}

	public void testOneData() throws Exception {
		bench("one.data", "one.data");
	}

	public void testOneHundredData() throws Exception {
		bench("onehundred.data", "onehundred.data");
	}

	public void testOneHundredXData() throws Exception {
		bench("onehundred.data", "onehundredX.data");
	}

	public void testOneXHundredXData() throws Exception {
		bench("onehundred.data", "oneXhundredX.data");
	}

	public void testPdeCoreJar() throws Exception {
		bench("org.eclipse.pde.core_3.2.jar", "org.eclipse.pde.core_3.3.jar");
	}

	public void testEclipse() throws Exception {
		bench("eclipse-3.2.exe", "eclipse-3.3.exe");
	}

	private void bench(String resource1, String resource2) throws Exception {

		byte[] oldData = getTestData(resource1);
		byte[] newData = getTestData(resource2);

		System.out.println(resource1 + "(" + (oldData.length / 1024)
				+ " kb) -> " + resource2 + "(" + (newData.length / 1024)
				+ " kb)");

		diffAndPatchJBDiff(oldData, newData);

		System.out.println("");
	}

	/**
	 * @param resource1
	 * @param resource2
	 * @throws IOException
	 */
	private void diffAndPatchJBDiff(byte[] oldData, byte[] newData)
			throws IOException {

		try {

			long start = System.currentTimeMillis();

			byte[] diff = JBDiff.bsdiff(oldData, oldData.length, newData,
					newData.length);

			long diffEnd = System.currentTimeMillis();

			byte[] patch = JBPatch.bspatch(oldData, oldData.length, diff);

			long patchEnd = System.currentTimeMillis();

			System.out.println("JBDiff: Size= " + diff.length + " b ("
					+ diff.length / 1024 + " kb), Diffing " + (diffEnd - start)
					+ " ms, Patching: " + (patchEnd - diffEnd) + " ms");

			assertTrue(Arrays.equals(newData, patch));

		} catch (RuntimeException re) {
			System.err.println("JBDiff: error: " + re.getMessage());
		}
	}

	private static byte[] getTestData(String name) throws IOException {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		InputStream input = new BufferedInputStream(Activator.getContext()
				.getBundle().getEntry("testData/" + name).openStream());
		int r;
		while ((r = input.read()) != -1) {
			out.write(r);
		}
		input.close();
		out.close();
		return out.toByteArray();
	}
}

Back to the top