Skip to main content
summaryrefslogtreecommitdiffstats
blob: a2cf28f9205cb329392041b2864e213ad41fe80d (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*******************************************************************************
 * Copyright (c) 2007, 2018 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 org.eclipse.equinox.p2.tests.sar;

import static org.junit.Assert.assertTrue;

import java.io.*;
import java.util.Arrays;
import org.eclipse.equinox.internal.p2.sar.SarUtil;
import org.eclipse.equinox.p2.tests.optimizers.TestData;
import org.junit.Test;

/**
 * Test the sar stuff.
 */
public class SarTest {

	@Test
	public void testJarToSarForJdt320() throws IOException {
		InputStream jdt320Jar = TestData.get("optimizers", "org.eclipse.jdt_3.2.0.v20060605-1400.njar");
		InputStream jdt320Sar = TestData.get("sar", "org.eclipse.jdt_3.2.0.v20060605-1400.sar");
		doJarToSar(jdt320Jar, jdt320Sar);
	}

	@Test
	public void testSarToJarForJdt320() throws IOException {
		InputStream jdt320Sar = TestData.get("sar", "org.eclipse.jdt_3.2.0.v20060605-1400.sar");
		InputStream jdt320Jar = TestData.get("optimizers", "org.eclipse.jdt_3.2.0.v20060605-1400.njar");
		doSarToJar(jdt320Sar, jdt320Jar);
	}

	@Test
	public void testJarToSarForJdt330() throws IOException {
		InputStream jdt330Jar = TestData.get("optimizers", "org.eclipse.jdt_3.3.0.v20070607-1300.njar");
		InputStream jdt330Sar = TestData.get("sar", "org.eclipse.jdt_3.3.0.v20070607-1300.sar");
		doJarToSar(jdt330Jar, jdt330Sar);
	}

	@Test
	public void testSarToJarForJdt330() throws IOException {
		InputStream jdt330Sar = TestData.get("sar", "org.eclipse.jdt_3.3.0.v20070607-1300.sar");
		InputStream jdt330Jar = TestData.get("optimizers", "org.eclipse.jdt_3.3.0.v20070607-1300.njar");
		doSarToJar(jdt330Sar, jdt330Jar);
	}

	/**
	 * @throws IOException
	 */
	private void doJarToSar(InputStream jar, InputStream expectedSar) throws IOException {
		File sar = TestData.createTempFile("doJarToSar.sar");
		OutputStream sarOut = new BufferedOutputStream(new FileOutputStream(sar));

		SarUtil.zipToSar(jar, sarOut);

		InputStream sarIn = new BufferedInputStream(new FileInputStream(sar));
		TestData.assertEquals(sarIn, expectedSar);
	}

	/**
	 * @throws IOException
	 */
	private void doSarToJar(InputStream sar, InputStream expectedJar) throws IOException {
		File jar = TestData.createTempFile("doSarToJar.jar");
		OutputStream jarOut = new BufferedOutputStream(new FileOutputStream(jar));

		SarUtil.sarToZip(sar, jarOut);

		InputStream jarIn = new BufferedInputStream(new FileInputStream(jar));
		TestData.assertEquals(jarIn, expectedJar);
	}

	@Test
	public void testZipToSarAndBack() throws IOException {
		File originalZipFile = TestData.getTempFile("sar", "test.zip");
		File sarFile = TestData.createTempFile("test.sar");
		File recreatedZipFile = TestData.createTempFile("test.zip");

		long before = System.currentTimeMillis();
		SarUtil.zipToSar(originalZipFile, sarFile);
		System.out.println("zipToSar took: " + (System.currentTimeMillis() - before));

		before = System.currentTimeMillis();
		SarUtil.sarToZip(sarFile, recreatedZipFile);
		System.out.println("sarToZip took: " + (System.currentTimeMillis() - before));

		TestData.assertEquals(originalZipFile, recreatedZipFile);
	}

	@Test
	public void testNormalizeOnFiles() throws IOException {
		File alienZip = TestData.getTempFile("sar", "alien.zip");

		File normalizedAlienZip = TestData.createTempFile("normalizedalien.zip");
		SarUtil.normalize(alienZip, normalizedAlienZip);

		assertTrue(alienZip.length() != normalizedAlienZip.length());

		File renormalizedAlienZip = TestData.createTempFile("renormalizedalien.zip");
		SarUtil.normalize(normalizedAlienZip, renormalizedAlienZip);

		TestData.assertEquals(normalizedAlienZip, renormalizedAlienZip);
	}

	/**
	 * @throws IOException
	 */
	@Test
	public void testNormalizeOnStreames() throws IOException {
		try (InputStream alienZip = TestData.get("sar", "alien.zip")) {
			ByteArrayOutputStream normalizedAlienZip = new ByteArrayOutputStream();
			ByteArrayOutputStream renormalizedAlienZip = new ByteArrayOutputStream();
			SarUtil.normalize(alienZip, normalizedAlienZip);

			SarUtil.normalize(new ByteArrayInputStream(normalizedAlienZip.toByteArray()), renormalizedAlienZip);

			assertTrue(Arrays.equals(normalizedAlienZip.toByteArray(), renormalizedAlienZip.toByteArray()));
		}

	}

}

Back to the top